fix(analyzer): add missing warning in outcome (#1687)

This commit is contained in:
Dexter Yan
2024-11-13 16:32:54 +13:00
committed by GitHub
parent 9970fb1371
commit 1a828fa90b
2 changed files with 135 additions and 39 deletions
+51 -39
View File
@@ -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
}
+84
View File
@@ -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{