resize_util_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "encoding/json"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/api/resource"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. )
  23. type conditionMergeTestCase struct {
  24. description string
  25. pvc *v1.PersistentVolumeClaim
  26. newConditions []v1.PersistentVolumeClaimCondition
  27. finalCondtions []v1.PersistentVolumeClaimCondition
  28. }
  29. func TestMergeResizeCondition(t *testing.T) {
  30. currentTime := metav1.Now()
  31. pvc := getPVC([]v1.PersistentVolumeClaimCondition{
  32. {
  33. Type: v1.PersistentVolumeClaimResizing,
  34. Status: v1.ConditionTrue,
  35. LastTransitionTime: currentTime,
  36. },
  37. })
  38. noConditionPVC := getPVC([]v1.PersistentVolumeClaimCondition{})
  39. conditionFalseTime := metav1.Now()
  40. newTime := metav1.NewTime(time.Now().Add(1 * time.Hour))
  41. testCases := []conditionMergeTestCase{
  42. {
  43. description: "when removing all conditions",
  44. pvc: pvc.DeepCopy(),
  45. newConditions: []v1.PersistentVolumeClaimCondition{},
  46. finalCondtions: []v1.PersistentVolumeClaimCondition{},
  47. },
  48. {
  49. description: "adding new condition",
  50. pvc: pvc.DeepCopy(),
  51. newConditions: []v1.PersistentVolumeClaimCondition{
  52. {
  53. Type: v1.PersistentVolumeClaimFileSystemResizePending,
  54. Status: v1.ConditionTrue,
  55. },
  56. },
  57. finalCondtions: []v1.PersistentVolumeClaimCondition{
  58. {
  59. Type: v1.PersistentVolumeClaimFileSystemResizePending,
  60. Status: v1.ConditionTrue,
  61. },
  62. },
  63. },
  64. {
  65. description: "adding same condition with new timestamp",
  66. pvc: pvc.DeepCopy(),
  67. newConditions: []v1.PersistentVolumeClaimCondition{
  68. {
  69. Type: v1.PersistentVolumeClaimResizing,
  70. Status: v1.ConditionTrue,
  71. LastTransitionTime: newTime,
  72. },
  73. },
  74. finalCondtions: []v1.PersistentVolumeClaimCondition{
  75. {
  76. Type: v1.PersistentVolumeClaimResizing,
  77. Status: v1.ConditionTrue,
  78. LastTransitionTime: currentTime,
  79. },
  80. },
  81. },
  82. {
  83. description: "adding same condition but with different status",
  84. pvc: pvc.DeepCopy(),
  85. newConditions: []v1.PersistentVolumeClaimCondition{
  86. {
  87. Type: v1.PersistentVolumeClaimResizing,
  88. Status: v1.ConditionFalse,
  89. LastTransitionTime: conditionFalseTime,
  90. },
  91. },
  92. finalCondtions: []v1.PersistentVolumeClaimCondition{
  93. {
  94. Type: v1.PersistentVolumeClaimResizing,
  95. Status: v1.ConditionFalse,
  96. LastTransitionTime: conditionFalseTime,
  97. },
  98. },
  99. },
  100. {
  101. description: "when no condition exists on pvc",
  102. pvc: noConditionPVC.DeepCopy(),
  103. newConditions: []v1.PersistentVolumeClaimCondition{
  104. {
  105. Type: v1.PersistentVolumeClaimResizing,
  106. Status: v1.ConditionTrue,
  107. LastTransitionTime: currentTime,
  108. },
  109. },
  110. finalCondtions: []v1.PersistentVolumeClaimCondition{
  111. {
  112. Type: v1.PersistentVolumeClaimResizing,
  113. Status: v1.ConditionTrue,
  114. LastTransitionTime: currentTime,
  115. },
  116. },
  117. },
  118. }
  119. for _, testcase := range testCases {
  120. updatePVC := MergeResizeConditionOnPVC(testcase.pvc, testcase.newConditions)
  121. updateConditions := updatePVC.Status.Conditions
  122. if !reflect.DeepEqual(updateConditions, testcase.finalCondtions) {
  123. t.Errorf("Expected updated conditions for test %s to be %v but got %v",
  124. testcase.description,
  125. testcase.finalCondtions, updateConditions)
  126. }
  127. }
  128. }
  129. func TestCreatePVCPatch(t *testing.T) {
  130. pvc1 := getPVC([]v1.PersistentVolumeClaimCondition{
  131. {
  132. Type: v1.PersistentVolumeClaimFileSystemResizePending,
  133. Status: v1.ConditionTrue,
  134. LastTransitionTime: metav1.Now(),
  135. },
  136. })
  137. pvc1.SetResourceVersion("10")
  138. pvc2 := pvc1.DeepCopy()
  139. pvc2.Status.Capacity = v1.ResourceList{
  140. v1.ResourceName("size"): resource.MustParse("10G"),
  141. }
  142. patchBytes, err := createPVCPatch(pvc1, pvc2)
  143. if err != nil {
  144. t.Errorf("error creating patch bytes %v", err)
  145. }
  146. var patchMap map[string]interface{}
  147. err = json.Unmarshal(patchBytes, &patchMap)
  148. if err != nil {
  149. t.Errorf("error unmarshalling json patch : %v", err)
  150. }
  151. metadata, ok := patchMap["metadata"].(map[string]interface{})
  152. if !ok {
  153. t.Errorf("error converting metadata to version map")
  154. }
  155. resourceVersion, _ := metadata["resourceVersion"].(string)
  156. if resourceVersion != "10" {
  157. t.Errorf("expected resource version to 10 got %s", resourceVersion)
  158. }
  159. }
  160. func getPVC(conditions []v1.PersistentVolumeClaimCondition) *v1.PersistentVolumeClaim {
  161. pvc := &v1.PersistentVolumeClaim{
  162. ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "resize"},
  163. Spec: v1.PersistentVolumeClaimSpec{
  164. AccessModes: []v1.PersistentVolumeAccessMode{
  165. v1.ReadWriteOnce,
  166. v1.ReadOnlyMany,
  167. },
  168. Resources: v1.ResourceRequirements{
  169. Requests: v1.ResourceList{
  170. v1.ResourceName(v1.ResourceStorage): resource.MustParse("2Gi"),
  171. },
  172. },
  173. },
  174. Status: v1.PersistentVolumeClaimStatus{
  175. Phase: v1.ClaimBound,
  176. Conditions: conditions,
  177. Capacity: v1.ResourceList{
  178. v1.ResourceStorage: resource.MustParse("2Gi"),
  179. },
  180. },
  181. }
  182. return pvc
  183. }