mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-04-15 07:16:34 +00:00
Adding support for inverted regex (#370)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user