|
| 1 | +package restartpolicy |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/suite" |
| 7 | + "golang.stackrox.io/kube-linter/pkg/diagnostic" |
| 8 | + "golang.stackrox.io/kube-linter/pkg/lintcontext/mocks" |
| 9 | + "golang.stackrox.io/kube-linter/pkg/templates" |
| 10 | + "golang.stackrox.io/kube-linter/pkg/templates/restartpolicy/internal/params" |
| 11 | + appsV1 "k8s.io/api/apps/v1" |
| 12 | + coreV1 "k8s.io/api/core/v1" |
| 13 | +) |
| 14 | + |
| 15 | +func TestRestartPolicy(t *testing.T) { |
| 16 | + suite.Run(t, new(RestartPolicyTestSuite)) |
| 17 | +} |
| 18 | + |
| 19 | +type RestartPolicyTestSuite struct { |
| 20 | + templates.TemplateTestSuite |
| 21 | + |
| 22 | + ctx *mocks.MockLintContext |
| 23 | +} |
| 24 | + |
| 25 | +func (s *RestartPolicyTestSuite) SetupTest() { |
| 26 | + s.Init(templateKey) |
| 27 | + s.ctx = mocks.NewMockContext() |
| 28 | +} |
| 29 | + |
| 30 | +func (s *RestartPolicyTestSuite) addDeploymentWithRestartPolicy(name string, policy coreV1.RestartPolicy) { |
| 31 | + s.ctx.AddMockDeployment(s.T(), name) |
| 32 | + s.ctx.ModifyDeployment(s.T(), name, func(deployment *appsV1.Deployment) { |
| 33 | + deployment.Spec.Template.Spec.RestartPolicy = policy |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func (s *RestartPolicyTestSuite) addDeploymentWithEmptyRestartPolicy(name string) { |
| 38 | + s.ctx.AddMockDeployment(s.T(), name) |
| 39 | + s.ctx.ModifyDeployment(s.T(), name, func(deployment *appsV1.Deployment) { |
| 40 | + deployment.Spec.Template.Spec.RestartPolicy = "" |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +func (s *RestartPolicyTestSuite) addDeploymentWithoutRestartPolicy(name string) { |
| 45 | + s.ctx.AddMockDeployment(s.T(), name) |
| 46 | +} |
| 47 | + |
| 48 | +func (s *RestartPolicyTestSuite) TestInvalidRestartPolicies() { |
| 49 | + const ( |
| 50 | + withoutRestartPolicy = "without-restart-policy" |
| 51 | + emptyRestartPolicy = "empty-restart-policy" |
| 52 | + restartPolicyNever = "restart-policy-never" |
| 53 | + restartPolicyOnFailure = "restart-policy-on-failure" |
| 54 | + ) |
| 55 | + |
| 56 | + s.addDeploymentWithoutRestartPolicy(withoutRestartPolicy) |
| 57 | + s.addDeploymentWithEmptyRestartPolicy(emptyRestartPolicy) |
| 58 | + s.addDeploymentWithRestartPolicy(restartPolicyNever, coreV1.RestartPolicyNever) |
| 59 | + s.addDeploymentWithRestartPolicy(restartPolicyOnFailure, coreV1.RestartPolicyOnFailure) |
| 60 | + |
| 61 | + s.Validate(s.ctx, []templates.TestCase{ |
| 62 | + { |
| 63 | + Param: params.Params{}, |
| 64 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 65 | + withoutRestartPolicy: { |
| 66 | + {Message: "object has a restart policy defined with '' but the only accepted restart policy is 'Always'"}, |
| 67 | + }, |
| 68 | + emptyRestartPolicy: { |
| 69 | + {Message: "object has a restart policy defined with '' but the only accepted restart policy is 'Always'"}, |
| 70 | + }, |
| 71 | + restartPolicyNever: { |
| 72 | + {Message: "object has a restart policy defined with 'Never' but the only accepted restart policy is 'Always'"}, |
| 73 | + }, |
| 74 | + restartPolicyOnFailure: { |
| 75 | + {Message: "object has a restart policy defined with 'OnFailure' but the only accepted restart policy is 'Always'"}, |
| 76 | + }, |
| 77 | + }, |
| 78 | + ExpectInstantiationError: false, |
| 79 | + }, |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +func (s *RestartPolicyTestSuite) TestAcceptableRestartPolicy() { |
| 84 | + const ( |
| 85 | + acceptableRestartPolicy = "acceptable-restart-policy" |
| 86 | + ) |
| 87 | + s.addDeploymentWithRestartPolicy(acceptableRestartPolicy, coreV1.RestartPolicyAlways) |
| 88 | + |
| 89 | + s.Validate(s.ctx, []templates.TestCase{ |
| 90 | + { |
| 91 | + Param: params.Params{}, |
| 92 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 93 | + acceptableRestartPolicy: nil, |
| 94 | + }, |
| 95 | + ExpectInstantiationError: false, |
| 96 | + }, |
| 97 | + }) |
| 98 | +} |
0 commit comments