Skip to content

Commit 6430b0e

Browse files
committed
Allowing more comperators for the http analyzer
1 parent 83b0dbb commit 6430b0e

File tree

2 files changed

+188
-4
lines changed

2 files changed

+188
-4
lines changed

pkg/analyze/host_http.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package analyzer
33
import (
44
"encoding/json"
55
"fmt"
6+
"slices"
67
"strconv"
78
"strings"
89

@@ -11,6 +12,8 @@ import (
1112
"github.com/replicatedhq/troubleshoot/pkg/collect"
1213
)
1314

15+
var allowedCompareOperators = []string{"==", "!=", "=", "===", "!==", ">=", ">", ">==", "<", "<=", "<=="}
16+
1417
type httpResult struct {
1518
Error *collect.HTTPError
1619
Response *collect.HTTPResponse
@@ -73,8 +76,10 @@ func compareHostHTTPConditionalToActual(conditional string, result *httpResult)
7376
return false, errors.New(`Conditional must begin with keyword "statusCode"`)
7477
}
7578

76-
if parts[1] != "=" && parts[1] != "==" && parts[1] != "===" {
77-
return false, errors.New(`Only supported operator is "=="`)
79+
operator := parts[1]
80+
// Check if operator is allowed
81+
if !slices.Contains(allowedCompareOperators, operator) {
82+
return false, fmt.Errorf("Operator %q is not supported. Must be one of: %s", operator, strings.Join(allowedCompareOperators, ", "))
7883
}
7984

8085
i, err := strconv.Atoi(parts[2])
@@ -83,9 +88,26 @@ func compareHostHTTPConditionalToActual(conditional string, result *httpResult)
8388
}
8489

8590
if result.Response == nil {
86-
return false, err
91+
return false, nil
92+
}
93+
94+
switch operator {
95+
case "==", "=", "===":
96+
return result.Response.Status == i, nil
97+
case "!=", "!==":
98+
return result.Response.Status != i, nil
99+
case ">=", ">==":
100+
return result.Response.Status >= i, nil
101+
case ">":
102+
return result.Response.Status > i, nil
103+
case "<=", "<==":
104+
return result.Response.Status <= i, nil
105+
case "<":
106+
return result.Response.Status < i, nil
107+
default:
108+
// This should never happen due to the validation above
109+
return false, fmt.Errorf("Operator %q is not supported. Must be one of: %s", operator, strings.Join(allowedCompareOperators, ", "))
87110
}
88-
return result.Response.Status == i, nil
89111
}
90112

91113
func analyzeHTTPResult(analyzer *troubleshootv1beta2.HTTPAnalyze, fileName string, getCollectedFileContents getCollectedFileContents, title string) ([]*AnalyzeResult, error) {

pkg/analyze/host_http_test.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package analyzer
22

33
import (
44
"encoding/json"
5+
"strconv"
56
"testing"
67

78
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
@@ -113,6 +114,62 @@ func TestAnalyzeHostHTTP(t *testing.T) {
113114
},
114115
},
115116
},
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+
},
116173
}
117174
for _, test := range tests {
118175
t.Run(test.name, func(t *testing.T) {
@@ -137,3 +194,108 @@ func TestAnalyzeHostHTTP(t *testing.T) {
137194
})
138195
}
139196
}
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

Comments
 (0)