util_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 persistentvolumeclaim
  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/core"
  22. "k8s.io/kubernetes/pkg/features"
  23. )
  24. func TestDropAlphaPVCVolumeMode(t *testing.T) {
  25. vmode := core.PersistentVolumeFilesystem
  26. pvcWithoutVolumeMode := func() *core.PersistentVolumeClaim {
  27. return &core.PersistentVolumeClaim{
  28. Spec: core.PersistentVolumeClaimSpec{
  29. VolumeMode: nil,
  30. },
  31. }
  32. }
  33. pvcWithVolumeMode := func() *core.PersistentVolumeClaim {
  34. return &core.PersistentVolumeClaim{
  35. Spec: core.PersistentVolumeClaimSpec{
  36. VolumeMode: &vmode,
  37. },
  38. }
  39. }
  40. pvcInfo := []struct {
  41. description string
  42. hasVolumeMode bool
  43. pvc func() *core.PersistentVolumeClaim
  44. }{
  45. {
  46. description: "pvc without VolumeMode",
  47. hasVolumeMode: false,
  48. pvc: pvcWithoutVolumeMode,
  49. },
  50. {
  51. description: "pvc with Filesystem VolumeMode",
  52. hasVolumeMode: true,
  53. pvc: pvcWithVolumeMode,
  54. },
  55. {
  56. description: "is nil",
  57. hasVolumeMode: false,
  58. pvc: func() *core.PersistentVolumeClaim { return nil },
  59. },
  60. }
  61. for _, enabled := range []bool{true, false} {
  62. for _, oldpvcInfo := range pvcInfo {
  63. for _, newpvcInfo := range pvcInfo {
  64. oldpvcHasVolumeMode, oldpvc := oldpvcInfo.hasVolumeMode, oldpvcInfo.pvc()
  65. newpvcHasVolumeMode, newpvc := newpvcInfo.hasVolumeMode, newpvcInfo.pvc()
  66. if newpvc == nil {
  67. continue
  68. }
  69. t.Run(fmt.Sprintf("feature enabled=%v, old pvc %v, new pvc %v", enabled, oldpvcInfo.description, newpvcInfo.description), func(t *testing.T) {
  70. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, enabled)()
  71. var oldpvcSpec *core.PersistentVolumeClaimSpec
  72. if oldpvc != nil {
  73. oldpvcSpec = &oldpvc.Spec
  74. }
  75. DropDisabledFields(&newpvc.Spec, oldpvcSpec)
  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 || oldpvcHasVolumeMode:
  82. // new pvc should not be changed if the feature is enabled, or if the old pvc had BlockVolume
  83. if !reflect.DeepEqual(newpvc, newpvcInfo.pvc()) {
  84. t.Errorf("new pvc changed: %v", diff.ObjectReflectDiff(newpvc, newpvcInfo.pvc()))
  85. }
  86. case newpvcHasVolumeMode:
  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 BlockVolume
  92. if !reflect.DeepEqual(newpvc, pvcWithoutVolumeMode()) {
  93. t.Errorf("new pvc had pvcBlockVolume: %v", diff.ObjectReflectDiff(newpvc, pvcWithoutVolumeMode()))
  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. }
  106. func TestDropDisabledSnapshotDataSource(t *testing.T) {
  107. pvcWithoutDataSource := func() *core.PersistentVolumeClaim {
  108. return &core.PersistentVolumeClaim{
  109. Spec: core.PersistentVolumeClaimSpec{
  110. DataSource: nil,
  111. },
  112. }
  113. }
  114. apiGroup := "snapshot.storage.k8s.io"
  115. pvcWithDataSource := func() *core.PersistentVolumeClaim {
  116. return &core.PersistentVolumeClaim{
  117. Spec: core.PersistentVolumeClaimSpec{
  118. DataSource: &core.TypedLocalObjectReference{
  119. APIGroup: &apiGroup,
  120. Kind: "VolumeSnapshot",
  121. Name: "test_snapshot",
  122. },
  123. },
  124. }
  125. }
  126. pvcInfo := []struct {
  127. description string
  128. hasDataSource bool
  129. pvc func() *core.PersistentVolumeClaim
  130. }{
  131. {
  132. description: "pvc without DataSource",
  133. hasDataSource: false,
  134. pvc: pvcWithoutDataSource,
  135. },
  136. {
  137. description: "pvc with DataSource",
  138. hasDataSource: true,
  139. pvc: pvcWithDataSource,
  140. },
  141. {
  142. description: "is nil",
  143. hasDataSource: false,
  144. pvc: func() *core.PersistentVolumeClaim { return nil },
  145. },
  146. }
  147. for _, enabled := range []bool{true, false} {
  148. for _, oldpvcInfo := range pvcInfo {
  149. for _, newpvcInfo := range pvcInfo {
  150. oldPvcHasDataSource, oldpvc := oldpvcInfo.hasDataSource, oldpvcInfo.pvc()
  151. newPvcHasDataSource, newpvc := newpvcInfo.hasDataSource, newpvcInfo.pvc()
  152. if newpvc == nil {
  153. continue
  154. }
  155. t.Run(fmt.Sprintf("feature enabled=%v, old pvc %v, new pvc %v", enabled, oldpvcInfo.description, newpvcInfo.description), func(t *testing.T) {
  156. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.VolumeSnapshotDataSource, enabled)()
  157. var oldpvcSpec *core.PersistentVolumeClaimSpec
  158. if oldpvc != nil {
  159. oldpvcSpec = &oldpvc.Spec
  160. }
  161. DropDisabledFields(&newpvc.Spec, oldpvcSpec)
  162. // old pvc should never be changed
  163. if !reflect.DeepEqual(oldpvc, oldpvcInfo.pvc()) {
  164. t.Errorf("old pvc changed: %v", diff.ObjectReflectDiff(oldpvc, oldpvcInfo.pvc()))
  165. }
  166. switch {
  167. case enabled || oldPvcHasDataSource:
  168. // new pvc should not be changed if the feature is enabled, or if the old pvc had DataSource
  169. if !reflect.DeepEqual(newpvc, newpvcInfo.pvc()) {
  170. t.Errorf("new pvc changed: %v", diff.ObjectReflectDiff(newpvc, newpvcInfo.pvc()))
  171. }
  172. case newPvcHasDataSource:
  173. // new pvc should be changed
  174. if reflect.DeepEqual(newpvc, newpvcInfo.pvc()) {
  175. t.Errorf("new pvc was not changed")
  176. }
  177. // new pvc should not have DataSource
  178. if !reflect.DeepEqual(newpvc, pvcWithoutDataSource()) {
  179. t.Errorf("new pvc had DataSource: %v", diff.ObjectReflectDiff(newpvc, pvcWithoutDataSource()))
  180. }
  181. default:
  182. // new pvc should not need to be changed
  183. if !reflect.DeepEqual(newpvc, newpvcInfo.pvc()) {
  184. t.Errorf("new pvc changed: %v", diff.ObjectReflectDiff(newpvc, newpvcInfo.pvc()))
  185. }
  186. }
  187. })
  188. }
  189. }
  190. }
  191. }
  192. // TestPVCDataSourceSpecFilter checks to ensure the DropDisabledFields function behaves correctly for PVCDataSource featuregate
  193. func TestPVCDataSourceSpecFilter(t *testing.T) {
  194. apiGroup := ""
  195. validSpec := core.PersistentVolumeClaimSpec{
  196. DataSource: &core.TypedLocalObjectReference{
  197. APIGroup: &apiGroup,
  198. Kind: "PersistentVolumeClaim",
  199. Name: "test_clone",
  200. },
  201. }
  202. validSpecNilAPIGroup := core.PersistentVolumeClaimSpec{
  203. DataSource: &core.TypedLocalObjectReference{
  204. Kind: "PersistentVolumeClaim",
  205. Name: "test_clone",
  206. },
  207. }
  208. invalidAPIGroup := "invalid.pvc.api.group"
  209. invalidSpec := core.PersistentVolumeClaimSpec{
  210. DataSource: &core.TypedLocalObjectReference{
  211. APIGroup: &invalidAPIGroup,
  212. Kind: "PersistentVolumeClaim",
  213. Name: "test_clone_invalid",
  214. },
  215. }
  216. var tests = map[string]struct {
  217. spec core.PersistentVolumeClaimSpec
  218. gateEnabled bool
  219. want *core.TypedLocalObjectReference
  220. }{
  221. "enabled with empty ds": {
  222. spec: core.PersistentVolumeClaimSpec{},
  223. gateEnabled: true,
  224. want: nil,
  225. },
  226. "enabled with invalid spec": {
  227. spec: invalidSpec,
  228. gateEnabled: true,
  229. want: nil,
  230. },
  231. "enabled with valid spec": {
  232. spec: validSpec,
  233. gateEnabled: true,
  234. want: validSpec.DataSource,
  235. },
  236. "disabled with invalid spec": {
  237. spec: invalidSpec,
  238. gateEnabled: false,
  239. want: nil,
  240. },
  241. "disabled with valid spec": {
  242. spec: validSpec,
  243. gateEnabled: false,
  244. want: nil,
  245. },
  246. "diabled with empty ds": {
  247. spec: core.PersistentVolumeClaimSpec{},
  248. gateEnabled: false,
  249. want: nil,
  250. },
  251. "enabled with valid spec but nil APIGroup": {
  252. spec: validSpecNilAPIGroup,
  253. gateEnabled: true,
  254. want: validSpecNilAPIGroup.DataSource,
  255. },
  256. "disabled with valid spec but nil APIGroup": {
  257. spec: validSpecNilAPIGroup,
  258. gateEnabled: false,
  259. want: nil,
  260. },
  261. }
  262. for testName, test := range tests {
  263. t.Run(testName, func(t *testing.T) {
  264. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.VolumePVCDataSource, test.gateEnabled)()
  265. DropDisabledFields(&test.spec, nil)
  266. if test.spec.DataSource != test.want {
  267. t.Errorf("expected drop datasource condition was not met, test: %s, gateEnabled: %v, spec: %v, expected: %v", testName, test.gateEnabled, test.spec, test.want)
  268. }
  269. })
  270. }
  271. }