util_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 persistentvolume
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/apimachinery/pkg/util/diff"
  18. utilfeature "k8s.io/apiserver/pkg/util/feature"
  19. featuregatetesting "k8s.io/component-base/featuregate/testing"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. "k8s.io/kubernetes/pkg/features"
  22. )
  23. func TestDropDisabledFields(t *testing.T) {
  24. specWithMode := func(mode *api.PersistentVolumeMode) *api.PersistentVolumeSpec {
  25. return &api.PersistentVolumeSpec{VolumeMode: mode}
  26. }
  27. secretRef := &api.SecretReference{
  28. Name: "expansion-secret",
  29. Namespace: "default",
  30. }
  31. modeBlock := api.PersistentVolumeBlock
  32. tests := map[string]struct {
  33. oldSpec *api.PersistentVolumeSpec
  34. newSpec *api.PersistentVolumeSpec
  35. expectOldSpec *api.PersistentVolumeSpec
  36. expectNewSpec *api.PersistentVolumeSpec
  37. blockEnabled bool
  38. csiExpansionEnabled bool
  39. }{
  40. "disabled block clears new": {
  41. blockEnabled: false,
  42. newSpec: specWithMode(&modeBlock),
  43. expectNewSpec: specWithMode(nil),
  44. oldSpec: nil,
  45. expectOldSpec: nil,
  46. },
  47. "disabled block clears update when old pv did not use block": {
  48. blockEnabled: false,
  49. newSpec: specWithMode(&modeBlock),
  50. expectNewSpec: specWithMode(nil),
  51. oldSpec: specWithMode(nil),
  52. expectOldSpec: specWithMode(nil),
  53. },
  54. "disabled block does not clear new on update when old pv did use block": {
  55. blockEnabled: false,
  56. newSpec: specWithMode(&modeBlock),
  57. expectNewSpec: specWithMode(&modeBlock),
  58. oldSpec: specWithMode(&modeBlock),
  59. expectOldSpec: specWithMode(&modeBlock),
  60. },
  61. "enabled block preserves new": {
  62. blockEnabled: true,
  63. newSpec: specWithMode(&modeBlock),
  64. expectNewSpec: specWithMode(&modeBlock),
  65. oldSpec: nil,
  66. expectOldSpec: nil,
  67. },
  68. "enabled block preserves update when old pv did not use block": {
  69. blockEnabled: true,
  70. newSpec: specWithMode(&modeBlock),
  71. expectNewSpec: specWithMode(&modeBlock),
  72. oldSpec: specWithMode(nil),
  73. expectOldSpec: specWithMode(nil),
  74. },
  75. "enabled block preserves update when old pv did use block": {
  76. blockEnabled: true,
  77. newSpec: specWithMode(&modeBlock),
  78. expectNewSpec: specWithMode(&modeBlock),
  79. oldSpec: specWithMode(&modeBlock),
  80. expectOldSpec: specWithMode(&modeBlock),
  81. },
  82. "disabled csi expansion clears secrets": {
  83. csiExpansionEnabled: false,
  84. newSpec: specWithCSISecrets(secretRef),
  85. expectNewSpec: specWithCSISecrets(nil),
  86. oldSpec: nil,
  87. expectOldSpec: nil,
  88. },
  89. "enabled csi expansion preserve secrets": {
  90. csiExpansionEnabled: true,
  91. newSpec: specWithCSISecrets(secretRef),
  92. expectNewSpec: specWithCSISecrets(secretRef),
  93. oldSpec: nil,
  94. expectOldSpec: nil,
  95. },
  96. "enabled csi expansion preserve secrets when both old and new have it": {
  97. csiExpansionEnabled: true,
  98. newSpec: specWithCSISecrets(secretRef),
  99. expectNewSpec: specWithCSISecrets(secretRef),
  100. oldSpec: specWithCSISecrets(secretRef),
  101. expectOldSpec: specWithCSISecrets(secretRef),
  102. },
  103. "disabled csi expansion old pv had secrets": {
  104. csiExpansionEnabled: false,
  105. newSpec: specWithCSISecrets(secretRef),
  106. expectNewSpec: specWithCSISecrets(secretRef),
  107. oldSpec: specWithCSISecrets(secretRef),
  108. expectOldSpec: specWithCSISecrets(secretRef),
  109. },
  110. "enabled csi expansion preserves secrets when old pv did not had secrets": {
  111. csiExpansionEnabled: true,
  112. newSpec: specWithCSISecrets(secretRef),
  113. expectNewSpec: specWithCSISecrets(secretRef),
  114. oldSpec: specWithCSISecrets(nil),
  115. expectOldSpec: specWithCSISecrets(nil),
  116. },
  117. }
  118. for name, tc := range tests {
  119. t.Run(name, func(t *testing.T) {
  120. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, tc.blockEnabled)()
  121. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExpandCSIVolumes, tc.csiExpansionEnabled)()
  122. DropDisabledFields(tc.newSpec, tc.oldSpec)
  123. if !reflect.DeepEqual(tc.newSpec, tc.expectNewSpec) {
  124. t.Error(diff.ObjectReflectDiff(tc.newSpec, tc.expectNewSpec))
  125. }
  126. if !reflect.DeepEqual(tc.oldSpec, tc.expectOldSpec) {
  127. t.Error(diff.ObjectReflectDiff(tc.oldSpec, tc.expectOldSpec))
  128. }
  129. })
  130. }
  131. }
  132. func specWithCSISecrets(secret *api.SecretReference) *api.PersistentVolumeSpec {
  133. pvSpec := &api.PersistentVolumeSpec{
  134. PersistentVolumeSource: api.PersistentVolumeSource{
  135. CSI: &api.CSIPersistentVolumeSource{
  136. Driver: "com.google.gcepd",
  137. VolumeHandle: "foobar",
  138. },
  139. },
  140. }
  141. if secret != nil {
  142. pvSpec.CSI.ControllerExpandSecretRef = secret
  143. }
  144. return pvSpec
  145. }