@@ -113,6 +113,124 @@ func TestAnalyzeHostHTTP(t *testing.T) {
113
113
},
114
114
},
115
115
},
116
+ {
117
+ name : "invalid compare operator" ,
118
+ expectErr : true ,
119
+ httpResult : & httpResult {
120
+ Response : & collect.HTTPResponse {
121
+ Status : 200 ,
122
+ },
123
+ },
124
+ hostAnalyzer : & troubleshootv1beta2.HTTPAnalyze {
125
+ CollectorName : "collector" ,
126
+ Outcomes : []* troubleshootv1beta2.Outcome {
127
+ {
128
+ Pass : & troubleshootv1beta2.SingleOutcome {
129
+ When : "statusCode #$ 200" ,
130
+ Message : "passed" ,
131
+ },
132
+ },
133
+ {
134
+ Warn : & troubleshootv1beta2.SingleOutcome {
135
+ Message : "default" ,
136
+ },
137
+ },
138
+ },
139
+ },
140
+ },
141
+ {
142
+ name : "!= compare operator" ,
143
+ httpResult : & httpResult {
144
+ Response : & collect.HTTPResponse {
145
+ Status : 201 ,
146
+ },
147
+ },
148
+ hostAnalyzer : & troubleshootv1beta2.HTTPAnalyze {
149
+ CollectorName : "collector" ,
150
+ Outcomes : []* troubleshootv1beta2.Outcome {
151
+ {
152
+ Pass : & troubleshootv1beta2.SingleOutcome {
153
+ When : "statusCode != 200" ,
154
+ Message : "passed" ,
155
+ },
156
+ },
157
+ {
158
+ Warn : & troubleshootv1beta2.SingleOutcome {
159
+ Message : "default" ,
160
+ },
161
+ },
162
+ },
163
+ },
164
+ result : []* AnalyzeResult {
165
+ {
166
+ Title : "HTTP Request" ,
167
+ IsPass : true ,
168
+ Message : "passed" ,
169
+ },
170
+ },
171
+ },
172
+ {
173
+ name : "Looking for 2xx status codes" ,
174
+ httpResult : & httpResult {
175
+ Response : & collect.HTTPResponse {
176
+ Status : 201 ,
177
+ },
178
+ },
179
+ hostAnalyzer : & troubleshootv1beta2.HTTPAnalyze {
180
+ CollectorName : "collector" ,
181
+ Outcomes : []* troubleshootv1beta2.Outcome {
182
+ {
183
+ Fail : & troubleshootv1beta2.SingleOutcome {
184
+ When : "statusCode >= 300 || statusCode < 200" ,
185
+ Message : "expected 2xx status code" ,
186
+ },
187
+ },
188
+ {
189
+ Pass : & troubleshootv1beta2.SingleOutcome {
190
+ Message : "default" ,
191
+ },
192
+ },
193
+ },
194
+ },
195
+ result : []* AnalyzeResult {
196
+ {
197
+ Title : "HTTP Request" ,
198
+ IsPass : true ,
199
+ Message : "default" ,
200
+ },
201
+ },
202
+ },
203
+ {
204
+ name : "Looking for 2xx status codes does not match" ,
205
+ httpResult : & httpResult {
206
+ Response : & collect.HTTPResponse {
207
+ Status : 300 ,
208
+ },
209
+ },
210
+ hostAnalyzer : & troubleshootv1beta2.HTTPAnalyze {
211
+ CollectorName : "collector" ,
212
+ Outcomes : []* troubleshootv1beta2.Outcome {
213
+ {
214
+ Fail : & troubleshootv1beta2.SingleOutcome {
215
+ When : "statusCode >= 300 || statusCode < 200" ,
216
+ Message : "expected 2xx status code" ,
217
+ },
218
+ },
219
+ {
220
+ Pass : & troubleshootv1beta2.SingleOutcome {
221
+ Message : "default" ,
222
+ },
223
+ },
224
+ },
225
+ },
226
+ result : []* AnalyzeResult {
227
+ {
228
+ Title : "HTTP Request" ,
229
+ IsFail : true ,
230
+ Message : "expected 2xx status code" ,
231
+ },
232
+ },
233
+ },
116
234
}
117
235
for _ , test := range tests {
118
236
t .Run (test .name , func (t * testing.T ) {
@@ -137,3 +255,77 @@ func TestAnalyzeHostHTTP(t *testing.T) {
137
255
})
138
256
}
139
257
}
258
+
259
+ func TestAnalyzeHostHTTPHTTPCodesAndCompareOperators (t * testing.T ) {
260
+ httpResult := & httpResult {
261
+ Response : & collect.HTTPResponse {
262
+ Status : 200 ,
263
+ },
264
+ }
265
+
266
+ tests := []struct {
267
+ name string
268
+ }{
269
+ {
270
+ name : "statusCode == 200" ,
271
+ },
272
+ {
273
+ name : "statusCode === 200" ,
274
+ },
275
+ {
276
+ name : "statusCode = 200" ,
277
+ },
278
+ {
279
+ name : "statusCode != 201" ,
280
+ },
281
+ {
282
+ name : "statusCode >= 200" ,
283
+ },
284
+ {
285
+ name : "statusCode > 199" ,
286
+ },
287
+ {
288
+ name : "statusCode <= 200" ,
289
+ },
290
+ {
291
+ name : "statusCode <= 201" ,
292
+ },
293
+ {
294
+ name : "statusCode < 201" ,
295
+ },
296
+ {
297
+ name : "statusCode < 201 && statusCode > 199" ,
298
+ },
299
+ {
300
+ name : "statusCode < 201 || statusCode > 199 && statusCode == 200" ,
301
+ },
302
+ }
303
+ for _ , test := range tests {
304
+ t .Run (test .name , func (t * testing.T ) {
305
+ hostAnalyzer := & troubleshootv1beta2.HTTPAnalyze {
306
+ CollectorName : "registry" ,
307
+ Outcomes : []* troubleshootv1beta2.Outcome {
308
+ {
309
+ Pass : & troubleshootv1beta2.SingleOutcome {
310
+ When : test .name },
311
+ },
312
+ },
313
+ }
314
+
315
+ req := require .New (t )
316
+ b , err := json .Marshal (httpResult )
317
+ if err != nil {
318
+ t .Fatal (err )
319
+ }
320
+
321
+ getCollectedFileContents := func (filename string ) ([]byte , error ) {
322
+ return b , nil
323
+ }
324
+
325
+ result , err := (& AnalyzeHostHTTP {hostAnalyzer }).Analyze (getCollectedFileContents , nil )
326
+ req .NoError (err )
327
+ req .Len (result , 1 )
328
+ req .Equal (true , result [0 ].IsPass )
329
+ })
330
+ }
331
+ }
0 commit comments