gomonop/pkg/results/results_test.go

176 lines
3.8 KiB
Go
Raw Normal View History

package results // import nocternity.net/gomonop/pkg/results
import (
"container/list"
"testing"
"github.com/stretchr/testify/assert"
"nocternity.net/gomonop/pkg/perfdata"
)
func TestNew(t *testing.T) {
p := New("test")
assert.Equal(t, p.name, "test")
assert.Equal(t, p.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(StatusWarning, "test")
assert.Equal(t, p.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: StatusWarning,
message: "test",
perfData: make(map[string]*perfdata.PerfData),
},
"test WARNING: test",
},
{
func() Results {
p := Results{
name: "test",
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: 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: 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 = StatusOK
assert.Equal(t, int(StatusOK), p.ExitCode())
p.status = StatusWarning
assert.Equal(t, int(StatusWarning), p.ExitCode())
p.status = StatusCritical
assert.Equal(t, int(StatusCritical), p.ExitCode())
p.status = StatusUnknown
assert.Equal(t, int(StatusUnknown), p.ExitCode())
}