|
| 1 | +package dnsconfigoptions |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/suite" |
| 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/dnsconfigoptions/internal/params" |
| 12 | + appsV1 "k8s.io/api/apps/v1" |
| 13 | + v1 "k8s.io/api/core/v1" |
| 14 | +) |
| 15 | + |
| 16 | +func TestDnsconfigOptions(t *testing.T) { |
| 17 | + suite.Run(t, new(DnsconfigOptionsTestSuite)) |
| 18 | +} |
| 19 | + |
| 20 | +type DnsconfigOptionsTestSuite struct { |
| 21 | + templates.TemplateTestSuite |
| 22 | + ctx *mocks.MockLintContext |
| 23 | +} |
| 24 | + |
| 25 | +const ( |
| 26 | + templateKey = "dnsconfig-options" |
| 27 | + deploymentName = "deployment" |
| 28 | + key = "ndots" |
| 29 | + value = "2" |
| 30 | +) |
| 31 | + |
| 32 | +func (s *DnsconfigOptionsTestSuite) SetupTest() { |
| 33 | + s.Init(templateKey) |
| 34 | + s.ctx = mocks.NewMockContext() |
| 35 | +} |
| 36 | + |
| 37 | +func (s *DnsconfigOptionsTestSuite) TestIgnoreDnsconfigOptionsCheckOnObjectWithoutDnsconfig() { |
| 38 | + s.ctx.AddMockClusterRole(s.T(), deploymentName) |
| 39 | + s.Validate(s.ctx, []templates.TestCase{ |
| 40 | + { |
| 41 | + Param: params.Params{Key: key, Value: value}, |
| 42 | + Diagnostics: nil, |
| 43 | + ExpectInstantiationError: false, |
| 44 | + }, |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func (s *DnsconfigOptionsTestSuite) TestNoPodTemplateSpecDnsconfigDefined() { |
| 49 | + s.ctx.AddMockDeployment(s.T(), deploymentName) |
| 50 | + |
| 51 | + s.Validate(s.ctx, []templates.TestCase{ |
| 52 | + { |
| 53 | + Param: params.Params{Key: key, Value: value}, |
| 54 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 55 | + deploymentName: { |
| 56 | + { |
| 57 | + Message: "Object does not define any DNSConfig rules.", |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + ExpectInstantiationError: false, |
| 62 | + }, |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func (s *DnsconfigOptionsTestSuite) TestNoDnsconfigOptionsDefined() { |
| 67 | + s.ctx.AddMockDeployment(s.T(), deploymentName) |
| 68 | + |
| 69 | + s.ctx.ModifyDeployment(s.T(), deploymentName, func(deployment *appsV1.Deployment) { |
| 70 | + dnsconfig := &v1.PodDNSConfig{ |
| 71 | + Options: nil, |
| 72 | + } |
| 73 | + deployment.Spec.Template.Spec.DNSConfig = dnsconfig |
| 74 | + }) |
| 75 | + |
| 76 | + s.Validate(s.ctx, []templates.TestCase{ |
| 77 | + { |
| 78 | + Param: params.Params{Key: key, Value: value}, |
| 79 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 80 | + deploymentName: { |
| 81 | + { |
| 82 | + Message: "Object does not define any DNSConfig Options.", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + ExpectInstantiationError: false, |
| 87 | + }, |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +func (s *DnsconfigOptionsTestSuite) TestDnsconfigOptionsNotMatched() { |
| 92 | + s.ctx.AddMockDeployment(s.T(), deploymentName) |
| 93 | + |
| 94 | + v := "5" |
| 95 | + s.ctx.ModifyDeployment(s.T(), deploymentName, func(deployment *appsV1.Deployment) { |
| 96 | + dnsconfig := &v1.PodDNSConfig{ |
| 97 | + Options: []v1.PodDNSConfigOption{ |
| 98 | + { |
| 99 | + Name: "foo", |
| 100 | + Value: &v, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + deployment.Spec.Template.Spec.DNSConfig = dnsconfig |
| 105 | + }) |
| 106 | + |
| 107 | + s.Validate(s.ctx, []templates.TestCase{ |
| 108 | + { |
| 109 | + Param: params.Params{Key: key, Value: value}, |
| 110 | + Diagnostics: map[string][]diagnostic.Diagnostic{ |
| 111 | + deploymentName: { |
| 112 | + { |
| 113 | + Message: fmt.Sprintf("DNSConfig Options \"%s:%s\" not found.", key, value), |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + ExpectInstantiationError: false, |
| 118 | + }, |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func (s *DnsconfigOptionsTestSuite) TestDnsconfigOptionsMatched() { |
| 123 | + s.ctx.AddMockDeployment(s.T(), deploymentName) |
| 124 | + |
| 125 | + v := "2" |
| 126 | + s.ctx.ModifyDeployment(s.T(), deploymentName, func(deployment *appsV1.Deployment) { |
| 127 | + dnsconfig := &v1.PodDNSConfig{ |
| 128 | + Options: []v1.PodDNSConfigOption{ |
| 129 | + { |
| 130 | + Name: "ndots", |
| 131 | + Value: &v, |
| 132 | + }, |
| 133 | + }, |
| 134 | + } |
| 135 | + deployment.Spec.Template.Spec.DNSConfig = dnsconfig |
| 136 | + }) |
| 137 | + |
| 138 | + s.Validate(s.ctx, []templates.TestCase{ |
| 139 | + { |
| 140 | + Param: params.Params{Key: key, Value: value}, |
| 141 | + Diagnostics: nil, |
| 142 | + ExpectInstantiationError: false, |
| 143 | + }, |
| 144 | + }) |
| 145 | +} |
0 commit comments