|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/pkg/errors" |
| 7 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 8 | + "github.com/replicatedhq/troubleshoot/pkg/collect" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_analyzeConfigMap(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + analyzer *troubleshootv1beta2.AnalyzeConfigMap |
| 17 | + mockFiles map[string][]byte |
| 18 | + want *AnalyzeResult |
| 19 | + wantErr bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "found", |
| 23 | + analyzer: &troubleshootv1beta2.AnalyzeConfigMap{ |
| 24 | + Namespace: "test-namespace", |
| 25 | + ConfigMapName: "test-configmap", |
| 26 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 27 | + { |
| 28 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 29 | + Message: "Not found", |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 34 | + Message: "Found", |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + mockFiles: map[string][]byte{ |
| 40 | + "configmaps/test-namespace/test-configmap.json": mustJSONMarshalIndent(t, collect.ConfigMapOutput{ |
| 41 | + Namespace: "test-namespace", |
| 42 | + Name: "test-configmap", |
| 43 | + ConfigMapExists: true, |
| 44 | + }), |
| 45 | + }, |
| 46 | + want: &AnalyzeResult{ |
| 47 | + IsPass: true, |
| 48 | + Message: "Found", |
| 49 | + Title: "ConfigMap test-configmap", |
| 50 | + IconKey: "kubernetes_analyze_secret", |
| 51 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16", |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "not found", |
| 56 | + analyzer: &troubleshootv1beta2.AnalyzeConfigMap{ |
| 57 | + AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{ |
| 58 | + CheckName: "test configmap analyzer", |
| 59 | + }, |
| 60 | + Namespace: "test-namespace", |
| 61 | + ConfigMapName: "test-configmap", |
| 62 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 63 | + { |
| 64 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 65 | + Message: "Not found", |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 70 | + Message: "Found", |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + mockFiles: map[string][]byte{ |
| 76 | + "configmaps/test-namespace/test-configmap.json": mustJSONMarshalIndent(t, collect.ConfigMapOutput{ |
| 77 | + Namespace: "test-namespace", |
| 78 | + Name: "test-configmap", |
| 79 | + ConfigMapExists: false, |
| 80 | + }), |
| 81 | + }, |
| 82 | + want: &AnalyzeResult{ |
| 83 | + IsFail: true, |
| 84 | + Message: "Not found", |
| 85 | + Title: "test configmap analyzer", |
| 86 | + IconKey: "kubernetes_analyze_secret", |
| 87 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16", |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "key found", |
| 92 | + analyzer: &troubleshootv1beta2.AnalyzeConfigMap{ |
| 93 | + AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{ |
| 94 | + CheckName: "test configmap analyzer", |
| 95 | + }, |
| 96 | + Namespace: "test-namespace", |
| 97 | + ConfigMapName: "test-configmap", |
| 98 | + Key: "test-key", |
| 99 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 100 | + { |
| 101 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 102 | + Message: "Key not found", |
| 103 | + }, |
| 104 | + }, |
| 105 | + { |
| 106 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 107 | + Message: "Key found", |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + mockFiles: map[string][]byte{ |
| 113 | + "configmaps/test-namespace/test-configmap/test-key.json": mustJSONMarshalIndent(t, collect.ConfigMapOutput{ |
| 114 | + Namespace: "test-namespace", |
| 115 | + Name: "test-configmap", |
| 116 | + Key: "test-key", |
| 117 | + ConfigMapExists: true, |
| 118 | + KeyExists: true, |
| 119 | + }), |
| 120 | + }, |
| 121 | + want: &AnalyzeResult{ |
| 122 | + IsPass: true, |
| 123 | + Message: "Key found", |
| 124 | + Title: "test configmap analyzer", |
| 125 | + IconKey: "kubernetes_analyze_secret", |
| 126 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16", |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "key not found", |
| 131 | + analyzer: &troubleshootv1beta2.AnalyzeConfigMap{ |
| 132 | + AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{ |
| 133 | + CheckName: "test configmap analyzer", |
| 134 | + }, |
| 135 | + Namespace: "test-namespace", |
| 136 | + ConfigMapName: "test-configmap", |
| 137 | + Key: "test-key", |
| 138 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 139 | + { |
| 140 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 141 | + Message: "Key not found", |
| 142 | + }, |
| 143 | + }, |
| 144 | + { |
| 145 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 146 | + Message: "Key found", |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + mockFiles: map[string][]byte{ |
| 152 | + "configmaps/test-namespace/test-configmap/test-key.json": mustJSONMarshalIndent(t, collect.ConfigMapOutput{ |
| 153 | + Namespace: "test-namespace", |
| 154 | + Name: "test-configmap", |
| 155 | + Key: "test-key", |
| 156 | + ConfigMapExists: true, |
| 157 | + KeyExists: false, |
| 158 | + }), |
| 159 | + }, |
| 160 | + want: &AnalyzeResult{ |
| 161 | + IsFail: true, |
| 162 | + Message: "Key not found", |
| 163 | + Title: "test configmap analyzer", |
| 164 | + IconKey: "kubernetes_analyze_secret", |
| 165 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16", |
| 166 | + }, |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "key not found configmap not found", |
| 170 | + analyzer: &troubleshootv1beta2.AnalyzeConfigMap{ |
| 171 | + AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{ |
| 172 | + CheckName: "test configmap analyzer", |
| 173 | + }, |
| 174 | + Namespace: "test-namespace", |
| 175 | + ConfigMapName: "test-configmap", |
| 176 | + Key: "test-key", |
| 177 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 178 | + { |
| 179 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 180 | + Message: "Key not found", |
| 181 | + }, |
| 182 | + }, |
| 183 | + { |
| 184 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 185 | + Message: "Key found", |
| 186 | + }, |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + wantErr: true, // TODO: should this be a not found error? This will not work with selectors. |
| 191 | + }, |
| 192 | + } |
| 193 | + for _, tt := range tests { |
| 194 | + t.Run(tt.name, func(t *testing.T) { |
| 195 | + getCollectedFileContents := func(fileName string) ([]byte, error) { |
| 196 | + contents, ok := tt.mockFiles[fileName] |
| 197 | + if !ok { |
| 198 | + return nil, errors.Errorf("file %s was not collected", fileName) |
| 199 | + } |
| 200 | + return contents, nil |
| 201 | + } |
| 202 | + got, err := analyzeConfigMap(tt.analyzer, getCollectedFileContents) |
| 203 | + if tt.wantErr { |
| 204 | + assert.Error(t, err) |
| 205 | + } else { |
| 206 | + require.NoError(t, err) |
| 207 | + assert.Equal(t, tt.want, got) |
| 208 | + } |
| 209 | + }) |
| 210 | + } |
| 211 | +} |
0 commit comments