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
50 changes: 50 additions & 0 deletions cmd/check_example2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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"
err := check1.SetState(check.OK)

if err != nil {
check.ExitError(err)
}

check1.Perfdata.Add(&perfdata.Perfdata{
Label: "foo",
Value: 23,
})

check2 := result.PartialResult{}

check2.Output = "Check2"
err = check2.SetState(check.Warning)

if err != nil {
check.ExitError(err)
}

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())
}
28 changes: 28 additions & 0 deletions cmd/check_example2/main_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
15 changes: 14 additions & 1 deletion perfdata/list_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package perfdata

import "fmt"
import (
"fmt"
"testing"

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

func ExamplePerfdataList() {
list := PerfdataList{}
Expand All @@ -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())
}
36 changes: 36 additions & 0 deletions result/overall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,42 @@ func TestOverall_withSubchecks_PartialResultStatus(t *testing.T) {
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 bar",
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 bar'=46\n"

assert.Equal(t, resultString, overall.GetOutput())
}

func TestDefaultStates1(t *testing.T) {
var overall Overall

Expand Down