|
1 | 1 | package configmap
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
| 4 | + "fmt" |
5 | 5 | "testing"
|
6 | 6 |
|
7 | 7 | "github.com/stretchr/testify/assert"
|
8 | 8 | "golang.stackrox.io/kube-linter/pkg/config"
|
9 |
| - corev1 "k8s.io/api/core/v1" |
10 |
| - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
11 |
| - "k8s.io/apimachinery/pkg/runtime" |
12 |
| - kubefake "k8s.io/client-go/kubernetes/fake" |
13 | 9 | )
|
14 | 10 |
|
15 |
| -func TestStaticConfigMapWatcher(t *testing.T) { |
16 |
| - var configMapNamespace = "deployment-validation-operator" |
17 |
| - |
18 |
| - testCases := []struct { |
19 |
| - name string |
20 |
| - data string |
21 |
| - checks config.ChecksConfig |
| 11 | +func TestReadConfig(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + configData string |
| 15 | + expectedConfig config.Config |
| 16 | + expectedError error |
22 | 17 | }{
|
23 | 18 | {
|
24 |
| - name: "kube-linter 'doNotAutoAddDefaults' is gathered from configuration", |
25 |
| - data: "checks:\n doNotAutoAddDefaults: true", |
26 |
| - checks: config.ChecksConfig{ |
27 |
| - DoNotAutoAddDefaults: true, |
| 19 | + name: "Basic valid config", |
| 20 | + configData: ` |
| 21 | +checks: |
| 22 | + doNotAutoAddDefaults: false |
| 23 | + addAllBuiltIn: true |
| 24 | + include: |
| 25 | + - "unset-memory-requirements" |
| 26 | + - "unset-cpu-requirements"`, |
| 27 | + expectedConfig: config.Config{ |
| 28 | + Checks: config.ChecksConfig{ |
| 29 | + AddAllBuiltIn: true, |
| 30 | + DoNotAutoAddDefaults: false, |
| 31 | + Include: []string{"unset-memory-requirements", "unset-cpu-requirements"}, // nolint: lll |
| 32 | + }, |
28 | 33 | },
|
| 34 | + expectedError: nil, |
29 | 35 | },
|
30 | 36 | {
|
31 |
| - name: "kube-linter 'addAllBuiltIn' is gathered from configuration", |
32 |
| - data: "checks:\n addAllBuiltIn: true", |
33 |
| - checks: config.ChecksConfig{ |
34 |
| - AddAllBuiltIn: true, |
| 37 | + name: "Invalid config field \"doNotAutoAddDefaultsAAA\"", |
| 38 | + configData: ` |
| 39 | +checks: |
| 40 | + doNotAutoAddDefaultsAAA: false |
| 41 | + addAllBuiltIn: true |
| 42 | + include: |
| 43 | + - "unset-memory-requirements" |
| 44 | + - "unset-cpu-requirements"`, |
| 45 | + expectedError: fmt.Errorf("unmarshalling configmap data: error unmarshaling JSON: while decoding JSON: json: unknown field \"doNotAutoAddDefaultsAAA\""), // nolint: lll |
| 46 | + expectedConfig: config.Config{ |
| 47 | + Checks: config.ChecksConfig{ |
| 48 | + AddAllBuiltIn: true, |
| 49 | + DoNotAutoAddDefaults: false, |
| 50 | + Include: []string{"unset-memory-requirements", "unset-cpu-requirements"}, // nolint: lll |
| 51 | + }, |
35 | 52 | },
|
36 | 53 | },
|
37 | 54 | {
|
38 |
| - name: "kube-linter 'exclude' is gathered from configuration", |
39 |
| - data: "checks:\n exclude: [\"check1\", \"check2\"]", |
40 |
| - checks: config.ChecksConfig{ |
41 |
| - Exclude: []string{"check1", "check2"}, |
| 55 | + name: "Invalid config field \"include\"", |
| 56 | + configData: ` |
| 57 | +checks: |
| 58 | + doNotAutoAddDefaults: false |
| 59 | + addAllBuiltIn: true |
| 60 | + includeaa: |
| 61 | + - "unset-memory-requirements" |
| 62 | + - "unset-cpu-requirements"`, |
| 63 | + expectedError: fmt.Errorf("unmarshalling configmap data: error unmarshaling JSON: while decoding JSON: json: unknown field \"includeaa\""), // nolint: lll |
| 64 | + expectedConfig: config.Config{ |
| 65 | + Checks: config.ChecksConfig{ |
| 66 | + AddAllBuiltIn: true, |
| 67 | + DoNotAutoAddDefaults: false, |
| 68 | + }, |
42 | 69 | },
|
43 | 70 | },
|
44 | 71 | {
|
45 |
| - name: "kube-linter 'include' is gathered from configuration", |
46 |
| - data: "checks:\n include: [\"check1\", \"check2\"]", |
47 |
| - checks: config.ChecksConfig{ |
48 |
| - Include: []string{"check1", "check2"}, |
| 72 | + name: "Valid config with custom check", |
| 73 | + configData: ` |
| 74 | +checks: |
| 75 | + doNotAutoAddDefaults: false |
| 76 | + addAllBuiltIn: true |
| 77 | + include: |
| 78 | + - "unset-memory-requirements" |
| 79 | +customChecks: |
| 80 | + - name: test-minimum-replicas |
| 81 | + description: "some description" |
| 82 | + remediation: "some remediation" |
| 83 | + template: minimum-replicas |
| 84 | + params: |
| 85 | + minReplicas: 3 |
| 86 | + scope: |
| 87 | + objectKinds: |
| 88 | + - DeploymentLike`, |
| 89 | + expectedError: nil, |
| 90 | + expectedConfig: config.Config{ |
| 91 | + Checks: config.ChecksConfig{ |
| 92 | + AddAllBuiltIn: true, |
| 93 | + DoNotAutoAddDefaults: false, |
| 94 | + Include: []string{"unset-memory-requirements"}, |
| 95 | + }, |
| 96 | + CustomChecks: []config.Check{ |
| 97 | + { |
| 98 | + Name: "test-minimum-replicas", |
| 99 | + Description: "some description", |
| 100 | + Remediation: "some remediation", |
| 101 | + Template: "minimum-replicas", |
| 102 | + Params: map[string]interface{}{ |
| 103 | + "minReplicas": float64(3), |
| 104 | + }, |
| 105 | + Scope: &config.ObjectKindsDesc{ |
| 106 | + ObjectKinds: []string{"DeploymentLike"}, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
49 | 110 | },
|
50 | 111 | },
|
51 | 112 | }
|
| 113 | + for _, tt := range tests { |
| 114 | + t.Run(tt.name, func(t *testing.T) { |
52 | 115 |
|
53 |
| - for _, testCase := range testCases { |
54 |
| - t.Run(testCase.name, func(t *testing.T) { |
55 |
| - // Given |
56 |
| - cm := &corev1.ConfigMap{ |
57 |
| - ObjectMeta: metav1.ObjectMeta{Namespace: configMapNamespace, Name: configMapName}, |
58 |
| - Data: map[string]string{ |
59 |
| - "deployment-validation-operator-config.yaml": testCase.data, |
60 |
| - }, |
| 116 | + cfg, err := readConfig(tt.configData) |
| 117 | + if tt.expectedError != nil { |
| 118 | + assert.Equal(t, tt.expectedError.Error(), err.Error()) |
| 119 | + } else { |
| 120 | + assert.NoError(t, err) |
61 | 121 | }
|
62 |
| - client := kubefake.NewSimpleClientset([]runtime.Object{cm}...) |
63 |
| - mock := Watcher{clientset: client, namespace: configMapNamespace} |
64 |
| - |
65 |
| - // When |
66 |
| - test, err := mock.GetStaticKubelinterConfig(context.Background()) |
67 |
| - |
68 |
| - // Assert |
69 |
| - assert.NoError(t, err) |
70 |
| - assert.Equal(t, testCase.checks, test.Checks) |
| 122 | + assert.Equal(t, tt.expectedConfig, cfg) |
71 | 123 | })
|
72 | 124 | }
|
73 | 125 | }
|
0 commit comments