Accumulate all longhorn pass results

If there are any error or warning results then return those. Otherwise
return a single healthy pass result.
This commit is contained in:
Andrew Reed
2021-07-08 18:25:10 +00:00
parent c119a16235
commit c95dc489a2
2 changed files with 98 additions and 1 deletions
+23 -1
View File
@@ -131,7 +131,7 @@ func longhorn(analyzer *troubleshootv1beta2.LonghornAnalyze, getCollectedFileCon
}
}
return results, nil
return simplifyLonghornResults(results), nil
}
func analyzeLonghornNodeSchedulable(node *longhornv1beta1.Node) *AnalyzeResult {
@@ -243,3 +243,25 @@ func analyzeLonghornReplicaChecksums(volumeName string, checksums []map[string]s
return result
}
// Keep warn/error results. Return a single pass result if there are no warn/errors.
func simplifyLonghornResults(results []*AnalyzeResult) []*AnalyzeResult {
out := []*AnalyzeResult{}
for _, result := range results {
if result.IsPass {
continue
}
out = append(out, result)
}
if len(out) == 0 {
out = append(out, &AnalyzeResult{
Title: "Longhorn Health Status",
IsPass: true,
Message: "Longhorn is healthy",
})
}
return out
}
+75
View File
@@ -382,3 +382,78 @@ func TestAnalyzeLonghornReplicaChecksums(t *testing.T) {
})
}
}
func TestSimplifyLonghornResults(t *testing.T) {
tests := []struct {
name string
input []*AnalyzeResult
expect []*AnalyzeResult
}{
{
name: "All pass",
input: []*AnalyzeResult{
{
Title: "Replica 1",
IsPass: true,
Message: "Replica 1 ok",
},
{
Title: "Node 1",
IsPass: true,
Message: "Node 1 ok",
},
},
expect: []*AnalyzeResult{
{
Title: "Longhorn Health Status",
IsPass: true,
Message: "Longhorn is healthy",
},
},
},
{
name: "Mixed results",
input: []*AnalyzeResult{
{
Title: "Replica 1",
IsPass: true,
Message: "Replica 1 ok",
},
{
Title: "Replica 2",
IsWarn: true,
Message: "Replica 1 is down",
},
{
Title: "Node 1",
IsPass: true,
Message: "Node 1 ok",
},
{
Title: "Node 2",
IsFail: true,
Message: "Node 2 is down",
},
},
expect: []*AnalyzeResult{
{
Title: "Replica 2",
IsWarn: true,
Message: "Replica 1 is down",
},
{
Title: "Node 2",
IsFail: true,
Message: "Node 2 is down",
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := simplifyLonghornResults(test.input)
assert.Equal(t, test.expect, got)
})
}
}