Skip to content

Commit 77a2475

Browse files
author
Pavan Sokke Nagaraj
authored
fix: return true when one of analyzers strict field is true (#552)
* fix: return true when one of analyzers is true * fix: return true when one of analyzers is true * update: add unit test * update: log errors while processing analyzers * fix: return parse error instead of logging * fix: update err message * fix: evaluate strict check err separately
1 parent 7bfb543 commit 77a2475

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

pkg/preflight/analyze.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
1010
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
11+
"github.com/replicatedhq/troubleshoot/pkg/logger"
1112
)
1213

1314
// Analyze runs the analyze phase of preflight checks
@@ -77,7 +78,11 @@ func doAnalyze(allCollectedData map[string][]byte, analyzers []*troubleshootv1be
7778
for _, analyzer := range analyzers {
7879
analyzeResult, err := analyze.Analyze(analyzer, getCollectedFileContents, getChildCollectedFileContents)
7980
if err != nil {
80-
strict, _ := HasStrictAnalyzer(analyzer)
81+
strict, strictErr := HasStrictAnalyzer(analyzer)
82+
if strictErr != nil {
83+
logger.Printf("failed to determine if analyzer %v is strict: %s", analyzer, strictErr)
84+
}
85+
8186
analyzeResult = []*analyze.AnalyzeResult{
8287
{
8388
Strict: strict,

pkg/preflight/util.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ func HasStrictAnalyzers(preflight *troubleshootv1beta2.Preflight) (bool, error)
2626

2727
// analyzerMap will ignore empty Analyzers and loop around Analyzer with data
2828
for _, analyzers := range analyzersMap { // for each analyzer: map["clusterVersion": map[string]interface{} ["exclude": "", "strict": "true", "outcomes": nil]
29-
return hasStrictAnalyzer(analyzers)
29+
hasStrictAnalyzer, err := hasStrictAnalyzer(analyzers)
30+
if err != nil {
31+
return false, errors.Wrap(err, "failed to check if analyzer has strict:true")
32+
}
33+
if hasStrictAnalyzer {
34+
return true, nil
35+
}
3036
}
3137
return false, nil
3238
}
@@ -58,7 +64,9 @@ func hasStrictAnalyzer(analyzerMap map[string]interface{}) (bool, error) {
5864
if err != nil {
5965
return false, errors.Wrap(err, "error while un-marshalling marshalledAnalyzers")
6066
}
61-
return analyzeMeta.Strict.BoolOrDefaultFalse(), nil
67+
if analyzeMeta.Strict.BoolOrDefaultFalse() {
68+
return true, nil
69+
}
6270
}
6371
return false, nil
6472
}

pkg/preflight/util_test.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,53 @@ func TestHasStrictAnalyzers(t *testing.T) {
190190
},
191191
want: false,
192192
wantErr: false,
193+
}, {
194+
name: "expect true when preflight spec's analyzer has analyzer with strict true in one of multiple analyzers",
195+
preflight: &troubleshootv1beta2.Preflight{
196+
Spec: troubleshootv1beta2.PreflightSpec{
197+
Analyzers: []*troubleshootv1beta2.Analyze{
198+
{
199+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{AnalyzeMeta: analyzeMetaStrictFalseInt},
200+
}, {
201+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{AnalyzeMeta: analyzeMetaStrictTrueBool},
202+
},
203+
},
204+
},
205+
},
206+
want: true,
207+
wantErr: false,
208+
}, {
209+
name: "expect true when preflight spec's analyzer has analyzer with strict true in one of multiple analyzers",
210+
preflight: &troubleshootv1beta2.Preflight{
211+
Spec: troubleshootv1beta2.PreflightSpec{
212+
Analyzers: []*troubleshootv1beta2.Analyze{
213+
{
214+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{AnalyzeMeta: analyzeMetaStrictFalseInt},
215+
}, {
216+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{AnalyzeMeta: analyzeMetaStrictTrueBool},
217+
}, {
218+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{AnalyzeMeta: analyzeMetaStrictInvalidStr},
219+
},
220+
},
221+
},
222+
},
223+
want: true,
224+
wantErr: false,
225+
}, {
226+
name: "expect true when preflight spec's analyzer has analyzer with strict true in one of multiple analyzers",
227+
preflight: &troubleshootv1beta2.Preflight{
228+
Spec: troubleshootv1beta2.PreflightSpec{
229+
Analyzers: []*troubleshootv1beta2.Analyze{
230+
{
231+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{AnalyzeMeta: analyzeMetaStrictFalseInt},
232+
StorageClass: &troubleshootv1beta2.StorageClass{AnalyzeMeta: analyzeMetaStrictFalseInt},
233+
Secret: &troubleshootv1beta2.AnalyzeSecret{AnalyzeMeta: analyzeMetaStrictTrueBool},
234+
},
235+
},
236+
},
237+
},
238+
want: true,
239+
wantErr: false,
193240
},
194241
}
195242
for _, tt := range tests {
@@ -298,7 +345,7 @@ func TestHasStrictAnalyzer(t *testing.T) {
298345
want: false,
299346
wantErr: false,
300347
}, {
301-
name: "expect strict=false, err=nil when ClusterVersion analyzer has strict=true",
348+
name: "expect strict=true, err=nil when ClusterVersion analyzer has strict=true",
302349
analyzer: &troubleshootv1beta2.Analyze{
303350
ClusterVersion: &troubleshootv1beta2.ClusterVersion{AnalyzeMeta: analyzeMetaStrictTrueInt},
304351
},
@@ -311,6 +358,15 @@ func TestHasStrictAnalyzer(t *testing.T) {
311358
},
312359
want: false,
313360
wantErr: false,
361+
}, {
362+
name: "expect strict=true, err=nil when one of the analyzers has strict=true",
363+
analyzer: &troubleshootv1beta2.Analyze{
364+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{AnalyzeMeta: analyzeMetaStrictFalseInt},
365+
StorageClass: &troubleshootv1beta2.StorageClass{AnalyzeMeta: analyzeMetaStrictFalseInt},
366+
Secret: &troubleshootv1beta2.AnalyzeSecret{AnalyzeMeta: analyzeMetaStrictTrueBool},
367+
},
368+
want: true,
369+
wantErr: false,
314370
},
315371
}
316372
for _, tt := range tests {

0 commit comments

Comments
 (0)