@@ -2,6 +2,7 @@ package analyzer
2
2
3
3
import (
4
4
"encoding/json"
5
+ "strconv"
5
6
"testing"
6
7
7
8
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
@@ -113,6 +114,62 @@ func TestAnalyzeHostHTTP(t *testing.T) {
113
114
},
114
115
},
115
116
},
117
+ {
118
+ name : "invalid compare operator" ,
119
+ expectErr : true ,
120
+ httpResult : & httpResult {
121
+ Response : & collect.HTTPResponse {
122
+ Status : 200 ,
123
+ },
124
+ },
125
+ hostAnalyzer : & troubleshootv1beta2.HTTPAnalyze {
126
+ CollectorName : "collector" ,
127
+ Outcomes : []* troubleshootv1beta2.Outcome {
128
+ {
129
+ Pass : & troubleshootv1beta2.SingleOutcome {
130
+ When : "statusCode #$ 200" ,
131
+ Message : "passed" ,
132
+ },
133
+ },
134
+ {
135
+ Warn : & troubleshootv1beta2.SingleOutcome {
136
+ Message : "default" ,
137
+ },
138
+ },
139
+ },
140
+ },
141
+ },
142
+ {
143
+ name : "!= compare operator" ,
144
+ httpResult : & httpResult {
145
+ Response : & collect.HTTPResponse {
146
+ Status : 201 ,
147
+ },
148
+ },
149
+ hostAnalyzer : & troubleshootv1beta2.HTTPAnalyze {
150
+ CollectorName : "collector" ,
151
+ Outcomes : []* troubleshootv1beta2.Outcome {
152
+ {
153
+ Pass : & troubleshootv1beta2.SingleOutcome {
154
+ When : "statusCode != 200" ,
155
+ Message : "passed" ,
156
+ },
157
+ },
158
+ {
159
+ Warn : & troubleshootv1beta2.SingleOutcome {
160
+ Message : "default" ,
161
+ },
162
+ },
163
+ },
164
+ },
165
+ result : []* AnalyzeResult {
166
+ {
167
+ Title : "HTTP Request" ,
168
+ IsPass : true ,
169
+ Message : "passed" ,
170
+ },
171
+ },
172
+ },
116
173
}
117
174
for _ , test := range tests {
118
175
t .Run (test .name , func (t * testing.T ) {
@@ -137,3 +194,108 @@ func TestAnalyzeHostHTTP(t *testing.T) {
137
194
})
138
195
}
139
196
}
197
+
198
+ func TestAnalyzeHostHTTPHTTPCodesAndCompareOperators (t * testing.T ) {
199
+ httpResult := & httpResult {
200
+ Response : & collect.HTTPResponse {
201
+ Status : 200 ,
202
+ },
203
+ }
204
+
205
+ tests := []struct {
206
+ name string
207
+ expectedStatusCode int
208
+ comparator string
209
+ expectOutcome bool
210
+ }{
211
+ {
212
+ name : "== 200" ,
213
+ expectedStatusCode : 200 ,
214
+ comparator : "==" ,
215
+ },
216
+ {
217
+ name : "=== 200" ,
218
+ expectedStatusCode : 200 ,
219
+ comparator : "===" ,
220
+ },
221
+ {
222
+ name : "= 200" ,
223
+ expectedStatusCode : 200 ,
224
+ comparator : "=" ,
225
+ },
226
+ {
227
+ name : "!= 201" ,
228
+ expectedStatusCode : 201 ,
229
+ comparator : "!=" ,
230
+ },
231
+ {
232
+ name : "!== 200" ,
233
+ expectedStatusCode : 201 ,
234
+ comparator : "!==" ,
235
+ },
236
+ {
237
+ name : ">= 200" ,
238
+ expectedStatusCode : 200 ,
239
+ comparator : ">=" ,
240
+ },
241
+ {
242
+ name : "> 199" ,
243
+ expectedStatusCode : 199 ,
244
+ comparator : ">" ,
245
+ },
246
+ {
247
+ name : ">== 200" ,
248
+ expectedStatusCode : 200 ,
249
+ comparator : ">==" ,
250
+ },
251
+ {
252
+ name : "<= 200" ,
253
+ expectedStatusCode : 200 ,
254
+ comparator : "<=" ,
255
+ },
256
+ {
257
+ name : "<= 201" ,
258
+ expectedStatusCode : 201 ,
259
+ comparator : "<=" ,
260
+ },
261
+ {
262
+ name : "< 201" ,
263
+ expectedStatusCode : 201 ,
264
+ comparator : "<" ,
265
+ },
266
+ {
267
+ name : "<== 200" ,
268
+ expectedStatusCode : 200 ,
269
+ comparator : "<==" ,
270
+ },
271
+ }
272
+ for _ , test := range tests {
273
+ t .Run (test .name , func (t * testing.T ) {
274
+ hostAnalyzer := & troubleshootv1beta2.HTTPAnalyze {
275
+ CollectorName : "registry" ,
276
+ Outcomes : []* troubleshootv1beta2.Outcome {
277
+ {
278
+ Pass : & troubleshootv1beta2.SingleOutcome {
279
+ When : "statusCode " + test .comparator + " " + strconv .Itoa (test .expectedStatusCode ),
280
+ },
281
+ },
282
+ },
283
+ }
284
+
285
+ req := require .New (t )
286
+ b , err := json .Marshal (httpResult )
287
+ if err != nil {
288
+ t .Fatal (err )
289
+ }
290
+
291
+ getCollectedFileContents := func (filename string ) ([]byte , error ) {
292
+ return b , nil
293
+ }
294
+
295
+ result , err := (& AnalyzeHostHTTP {hostAnalyzer }).Analyze (getCollectedFileContents , nil )
296
+ req .NoError (err )
297
+ assert .Equal (t , 1 , len (result ))
298
+ assert .Equal (t , true , result [0 ].IsPass )
299
+ })
300
+ }
301
+ }
0 commit comments