add unit test for RunAsGroup in both pod and podsecuritypolicy

This commit is contained in:
Zheng Dayu
2018-12-31 01:10:06 +08:00
parent d4c85e977f
commit 020e54cce7
2 changed files with 217 additions and 0 deletions

View File

@@ -107,3 +107,83 @@ func TestDropAllowedProcMountTypes(t *testing.T) {
}
}
}
func TestDropRunAsGroup(t *testing.T) {
group := func() *policy.RunAsGroupStrategyOptions {
return &policy.RunAsGroupStrategyOptions{}
}
scWithoutRunAsGroup := func() *policy.PodSecurityPolicySpec {
return &policy.PodSecurityPolicySpec{}
}
scWithRunAsGroup := func() *policy.PodSecurityPolicySpec {
return &policy.PodSecurityPolicySpec{
RunAsGroup: group(),
}
}
scInfo := []struct {
description string
hasRunAsGroup bool
sc func() *policy.PodSecurityPolicySpec
}{
{
description: "PodSecurityPolicySpec Without RunAsGroup",
hasRunAsGroup: false,
sc: scWithoutRunAsGroup,
},
{
description: "PodSecurityPolicySpec With RunAsGroup",
hasRunAsGroup: true,
sc: scWithRunAsGroup,
},
{
description: "is nil",
hasRunAsGroup: false,
sc: func() *policy.PodSecurityPolicySpec { return nil },
},
}
for _, enabled := range []bool{true, false} {
for _, oldPSPSpecInfo := range scInfo {
for _, newPSPSpecInfo := range scInfo {
oldPSPSpecHasRunAsGroup, oldPSPSpec := oldPSPSpecInfo.hasRunAsGroup, oldPSPSpecInfo.sc()
newPSPSpecHasRunAsGroup, newPSPSpec := newPSPSpecInfo.hasRunAsGroup, newPSPSpecInfo.sc()
if newPSPSpec == nil {
continue
}
t.Run(fmt.Sprintf("feature enabled=%v, old PodSecurityPolicySpec %v, new PodSecurityPolicySpec %v", enabled, oldPSPSpecInfo.description, newPSPSpecInfo.description), func(t *testing.T) {
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.RunAsGroup, enabled)()
DropDisabledFields(newPSPSpec, oldPSPSpec)
// old PodSecurityPolicySpec should never be changed
if !reflect.DeepEqual(oldPSPSpec, oldPSPSpecInfo.sc()) {
t.Errorf("old PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(oldPSPSpec, oldPSPSpecInfo.sc()))
}
switch {
case enabled || oldPSPSpecHasRunAsGroup:
// new PodSecurityPolicySpec should not be changed if the feature is enabled, or if the old PodSecurityPolicySpec had RunAsGroup
if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
}
case newPSPSpecHasRunAsGroup:
// new PodSecurityPolicySpec should be changed
if reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
t.Errorf("new PodSecurityPolicySpec was not changed")
}
// new PodSecurityPolicySpec should not have RunAsGroup
if !reflect.DeepEqual(newPSPSpec, scWithoutRunAsGroup()) {
t.Errorf("new PodSecurityPolicySpec had RunAsGroup: %v", diff.ObjectReflectDiff(newPSPSpec, scWithoutRunAsGroup()))
}
default:
// new PodSecurityPolicySpec should not need to be changed
if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
}
}
})
}
}
}
}