strategy_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2015 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 persistentvolumeclaim
  14. import (
  15. "fmt"
  16. "reflect"
  17. "testing"
  18. "k8s.io/apimachinery/pkg/util/diff"
  19. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  20. utilfeature "k8s.io/apiserver/pkg/util/feature"
  21. featuregatetesting "k8s.io/component-base/featuregate/testing"
  22. apitesting "k8s.io/kubernetes/pkg/api/testing"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. "k8s.io/kubernetes/pkg/features"
  25. // install all api groups for testing
  26. _ "k8s.io/kubernetes/pkg/api/testapi"
  27. )
  28. func TestSelectableFieldLabelConversions(t *testing.T) {
  29. apitesting.TestSelectableFieldLabelConversionsOfKind(t,
  30. "v1",
  31. "PersistentVolumeClaim",
  32. PersistentVolumeClaimToSelectableFields(&api.PersistentVolumeClaim{}),
  33. map[string]string{"name": "metadata.name"},
  34. )
  35. }
  36. func TestDropConditions(t *testing.T) {
  37. ctx := genericapirequest.NewDefaultContext()
  38. pvcWithConditions := func() *api.PersistentVolumeClaim {
  39. return &api.PersistentVolumeClaim{
  40. Status: api.PersistentVolumeClaimStatus{
  41. Conditions: []api.PersistentVolumeClaimCondition{
  42. {Type: api.PersistentVolumeClaimResizing, Status: api.ConditionTrue},
  43. },
  44. },
  45. }
  46. }
  47. pvcWithoutConditions := func() *api.PersistentVolumeClaim {
  48. return &api.PersistentVolumeClaim{
  49. Status: api.PersistentVolumeClaimStatus{},
  50. }
  51. }
  52. pvcInfo := []struct {
  53. description string
  54. hasConditions bool
  55. pvc func() *api.PersistentVolumeClaim
  56. }{
  57. {
  58. description: "has Conditions",
  59. hasConditions: true,
  60. pvc: pvcWithConditions,
  61. },
  62. {
  63. description: "does not have Conditions",
  64. hasConditions: false,
  65. pvc: pvcWithoutConditions,
  66. },
  67. }
  68. for _, enabled := range []bool{true, false} {
  69. for _, oldPvcInfo := range pvcInfo {
  70. for _, newPvcInfo := range pvcInfo {
  71. oldPvcHasConditins, oldPvc := oldPvcInfo.hasConditions, oldPvcInfo.pvc()
  72. newPvcHasConditions, newPvc := newPvcInfo.hasConditions, newPvcInfo.pvc()
  73. t.Run(fmt.Sprintf("feature enabled=%v, old pvc %v, new pvc %v", enabled, oldPvcInfo.description, newPvcInfo.description), func(t *testing.T) {
  74. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExpandPersistentVolumes, enabled)()
  75. StatusStrategy.PrepareForUpdate(ctx, newPvc, oldPvc)
  76. // old pvc should never be changed
  77. if !reflect.DeepEqual(oldPvc, oldPvcInfo.pvc()) {
  78. t.Errorf("old pvc changed: %v", diff.ObjectReflectDiff(oldPvc, oldPvcInfo.pvc()))
  79. }
  80. switch {
  81. case enabled || oldPvcHasConditins:
  82. // new pvc should not be changed if the feature is enabled, or if the old pvc had Conditions
  83. if !reflect.DeepEqual(newPvc, newPvcInfo.pvc()) {
  84. t.Errorf("new pvc changed: %v", diff.ObjectReflectDiff(newPvc, newPvcInfo.pvc()))
  85. }
  86. case newPvcHasConditions:
  87. // new pvc should be changed
  88. if reflect.DeepEqual(newPvc, newPvcInfo.pvc()) {
  89. t.Errorf("new pvc was not changed")
  90. }
  91. // new pvc should not have Conditions
  92. if !reflect.DeepEqual(newPvc, pvcWithoutConditions()) {
  93. t.Errorf("new pvc had Conditions: %v", diff.ObjectReflectDiff(newPvc, pvcWithoutConditions()))
  94. }
  95. default:
  96. // new pvc should not need to be changed
  97. if !reflect.DeepEqual(newPvc, newPvcInfo.pvc()) {
  98. t.Errorf("new pvc changed: %v", diff.ObjectReflectDiff(newPvc, newPvcInfo.pvc()))
  99. }
  100. }
  101. })
  102. }
  103. }
  104. }
  105. }