refactor: make internals easier to test and add unit tests (#2)
This PR refactors most of the internals to make them easier to test (and also because the names didn't make sense). It adds unit tests for all internal components. Reviewed-on: #2 Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net> Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
This commit is contained in:
parent
dcd732cc34
commit
78af496fe9
20 changed files with 939 additions and 369 deletions
107
pkg/results/results.go
Normal file
107
pkg/results/results.go
Normal file
|
@ -0,0 +1,107 @@
|
|||
// Package results implements a helper that can be used to store the results of
|
||||
// a Nagios, Centreon, Icinga... service monitoring plugin, and convert them to
|
||||
// text which can be sent to the monitoring server.
|
||||
package results // import nocternity.net/gomonop/pkg/results
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"nocternity.net/gomonop/pkg/perfdata"
|
||||
)
|
||||
|
||||
// Results represents the monitoring plugin's results, including its name,
|
||||
// return status and message, additional lines of text, and performance
|
||||
// data to be encoded in the output.
|
||||
type Results struct {
|
||||
name string
|
||||
status Status
|
||||
message string
|
||||
extraText *list.List
|
||||
perfData map[string]*perfdata.PerfData
|
||||
}
|
||||
|
||||
// New creates the plugin with `name` as its name and an unknown status.
|
||||
func New(name string) *Results {
|
||||
p := new(Results)
|
||||
p.name = name
|
||||
p.status = StatusUnknown
|
||||
p.message = "no status set"
|
||||
p.perfData = make(map[string]*perfdata.PerfData)
|
||||
return p
|
||||
}
|
||||
|
||||
// SetState sets the plugin's output code to `status` and its message to
|
||||
// the specified `message`.
|
||||
func (p *Results) SetState(status Status, message string) {
|
||||
p.status = status
|
||||
p.message = message
|
||||
}
|
||||
|
||||
// AddLine adds the specified string to the extra output text buffer.
|
||||
func (p *Results) AddLine(line string) {
|
||||
if p.extraText == nil {
|
||||
p.extraText = list.New()
|
||||
}
|
||||
p.extraText.PushBack(line)
|
||||
}
|
||||
|
||||
// AddLinef formats the input and adds it to the text buffer.
|
||||
func (p *Results) AddLinef(format string, data ...interface{}) {
|
||||
p.AddLine(fmt.Sprintf(format, data...))
|
||||
}
|
||||
|
||||
// AddLines add the specified `lines` to the output text.
|
||||
func (p *Results) AddLines(lines []string) {
|
||||
for _, line := range lines {
|
||||
p.AddLine(line)
|
||||
}
|
||||
}
|
||||
|
||||
// AddPerfData adds performance data described by the "pd" argument to the
|
||||
// output's performance data. If two performance data records are added for
|
||||
// the same label, the program panics.
|
||||
func (p *Results) AddPerfData(pd *perfdata.PerfData) {
|
||||
_, exists := p.perfData[pd.Label]
|
||||
if exists {
|
||||
panic("duplicate performance data " + pd.Label)
|
||||
}
|
||||
p.perfData[pd.Label] = pd
|
||||
}
|
||||
|
||||
// String generates the plugin's text output from its name, status, text data
|
||||
// and performance data.
|
||||
func (p *Results) String() string {
|
||||
var strBuilder strings.Builder
|
||||
strBuilder.WriteString(p.name)
|
||||
strBuilder.WriteString(" ")
|
||||
strBuilder.WriteString(p.status.String())
|
||||
strBuilder.WriteString(": ")
|
||||
strBuilder.WriteString(p.message)
|
||||
if len(p.perfData) > 0 {
|
||||
strBuilder.WriteString(" | ")
|
||||
needSep := false
|
||||
for _, data := range p.perfData {
|
||||
if needSep {
|
||||
strBuilder.WriteString(", ")
|
||||
} else {
|
||||
needSep = true
|
||||
}
|
||||
strBuilder.WriteString(data.String())
|
||||
}
|
||||
}
|
||||
if p.extraText != nil {
|
||||
for em := p.extraText.Front(); em != nil; em = em.Next() {
|
||||
strBuilder.WriteString("\n")
|
||||
//nolint:forcetypeassert // we want to panic if this isn't a string
|
||||
strBuilder.WriteString(em.Value.(string))
|
||||
}
|
||||
}
|
||||
return strBuilder.String()
|
||||
}
|
||||
|
||||
// ExitCode returns the plugin's exit code.
|
||||
func (p *Results) ExitCode() int {
|
||||
return int(p.status)
|
||||
}
|
175
pkg/results/results_test.go
Normal file
175
pkg/results/results_test.go
Normal file
|
@ -0,0 +1,175 @@
|
|||
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())
|
||||
}
|
19
pkg/results/status.go
Normal file
19
pkg/results/status.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package results // import nocternity.net/gomonop/pkg/results
|
||||
|
||||
// Status represents the return status of the monitoring plugin. The
|
||||
// corresponding integer value will be used as the program's exit code,
|
||||
// to be interpreted by the monitoring system.
|
||||
type Status int
|
||||
|
||||
// Plugin exit statuses.
|
||||
const (
|
||||
StatusOK Status = iota
|
||||
StatusWarning
|
||||
StatusCritical
|
||||
StatusUnknown
|
||||
)
|
||||
|
||||
// String representations of the plugin statuses.
|
||||
func (s Status) String() string {
|
||||
return [...]string{"OK", "WARNING", "ERROR", "UNKNOWN"}[s]
|
||||
}
|
26
pkg/results/status_test.go
Normal file
26
pkg/results/status_test.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package results // import nocternity.net/gomonop/pkg/results
|
||||
|
||||
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…
Add table
Add a link
Reference in a new issue