@@ -17,48 +17,93 @@ limitations under the License.
17
17
package podsecuritypolicy
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
api "k8s.io/kubernetes/pkg/apis/core"
25
28
"k8s.io/kubernetes/pkg/apis/policy"
26
29
"k8s.io/kubernetes/pkg/features"
27
30
)
28
31
29
- func TestDropAlphaProcMountType (t * testing.T ) {
30
- // PodSecurityPolicy with AllowedProcMountTypes set
31
- psp := policy.PodSecurityPolicy {
32
- Spec : policy.PodSecurityPolicySpec {
33
- AllowedProcMountTypes : []api.ProcMountType {api .UnmaskedProcMount },
34
- },
32
+ func TestDropAllowedProcMountTypes (t * testing.T ) {
33
+ allowedProcMountTypes := []api.ProcMountType {api .UnmaskedProcMount }
34
+ scWithoutAllowedProcMountTypes := func () * policy.PodSecurityPolicySpec {
35
+ return & policy.PodSecurityPolicySpec {}
36
+ }
37
+ scWithAllowedProcMountTypes := func () * policy.PodSecurityPolicySpec {
38
+ return & policy.PodSecurityPolicySpec {
39
+ AllowedProcMountTypes : allowedProcMountTypes ,
40
+ }
35
41
}
36
42
37
- // Enable alpha feature ProcMountType
38
- defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ProcMountType , true )()
43
+ scInfo := []struct {
44
+ description string
45
+ hasAllowedProcMountTypes bool
46
+ sc func () * policy.PodSecurityPolicySpec
47
+ }{
48
+ {
49
+ description : "PodSecurityPolicySpec Without AllowedProcMountTypes" ,
50
+ hasAllowedProcMountTypes : false ,
51
+ sc : scWithoutAllowedProcMountTypes ,
52
+ },
53
+ {
54
+ description : "PodSecurityPolicySpec With AllowedProcMountTypes" ,
55
+ hasAllowedProcMountTypes : true ,
56
+ sc : scWithAllowedProcMountTypes ,
57
+ },
58
+ {
59
+ description : "is nil" ,
60
+ hasAllowedProcMountTypes : false ,
61
+ sc : func () * policy.PodSecurityPolicySpec { return nil },
62
+ },
63
+ }
39
64
40
- // now test dropping the fields - should not be dropped
41
- DropDisabledFields (& psp .Spec , nil )
65
+ for _ , enabled := range []bool {true , false } {
66
+ for _ , oldPSPSpecInfo := range scInfo {
67
+ for _ , newPSPSpecInfo := range scInfo {
68
+ oldPSPSpecHasAllowedProcMountTypes , oldPSPSpec := oldPSPSpecInfo .hasAllowedProcMountTypes , oldPSPSpecInfo .sc ()
69
+ newPSPSpecHasAllowedProcMountTypes , newPSPSpec := newPSPSpecInfo .hasAllowedProcMountTypes , newPSPSpecInfo .sc ()
70
+ if newPSPSpec == nil {
71
+ continue
72
+ }
42
73
43
- // check to make sure AllowedProcMountTypes is still present
44
- // if featureset is set to true
45
- if utilfeature .DefaultFeatureGate .Enabled (features .ProcMountType ) {
46
- if psp .Spec .AllowedProcMountTypes == nil {
47
- t .Error ("AllowedProcMountTypes in pvc.Spec should not have been dropped based on feature-gate" )
48
- }
49
- }
74
+ t .Run (fmt .Sprintf ("feature enabled=%v, old PodSecurityPolicySpec %v, new PodSecurityPolicySpec %v" , enabled , oldPSPSpecInfo .description , newPSPSpecInfo .description ), func (t * testing.T ) {
75
+ defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ProcMountType , enabled )()
50
76
51
- // Disable alpha feature ProcMountType
52
- defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ProcMountType , false )()
77
+ DropDisabledFields (newPSPSpec , oldPSPSpec )
53
78
54
- // now test dropping the fields
55
- DropDisabledFields (& psp .Spec , nil )
79
+ // old PodSecurityPolicySpec should never be changed
80
+ if ! reflect .DeepEqual (oldPSPSpec , oldPSPSpecInfo .sc ()) {
81
+ t .Errorf ("old PodSecurityPolicySpec changed: %v" , diff .ObjectReflectDiff (oldPSPSpec , oldPSPSpecInfo .sc ()))
82
+ }
56
83
57
- // check to make sure AllowedProcMountTypes is nil
58
- // if featureset is set to false
59
- if utilfeature .DefaultFeatureGate .Enabled (features .ProcMountType ) {
60
- if psp .Spec .AllowedProcMountTypes != nil {
61
- t .Error ("DropDisabledFields AllowedProcMountTypes for psp.Spec failed" )
84
+ switch {
85
+ case enabled || oldPSPSpecHasAllowedProcMountTypes :
86
+ // new PodSecurityPolicySpec should not be changed if the feature is enabled, or if the old PodSecurityPolicySpec had AllowedProcMountTypes
87
+ if ! reflect .DeepEqual (newPSPSpec , newPSPSpecInfo .sc ()) {
88
+ t .Errorf ("new PodSecurityPolicySpec changed: %v" , diff .ObjectReflectDiff (newPSPSpec , newPSPSpecInfo .sc ()))
89
+ }
90
+ case newPSPSpecHasAllowedProcMountTypes :
91
+ // new PodSecurityPolicySpec should be changed
92
+ if reflect .DeepEqual (newPSPSpec , newPSPSpecInfo .sc ()) {
93
+ t .Errorf ("new PodSecurityPolicySpec was not changed" )
94
+ }
95
+ // new PodSecurityPolicySpec should not have AllowedProcMountTypes
96
+ if ! reflect .DeepEqual (newPSPSpec , scWithoutAllowedProcMountTypes ()) {
97
+ t .Errorf ("new PodSecurityPolicySpec had PodSecurityPolicySpecAllowedProcMountTypes: %v" , diff .ObjectReflectDiff (newPSPSpec , scWithoutAllowedProcMountTypes ()))
98
+ }
99
+ default :
100
+ // new PodSecurityPolicySpec should not need to be changed
101
+ if ! reflect .DeepEqual (newPSPSpec , newPSPSpecInfo .sc ()) {
102
+ t .Errorf ("new PodSecurityPolicySpec changed: %v" , diff .ObjectReflectDiff (newPSPSpec , newPSPSpecInfo .sc ()))
103
+ }
104
+ }
105
+ })
106
+ }
62
107
}
63
108
}
64
109
}
0 commit comments