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 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.go b/result/overall.go index 86bd8e7..652ee4d 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,23 +17,26 @@ 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 } func (s *PartialResult) String() string { + if len(s.Perfdata) == 0 { + return fmt.Sprintf("[%s] %s", check.StatusText(s.State), s.Output) + } return fmt.Sprintf("[%s] %s|%s", check.StatusText(s.State), s.Output, s.Perfdata.String()) } @@ -157,8 +161,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) } } 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()) }