refactor: make internals easier to test and add unit tests #2
2 changed files with 201 additions and 0 deletions
175
pkg/plugin/plugin_test.go
Normal file
175
pkg/plugin/plugin_test.go
Normal file
|
@ -0,0 +1,175 @@
|
|||
package plugin // import nocternity.net/gomonop/pkg/plugin
|
||||
|
||||
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 := Plugin{}
|
||||
|
||||
p.SetState(StatusWarning, "test")
|
||||
|
||||
assert.Equal(t, p.status, StatusWarning)
|
||||
assert.Equal(t, p.message, "test")
|
||||
}
|
||||
|
||||
func TestAddLineFirst(t *testing.T) {
|
||||
p := Plugin{}
|
||||
|
||||
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 := Plugin{}
|
||||
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 := Plugin{}
|
||||
|
||||
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 := Plugin{}
|
||||
|
||||
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 := Plugin{}
|
||||
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 := Plugin{}
|
||||
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 {
|
||||
Plugin
|
||||
expected string
|
||||
}
|
||||
|
||||
pdat := perfdata.PerfData{Label: "test"}
|
||||
tests := []Test{
|
||||
{
|
||||
Plugin{
|
||||
name: "test",
|
||||
status: StatusWarning,
|
||||
message: "test",
|
||||
perfData: make(map[string]*perfdata.PerfData),
|
||||
},
|
||||
"test WARNING: test",
|
||||
},
|
||||
{
|
||||
func() Plugin {
|
||||
p := Plugin{
|
||||
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() Plugin {
|
||||
p := Plugin{
|
||||
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() Plugin {
|
||||
p := Plugin{
|
||||
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.Plugin.String()
|
||||
assert.Equal(t, test.expected, result, "Expected '%s', got '%s'", test.expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExitCode(t *testing.T) {
|
||||
p := Plugin{}
|
||||
|
||||
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())
|
||||
}
|
26
pkg/plugin/status_test.go
Normal file
26
pkg/plugin/status_test.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package plugin // import nocternity.net/gomonop/pkg/plugin
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStatusDefaultOK(t *testing.T) {
|
||||
var s Status
|
||||
assert.Equal(t, StatusOK, s)
|
||||
}
|
||||
|
||||
func TestStatusToString(t *testing.T) {
|
||||
assert.Equal(t, "OK", StatusOK.String())
|
||||
assert.Equal(t, "WARNING", StatusWarning.String())
|
||||
assert.Equal(t, "ERROR", StatusCritical.String())
|
||||
assert.Equal(t, "UNKNOWN", StatusUnknown.String())
|
||||
}
|
||||
|
||||
func TestStatusToInt(t *testing.T) {
|
||||
assert.Equal(t, 0, int(StatusOK))
|
||||
assert.Equal(t, 1, int(StatusWarning))
|
||||
assert.Equal(t, 2, int(StatusCritical))
|
||||
assert.Equal(t, 3, int(StatusUnknown))
|
||||
}
|
Loading…
Reference in a new issue