From c95dc489a2d0da1e6e04d7bf387a8d4a86380db8 Mon Sep 17 00:00:00 2001 From: Andrew Reed Date: Thu, 8 Jul 2021 18:25:10 +0000 Subject: [PATCH] Accumulate all longhorn pass results If there are any error or warning results then return those. Otherwise return a single healthy pass result. --- pkg/analyze/longhorn.go | 24 +++++++++++- pkg/analyze/longhorn_test.go | 75 ++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/pkg/analyze/longhorn.go b/pkg/analyze/longhorn.go index 4f4c090b..f780328a 100644 --- a/pkg/analyze/longhorn.go +++ b/pkg/analyze/longhorn.go @@ -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 +} diff --git a/pkg/analyze/longhorn_test.go b/pkg/analyze/longhorn_test.go index fe47db32..9dccdd5f 100644 --- a/pkg/analyze/longhorn_test.go +++ b/pkg/analyze/longhorn_test.go @@ -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) + }) + } +}