From d5dae972890fdc8d00521d2925f7c2b65252ba6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= Date: Sun, 16 Oct 2022 00:15:22 +0200 Subject: [PATCH 1/5] Do not add a "|" to output if there is no perfdata --- result/overall.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/result/overall.go b/result/overall.go index 86bd8e7..6431d9e 100644 --- a/result/overall.go +++ b/result/overall.go @@ -33,7 +33,11 @@ type PartialResult struct { } func (s *PartialResult) String() string { - return fmt.Sprintf("[%s] %s|%s", check.StatusText(s.State), s.Output, s.Perfdata.String()) + if len(s.Perfdata) == 0 { + return fmt.Sprintf("[%s] %s", check.StatusText(s.State), s.Output) + } else { + return fmt.Sprintf("[%s] %s|%s", check.StatusText(s.State), s.Output, s.Perfdata.String()) + } } // Deprecate this in a future version From a836bf7f89779d09e2b91b997ca9807e925680f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= Date: Sun, 16 Oct 2022 00:16:00 +0200 Subject: [PATCH 2/5] Fix and enhance test cases --- perfdata/type_test.go | 37 +++++++++++++++++++++++- result/overall_test.go | 65 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/perfdata/type_test.go b/perfdata/type_test.go index a0c7a17..23bdbd6 100644 --- a/perfdata/type_test.go +++ b/perfdata/type_test.go @@ -2,7 +2,10 @@ package perfdata import ( "fmt" + "testing" + "github.com/NETWAYS/go-check" + "github.com/stretchr/testify/assert" ) func ExamplePerfdata() { @@ -12,10 +15,42 @@ func ExamplePerfdata() { Uom: "%", Warn: &check.Threshold{Upper: 80}, Crit: &check.Threshold{Upper: 90}, - Min: 0, Max: 100} + Min: 0, + Max: 100} fmt.Println(perf) // Output: // test=10.1%;80;90;0;100 } + +func TestPerfdata(t *testing.T) { + perf := Perfdata{ + Label: "test", + Value: 2, + } + + assert.Equal(t, "test=2", perf.String()) +} + +func TestPerfdata2(t *testing.T) { + perf := Perfdata{ + Label: "test", + Value: 2, + Uom: "%", + } + + assert.Equal(t, "test=2%", perf.String()) +} + +func TestPerfdata3(t *testing.T) { + perf := Perfdata{ + Label: "foo bar", + Value: 2.76, + Uom: "m", + Warn: &check.Threshold{Lower: 10, Upper: 25, Inside: true}, + Crit: &check.Threshold{Lower: 15, Upper: 20, Inside: false}, + } + + assert.Equal(t, "'foo bar'=2.76m;@10:25;15:20", perf.String()) +} diff --git a/result/overall_test.go b/result/overall_test.go index 36f43a4..f5c414c 100644 --- a/result/overall_test.go +++ b/result/overall_test.go @@ -2,10 +2,11 @@ package result import ( "fmt" + "testing" + "github.com/NETWAYS/go-check" "github.com/NETWAYS/go-check/perfdata" "github.com/stretchr/testify/assert" - "testing" ) func TestOverall_AddOK(t *testing.T) { @@ -138,8 +139,7 @@ func ExampleOverall_withSubchecks() { // \_ [OK] Subcheck1 Test|pd_test=5s } -//nolint -func ExampleOverall_withSubchecks2() { +func TestOverall_withEnhancedSubchecks(t *testing.T) { var overall Overall example_perfdata := perfdata.Perfdata{Label: "pd_test", Value: 5, Uom: "s"} @@ -176,14 +176,41 @@ func ExampleOverall_withSubchecks2() { overall.AddSubcheck(subcheck) overall.AddSubcheck(subcheck2) - fmt.Println(overall.GetOutput()) + resString := overall.GetOutput() + //nolint:lll + expectedString := `states: warning=1 ok=1 +\_ [OK] Subcheck1 Test|pd_test=5s pd_test2=1099511627776kB;@3.14:7036874417766;549755813887:1208925819614629174706176;;18446744073709551615 +\_ [WARNING] Subcheck2 Test|kl;jr2if;l2rkjasdf=5m asdf=18446744073709551615B +` + assert.Equal(t, expectedString, resString) +} + +func TestOverall_withSubchecks3(t *testing.T) { + var overall Overall + + subcheck2 := PartialResult{ + State: check.OK, + Output: "SubSubcheck", + } + subcheck := PartialResult{ + State: check.OK, + Output: "PartialResult", + } + subcheck.partialResults = append(subcheck.partialResults, subcheck2) + + overall.AddSubcheck(subcheck) + + output := overall.GetOutput() - // states: warning=1 ok=1 - // \_ [OK] Subcheck1 Test|pd_test=5s pd_test2=1099511627776kB;@3.14:7036874417766;549755813887:1208925819614629174706176;;18446744073709551615 - // \_ [WARNING] Subcheck2 Test|kl;jr2if;l2rkjasdf=5m asdf=18446744073709551615B + resString := `states: ok=1 +\_ [OK] PartialResult + \_ [OK] SubSubcheck +` + + assert.Equal(t, resString, output) } -func ExampleOverall_withSubchecks3() { +func TestOverall_withSubchecks4(t *testing.T) { var overall Overall subcheck2 := PartialResult{ @@ -194,13 +221,27 @@ func ExampleOverall_withSubchecks3() { State: check.OK, Output: "PartialResult", } + + perf1 := perfdata.Perfdata{ + Label: "foo", + Value: 3, + } + perf2 := perfdata.Perfdata{ + Label: "bar", + Value: 300, + Uom: "%", + } + + subcheck2.Perfdata.Add(&perf1) + subcheck2.Perfdata.Add(&perf2) subcheck.partialResults = append(subcheck.partialResults, subcheck2) overall.AddSubcheck(subcheck) - fmt.Println(overall.GetOutput()) + res := `states: ok=1 +\_ [OK] PartialResult + \_ [OK] SubSubcheck|foo=3 bar=300% +` - // states: ok=1 - // \_ [OK] PartialResult| - // \_ [OK] SubSubcheck| + assert.Equal(t, res, overall.GetOutput()) } From e8c9e1929bff848cc7ede917a50a87d0a116ac91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= Date: Sun, 16 Oct 2022 00:16:12 +0200 Subject: [PATCH 3/5] Remove deprecated linters --- .golangci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index e0e4da0..0ba6911 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -13,6 +13,9 @@ linters: - exportloopref disable: - scopelint + - deadcode + - structcheck + - varcheck presets: - bugs - unused From 3b6adfad6b194d70bee60d6051e770a1ad444de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= Date: Sun, 16 Oct 2022 00:16:49 +0200 Subject: [PATCH 4/5] Code formatting and a little improvement --- result/overall.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/result/overall.go b/result/overall.go index 6431d9e..53730cd 100644 --- a/result/overall.go +++ b/result/overall.go @@ -3,9 +3,10 @@ package result import ( "fmt" + "strings" + "github.com/NETWAYS/go-check" "github.com/NETWAYS/go-check/perfdata" - "strings" ) // So, this is the idea: @@ -16,19 +17,19 @@ import ( // one suffices, but one fails, the whole check might be OK and only the subcheck // Warning or Critical. type Overall struct { - OKs int - Warnings int - Criticals int - Unknowns int - Summary string - Outputs []string // Deprecate this in a future version + OKs int + Warnings int + Criticals int + Unknowns int + Summary string + Outputs []string // Deprecate this in a future version partialResults []PartialResult } type PartialResult struct { - State int - Output string - Perfdata perfdata.PerfdataList + State int + Output string + Perfdata perfdata.PerfdataList partialResults []PartialResult } @@ -161,8 +162,8 @@ func (o *Overall) GetOutput() string { } if o.partialResults != nil { - for _, s := range o.partialResults { - output += s.getOutput(0) + for i := range o.partialResults { + output += o.partialResults[i].getOutput(0) } } From e3a1910a6e90ec20a2c1e44b292d86c5ecbdac0d Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Thu, 8 Dec 2022 23:08:03 +0100 Subject: [PATCH 5/5] Small code reduction by removing unnecessary else --- result/overall.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/result/overall.go b/result/overall.go index 53730cd..652ee4d 100644 --- a/result/overall.go +++ b/result/overall.go @@ -36,9 +36,8 @@ type PartialResult struct { func (s *PartialResult) String() string { if len(s.Perfdata) == 0 { return fmt.Sprintf("[%s] %s", check.StatusText(s.State), s.Output) - } else { - return fmt.Sprintf("[%s] %s|%s", check.StatusText(s.State), s.Output, s.Perfdata.String()) } + return fmt.Sprintf("[%s] %s|%s", check.StatusText(s.State), s.Output, s.Perfdata.String()) } // Deprecate this in a future version