Skip to content

Commit 6349ae8

Browse files
authored
Adding support for inverted regex (#370)
1 parent cf4d510 commit 6349ae8

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

pkg/analyze/text_analyze.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,35 @@ func analyzeRegexPattern(pattern string, collected []byte, outcomes []*troublesh
9797
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
9898
}
9999

100-
if re.MatchString(string(collected)) {
100+
reMatch := re.MatchString(string(collected))
101+
failWhen := false
102+
if failOutcome.When != "" {
103+
failWhen, err = strconv.ParseBool(failOutcome.When)
104+
if err != nil {
105+
return nil, errors.Wrapf(err, "failed to process when statement: %s", failOutcome.When)
106+
}
107+
}
108+
passWhen := true
109+
if passOutcome.When != "" {
110+
passWhen, err = strconv.ParseBool(passOutcome.When)
111+
if err != nil {
112+
return nil, errors.Wrapf(err, "failed to process when statement: %s", passOutcome.When)
113+
}
114+
}
115+
116+
if passWhen == failWhen {
117+
return nil, errors.Wrap(err, "outcome when conditions for fail and pass are equal")
118+
}
119+
120+
if reMatch == passWhen {
101121
result.IsPass = true
102122
if passOutcome != nil {
103123
result.Message = passOutcome.Message
104124
result.URI = passOutcome.URI
105125
}
106126
return &result, nil
107127
}
128+
108129
result.IsFail = true
109130
if failOutcome != nil {
110131
result.Message = failOutcome.Message

pkg/analyze/text_analyze_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,42 @@ func Test_textAnalyze(t *testing.T) {
302302
"text-collector-2/cfile-3.txt": []byte("Yes it all succeeded"),
303303
},
304304
},
305+
{
306+
name: "Fail on error case 1", // regexes are not case insensitive by default
307+
analyzer: troubleshootv1beta2.TextAnalyze{
308+
Outcomes: []*troubleshootv1beta2.Outcome{
309+
{
310+
Pass: &troubleshootv1beta2.SingleOutcome{
311+
Message: "pass",
312+
When: "false",
313+
},
314+
},
315+
{
316+
Fail: &troubleshootv1beta2.SingleOutcome{
317+
Message: "fail",
318+
When: "true",
319+
},
320+
},
321+
},
322+
CollectorName: "text-collector-1",
323+
FileName: "cfile-1.txt",
324+
RegexPattern: "error",
325+
},
326+
expectResult: []AnalyzeResult{
327+
{
328+
IsPass: false,
329+
IsWarn: false,
330+
IsFail: true,
331+
Title: "text-collector-1",
332+
Message: "fail",
333+
IconKey: "kubernetes_text_analyze",
334+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
335+
},
336+
},
337+
files: map[string][]byte{
338+
"text-collector-1/cfile-1.txt": []byte("There is an error."),
339+
},
340+
},
305341
{
306342
name: "case insensitive failure case 1", // regexes are not case insensitive by default
307343
analyzer: troubleshootv1beta2.TextAnalyze{

0 commit comments

Comments
 (0)