util_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2017 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. "fmt"
  16. "reflect"
  17. "testing"
  18. "k8s.io/apimachinery/pkg/util/diff"
  19. utilfeature "k8s.io/apiserver/pkg/util/feature"
  20. featuregatetesting "k8s.io/component-base/featuregate/testing"
  21. "k8s.io/kubernetes/pkg/apis/storage"
  22. "k8s.io/kubernetes/pkg/features"
  23. )
  24. func TestDropAllowVolumeExpansion(t *testing.T) {
  25. allowVolumeExpansion := false
  26. scWithoutAllowVolumeExpansion := func() *storage.StorageClass {
  27. return &storage.StorageClass{}
  28. }
  29. scWithAllowVolumeExpansion := func() *storage.StorageClass {
  30. return &storage.StorageClass{
  31. AllowVolumeExpansion: &allowVolumeExpansion,
  32. }
  33. }
  34. scInfo := []struct {
  35. description string
  36. hasAllowVolumeExpansion bool
  37. sc func() *storage.StorageClass
  38. }{
  39. {
  40. description: "StorageClass Without AllowVolumeExpansion",
  41. hasAllowVolumeExpansion: false,
  42. sc: scWithoutAllowVolumeExpansion,
  43. },
  44. {
  45. description: "StorageClass With AllowVolumeExpansion",
  46. hasAllowVolumeExpansion: true,
  47. sc: scWithAllowVolumeExpansion,
  48. },
  49. {
  50. description: "is nil",
  51. hasAllowVolumeExpansion: false,
  52. sc: func() *storage.StorageClass { return nil },
  53. },
  54. }
  55. for _, enabled := range []bool{true, false} {
  56. for _, oldStorageClassInfo := range scInfo {
  57. for _, newStorageClassInfo := range scInfo {
  58. oldStorageClassHasAllowVolumeExpansion, oldStorageClass := oldStorageClassInfo.hasAllowVolumeExpansion, oldStorageClassInfo.sc()
  59. newStorageClassHasAllowVolumeExpansion, newStorageClass := newStorageClassInfo.hasAllowVolumeExpansion, newStorageClassInfo.sc()
  60. if newStorageClass == nil {
  61. continue
  62. }
  63. t.Run(fmt.Sprintf("feature enabled=%v, old StorageClass %v, new StorageClass %v", enabled, oldStorageClassInfo.description, newStorageClassInfo.description), func(t *testing.T) {
  64. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExpandPersistentVolumes, enabled)()
  65. DropDisabledFields(newStorageClass, oldStorageClass)
  66. // old StorageClass should never be changed
  67. if !reflect.DeepEqual(oldStorageClass, oldStorageClassInfo.sc()) {
  68. t.Errorf("old StorageClass changed: %v", diff.ObjectReflectDiff(oldStorageClass, oldStorageClassInfo.sc()))
  69. }
  70. switch {
  71. case enabled || oldStorageClassHasAllowVolumeExpansion:
  72. // new StorageClass should not be changed if the feature is enabled, or if the old StorageClass had AllowVolumeExpansion
  73. if !reflect.DeepEqual(newStorageClass, newStorageClassInfo.sc()) {
  74. t.Errorf("new StorageClass changed: %v", diff.ObjectReflectDiff(newStorageClass, newStorageClassInfo.sc()))
  75. }
  76. case newStorageClassHasAllowVolumeExpansion:
  77. // new StorageClass should be changed
  78. if reflect.DeepEqual(newStorageClass, newStorageClassInfo.sc()) {
  79. t.Errorf("new StorageClass was not changed")
  80. }
  81. // new StorageClass should not have AllowVolumeExpansion
  82. if !reflect.DeepEqual(newStorageClass, scWithoutAllowVolumeExpansion()) {
  83. t.Errorf("new StorageClass had StorageClassAllowVolumeExpansion: %v", diff.ObjectReflectDiff(newStorageClass, scWithoutAllowVolumeExpansion()))
  84. }
  85. default:
  86. // new StorageClass should not need to be changed
  87. if !reflect.DeepEqual(newStorageClass, newStorageClassInfo.sc()) {
  88. t.Errorf("new StorageClass changed: %v", diff.ObjectReflectDiff(newStorageClass, newStorageClassInfo.sc()))
  89. }
  90. }
  91. })
  92. }
  93. }
  94. }
  95. }