From e7c95979098759a4d475904b1be35ff60a3897cc Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Fri, 16 Jun 2023 15:46:32 +0200 Subject: [PATCH 1/5] More tests for output formatting to be safe --- perfdata/list_test.go | 15 ++++++++++++++- result/overall_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/perfdata/list_test.go b/perfdata/list_test.go index d629d3e..ac758d6 100644 --- a/perfdata/list_test.go +++ b/perfdata/list_test.go @@ -1,6 +1,11 @@ package perfdata -import "fmt" +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) func ExamplePerfdataList() { list := PerfdataList{} @@ -12,3 +17,11 @@ func ExamplePerfdataList() { // Output: // test1=23 test2=42 } + +func TestPerfdataListFormating(t *testing.T) { + list := PerfdataList{} + list.Add(&Perfdata{Label: "test1", Value: 23}) + list.Add(&Perfdata{Label: "test2", Value: 42}) + + assert.Equal(t, "test1=23 test2=42", list.String()) +} diff --git a/result/overall_test.go b/result/overall_test.go index 403a2a0..143bdce 100644 --- a/result/overall_test.go +++ b/result/overall_test.go @@ -340,3 +340,39 @@ func TestOverall_withSubchecks_PartialResultStatus(t *testing.T) { assert.Equal(t, res, overall.GetOutput()) assert.Equal(t, 0, overall.GetStatus()) } + +func TestSubchecksPerfdata(t *testing.T) { + var overall Overall + + check1 := PartialResult{ + State: check.OK, + Output: "Check1", + Perfdata: perfdata.PerfdataList{ + &perfdata.Perfdata{ + Label: "foo", + Value: 23, + }, + &perfdata.Perfdata{ + Label: "bar", + Value: 42, + }, + }, + } + check2 := PartialResult{ + State: check.Warning, + Output: "Check2", + Perfdata: perfdata.PerfdataList{ + &perfdata.Perfdata{ + Label: "foo2", + Value: 46, + }, + }, + } + + overall.AddSubcheck(check1) + overall.AddSubcheck(check2) + + resultString := "states: warning=1 ok=1\n\\_ [OK] Check1\n\\_ [WARNING] Check2\n|foo=23 bar=42 foo2=46\n" + + assert.Equal(t, resultString, overall.GetOutput()) +} From e52f2aaf9d983c1252b4beab5b32ec92bf641e5c Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Fri, 16 Jun 2023 16:49:15 +0200 Subject: [PATCH 2/5] Add another E2E example/test --- cmd/check_example2/main.go | 41 +++++++++++++++++++++++++++++++++ cmd/check_example2/main_test.go | 28 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 cmd/check_example2/main.go create mode 100644 cmd/check_example2/main_test.go diff --git a/cmd/check_example2/main.go b/cmd/check_example2/main.go new file mode 100644 index 0000000..2d1af20 --- /dev/null +++ b/cmd/check_example2/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "github.com/NETWAYS/go-check" + "github.com/NETWAYS/go-check/perfdata" + "github.com/NETWAYS/go-check/result" +) + +func main() { + defer check.CatchPanic() + + var overall result.Overall + + check1 := result.PartialResult{} + + check1.Output = "Check1" + check1.SetState(check.OK) + check1.Perfdata.Add(&perfdata.Perfdata{ + Label: "foo", + Value: 23, + }) + + check2 := result.PartialResult{} + + check2.Output = "Check2" + check2.SetState(check.Warning) + + check2.Perfdata.Add(&perfdata.Perfdata{ + Label: "bar", + Value: 42, + }) + check2.Perfdata.Add(&perfdata.Perfdata{ + Label: "foo2 bar", + Value: 46, + }) + + overall.AddSubcheck(check1) + overall.AddSubcheck(check2) + + check.ExitRaw(overall.GetStatus(), overall.GetOutput()) +} diff --git a/cmd/check_example2/main_test.go b/cmd/check_example2/main_test.go new file mode 100644 index 0000000..879cadb --- /dev/null +++ b/cmd/check_example2/main_test.go @@ -0,0 +1,28 @@ +package main + +import ( + "os" + "testing" + + "github.com/NETWAYS/go-check/testhelper" + "github.com/stretchr/testify/assert" +) + +func TestMyMain(t *testing.T) { + stdout := testhelper.RunMainTest(main) + + resultString := `WARNING - states: warning=1 ok=1 +\_ [OK] Check1 +\_ [WARNING] Check2 +|foo=23 bar=42 'foo2 bar'=46 + +would exit with code 1 +` + + assert.Equal(t, resultString, stdout) +} + +func TestMain(m *testing.M) { + testhelper.EnableTestMode() + os.Exit(m.Run()) +} From 3b791235c0a57fbe8787c95d72f2156233a4e0d8 Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Fri, 16 Jun 2023 16:58:28 +0200 Subject: [PATCH 3/5] Make a test slightly more complex --- result/overall_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/result/overall_test.go b/result/overall_test.go index e35c048..ce8ede0 100644 --- a/result/overall_test.go +++ b/result/overall_test.go @@ -370,7 +370,7 @@ func TestSubchecksPerfdata(t *testing.T) { Output: "Check2", Perfdata: perfdata.PerfdataList{ &perfdata.Perfdata{ - Label: "foo2", + Label: "foo2 bar", Value: 46, }, }, @@ -379,7 +379,7 @@ func TestSubchecksPerfdata(t *testing.T) { overall.AddSubcheck(check1) overall.AddSubcheck(check2) - resultString := "states: warning=1 ok=1\n\\_ [OK] Check1\n\\_ [WARNING] Check2\n|foo=23 bar=42 foo2=46\n" + resultString := "states: warning=1 ok=1\n\\_ [OK] Check1\n\\_ [WARNING] Check2\n|foo=23 bar=42 'foo2 bar'=46\n" assert.Equal(t, resultString, overall.GetOutput()) } From 6bcbd85e6d5d6e19a29466e30d22f1b968e2368b Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Sat, 17 Jun 2023 11:06:02 +0200 Subject: [PATCH 4/5] Error check example2 --- cmd/check_example2/main.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cmd/check_example2/main.go b/cmd/check_example2/main.go index 2d1af20..ba23761 100644 --- a/cmd/check_example2/main.go +++ b/cmd/check_example2/main.go @@ -14,7 +14,12 @@ func main() { check1 := result.PartialResult{} check1.Output = "Check1" - check1.SetState(check.OK) + err := check1.SetState(check.OK) + + if err != nil { + check.ExitError(err) + } + check1.Perfdata.Add(&perfdata.Perfdata{ Label: "foo", Value: 23, @@ -23,7 +28,11 @@ func main() { check2 := result.PartialResult{} check2.Output = "Check2" - check2.SetState(check.Warning) + err = check2.SetState(check.Warning) + + if err != nil { + check.ExitError(err) + } check2.Perfdata.Add(&perfdata.Perfdata{ Label: "bar", From 0392ff62cd10a5ce868537a6111dfd1d728ff789 Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Sat, 17 Jun 2023 11:23:06 +0200 Subject: [PATCH 5/5] Fix test expected for check_example2 --- cmd/check_example2/main_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/check_example2/main_test.go b/cmd/check_example2/main_test.go index 879cadb..4e644a4 100644 --- a/cmd/check_example2/main_test.go +++ b/cmd/check_example2/main_test.go @@ -11,7 +11,7 @@ import ( func TestMyMain(t *testing.T) { stdout := testhelper.RunMainTest(main) - resultString := `WARNING - states: warning=1 ok=1 + resultString := `[WARNING] - states: warning=1 ok=1 \_ [OK] Check1 \_ [WARNING] Check2 |foo=23 bar=42 'foo2 bar'=46