From 90b98086814f86a631c9cb01a891e6aa4c96fda3 Mon Sep 17 00:00:00 2001 From: mourya007 Date: Thu, 7 Feb 2019 11:14:53 +0530 Subject: [PATCH] Add missing testcases --- pkg/apis/policy/validation/validation_test.go | 79 ++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/pkg/apis/policy/validation/validation_test.go b/pkg/apis/policy/validation/validation_test.go index aecd163afd..b36f03d01a 100644 --- a/pkg/apis/policy/validation/validation_test.go +++ b/pkg/apis/policy/validation/validation_test.go @@ -802,7 +802,7 @@ func TestIsValidSysctlPattern(t *testing.T) { } } -func Test_validatePSPRunAsUser(t *testing.T) { +func TestValidatePSPRunAsUser(t *testing.T) { var testCases = []struct { name string runAsUserStrategy policy.RunAsUserStrategyOptions @@ -829,3 +829,80 @@ func Test_validatePSPRunAsUser(t *testing.T) { }) } } +func TestValidatePSPFSGroup(t *testing.T) { + var testCases = []struct { + name string + fsGroupStrategy policy.FSGroupStrategyOptions + fail bool + }{ + {"Invalid FSGroupStrategy", policy.FSGroupStrategyOptions{Rule: policy.FSGroupStrategyType("someInvalidStrategy")}, true}, + {"FSGroupStrategyMustRunAs", policy.FSGroupStrategyOptions{Rule: policy.FSGroupStrategyMustRunAs}, false}, + {"FSGroupStrategyMayRunAs", policy.FSGroupStrategyOptions{Rule: policy.FSGroupStrategyMayRunAs, Ranges: []policy.IDRange{{Min: 1, Max: 5}}}, false}, + {"FSGroupStrategyRunAsAny", policy.FSGroupStrategyOptions{Rule: policy.FSGroupStrategyRunAsAny}, false}, + } + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + errList := validatePSPFSGroup(field.NewPath("Status"), &testCase.fsGroupStrategy) + actualErrors := len(errList) + expectedErrors := 1 + if !testCase.fail { + expectedErrors = 0 + } + if actualErrors != expectedErrors { + t.Errorf("In testCase %v, expected %v errors, got %v errors", testCase.name, expectedErrors, actualErrors) + } + }) + } + +} + +func TestValidatePSPSupplementalGroup(t *testing.T) { + var testCases = []struct { + name string + supplementalGroupStrategy policy.SupplementalGroupsStrategyOptions + fail bool + }{ + {"Invalid SupplementalGroupStrategy", policy.SupplementalGroupsStrategyOptions{Rule: policy.SupplementalGroupsStrategyType("someInvalidStrategy")}, true}, + {"SupplementalGroupsStrategyMustRunAs", policy.SupplementalGroupsStrategyOptions{Rule: policy.SupplementalGroupsStrategyMustRunAs}, false}, + {"SupplementalGroupsStrategyMayRunAs", policy.SupplementalGroupsStrategyOptions{Rule: policy.SupplementalGroupsStrategyMayRunAs, Ranges: []policy.IDRange{{Min: 1, Max: 5}}}, false}, + {"SupplementalGroupsStrategyRunAsAny", policy.SupplementalGroupsStrategyOptions{Rule: policy.SupplementalGroupsStrategyRunAsAny}, false}, + } + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + errList := validatePSPSupplementalGroup(field.NewPath("Status"), &testCase.supplementalGroupStrategy) + actualErrors := len(errList) + expectedErrors := 1 + if !testCase.fail { + expectedErrors = 0 + } + if actualErrors != expectedErrors { + t.Errorf("In testCase %v, expected %v errors, got %v errors", testCase.name, expectedErrors, actualErrors) + } + }) + } +} + +func TestValidatePSPRunAsGroup(t *testing.T) { + var testCases = []struct { + name string + runAsGroup policy.RunAsGroupStrategyOptions + fail bool + }{ + {"RunAsGroupStrategyMayRunAs", policy.RunAsGroupStrategyOptions{Rule: policy.RunAsGroupStrategyMayRunAs, Ranges: []policy.IDRange{{Min: 1, Max: 5}}}, false}, + {"RunAsGroupStrategyMustRunAs", policy.RunAsGroupStrategyOptions{Rule: policy.RunAsGroupStrategyMustRunAs, Ranges: []policy.IDRange{{Min: 1, Max: 5}}}, false}, + {"RunAsGroupStrategyRunAsAny", policy.RunAsGroupStrategyOptions{Rule: policy.RunAsGroupStrategyRunAsAny}, false}, + } + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + errList := validatePSPRunAsGroup(field.NewPath("Status"), &testCase.runAsGroup) + actualErrors := len(errList) + expectedErrors := 1 + if !testCase.fail { + expectedErrors = 0 + } + if actualErrors != expectedErrors { + t.Errorf("In testCase %v, expected %v errors, got %v errors", testCase.name, expectedErrors, actualErrors) + } + }) + } +}