diff --git a/pkg/analyze/text_analyze.go b/pkg/analyze/text_analyze.go index 2477fe11..a320b74e 100644 --- a/pkg/analyze/text_analyze.go +++ b/pkg/analyze/text_analyze.go @@ -97,7 +97,27 @@ func analyzeRegexPattern(pattern string, collected []byte, outcomes []*troublesh IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg", } - if re.MatchString(string(collected)) { + reMatch := re.MatchString(string(collected)) + failWhen := false + if failOutcome.When != "" { + failWhen, err = strconv.ParseBool(failOutcome.When) + if err != nil { + return nil, errors.Wrapf(err, "failed to process when statement: %s", failOutcome.When) + } + } + passWhen := true + if passOutcome.When != "" { + passWhen, err = strconv.ParseBool(passOutcome.When) + if err != nil { + return nil, errors.Wrapf(err, "failed to process when statement: %s", passOutcome.When) + } + } + + if passWhen == failWhen { + return nil, errors.Wrap(err, "outcome when conditions for fail and pass are equal") + } + + if reMatch == passWhen { result.IsPass = true if passOutcome != nil { result.Message = passOutcome.Message @@ -105,6 +125,7 @@ func analyzeRegexPattern(pattern string, collected []byte, outcomes []*troublesh } return &result, nil } + result.IsFail = true if failOutcome != nil { result.Message = failOutcome.Message diff --git a/pkg/analyze/text_analyze_test.go b/pkg/analyze/text_analyze_test.go index 55d47b6b..51f036f0 100644 --- a/pkg/analyze/text_analyze_test.go +++ b/pkg/analyze/text_analyze_test.go @@ -302,6 +302,42 @@ func Test_textAnalyze(t *testing.T) { "text-collector-2/cfile-3.txt": []byte("Yes it all succeeded"), }, }, + { + name: "Fail on error case 1", // regexes are not case insensitive by default + analyzer: troubleshootv1beta2.TextAnalyze{ + Outcomes: []*troubleshootv1beta2.Outcome{ + { + Pass: &troubleshootv1beta2.SingleOutcome{ + Message: "pass", + When: "false", + }, + }, + { + Fail: &troubleshootv1beta2.SingleOutcome{ + Message: "fail", + When: "true", + }, + }, + }, + CollectorName: "text-collector-1", + FileName: "cfile-1.txt", + RegexPattern: "error", + }, + expectResult: []AnalyzeResult{ + { + IsPass: false, + IsWarn: false, + IsFail: true, + Title: "text-collector-1", + Message: "fail", + IconKey: "kubernetes_text_analyze", + IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg", + }, + }, + files: map[string][]byte{ + "text-collector-1/cfile-1.txt": []byte("There is an error."), + }, + }, { name: "case insensitive failure case 1", // regexes are not case insensitive by default analyzer: troubleshootv1beta2.TextAnalyze{