Skip to content

Commit ba65f92

Browse files
authored
Merge pull request #389 from replicatedhq/laverya/simplify-text-analyze-code
simplify the text analyze code by combining with compareRegex code
2 parents 1ed6100 + 6a0fb2e commit ba65f92

File tree

2 files changed

+82
-27
lines changed

2 files changed

+82
-27
lines changed

pkg/analyze/text_analyze.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,6 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
137137
// allow fallthrough
138138
for _, outcome := range outcomes {
139139
if outcome.Fail != nil {
140-
if outcome.Fail.When == "" {
141-
result.IsFail = true
142-
result.Message = outcome.Fail.Message
143-
result.URI = outcome.Fail.URI
144-
145-
return result, nil
146-
}
147-
148140
isMatch, err := compareRegex(outcome.Fail.When, foundMatches)
149141
if err != nil {
150142
return result, errors.Wrap(err, "failed to compare regex fail conditional")
@@ -158,14 +150,6 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
158150
return result, nil
159151
}
160152
} else if outcome.Warn != nil {
161-
if outcome.Warn.When == "" {
162-
result.IsWarn = true
163-
result.Message = outcome.Warn.Message
164-
result.URI = outcome.Warn.URI
165-
166-
return result, nil
167-
}
168-
169153
isMatch, err := compareRegex(outcome.Warn.When, foundMatches)
170154
if err != nil {
171155
return result, errors.Wrap(err, "failed to compare regex warn conditional")
@@ -179,14 +163,6 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
179163
return result, nil
180164
}
181165
} else if outcome.Pass != nil {
182-
if outcome.Pass.When == "" {
183-
result.IsPass = true
184-
result.Message = outcome.Pass.Message
185-
result.URI = outcome.Pass.URI
186-
187-
return result, nil
188-
}
189-
190166
isMatch, err := compareRegex(outcome.Pass.When, foundMatches)
191167
if err != nil {
192168
return result, errors.Wrap(err, "failed to compare regex pass conditional")
@@ -206,6 +182,9 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
206182
}
207183

208184
func compareRegex(conditional string, foundMatches map[string]string) (bool, error) {
185+
if conditional == "" {
186+
return true, nil
187+
}
209188
parts := strings.Split(strings.TrimSpace(conditional), " ")
210189

211190
if len(parts) != 3 {

pkg/analyze/text_analyze_test.go

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"testing"
88

99
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
10-
"github.com/stretchr/testify/assert"
1110
"github.com/stretchr/testify/require"
1211
)
1312

@@ -371,6 +370,77 @@ func Test_textAnalyze(t *testing.T) {
371370
"text-collector-1/cfile-1.txt": []byte("Yes it all SUCCEEDED"),
372371
},
373372
},
373+
374+
{
375+
name: "compare group with integer",
376+
analyzer: troubleshootv1beta2.TextAnalyze{
377+
Outcomes: []*troubleshootv1beta2.Outcome{
378+
{
379+
Pass: &troubleshootv1beta2.SingleOutcome{
380+
When: "val > 10",
381+
Message: "val is greater than 10",
382+
},
383+
},
384+
{
385+
Fail: &troubleshootv1beta2.SingleOutcome{
386+
Message: "val is not greater than 10",
387+
},
388+
},
389+
},
390+
CollectorName: "text-collector-1",
391+
FileName: "cfile-1.txt",
392+
RegexGroups: `value: (?P<val>\d+)`,
393+
},
394+
expectResult: []AnalyzeResult{
395+
{
396+
IsPass: true,
397+
IsWarn: false,
398+
IsFail: false,
399+
Title: "text-collector-1",
400+
Message: "val is greater than 10",
401+
IconKey: "kubernetes_text_analyze",
402+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg?w=13&h=16",
403+
},
404+
},
405+
files: map[string][]byte{
406+
"text-collector-1/cfile-1.txt": []byte("value: 15\nother: 10"),
407+
},
408+
},
409+
{
410+
name: "compare group with integer (failure)",
411+
analyzer: troubleshootv1beta2.TextAnalyze{
412+
Outcomes: []*troubleshootv1beta2.Outcome{
413+
{
414+
Pass: &troubleshootv1beta2.SingleOutcome{
415+
When: "val > 10",
416+
Message: "val is greater than 10",
417+
},
418+
},
419+
{
420+
Fail: &troubleshootv1beta2.SingleOutcome{
421+
Message: "val is not greater than 10",
422+
},
423+
},
424+
},
425+
CollectorName: "text-collector-1",
426+
FileName: "cfile-1.txt",
427+
RegexGroups: `value: (?P<val>\d+)`,
428+
},
429+
expectResult: []AnalyzeResult{
430+
{
431+
IsPass: false,
432+
IsWarn: false,
433+
IsFail: true,
434+
Title: "text-collector-1",
435+
Message: "val is not greater than 10",
436+
IconKey: "kubernetes_text_analyze",
437+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg?w=13&h=16",
438+
},
439+
},
440+
files: map[string][]byte{
441+
"text-collector-1/cfile-1.txt": []byte("value: 2\nother: 10"),
442+
},
443+
},
374444
}
375445

376446
for _, test := range tests {
@@ -404,7 +474,7 @@ func Test_textAnalyze(t *testing.T) {
404474
for _, v := range actual {
405475
unPointered = append(unPointered, *v)
406476
}
407-
assert.ElementsMatch(t, test.expectResult, unPointered)
477+
req.ElementsMatch(test.expectResult, unPointered)
408478
})
409479
}
410480
}
@@ -444,6 +514,12 @@ func Test_compareRegex(t *testing.T) {
444514
},
445515
expected: true,
446516
},
517+
{
518+
name: "empty conditional",
519+
conditional: "",
520+
foundMatches: map[string]string{},
521+
expected: true,
522+
},
447523
}
448524
for _, test := range tests {
449525
t.Run(test.name, func(t *testing.T) {
@@ -452,7 +528,7 @@ func Test_compareRegex(t *testing.T) {
452528
actual, err := compareRegex(test.conditional, test.foundMatches)
453529
req.NoError(err)
454530

455-
assert.Equal(t, test.expected, actual)
531+
req.Equal(test.expected, actual)
456532
})
457533
}
458534
}

0 commit comments

Comments
 (0)