gomonop/pkg/results/results_test.go

177 lines
3.9 KiB
Go

package results // import nocternity.net/gomonop/pkg/results
import (
"container/list"
"testing"
"github.com/stretchr/testify/assert"
"nocternity.net/gomonop/pkg/perfdata"
"nocternity.net/gomonop/pkg/status"
)
func TestNew(t *testing.T) {
p := New("test")
assert.Equal(t, p.name, "test")
assert.Equal(t, p.status, status.StatusUnknown)
assert.Equal(t, p.message, "no status set")
assert.Nil(t, p.extraText)
assert.NotNil(t, p.perfData)
}
func TestSetState(t *testing.T) {
p := Results{}
p.SetState(status.StatusWarning, "test")
assert.Equal(t, p.status, status.StatusWarning)
assert.Equal(t, p.message, "test")
}
func TestAddLineFirst(t *testing.T) {
p := Results{}
p.AddLine("test")
assert.NotNil(t, p.extraText)
assert.Equal(t, p.extraText.Len(), 1)
assert.Equal(t, p.extraText.Front().Value, "test")
}
func TestAddLineNext(t *testing.T) {
p := Results{}
p.extraText = list.New()
p.AddLine("test")
assert.Equal(t, p.extraText.Len(), 1)
assert.Equal(t, p.extraText.Front().Value, "test")
}
func TestAddLinef(t *testing.T) {
p := Results{}
p.AddLinef("test %d", 123)
assert.Equal(t, p.extraText.Len(), 1)
assert.Equal(t, p.extraText.Front().Value, "test 123")
}
func TestAddLines(t *testing.T) {
p := Results{}
p.AddLines([]string{"test", "test2"})
assert.Equal(t, p.extraText.Len(), 2)
assert.Equal(t, p.extraText.Front().Value, "test")
assert.Equal(t, p.extraText.Front().Next().Value, "test2")
}
func TestAddPerfData(t *testing.T) {
p := Results{}
p.perfData = make(map[string]*perfdata.PerfData)
p.AddPerfData(&perfdata.PerfData{Label: "test"})
value, ok := p.perfData["test"]
assert.True(t, ok)
assert.Equal(t, value.Label, "test")
}
func TestAddPerfDataDuplicate(t *testing.T) {
p := Results{}
p.perfData = make(map[string]*perfdata.PerfData)
p.perfData["test"] = &perfdata.PerfData{Label: "test"}
assert.Panics(t, func() { p.AddPerfData(&perfdata.PerfData{Label: "test"}) })
}
func TestString(t *testing.T) {
type Test struct {
Results
expected string
}
pdat := perfdata.PerfData{Label: "test"}
tests := []Test{
{
Results{
name: "test",
status: status.StatusWarning,
message: "test",
perfData: make(map[string]*perfdata.PerfData),
},
"test WARNING: test",
},
{
func() Results {
p := Results{
name: "test",
status: status.StatusWarning,
message: "test",
perfData: make(map[string]*perfdata.PerfData),
extraText: list.New(),
}
p.extraText.PushBack("test 1")
p.extraText.PushBack("test 2")
return p
}(),
"test WARNING: test\ntest 1\ntest 2",
},
{
func() Results {
p := Results{
name: "test",
status: status.StatusWarning,
message: "test",
perfData: make(map[string]*perfdata.PerfData),
}
p.perfData["test 1"] = &pdat
p.perfData["test 2"] = &pdat
return p
}(),
"test WARNING: test | " + pdat.String() + ", " +
pdat.String(),
},
{
func() Results {
p := Results{
name: "test",
status: status.StatusWarning,
message: "test",
perfData: make(map[string]*perfdata.PerfData),
extraText: list.New(),
}
p.perfData["test 1"] = &pdat
p.perfData["test 2"] = &pdat
p.extraText.PushBack("test 1")
p.extraText.PushBack("test 2")
return p
}(),
"test WARNING: test | " + pdat.String() + ", " +
pdat.String() + "\ntest 1\ntest 2",
},
}
for _, test := range tests {
result := test.Results.String()
assert.Equal(t, test.expected, result, "Expected '%s', got '%s'", test.expected, result)
}
}
func TestExitCode(t *testing.T) {
p := Results{}
p.status = status.StatusOK
assert.Equal(t, int(status.StatusOK), p.ExitCode())
p.status = status.StatusWarning
assert.Equal(t, int(status.StatusWarning), p.ExitCode())
p.status = status.StatusCritical
assert.Equal(t, int(status.StatusCritical), p.ExitCode())
p.status = status.StatusUnknown
assert.Equal(t, int(status.StatusUnknown), p.ExitCode())
}