diff --git a/pkg/analyze/text_analyze.go b/pkg/analyze/text_analyze.go index 3b618fb5..5ba0bd7e 100644 --- a/pkg/analyze/text_analyze.go +++ b/pkg/analyze/text_analyze.go @@ -115,54 +115,66 @@ func analyzeRegexPattern(pattern string, collected []byte, outcomes []*troublesh return nil, errors.Wrapf(err, "failed to compile regex: %s", pattern) } - var failOutcome *troubleshootv1beta2.SingleOutcome - var passOutcome *troubleshootv1beta2.SingleOutcome - for _, outcome := range outcomes { - if outcome.Fail != nil { - failOutcome = outcome.Fail - } else if outcome.Pass != nil { - passOutcome = outcome.Pass - } - } result := AnalyzeResult{ Title: checkName, IconKey: "kubernetes_text_analyze", IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg", } - reMatch := re.MatchString(string(collected)) - failWhen := false - if failOutcome != nil && 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 != nil && passOutcome.When != "" { - passWhen, err = strconv.ParseBool(passOutcome.When) - if err != nil { - return nil, errors.Wrapf(err, "failed to process when statement: %s", passOutcome.When) - } - } + isMatch := re.MatchString(string(collected)) - if passWhen == failWhen { - return nil, errors.Wrap(err, "outcome when conditions for fail and pass are equal") - } + for _, outcome := range outcomes { + if outcome.Fail != nil { - if reMatch == passWhen { - result.IsPass = true - if passOutcome != nil { - result.Message = passOutcome.Message - result.URI = passOutcome.URI + // if the outcome.Fail.When is not set, default to false + if outcome.Fail.When == "" { + outcome.Fail.When = "false" + } + + failWhen, err := strconv.ParseBool(outcome.Fail.When) + if err != nil { + return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Fail.When) + } + + if isMatch == failWhen { + result.IsFail = true + result.IsWarn = false + result.Message = outcome.Fail.Message + result.URI = outcome.Fail.URI + } + } else if outcome.Warn != nil { + // if the outcome.Warn.When is not set, default to false + if outcome.Warn.When == "" { + outcome.Warn.When = "false" + } + + warnWhen, err := strconv.ParseBool(outcome.Warn.When) + if err != nil { + return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Warn.When) + } + + if isMatch == warnWhen { + result.IsWarn = true + result.Message = outcome.Warn.Message + result.URI = outcome.Warn.URI + } + } else if outcome.Pass != nil { + // if the outcome.Pass.When is not set, default to true + if outcome.Pass.When == "" { + outcome.Pass.When = "true" + } + + passWhen, err := strconv.ParseBool(outcome.Pass.When) + if err != nil { + return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Pass.When) + } + + if isMatch == passWhen { + result.IsPass = true + result.Message = outcome.Pass.Message + result.URI = outcome.Pass.URI + } } - return &result, nil - } - - result.IsFail = true - if failOutcome != nil { - result.Message = failOutcome.Message - result.URI = failOutcome.URI } return &result, nil } diff --git a/pkg/analyze/text_analyze_test.go b/pkg/analyze/text_analyze_test.go index e5d083db..f3c7faa4 100644 --- a/pkg/analyze/text_analyze_test.go +++ b/pkg/analyze/text_analyze_test.go @@ -222,6 +222,40 @@ func Test_textAnalyze(t *testing.T) { "text-collector-6/cfile-6.txt": []byte("A different message"), }, }, + { + name: "warn case 1", + analyzer: troubleshootv1beta2.TextAnalyze{ + Outcomes: []*troubleshootv1beta2.Outcome{ + { + Pass: &troubleshootv1beta2.SingleOutcome{ + Message: "success", + }, + }, + { + Warn: &troubleshootv1beta2.SingleOutcome{ + Message: "warning", + }, + }, + }, + CollectorName: "text-collector-6", + FileName: "cfile-6.txt", + RegexPattern: "([a-zA-Z0-9\\-_:*\\s])*succe([a-zA-Z0-9\\-_:*\\s!])*", + }, + expectResult: []AnalyzeResult{ + { + IsPass: false, + IsWarn: true, + IsFail: false, + Title: "text-collector-6", + Message: "warning", + IconKey: "kubernetes_text_analyze", + IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg", + }, + }, + files: map[string][]byte{ + "text-collector-6/cfile-6.txt": []byte("A different message"), + }, + }, { name: "multiple results case 1", analyzer: troubleshootv1beta2.TextAnalyze{ @@ -303,6 +337,56 @@ func Test_textAnalyze(t *testing.T) { "text-collector-2/cfile-3.txt": []byte("Yes it all succeeded"), }, }, + { + name: "multiple results with both warn and fail case 1, only fail", + analyzer: troubleshootv1beta2.TextAnalyze{ + Outcomes: []*troubleshootv1beta2.Outcome{ + { + Pass: &troubleshootv1beta2.SingleOutcome{ + Message: "pass", + }, + }, + { + Warn: &troubleshootv1beta2.SingleOutcome{ + Message: "warning", + }, + }, + { + Fail: &troubleshootv1beta2.SingleOutcome{ + Message: "fail", + }, + }, + }, + CollectorName: "text-collector-1", + FileName: "cfile", + RegexPattern: "succeeded", + }, + expectResult: []AnalyzeResult{ + { + IsPass: true, + IsWarn: false, + IsFail: false, + Title: "text-collector-1", + Message: "pass", + IconKey: "kubernetes_text_analyze", + IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg", + }, + { + 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("Yes it all succeeded"), + "text-collector-1/cfile-2.txt": []byte("no success here"), + "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{