Skip to content

Commit

Permalink
fix(analyzer): add missing warning in outcome (#1687)
Browse files Browse the repository at this point in the history
  • Loading branch information
DexterYan authored Nov 13, 2024
1 parent 9970fb1 commit 1a828fa
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 39 deletions.
90 changes: 51 additions & 39 deletions pkg/analyze/text_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
return &result, nil
}
// 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"
}

result.IsFail = true
if failOutcome != nil {
result.Message = failOutcome.Message
result.URI = failOutcome.URI
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
}
Expand Down
84 changes: 84 additions & 0 deletions pkg/analyze/text_analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 1a828fa

Please sign in to comment.