|
| 1 | +package serviceaccount |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/suite" |
| 7 | + "golang.stackrox.io/kube-linter/internal/pointers" |
| 8 | + "golang.stackrox.io/kube-linter/pkg/diagnostic" |
| 9 | + "golang.stackrox.io/kube-linter/pkg/lintcontext/mocks" |
| 10 | + "golang.stackrox.io/kube-linter/pkg/templates" |
| 11 | + "golang.stackrox.io/kube-linter/pkg/templates/serviceaccount/internal/params" |
| 12 | + appsV1 "k8s.io/api/apps/v1" |
| 13 | +) |
| 14 | + |
| 15 | +func TestServiceAccount(t *testing.T) { |
| 16 | + suite.Run(t, new(ServiceAccountTestSuite)) |
| 17 | +} |
| 18 | + |
| 19 | +type ServiceAccountTestSuite struct { |
| 20 | + templates.TemplateTestSuite |
| 21 | + |
| 22 | + ctx *mocks.MockLintContext |
| 23 | +} |
| 24 | + |
| 25 | +func (s *ServiceAccountTestSuite) SetupTest() { |
| 26 | + s.Init(templateKey) |
| 27 | + s.ctx = mocks.NewMockContext() |
| 28 | +} |
| 29 | + |
| 30 | +func (s *ServiceAccountTestSuite) addDeploymentWithServiceAccount(name, serviceAccountName string, automountServiceAccountToken *bool) { |
| 31 | + s.ctx.AddMockDeployment(s.T(), name) |
| 32 | + s.ctx.ModifyDeployment(s.T(), name, func(deployment *appsV1.Deployment) { |
| 33 | + deployment.Spec.Template.Spec.ServiceAccountName = serviceAccountName |
| 34 | + deployment.Spec.Template.Spec.AutomountServiceAccountToken = automountServiceAccountToken |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +func (s *ServiceAccountTestSuite) TestServiceAccountName() { |
| 39 | + const ( |
| 40 | + matchingSAWithAutoMountTokenNil = "match-sa-token-mount-nil" |
| 41 | + matchingSAWithAutoMountTokenTrue = "match-sa-token-mount-true" |
| 42 | + matchingSAWithAutoMountTokenFalse = "match-sa-token-mount-false" |
| 43 | + nonMatchingSAWithAutoMountTokenNil = "non-match-sa-token-mount-nil" |
| 44 | + nonMatchingSAWithAutoMountTokenTrue = "non-match-sa-token-mount-true" |
| 45 | + nonMatchingSAWithAutoMountTokenFalse = "non-match-sa-token-mount-false" |
| 46 | + ) |
| 47 | + |
| 48 | + s.addDeploymentWithServiceAccount(matchingSAWithAutoMountTokenNil, "non-default", nil) |
| 49 | + s.addDeploymentWithServiceAccount(matchingSAWithAutoMountTokenTrue, "non-default", pointers.Bool(true)) |
| 50 | + s.addDeploymentWithServiceAccount(matchingSAWithAutoMountTokenFalse, "non-default", pointers.Bool(false)) |
| 51 | + s.addDeploymentWithServiceAccount(nonMatchingSAWithAutoMountTokenNil, "", nil) |
| 52 | + s.addDeploymentWithServiceAccount(nonMatchingSAWithAutoMountTokenTrue, "", pointers.Bool(true)) |
| 53 | + s.addDeploymentWithServiceAccount(nonMatchingSAWithAutoMountTokenFalse, "", pointers.Bool(false)) |
| 54 | + |
| 55 | + s.Validate(s.ctx, []templates.TestCase{ |
| 56 | + { |
| 57 | + Param: params.Params{ |
| 58 | + ServiceAccount: "non-default", |
| 59 | + }, |
| 60 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 61 | + matchingSAWithAutoMountTokenNil: { |
| 62 | + {Message: "found matching serviceAccount (\"non-default\")"}, |
| 63 | + }, |
| 64 | + matchingSAWithAutoMountTokenTrue: { |
| 65 | + {Message: "found matching serviceAccount (\"non-default\")"}, |
| 66 | + }, |
| 67 | + matchingSAWithAutoMountTokenFalse: {}, |
| 68 | + nonMatchingSAWithAutoMountTokenNil: {}, |
| 69 | + nonMatchingSAWithAutoMountTokenTrue: {}, |
| 70 | + nonMatchingSAWithAutoMountTokenFalse: {}, |
| 71 | + }, |
| 72 | + ExpectInstantiationError: false, |
| 73 | + }, |
| 74 | + { |
| 75 | + Param: params.Params{ |
| 76 | + ServiceAccount: "[a)", // Wrong Regex which should raise an error |
| 77 | + }, |
| 78 | + ExpectInstantiationError: true, |
| 79 | + }, |
| 80 | + }) |
| 81 | +} |
0 commit comments