|
| 1 | +package priorityclassname |
| 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/priorityclassname/internal/params" |
| 11 | + appsV1 "k8s.io/api/apps/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func TestPriorityClassName(t *testing.T) { |
| 15 | + suite.Run(t, new(PriorityClassNameTestSuite)) |
| 16 | +} |
| 17 | + |
| 18 | +type PriorityClassNameTestSuite struct { |
| 19 | + templates.TemplateTestSuite |
| 20 | + |
| 21 | + ctx *mocks.MockLintContext |
| 22 | +} |
| 23 | + |
| 24 | +func (s *PriorityClassNameTestSuite) SetupTest() { |
| 25 | + s.Init(templateKey) |
| 26 | + s.ctx = mocks.NewMockContext() |
| 27 | +} |
| 28 | + |
| 29 | +func (s *PriorityClassNameTestSuite) addDeploymentWithPriorityClassName(name, priorityClassName string) { |
| 30 | + s.ctx.AddMockDeployment(s.T(), name) |
| 31 | + s.ctx.ModifyDeployment(s.T(), name, func(deployment *appsV1.Deployment) { |
| 32 | + deployment.Spec.Template.Spec.PriorityClassName = priorityClassName |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +func (s *PriorityClassNameTestSuite) addDeploymentWithEmptyPriorityClassName(name string) { |
| 37 | + s.ctx.AddMockDeployment(s.T(), name) |
| 38 | + s.ctx.ModifyDeployment(s.T(), name, func(deployment *appsV1.Deployment) { |
| 39 | + deployment.Spec.Template.Spec.PriorityClassName = "" |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func (s *PriorityClassNameTestSuite) addDeploymentWithoutPriorityClassName(name string) { |
| 44 | + s.ctx.AddMockDeployment(s.T(), name) |
| 45 | +} |
| 46 | + |
| 47 | +func (s *PriorityClassNameTestSuite) addObjectWithoutPodSpec(name string) { |
| 48 | + s.ctx.AddMockService(s.T(), name) |
| 49 | +} |
| 50 | + |
| 51 | +func (s *PriorityClassNameTestSuite) TestInvalidPriorityClassName() { |
| 52 | + const ( |
| 53 | + invalidPriorityClassName = "invalid-priority-class-name" |
| 54 | + ) |
| 55 | + |
| 56 | + s.addDeploymentWithPriorityClassName(invalidPriorityClassName, "system-node-critical") |
| 57 | + |
| 58 | + s.Validate(s.ctx, []templates.TestCase{ |
| 59 | + { |
| 60 | + Param: params.Params{ |
| 61 | + AcceptedPriorityClassNames: []string{"system-cluster-critical", "custom-priority-class-name"}, |
| 62 | + }, |
| 63 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 64 | + invalidPriorityClassName: { |
| 65 | + {Message: "object has a priority class name defined with 'system-node-critical' but the only accepted priority class names are '[system-cluster-critical custom-priority-class-name]'"}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + ExpectInstantiationError: false, |
| 69 | + }, |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +func (s *PriorityClassNameTestSuite) TestAcceptablePriorityClassName() { |
| 74 | + const ( |
| 75 | + validPriorityClassName = "valid-priority-class-name" |
| 76 | + emptyPriorityClassName = "empty-priotity-class-name" |
| 77 | + withoutPriorityClassName = "without-piority-class-name" |
| 78 | + ) |
| 79 | + |
| 80 | + s.addDeploymentWithPriorityClassName(validPriorityClassName, "system-cluster-critical") |
| 81 | + s.addDeploymentWithEmptyPriorityClassName(emptyPriorityClassName) |
| 82 | + s.addDeploymentWithoutPriorityClassName(withoutPriorityClassName) |
| 83 | + |
| 84 | + s.Validate(s.ctx, []templates.TestCase{ |
| 85 | + { |
| 86 | + Param: params.Params{ |
| 87 | + AcceptedPriorityClassNames: []string{"system-cluster-critical", "custom-priority-class-name"}, |
| 88 | + }, |
| 89 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 90 | + validPriorityClassName: nil, |
| 91 | + emptyPriorityClassName: nil, |
| 92 | + withoutPriorityClassName: nil, |
| 93 | + }, |
| 94 | + ExpectInstantiationError: false, |
| 95 | + }, |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +func (s *PriorityClassNameTestSuite) TestObjectWithoutPodSpec() { |
| 100 | + const ( |
| 101 | + objectWithoutPodSpec = "object-without-pod-spec" |
| 102 | + ) |
| 103 | + |
| 104 | + s.addObjectWithoutPodSpec(objectWithoutPodSpec) |
| 105 | + |
| 106 | + s.Validate(s.ctx, []templates.TestCase{ |
| 107 | + { |
| 108 | + Param: params.Params{}, |
| 109 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 110 | + objectWithoutPodSpec: nil, |
| 111 | + }, |
| 112 | + ExpectInstantiationError: false, |
| 113 | + }, |
| 114 | + }) |
| 115 | +} |
0 commit comments