@@ -17,8 +17,11 @@ limitations under the License.
17
17
package persistentvolumeclaim
18
18
19
19
import (
20
+ "fmt"
21
+ "reflect"
20
22
"testing"
21
23
24
+ "k8s.io/apimachinery/pkg/util/diff"
22
25
utilfeature "k8s.io/apiserver/pkg/util/feature"
23
26
utilfeaturetesting "k8s.io/apiserver/pkg/util/feature/testing"
24
27
"k8s.io/kubernetes/pkg/apis/core"
@@ -28,33 +31,89 @@ import (
28
31
func TestDropAlphaPVCVolumeMode (t * testing.T ) {
29
32
vmode := core .PersistentVolumeFilesystem
30
33
31
- // PersistentVolume with VolumeMode set
32
- pvc := core.PersistentVolumeClaim {
33
- Spec : core.PersistentVolumeClaimSpec {
34
- AccessModes : []core.PersistentVolumeAccessMode {core .ReadWriteOnce },
35
- VolumeMode : & vmode ,
34
+ pvcWithoutVolumeMode := func () * core.PersistentVolumeClaim {
35
+ return & core.PersistentVolumeClaim {
36
+ Spec : core.PersistentVolumeClaimSpec {
37
+ VolumeMode : nil ,
38
+ },
39
+ }
40
+ }
41
+ pvcWithVolumeMode := func () * core.PersistentVolumeClaim {
42
+ return & core.PersistentVolumeClaim {
43
+ Spec : core.PersistentVolumeClaimSpec {
44
+ VolumeMode : & vmode ,
45
+ },
46
+ }
47
+ }
48
+
49
+ pvcInfo := []struct {
50
+ description string
51
+ hasVolumeMode bool
52
+ pvc func () * core.PersistentVolumeClaim
53
+ }{
54
+ {
55
+ description : "pvc without VolumeMode" ,
56
+ hasVolumeMode : false ,
57
+ pvc : pvcWithoutVolumeMode ,
58
+ },
59
+ {
60
+ description : "pvc with Filesystem VolumeMode" ,
61
+ hasVolumeMode : true ,
62
+ pvc : pvcWithVolumeMode ,
63
+ },
64
+ {
65
+ description : "is nil" ,
66
+ hasVolumeMode : false ,
67
+ pvc : func () * core.PersistentVolumeClaim { return nil },
36
68
},
37
69
}
38
70
39
- // Enable alpha feature BlockVolume
40
- defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .BlockVolume , true )()
41
- // now test dropping the fields - should not be dropped
42
- DropDisabledFields (& pvc .Spec , nil )
71
+ for _ , enabled := range []bool {true , false } {
72
+ for _ , oldpvcInfo := range pvcInfo {
73
+ for _ , newpvcInfo := range pvcInfo {
74
+ oldpvcHasVolumeMode , oldpvc := oldpvcInfo .hasVolumeMode , oldpvcInfo .pvc ()
75
+ newpvcHasVolumeMode , newpvc := newpvcInfo .hasVolumeMode , newpvcInfo .pvc ()
76
+ if newpvc == nil {
77
+ continue
78
+ }
43
79
44
- // check to make sure VolumeDevices is still present
45
- // if featureset is set to true
46
- if pvc .Spec .VolumeMode == nil {
47
- t .Error ("VolumeMode in pvc.Spec should not have been dropped based on feature-gate" )
48
- }
80
+ t .Run (fmt .Sprintf ("feature enabled=%v, old pvc %v, new pvc %v" , enabled , oldpvcInfo .description , newpvcInfo .description ), func (t * testing.T ) {
81
+ defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .BlockVolume , enabled )()
82
+
83
+ var oldpvcSpec * core.PersistentVolumeClaimSpec
84
+ if oldpvc != nil {
85
+ oldpvcSpec = & oldpvc .Spec
86
+ }
87
+ DropDisabledFields (& newpvc .Spec , oldpvcSpec )
49
88
50
- // Disable alpha feature BlockVolume
51
- defer utilfeaturetesting . SetFeatureGateDuringTest ( t , utilfeature . DefaultFeatureGate , features . BlockVolume , false )()
52
- // now test dropping the fields
53
- DropDisabledFields ( & pvc . Spec , nil )
89
+ // old pvc should never be changed
90
+ if ! reflect . DeepEqual ( oldpvc , oldpvcInfo . pvc ()) {
91
+ t . Errorf ( "old pvc changed: %v" , diff . ObjectReflectDiff ( oldpvc , oldpvcInfo . pvc ()))
92
+ }
54
93
55
- // check to make sure VolumeDevices is nil
56
- // if featureset is set to false
57
- if pvc .Spec .VolumeMode != nil {
58
- t .Error ("DropDisabledFields VolumeMode for pvc.Spec failed" )
94
+ switch {
95
+ case enabled || oldpvcHasVolumeMode :
96
+ // new pvc should not be changed if the feature is enabled, or if the old pvc had BlockVolume
97
+ if ! reflect .DeepEqual (newpvc , newpvcInfo .pvc ()) {
98
+ t .Errorf ("new pvc changed: %v" , diff .ObjectReflectDiff (newpvc , newpvcInfo .pvc ()))
99
+ }
100
+ case newpvcHasVolumeMode :
101
+ // new pvc should be changed
102
+ if reflect .DeepEqual (newpvc , newpvcInfo .pvc ()) {
103
+ t .Errorf ("new pvc was not changed" )
104
+ }
105
+ // new pvc should not have BlockVolume
106
+ if ! reflect .DeepEqual (newpvc , pvcWithoutVolumeMode ()) {
107
+ t .Errorf ("new pvc had pvcBlockVolume: %v" , diff .ObjectReflectDiff (newpvc , pvcWithoutVolumeMode ()))
108
+ }
109
+ default :
110
+ // new pvc should not need to be changed
111
+ if ! reflect .DeepEqual (newpvc , newpvcInfo .pvc ()) {
112
+ t .Errorf ("new pvc changed: %v" , diff .ObjectReflectDiff (newpvc , newpvcInfo .pvc ()))
113
+ }
114
+ }
115
+ })
116
+ }
117
+ }
59
118
}
60
119
}
0 commit comments