Skip to content

Commit 1a828fa

Browse files
authored
fix(analyzer): add missing warning in outcome (#1687)
1 parent 9970fb1 commit 1a828fa

File tree

2 files changed

+135
-39
lines changed

2 files changed

+135
-39
lines changed

pkg/analyze/text_analyze.go

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -115,54 +115,66 @@ func analyzeRegexPattern(pattern string, collected []byte, outcomes []*troublesh
115115
return nil, errors.Wrapf(err, "failed to compile regex: %s", pattern)
116116
}
117117

118-
var failOutcome *troubleshootv1beta2.SingleOutcome
119-
var passOutcome *troubleshootv1beta2.SingleOutcome
120-
for _, outcome := range outcomes {
121-
if outcome.Fail != nil {
122-
failOutcome = outcome.Fail
123-
} else if outcome.Pass != nil {
124-
passOutcome = outcome.Pass
125-
}
126-
}
127118
result := AnalyzeResult{
128119
Title: checkName,
129120
IconKey: "kubernetes_text_analyze",
130121
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
131122
}
132123

133-
reMatch := re.MatchString(string(collected))
134-
failWhen := false
135-
if failOutcome != nil && failOutcome.When != "" {
136-
failWhen, err = strconv.ParseBool(failOutcome.When)
137-
if err != nil {
138-
return nil, errors.Wrapf(err, "failed to process when statement: %s", failOutcome.When)
139-
}
140-
}
141-
passWhen := true
142-
if passOutcome != nil && passOutcome.When != "" {
143-
passWhen, err = strconv.ParseBool(passOutcome.When)
144-
if err != nil {
145-
return nil, errors.Wrapf(err, "failed to process when statement: %s", passOutcome.When)
146-
}
147-
}
124+
isMatch := re.MatchString(string(collected))
148125

149-
if passWhen == failWhen {
150-
return nil, errors.Wrap(err, "outcome when conditions for fail and pass are equal")
151-
}
126+
for _, outcome := range outcomes {
127+
if outcome.Fail != nil {
152128

153-
if reMatch == passWhen {
154-
result.IsPass = true
155-
if passOutcome != nil {
156-
result.Message = passOutcome.Message
157-
result.URI = passOutcome.URI
158-
}
159-
return &result, nil
160-
}
129+
// if the outcome.Fail.When is not set, default to false
130+
if outcome.Fail.When == "" {
131+
outcome.Fail.When = "false"
132+
}
133+
134+
failWhen, err := strconv.ParseBool(outcome.Fail.When)
135+
if err != nil {
136+
return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Fail.When)
137+
}
138+
139+
if isMatch == failWhen {
140+
result.IsFail = true
141+
result.IsWarn = false
142+
result.Message = outcome.Fail.Message
143+
result.URI = outcome.Fail.URI
144+
}
145+
} else if outcome.Warn != nil {
146+
// if the outcome.Warn.When is not set, default to false
147+
if outcome.Warn.When == "" {
148+
outcome.Warn.When = "false"
149+
}
161150

162-
result.IsFail = true
163-
if failOutcome != nil {
164-
result.Message = failOutcome.Message
165-
result.URI = failOutcome.URI
151+
warnWhen, err := strconv.ParseBool(outcome.Warn.When)
152+
if err != nil {
153+
return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Warn.When)
154+
}
155+
156+
if isMatch == warnWhen {
157+
result.IsWarn = true
158+
result.Message = outcome.Warn.Message
159+
result.URI = outcome.Warn.URI
160+
}
161+
} else if outcome.Pass != nil {
162+
// if the outcome.Pass.When is not set, default to true
163+
if outcome.Pass.When == "" {
164+
outcome.Pass.When = "true"
165+
}
166+
167+
passWhen, err := strconv.ParseBool(outcome.Pass.When)
168+
if err != nil {
169+
return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Pass.When)
170+
}
171+
172+
if isMatch == passWhen {
173+
result.IsPass = true
174+
result.Message = outcome.Pass.Message
175+
result.URI = outcome.Pass.URI
176+
}
177+
}
166178
}
167179
return &result, nil
168180
}

pkg/analyze/text_analyze_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,40 @@ func Test_textAnalyze(t *testing.T) {
222222
"text-collector-6/cfile-6.txt": []byte("A different message"),
223223
},
224224
},
225+
{
226+
name: "warn case 1",
227+
analyzer: troubleshootv1beta2.TextAnalyze{
228+
Outcomes: []*troubleshootv1beta2.Outcome{
229+
{
230+
Pass: &troubleshootv1beta2.SingleOutcome{
231+
Message: "success",
232+
},
233+
},
234+
{
235+
Warn: &troubleshootv1beta2.SingleOutcome{
236+
Message: "warning",
237+
},
238+
},
239+
},
240+
CollectorName: "text-collector-6",
241+
FileName: "cfile-6.txt",
242+
RegexPattern: "([a-zA-Z0-9\\-_:*\\s])*succe([a-zA-Z0-9\\-_:*\\s!])*",
243+
},
244+
expectResult: []AnalyzeResult{
245+
{
246+
IsPass: false,
247+
IsWarn: true,
248+
IsFail: false,
249+
Title: "text-collector-6",
250+
Message: "warning",
251+
IconKey: "kubernetes_text_analyze",
252+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
253+
},
254+
},
255+
files: map[string][]byte{
256+
"text-collector-6/cfile-6.txt": []byte("A different message"),
257+
},
258+
},
225259
{
226260
name: "multiple results case 1",
227261
analyzer: troubleshootv1beta2.TextAnalyze{
@@ -303,6 +337,56 @@ func Test_textAnalyze(t *testing.T) {
303337
"text-collector-2/cfile-3.txt": []byte("Yes it all succeeded"),
304338
},
305339
},
340+
{
341+
name: "multiple results with both warn and fail case 1, only fail",
342+
analyzer: troubleshootv1beta2.TextAnalyze{
343+
Outcomes: []*troubleshootv1beta2.Outcome{
344+
{
345+
Pass: &troubleshootv1beta2.SingleOutcome{
346+
Message: "pass",
347+
},
348+
},
349+
{
350+
Warn: &troubleshootv1beta2.SingleOutcome{
351+
Message: "warning",
352+
},
353+
},
354+
{
355+
Fail: &troubleshootv1beta2.SingleOutcome{
356+
Message: "fail",
357+
},
358+
},
359+
},
360+
CollectorName: "text-collector-1",
361+
FileName: "cfile",
362+
RegexPattern: "succeeded",
363+
},
364+
expectResult: []AnalyzeResult{
365+
{
366+
IsPass: true,
367+
IsWarn: false,
368+
IsFail: false,
369+
Title: "text-collector-1",
370+
Message: "pass",
371+
IconKey: "kubernetes_text_analyze",
372+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
373+
},
374+
{
375+
IsPass: false,
376+
IsWarn: false,
377+
IsFail: true,
378+
Title: "text-collector-1",
379+
Message: "fail",
380+
IconKey: "kubernetes_text_analyze",
381+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
382+
},
383+
},
384+
files: map[string][]byte{
385+
"text-collector-1/cfile-1.txt": []byte("Yes it all succeeded"),
386+
"text-collector-1/cfile-2.txt": []byte("no success here"),
387+
"text-collector-2/cfile-3.txt": []byte("Yes it all succeeded"),
388+
},
389+
},
306390
{
307391
name: "Fail on error case 1", // regexes are not case insensitive by default
308392
analyzer: troubleshootv1beta2.TextAnalyze{

0 commit comments

Comments
 (0)