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()) }