Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overall output fixes #55

Merged
merged 5 commits into from
Dec 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ linters:
- exportloopref
disable:
- scopelint
- deadcode
- structcheck
- varcheck
presets:
- bugs
- unused
Expand Down
37 changes: 36 additions & 1 deletion perfdata/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package perfdata

import (
"fmt"
"testing"

"github.com/NETWAYS/go-check"
"github.com/stretchr/testify/assert"
)

func ExamplePerfdata() {
Expand All @@ -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())
}
28 changes: 16 additions & 12 deletions result/overall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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())
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
65 changes: 53 additions & 12 deletions result/overall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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{
Expand All @@ -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())
}