Adding support for inverted regex (#370)

This commit is contained in:
Joris 'Josh' De Winne
2021-07-26 13:06:30 -04:00
committed by GitHub
parent cf4d510413
commit 6349ae8aee
2 changed files with 58 additions and 1 deletions
+22 -1
View File
@@ -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
+36
View File
@@ -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{