From dd9f67ff9b3fc2b4f16049840bc38a88eb8e7fbd Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Mon, 14 Apr 2025 16:07:38 +0200 Subject: [PATCH] Rework to use stdlib testing --- config_test.go | 20 ++- convert/bytes_common_test.go | 73 ++++++++--- convert/bytes_iec_test.go | 61 ++++++--- convert/bytes_si_test.go | 70 ++++++---- examples/check_example/main_test.go | 27 ++-- examples/check_example2/main_test.go | 9 +- go.mod | 11 +- go.sum | 10 -- perfdata/list_test.go | 9 +- perfdata/type_test.go | 30 +++-- result/overall_test.go | 144 +++++++++++++++----- result/worst_test.go | 44 +++++-- status_test.go | 2 +- threshold_test.go | 189 ++++++++++++++++++++------- 14 files changed, 497 insertions(+), 202 deletions(-) diff --git a/config_test.go b/config_test.go index 663d3bc..2e78a8d 100644 --- a/config_test.go +++ b/config_test.go @@ -3,8 +3,6 @@ package check import ( "os" "testing" - - "github.com/stretchr/testify/assert" ) func ExampleConfig() { @@ -37,11 +35,21 @@ func TestLoadFromEnv(t *testing.T) { err := os.Setenv("EXAMPLE", "foobar") defer os.Unsetenv("EXAMPLE") // just to not create any side effects - assert.NoError(t, err) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } LoadFromEnv(&c) - assert.Equal(t, "foobar", c.Bearer) - assert.Equal(t, "", c.Auth) - assert.Equal(t, "", c.OneMoreThanTags) + if c.Bearer != "foobar" { + t.Fatalf("expected %v, got %v", c.Bearer, "foobar") + } + + if c.Auth != "" { + t.Fatalf("expected %v, got %v", c.Auth, "") + } + + if c.OneMoreThanTags != "" { + t.Fatalf("expected %v, got %v", c.OneMoreThanTags, "") + } } diff --git a/convert/bytes_common_test.go b/convert/bytes_common_test.go index 8a258ef..66f346a 100644 --- a/convert/bytes_common_test.go +++ b/convert/bytes_common_test.go @@ -1,42 +1,77 @@ package convert import ( - "github.com/stretchr/testify/assert" "testing" ) func TestParseBytes(t *testing.T) { b, err := ParseBytes("1024") - assert.NoError(t, err) - assert.IsType(t, BytesIEC(0), b) - assert.Equal(t, uint64(1024), b.Bytes()) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if _, ok := b.(BytesIEC); !ok { + t.Fatalf("expected type BytesIEC, got %T", b) + } + if b.Bytes() != 1024 { + t.Fatalf("expected 1024 bytes, got %d", b.Bytes()) + } b, err = ParseBytes("1MB") - assert.NoError(t, err) - assert.IsType(t, BytesSI(0), b) - assert.Equal(t, uint64(1000*1000), b.Bytes()) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if _, ok := b.(BytesSI); !ok { + t.Fatalf("expected type BytesSI, got %T", b) + } + if b.Bytes() != 1000*1000 { + t.Fatalf("expected 1000000 bytes, got %d", b.Bytes()) + } b, err = ParseBytes("1 MiB") - assert.NoError(t, err) - assert.IsType(t, BytesIEC(0), b) - assert.Equal(t, uint64(1024*1024), b.Bytes()) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if _, ok := b.(BytesIEC); !ok { + t.Fatalf("expected type BytesIEC, got %T", b) + } + if b.Bytes() != 1024*1024 { + t.Fatalf("expected 1048576 bytes, got %d", b.Bytes()) + } b, err = ParseBytes("100MB") - assert.NoError(t, err) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } b, err = ParseBytes("100MiB") - assert.NoError(t, err) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } b, err = ParseBytes(" 23 GiB ") - assert.NoError(t, err) - assert.IsType(t, BytesIEC(0), b) - assert.Equal(t, uint64(23*1024*1024*1024), b.Bytes()) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if _, ok := b.(BytesIEC); !ok { + t.Fatalf("expected type BytesIEC, got %T", b) + } + if b.Bytes() != 23*1024*1024*1024 { + t.Fatalf("expected 24742653952 bytes, got %d", b.Bytes()) + } b, err = ParseBytes("1.2.3.4MB") - assert.Error(t, err) - assert.Nil(t, b) + if err == nil { + t.Fatalf("expected error, got nil") + } + if b != nil { + t.Fatalf("expected nil, got %v", b) + } b, err = ParseBytes("1PHD") - assert.Error(t, err) - assert.Nil(t, b) + if err == nil { + t.Fatalf("expected error, got nil") + } + if b != nil { + t.Fatalf("expected nil, got %v", b) + } } diff --git a/convert/bytes_iec_test.go b/convert/bytes_iec_test.go index 2beb17b..d9ff2df 100644 --- a/convert/bytes_iec_test.go +++ b/convert/bytes_iec_test.go @@ -1,27 +1,50 @@ package convert import ( - "github.com/stretchr/testify/assert" "testing" ) func TestBytesIEC_HumanReadable(t *testing.T) { - assert.Equal(t, "0B", BytesIEC(0).HumanReadable()) - assert.Equal(t, "999B", BytesIEC(999).HumanReadable()) - assert.Equal(t, "999KiB", BytesIEC(999*1024).HumanReadable()) - assert.Equal(t, "999MiB", BytesIEC(999*1024*1024).HumanReadable()) - assert.Equal(t, "999GiB", BytesIEC(999*1024*1024*1024).HumanReadable()) - assert.Equal(t, "999TiB", BytesIEC(999*1024*1024*1024*1024).HumanReadable()) - assert.Equal(t, "4PiB", BytesIEC(4*1024*1024*1024*1024*1024).HumanReadable()) - assert.Equal(t, "4096PiB", BytesIEC(4*1024*1024*1024*1024*1024*1024).HumanReadable()) - - assert.Equal(t, "1263MiB", BytesIEC(1263*1024*1024).HumanReadable()) // and not 1.23GiB - - assert.Equal(t, "100MiB", BytesIEC(100*1024*1024).HumanReadable()) - - assert.Equal(t, "123.05MiB", BytesIEC(129032519).HumanReadable()) - assert.Equal(t, "14.67GiB", BytesIEC(15756365824).HumanReadable()) - - assert.Equal(t, "1024KiB", BytesIEC(1024*1024).HumanReadable()) - assert.Equal(t, "2MiB", BytesIEC(2*1024*1024).HumanReadable()) + if BytesIEC(0).HumanReadable() != "0B" { + t.Fatalf("expected '0B', got %s", BytesIEC(0).HumanReadable()) + } + if BytesIEC(999).HumanReadable() != "999B" { + t.Fatalf("expected '999B', got %s", BytesIEC(999).HumanReadable()) + } + if BytesIEC(999*1024).HumanReadable() != "999KiB" { + t.Fatalf("expected '999KiB', got %s", BytesIEC(999*1024).HumanReadable()) + } + if BytesIEC(999*1024*1024).HumanReadable() != "999MiB" { + t.Fatalf("expected '999MiB', got %s", BytesIEC(999*1024*1024).HumanReadable()) + } + if BytesIEC(999*1024*1024*1024).HumanReadable() != "999GiB" { + t.Fatalf("expected '999GiB', got %s", BytesIEC(999*1024*1024*1024).HumanReadable()) + } + if BytesIEC(999*1024*1024*1024*1024).HumanReadable() != "999TiB" { + t.Fatalf("expected '999TiB', got %s", BytesIEC(999*1024*1024*1024*1024).HumanReadable()) + } + if BytesIEC(4*1024*1024*1024*1024*1024).HumanReadable() != "4PiB" { + t.Fatalf("expected '4PiB', got %s", BytesIEC(4*1024*1024*1024*1024*1024).HumanReadable()) + } + if BytesIEC(4*1024*1024*1024*1024*1024*1024).HumanReadable() != "4096PiB" { + t.Fatalf("expected '4096PiB', got %s", BytesIEC(4*1024*1024*1024*1024*1024*1024).HumanReadable()) + } + if BytesIEC(1263*1024*1024).HumanReadable() != "1263MiB" { + t.Fatalf("expected '1263MiB', got %s", BytesIEC(1263*1024*1024).HumanReadable()) + } + if BytesIEC(100*1024*1024).HumanReadable() != "100MiB" { + t.Fatalf("expected '100MiB', got %s", BytesIEC(100*1024*1024).HumanReadable()) + } + if BytesIEC(129032519).HumanReadable() != "123.05MiB" { + t.Fatalf("expected '123.05MiB', got %s", BytesIEC(129032519).HumanReadable()) + } + if BytesIEC(15756365824).HumanReadable() != "14.67GiB" { + t.Fatalf("expected '14.67GiB', got %s", BytesIEC(15756365824).HumanReadable()) + } + if BytesIEC(1024*1024).HumanReadable() != "1024KiB" { + t.Fatalf("expected '1024KiB', got %s", BytesIEC(1024*1024).HumanReadable()) + } + if BytesIEC(2*1024*1024).HumanReadable() != "2MiB" { + t.Fatalf("expected '2MiB', got %s", BytesIEC(2*1024*1024).HumanReadable()) + } } diff --git a/convert/bytes_si_test.go b/convert/bytes_si_test.go index 98d8b6e..e21dfc2 100644 --- a/convert/bytes_si_test.go +++ b/convert/bytes_si_test.go @@ -1,30 +1,56 @@ package convert import ( - "github.com/stretchr/testify/assert" "testing" ) func TestBytesSI_HumanReadable(t *testing.T) { - assert.Equal(t, "0B", BytesSI(0).HumanReadable()) - assert.Equal(t, "999B", BytesSI(999).HumanReadable()) - assert.Equal(t, "999KB", BytesSI(999*1000).HumanReadable()) - assert.Equal(t, "999MB", BytesSI(999*1000*1000).HumanReadable()) - assert.Equal(t, "999GB", BytesSI(999*1000*1000*1000).HumanReadable()) - assert.Equal(t, "999TB", BytesSI(999*1000*1000*1000*1000).HumanReadable()) - - assert.Equal(t, "4PB", BytesSI(4*1000*1000*1000*1000*1000).HumanReadable()) - assert.Equal(t, "4000PB", BytesSI(4*1000*1000*1000*1000*1000*1000).HumanReadable()) - - assert.Equal(t, "4TB", BytesSI(4*1000*1000*1000*1000).HumanReadable()) - assert.Equal(t, "4PB", BytesSI(4*1000*1000*1000*1000*1000).HumanReadable()) - - assert.Equal(t, "1263MB", BytesSI(1263*1000*1000).HumanReadable()) // and not 1.23GiB - - assert.Equal(t, "123.05MB", BytesSI(123050*1000).HumanReadable()) - assert.Equal(t, "14.67GB", BytesSI(14670*1000*1000).HumanReadable()) - - assert.Equal(t, "1000KB", BytesSI(1000*1000).HumanReadable()) - assert.Equal(t, "2MB", BytesSI(2*1000*1000).HumanReadable()) - assert.Equal(t, "3MB", BytesSI(3*1000*1000).HumanReadable()) + if BytesSI(0).HumanReadable() != "0B" { + t.Fatalf("expected '0B', got %s", BytesSI(0).HumanReadable()) + } + if BytesSI(999).HumanReadable() != "999B" { + t.Fatalf("expected '999B', got %s", BytesSI(999).HumanReadable()) + } + if BytesSI(999*1000).HumanReadable() != "999KB" { + t.Fatalf("expected '999KB', got %s", BytesSI(999*1000).HumanReadable()) + } + if BytesSI(999*1000*1000).HumanReadable() != "999MB" { + t.Fatalf("expected '999MB', got %s", BytesSI(999*1000*1000).HumanReadable()) + } + if BytesSI(999*1000*1000*1000).HumanReadable() != "999GB" { + t.Fatalf("expected '999GB', got %s", BytesSI(999*1000*1000*1000).HumanReadable()) + } + if BytesSI(999*1000*1000*1000*1000).HumanReadable() != "999TB" { + t.Fatalf("expected '999TB', got %s", BytesSI(999*1000*1000*1000*1000).HumanReadable()) + } + if BytesSI(4*1000*1000*1000*1000*1000).HumanReadable() != "4PB" { + t.Fatalf("expected '4PB', got %s", BytesSI(4*1000*1000*1000*1000*1000).HumanReadable()) + } + if BytesSI(4*1000*1000*1000*1000*1000*1000).HumanReadable() != "4000PB" { + t.Fatalf("expected '4000PB', got %s", BytesSI(4*1000*1000*1000*1000*1000*1000).HumanReadable()) + } + if BytesSI(4*1000*1000*1000*1000).HumanReadable() != "4TB" { + t.Fatalf("expected '4TB', got %s", BytesSI(4*1000*1000*1000*1000).HumanReadable()) + } + if BytesSI(4*1000*1000*1000*1000*1000).HumanReadable() != "4PB" { + t.Fatalf("expected '4PB', got %s", BytesSI(4*1000*1000*1000*1000*1000).HumanReadable()) + } + if BytesSI(1263*1000*1000).HumanReadable() != "1263MB" { + t.Fatalf("expected '1263MB', got %s", BytesSI(1263*1000*1000).HumanReadable()) + } + if BytesSI(123050*1000).HumanReadable() != "123.05MB" { + t.Fatalf("expected '123.05MB', got %s", BytesSI(123050*1000).HumanReadable()) + } + if BytesSI(14670*1000*1000).HumanReadable() != "14.67GB" { + t.Fatalf("expected '14.67GB', got %s", BytesSI(14670*1000*1000).HumanReadable()) + } + if BytesSI(1000*1000).HumanReadable() != "1000KB" { + t.Fatalf("expected '1000KB', got %s", BytesSI(1000*1000).HumanReadable()) + } + if BytesSI(2*1000*1000).HumanReadable() != "2MB" { + t.Fatalf("expected '2MB', got %s", BytesSI(2*1000*1000).HumanReadable()) + } + if BytesSI(3*1000*1000).HumanReadable() != "3MB" { + t.Fatalf("expected '3MB', got %s", BytesSI(3*1000*1000).HumanReadable()) + } } diff --git a/examples/check_example/main_test.go b/examples/check_example/main_test.go index 247693a..33187da 100644 --- a/examples/check_example/main_test.go +++ b/examples/check_example/main_test.go @@ -2,22 +2,33 @@ package main import ( "os" - "regexp" + "strings" "testing" "github.com/NETWAYS/go-check/testhelper" - "github.com/stretchr/testify/assert" ) func TestMyMain(t *testing.T) { - stdout := testhelper.RunMainTest(main, "--help") - assert.Regexp(t, regexp.MustCompile(`would exit with code 3`), stdout) + actual := testhelper.RunMainTest(main, "--help") + expected := `would exit with code 3` - stdout = testhelper.RunMainTest(main, "--warning", "20") - assert.Regexp(t, regexp.MustCompile(`^\[OK\] - value is 10\nwould exit with code 0\n$`), stdout) + if !strings.Contains(actual, expected) { + t.Fatalf("expected %v, got %v", expected, actual) + } - stdout = testhelper.RunMainTest(main, "--warning", "10", "--value", "11") - assert.Regexp(t, regexp.MustCompile(`^\[WARNING\] - value is 11\nwould exit with code 1\n$`), stdout) + actual = testhelper.RunMainTest(main, "--warning", "20") + expected = "[OK] - value is 10" + + if !strings.Contains(actual, expected) { + t.Fatalf("expected %v, got %v", expected, actual) + } + + actual = testhelper.RunMainTest(main, "--warning", "10", "--value", "11") + expected = "[WARNING] - value is 11" + + if !strings.Contains(actual, expected) { + t.Fatalf("expected %v, got %v", expected, actual) + } } func TestMain(m *testing.M) { diff --git a/examples/check_example2/main_test.go b/examples/check_example2/main_test.go index 4e644a4..55460de 100644 --- a/examples/check_example2/main_test.go +++ b/examples/check_example2/main_test.go @@ -5,13 +5,12 @@ import ( "testing" "github.com/NETWAYS/go-check/testhelper" - "github.com/stretchr/testify/assert" ) func TestMyMain(t *testing.T) { - stdout := testhelper.RunMainTest(main) + actual := testhelper.RunMainTest(main) - resultString := `[WARNING] - states: warning=1 ok=1 + expected := `[WARNING] - states: warning=1 ok=1 \_ [OK] Check1 \_ [WARNING] Check2 |foo=23 bar=42 'foo2 bar'=46 @@ -19,7 +18,9 @@ func TestMyMain(t *testing.T) { would exit with code 1 ` - assert.Equal(t, resultString, stdout) + if actual != expected { + t.Fatalf("expected %v, got %v", expected, actual) + } } func TestMain(m *testing.M) { diff --git a/go.mod b/go.mod index fe1a0d5..9234648 100644 --- a/go.mod +++ b/go.mod @@ -2,13 +2,4 @@ module github.com/NETWAYS/go-check go 1.22 -require ( - github.com/spf13/pflag v1.0.6 - github.com/stretchr/testify v1.10.0 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) +require github.com/spf13/pflag v1.0.6 diff --git a/go.sum b/go.sum index 2ae7370..7cf1763 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,2 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/perfdata/list_test.go b/perfdata/list_test.go index 5694c52..5c53005 100644 --- a/perfdata/list_test.go +++ b/perfdata/list_test.go @@ -3,8 +3,6 @@ package perfdata import ( "fmt" "testing" - - "github.com/stretchr/testify/assert" ) func ExamplePerfdataList() { @@ -23,7 +21,12 @@ func TestPerfdataListFormating(t *testing.T) { list.Add(&Perfdata{Label: "test1", Value: 23}) list.Add(&Perfdata{Label: "test2", Value: 42}) - assert.Equal(t, "test1=23 test2=42", list.String()) + actual := list.String() + expected := "test1=23 test2=42" + + if actual != expected { + t.Fatalf("expected %v, got %v", expected, actual) + } } func BenchmarkPerfdataListFormating(b *testing.B) { diff --git a/perfdata/type_test.go b/perfdata/type_test.go index 5eff5e3..ed4424d 100644 --- a/perfdata/type_test.go +++ b/perfdata/type_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/NETWAYS/go-check" - "github.com/stretchr/testify/assert" ) func BenchmarkPerfdataString(b *testing.B) { @@ -88,23 +87,33 @@ func TestRenderPerfdata(t *testing.T) { for name, tc := range testcases { t.Run(name, func(t *testing.T) { pfVal, err := tc.perf.ValidatedString() - assert.NoError(t, err) - assert.Equal(t, tc.expected, pfVal) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + if tc.expected != pfVal { + t.Fatalf("expected %v, got %v", tc.expected, pfVal) + } }) } for name, tc := range testcasesWithErrors { t.Run(name, func(t *testing.T) { pfVal, err := tc.perf.ValidatedString() - assert.Error(t, err) - assert.Equal(t, tc.expected, pfVal) + if err == nil { + t.Fatalf("expected error, got none") + } + + if tc.expected != pfVal { + t.Fatalf("expected %v, got %v", tc.expected, pfVal) + } }) } } type pfFormatTest struct { Result string - InputValue interface{} + InputValue any } func TestFormatNumeric(t *testing.T) { @@ -137,7 +146,12 @@ func TestFormatNumeric(t *testing.T) { for _, val := range testdata { formatted, err := formatNumeric(val.InputValue) - assert.NoError(t, err) - assert.Equal(t, val.Result, formatted) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + if val.Result != formatted { + t.Fatalf("expected %v, got %v", formatted, val.Result) + } } } diff --git a/result/overall_test.go b/result/overall_test.go index ab84109..a1a8dfe 100644 --- a/result/overall_test.go +++ b/result/overall_test.go @@ -2,43 +2,67 @@ package result import ( "fmt" + "reflect" "testing" "github.com/NETWAYS/go-check" "github.com/NETWAYS/go-check/perfdata" - "github.com/stretchr/testify/assert" ) func TestOverall_AddOK(t *testing.T) { overall := Overall{} overall.Add(0, "test ok") - assert.Equal(t, 1, overall.oks) - assert.ElementsMatch(t, overall.Outputs, []string{"[OK] test ok"}) + if overall.oks != 1 { + t.Fatalf("expected 1, got %d", overall.oks) + } + + expectedOutputs := []string{"[OK] test ok"} + if !reflect.DeepEqual(overall.Outputs, expectedOutputs) { + t.Fatalf("expected %v, got %v", expectedOutputs, overall.Outputs) + } } func TestOverall_AddWarning(t *testing.T) { overall := Overall{} overall.Add(1, "test warning") - assert.Equal(t, 1, overall.warnings) - assert.ElementsMatch(t, overall.Outputs, []string{"[WARNING] test warning"}) + if overall.warnings != 1 { + t.Fatalf("expected 1, got %d", overall.warnings) + } + + expectedOutputs := []string{"[WARNING] test warning"} + if !reflect.DeepEqual(overall.Outputs, expectedOutputs) { + t.Fatalf("expected %v, got %v", expectedOutputs, overall.Outputs) + } } func TestOverall_AddCritical(t *testing.T) { overall := Overall{} overall.Add(2, "test critical") - assert.Equal(t, 1, overall.criticals) - assert.ElementsMatch(t, overall.Outputs, []string{"[CRITICAL] test critical"}) + if overall.criticals != 1 { + t.Fatalf("expected 1, got %d", overall.criticals) + } + + expectedOutputs := []string{"[CRITICAL] test critical"} + if !reflect.DeepEqual(overall.Outputs, expectedOutputs) { + t.Fatalf("expected %v, got %v", expectedOutputs, overall.Outputs) + } } func TestOverall_AddUnknown(t *testing.T) { overall := Overall{} overall.Add(3, "test unknown") - assert.Equal(t, 1, overall.unknowns) - assert.ElementsMatch(t, overall.Outputs, []string{"[UNKNOWN] test unknown"}) + if overall.unknowns != 1 { + t.Fatalf("expected 1, got %d", overall.unknowns) + } + + expectedOutputs := []string{"[UNKNOWN] test unknown"} + if !reflect.DeepEqual(overall.Outputs, expectedOutputs) { + t.Fatalf("expected %v, got %v", expectedOutputs, overall.Outputs) + } } func TestOverall_GetStatus_GetSummary(t *testing.T) { @@ -80,8 +104,13 @@ func TestOverall_GetStatus_GetSummary(t *testing.T) { } for _, test := range testcases { - assert.Equal(t, test.expectedStatus, test.actual.GetStatus()) - assert.Equal(t, test.expectedSummary, test.actual.GetSummary()) + if test.expectedStatus != test.actual.GetStatus() { + t.Fatalf("expected %d, got %d", test.expectedStatus, test.actual.GetStatus()) + } + + if test.expectedSummary != test.actual.GetSummary() { + t.Fatalf("expected %s, got %s", test.expectedSummary, test.actual.GetSummary()) + } } } @@ -92,18 +121,32 @@ func TestOverall_GetOutput(t *testing.T) { overall.Add(0, "First OK") overall.Add(0, "Second OK") - assert.Equal(t, "states: ok=2\n[OK] First OK\n[OK] Second OK\n", overall.GetOutput()) + expected := "states: ok=2\n[OK] First OK\n[OK] Second OK\n" + + if expected != overall.GetOutput() { + t.Fatalf("expected %s, got %s", expected, overall.GetOutput()) + } overall = Overall{} overall.Add(0, "State OK") - // TODO: compress when only one state - assert.Equal(t, "states: ok=1\n[OK] State OK\n", overall.GetOutput()) + expected = "states: ok=1\n[OK] State OK\n" + + if expected != overall.GetOutput() { + t.Fatalf("expected %s, got %s", expected, overall.GetOutput()) + } + + // TODO: compress when only one state overall = Overall{} overall.Add(0, "First OK") overall.Add(2, "Second Critical") overall.Summary = "Custom Summary" - assert.Equal(t, "Custom Summary\n[OK] First OK\n[CRITICAL] Second Critical\n", overall.GetOutput()) + + expected = "Custom Summary\n[OK] First OK\n[CRITICAL] Second Critical\n" + + if expected != overall.GetOutput() { + t.Fatalf("expected %s, got %s", expected, overall.GetOutput()) + } } func ExampleOverall_Add() { @@ -208,9 +251,14 @@ func TestOverall_withEnhancedSubchecks(t *testing.T) { \_ [WARNING] Subcheck2 Test |pd_test=5s pd_test2=1099511627776kB;@3.14:7036874417766;549755813887:1208925819614629174706176;;18446744073709551615 kl;jr2if;l2rkjasdf=5m asdf=18446744073709551615B ` - assert.Equal(t, expectedString, resString) - assert.Equal(t, check.Warning, overall.GetStatus()) + if expectedString != resString { + t.Fatalf("expected %s, got %s", expectedString, resString) + } + + if check.Warning != overall.GetStatus() { + t.Fatalf("expected %d, got %d", check.Warning, overall.GetStatus()) + } } func TestOverall_withSubchecks_Simple_Output(t *testing.T) { @@ -239,7 +287,9 @@ func TestOverall_withSubchecks_Simple_Output(t *testing.T) { \_ [OK] SubSubcheck ` - assert.Equal(t, resString, output) + if output != resString { + t.Fatalf("expected %s, got %s", output, resString) + } } func TestOverall_withSubchecks_Perfdata(t *testing.T) { @@ -279,8 +329,13 @@ func TestOverall_withSubchecks_Perfdata(t *testing.T) { |foo=3 bar=300% ` - assert.Equal(t, res, overall.GetOutput()) - assert.Equal(t, 0, overall.GetStatus()) + if res != overall.GetOutput() { + t.Fatalf("expected %s, got %s", res, overall.GetOutput()) + } + + if 0 != overall.GetStatus() { + t.Fatalf("expected %d, got %d", 0, overall.GetStatus()) + } } func TestOverall_withSubchecks_PartialResult(t *testing.T) { @@ -330,8 +385,13 @@ func TestOverall_withSubchecks_PartialResult(t *testing.T) { |foo=3 bar=300% baz=23B ` - assert.Equal(t, res, overall.GetOutput()) - assert.Equal(t, check.Critical, overall.GetStatus()) + if res != overall.GetOutput() { + t.Fatalf("expected %s, got %s", res, overall.GetOutput()) + } + + if check.Critical != overall.GetStatus() { + t.Fatalf("expected %d, got %d", 2, overall.GetStatus()) + } } func TestOverall_withSubchecks_PartialResultStatus(t *testing.T) { @@ -364,8 +424,14 @@ func TestOverall_withSubchecks_PartialResultStatus(t *testing.T) { \_ [WARNING] SubSubcheck \_ [CRITICAL] SubSubSubcheck ` - assert.Equal(t, res, overall.GetOutput()) - assert.Equal(t, 0, overall.GetStatus()) + + if res != overall.GetOutput() { + t.Fatalf("expected %s, got %s", res, overall.GetOutput()) + } + + if 0 != overall.GetStatus() { + t.Fatalf("expected %d, got %d", 0, overall.GetStatus()) + } } func TestSubchecksPerfdata(t *testing.T) { @@ -404,7 +470,9 @@ func TestSubchecksPerfdata(t *testing.T) { 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()) + if resultString != overall.GetOutput() { + t.Fatalf("expected %s, got %s", resultString, overall.GetOutput()) + } } func TestDefaultStates1(t *testing.T) { @@ -416,7 +484,9 @@ func TestDefaultStates1(t *testing.T) { overall.AddSubcheck(subcheck) - assert.Equal(t, check.OK, overall.GetStatus()) + if check.OK != overall.GetStatus() { + t.Fatalf("expected %d, got %d", check.OK, overall.GetStatus()) + } } func TestDefaultStates2(t *testing.T) { @@ -426,8 +496,13 @@ func TestDefaultStates2(t *testing.T) { overall.AddSubcheck(subcheck) - assert.Equal(t, check.Unknown, subcheck.GetStatus()) - assert.Equal(t, check.Unknown, overall.GetStatus()) + if check.Unknown != subcheck.GetStatus() { + t.Fatalf("expected %d, got %d", check.Unknown, subcheck.GetStatus()) + } + + if check.Unknown != overall.GetStatus() { + t.Fatalf("expected %d, got %d", check.Unknown, overall.GetStatus()) + } } func TestDefaultStates3(t *testing.T) { @@ -440,7 +515,9 @@ func TestDefaultStates3(t *testing.T) { overall.AddSubcheck(subcheck) - assert.Equal(t, check.Warning, overall.GetStatus()) + if check.Warning != overall.GetStatus() { + t.Fatalf("expected %d, got %d", check.Warning, overall.GetStatus()) + } } func TestOverallOutputWithMultiLayerPartials(t *testing.T) { @@ -465,6 +542,11 @@ func TestOverallOutputWithMultiLayerPartials(t *testing.T) { resultString := "states: critical=1 warning=1\n\\_ [WARNING] \n\\_ [CRITICAL] \n \\_ [OK] \n \\_ [CRITICAL] \n" - assert.Equal(t, resultString, overall.GetOutput()) - assert.Equal(t, check.Critical, overall.GetStatus()) + if resultString != overall.GetOutput() { + t.Fatalf("expected %s, got %s", resultString, overall.GetOutput()) + } + + if check.Critical != overall.GetStatus() { + t.Fatalf("expected %d, got %d", check.Critical, overall.GetStatus()) + } } diff --git a/result/worst_test.go b/result/worst_test.go index 5795e7d..c4e509b 100644 --- a/result/worst_test.go +++ b/result/worst_test.go @@ -1,24 +1,42 @@ package result import ( - "github.com/stretchr/testify/assert" "testing" ) -func TestWorstState(t *testing.T) { - - assert.Equal(t, 3, WorstState(3)) - assert.Equal(t, 2, WorstState(2)) - assert.Equal(t, 1, WorstState(1)) - assert.Equal(t, 0, WorstState(0)) +func TestWorstState2(t *testing.T) { + if WorstState(3) != 3 { + t.Fatalf("expected 3, got %d", WorstState(3)) + } + if WorstState(2) != 2 { + t.Fatalf("expected 2, got %d", WorstState(2)) + } + if WorstState(1) != 1 { + t.Fatalf("expected 1, got %d", WorstState(1)) + } + if WorstState(0) != 0 { + t.Fatalf("expected 0, got %d", WorstState(0)) + } - assert.Equal(t, 2, WorstState(0, 1, 2, 3)) - assert.Equal(t, 3, WorstState(0, 1, 3)) - assert.Equal(t, 1, WorstState(1, 0, 0)) - assert.Equal(t, 0, WorstState(0, 0, 0)) + if WorstState(0, 1, 2, 3) != 2 { + t.Fatalf("expected 2, got %d", WorstState(0, 1, 2, 3)) + } + if WorstState(0, 1, 3) != 3 { + t.Fatalf("expected 3, got %d", WorstState(0, 1, 3)) + } + if WorstState(1, 0, 0) != 1 { + t.Fatalf("expected 1, got %d", WorstState(1, 0, 0)) + } + if WorstState(0, 0, 0) != 0 { + t.Fatalf("expected 0, got %d", WorstState(0, 0, 0)) + } - assert.Equal(t, 3, WorstState(-1)) - assert.Equal(t, 3, WorstState(4)) + if WorstState(-1) != 3 { + t.Fatalf("expected 3, got %d", WorstState(-1)) + } + if WorstState(4) != 3 { + t.Fatalf("expected 3, got %d", WorstState(4)) + } } func BenchmarkWorstState(b *testing.B) { diff --git a/status_test.go b/status_test.go index d2f2377..e2d5441 100644 --- a/status_test.go +++ b/status_test.go @@ -32,7 +32,7 @@ func TestStatusText(t *testing.T) { actual := StatusText(tc.input) if actual != tc.expected { - t.Error("\nActual: ", actual, "\nExpected: ", tc.expected) + t.Fatalf("expected %v, got %v", tc.expected, actual) } }) } diff --git a/threshold_test.go b/threshold_test.go index 8270d34..f89ae0c 100644 --- a/threshold_test.go +++ b/threshold_test.go @@ -1,7 +1,8 @@ package check import ( - "github.com/stretchr/testify/assert" + "math" + "reflect" "testing" ) @@ -17,9 +18,15 @@ var testThresholds = map[string]*Threshold{ } func TestBoundaryToString(t *testing.T) { - assert.Equal(t, "10", BoundaryToString(10)) - assert.Equal(t, "10.1", BoundaryToString(10.1)) - assert.Equal(t, "10.001", BoundaryToString(10.001)) + if BoundaryToString(10) != "10" { + t.Fatalf("expected '10', got %s", BoundaryToString(10)) + } + if BoundaryToString(10.1) != "10.1" { + t.Fatalf("expected '10.1', got %s", BoundaryToString(10.1)) + } + if BoundaryToString(10.001) != "10.001" { + t.Fatalf("expected '10.001', got %s", BoundaryToString(10.001)) + } } func TestParseThreshold(t *testing.T) { @@ -27,11 +34,20 @@ func TestParseThreshold(t *testing.T) { th, err := ParseThreshold(spec) if ref == nil { - assert.Error(t, err) + if err == nil { + t.Errorf("Expected error, got nil") + } } else { - assert.NoError(t, err) - assert.Equal(t, ref, th) - assert.Equal(t, spec, th.String()) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + if !reflect.DeepEqual(ref, th) { + t.Fatalf("expected %v, got %v for spec %s", ref, th, spec) + } + if th.String() != spec { + t.Fatalf("expected %s, got %s for spec %s", spec, th.String(), spec) + } } } } @@ -39,7 +55,9 @@ func TestParseThreshold(t *testing.T) { func TestThreshold_String(t *testing.T) { for spec, ref := range testThresholds { if ref != nil { - assert.Equal(t, spec, ref.String()) + if spec != ref.String() { + t.Fatalf("expected %v, got %v", ref.String(), spec) + } } } } @@ -52,54 +70,129 @@ func TestThreshold_String(t *testing.T) { // @10:20 ≥ 10 and ≤ 20, (inside the range of {10 .. 20}) func TestThreshold_DoesViolate(t *testing.T) { thr, err := ParseThreshold("10") - assert.NoError(t, err) - assert.True(t, thr.DoesViolate(11)) - assert.False(t, thr.DoesViolate(10)) - assert.False(t, thr.DoesViolate(0)) - assert.True(t, thr.DoesViolate(-1)) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if !thr.DoesViolate(11) { + t.Fatalf("expected true, got false") + } + if thr.DoesViolate(10) { + t.Fatalf("expected false, got true") + } + if thr.DoesViolate(0) { + t.Fatalf("expected false, got true") + } + if !thr.DoesViolate(-1) { + t.Fatalf("expected true, got false") + } thr, err = ParseThreshold("10:") - assert.NoError(t, err) - assert.False(t, thr.DoesViolate(3000)) - assert.False(t, thr.DoesViolate(10)) - assert.True(t, thr.DoesViolate(9)) - assert.True(t, thr.DoesViolate(0)) - assert.True(t, thr.DoesViolate(-1)) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if thr.DoesViolate(3000) { + t.Fatalf("expected false, got true") + } + if thr.DoesViolate(10) { + t.Fatalf("expected false, got true") + } + if !thr.DoesViolate(9) { + t.Fatalf("expected true, got false") + } + if !thr.DoesViolate(0) { + t.Fatalf("expected true, got false") + } + if !thr.DoesViolate(-1) { + t.Fatalf("expected true, got false") + } thr, err = ParseThreshold("~:10") - assert.NoError(t, err) - assert.False(t, thr.DoesViolate(-3000)) - assert.False(t, thr.DoesViolate(0)) - assert.False(t, thr.DoesViolate(10)) - assert.True(t, thr.DoesViolate(11)) - assert.True(t, thr.DoesViolate(3000)) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if thr.DoesViolate(-3000) { + t.Fatalf("expected false, got true") + } + if thr.DoesViolate(0) { + t.Fatalf("expected false, got true") + } + if thr.DoesViolate(10) { + t.Fatalf("expected false, got true") + } + if !thr.DoesViolate(11) { + t.Fatalf("expected true, got false") + } + if !thr.DoesViolate(3000) { + t.Fatalf("expected true, got false") + } thr, err = ParseThreshold("10:20") - assert.NoError(t, err) - assert.False(t, thr.DoesViolate(10)) - assert.False(t, thr.DoesViolate(15)) - assert.False(t, thr.DoesViolate(20)) - assert.True(t, thr.DoesViolate(9)) - assert.True(t, thr.DoesViolate(-1)) - assert.True(t, thr.DoesViolate(20.1)) - assert.True(t, thr.DoesViolate(3000)) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if thr.DoesViolate(10) { + t.Fatalf("expected false, got true") + } + if thr.DoesViolate(15) { + t.Fatalf("expected false, got true") + } + if thr.DoesViolate(20) { + t.Fatalf("expected false, got true") + } + if !thr.DoesViolate(9) { + t.Fatalf("expected true, got false") + } + if !thr.DoesViolate(-1) { + t.Fatalf("expected true, got false") + } + if !thr.DoesViolate(20.1) { + t.Fatalf("expected true, got false") + } + if !thr.DoesViolate(3000) { + t.Fatalf("expected true, got false") + } thr, err = ParseThreshold("@10:20") - assert.NoError(t, err) - assert.True(t, thr.DoesViolate(10)) - assert.True(t, thr.DoesViolate(15)) - assert.True(t, thr.DoesViolate(20)) - assert.False(t, thr.DoesViolate(9)) - assert.False(t, thr.DoesViolate(-1)) - assert.False(t, thr.DoesViolate(20.1)) - assert.False(t, thr.DoesViolate(3000)) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if !thr.DoesViolate(10) { + t.Fatalf("expected true, got false") + } + if !thr.DoesViolate(15) { + t.Fatalf("expected true, got false") + } + if !thr.DoesViolate(20) { + t.Fatalf("expected true, got false") + } + if thr.DoesViolate(9) { + t.Fatalf("expected false, got true") + } + if thr.DoesViolate(-1) { + t.Fatalf("expected false, got true") + } + if thr.DoesViolate(20.1) { + t.Fatalf("expected false, got true") + } + if thr.DoesViolate(3000) { + t.Fatalf("expected false, got true") + } } func TestFormatFloat(t *testing.T) { - assert.Equal(t, "1000000000000", FormatFloat(1000000000000)) - assert.Equal(t, "1000000000", FormatFloat(1000000000)) - assert.Equal(t, "1234567890.988", FormatFloat(1234567890.9877)) - - assert.Equal(t, "-Inf", FormatFloat(NegInf)) - assert.Equal(t, "+Inf", FormatFloat(PosInf)) + if FormatFloat(1000000000000) != "1000000000000" { + t.Fatalf("expected '1000000000000', got %s", FormatFloat(1000000000000)) + } + if FormatFloat(1000000000) != "1000000000" { + t.Fatalf("expected '1000000000', got %s", FormatFloat(1000000000)) + } + if FormatFloat(1234567890.9877) != "1234567890.988" { + t.Fatalf("expected '1234567890.988', got %s", FormatFloat(1234567890.9877)) + } + if FormatFloat(math.Inf(-1)) != "-Inf" { + t.Fatalf("expected '-Inf', got %s", FormatFloat(math.Inf(-1))) + } + if FormatFloat(math.Inf(1)) != "+Inf" { + t.Fatalf("expected '+Inf', got %s", FormatFloat(math.Inf(1))) + } }