Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package check
import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func ExampleConfig() {
Expand Down Expand Up @@ -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, "")
}
}
73 changes: 54 additions & 19 deletions convert/bytes_common_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
61 changes: 42 additions & 19 deletions convert/bytes_iec_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
70 changes: 48 additions & 22 deletions convert/bytes_si_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
27 changes: 19 additions & 8 deletions examples/check_example/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions examples/check_example2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ 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

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) {
Expand Down
11 changes: 1 addition & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Loading
Loading