|
| 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) addObjectWithoutPodSpec(name string) { |
| 49 | + s.ctx.AddMockService(s.T(), name) |
| 50 | +} |
| 51 | + |
| 52 | +func (s *RestartPolicyTestSuite) TestInvalidRestartPolicies() { |
| 53 | + const ( |
| 54 | + withoutRestartPolicy = "without-restart-policy" |
| 55 | + emptyRestartPolicy = "empty-restart-policy" |
| 56 | + restartPolicyNever = "restart-policy-never" |
| 57 | + ) |
| 58 | + |
| 59 | + s.addDeploymentWithoutRestartPolicy(withoutRestartPolicy) |
| 60 | + s.addDeploymentWithEmptyRestartPolicy(emptyRestartPolicy) |
| 61 | + s.addDeploymentWithRestartPolicy(restartPolicyNever, coreV1.RestartPolicyNever) |
| 62 | + |
| 63 | + s.Validate(s.ctx, []templates.TestCase{ |
| 64 | + { |
| 65 | + Param: params.Params{}, |
| 66 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 67 | + withoutRestartPolicy: { |
| 68 | + {Message: "object has a restart policy defined with '' but the only accepted restart policies are '[Always OnFailure]'"}, |
| 69 | + }, |
| 70 | + emptyRestartPolicy: { |
| 71 | + {Message: "object has a restart policy defined with '' but the only accepted restart policies are '[Always OnFailure]'"}, |
| 72 | + }, |
| 73 | + restartPolicyNever: { |
| 74 | + {Message: "object has a restart policy defined with 'Never' but the only accepted restart policies are '[Always OnFailure]'"}, |
| 75 | + }, |
| 76 | + }, |
| 77 | + ExpectInstantiationError: false, |
| 78 | + }, |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func (s *RestartPolicyTestSuite) TestAcceptableRestartPolicy() { |
| 83 | + const ( |
| 84 | + alwaysRestartPolicy = "restart-policy-always" |
| 85 | + onFailureRestartPolicy = "restart-policy-on-failure" |
| 86 | + ) |
| 87 | + s.addDeploymentWithRestartPolicy(alwaysRestartPolicy, coreV1.RestartPolicyAlways) |
| 88 | + s.addDeploymentWithRestartPolicy(onFailureRestartPolicy, coreV1.RestartPolicyOnFailure) |
| 89 | + |
| 90 | + s.Validate(s.ctx, []templates.TestCase{ |
| 91 | + { |
| 92 | + Param: params.Params{}, |
| 93 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 94 | + alwaysRestartPolicy: nil, |
| 95 | + onFailureRestartPolicy: nil, |
| 96 | + }, |
| 97 | + ExpectInstantiationError: false, |
| 98 | + }, |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +func (s *RestartPolicyTestSuite) TestObjectWithoutPodSpec() { |
| 103 | + const ( |
| 104 | + objectWithoutPodSpec = "object-without-pod-spec" |
| 105 | + ) |
| 106 | + |
| 107 | + s.addObjectWithoutPodSpec(objectWithoutPodSpec) |
| 108 | + |
| 109 | + s.Validate(s.ctx, []templates.TestCase{ |
| 110 | + { |
| 111 | + Param: params.Params{}, |
| 112 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 113 | + objectWithoutPodSpec: nil, |
| 114 | + }, |
| 115 | + ExpectInstantiationError: false, |
| 116 | + }, |
| 117 | + }) |
| 118 | +} |
0 commit comments