refactor: make internals easier to test and add unit tests #2
4 changed files with 371 additions and 75 deletions
50
pkg/perfdata/internals_test.go
Normal file
50
pkg/perfdata/internals_test.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package perfdata // import nocternity.net/gomonop/pkg/perfdata
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValueCheckValid(t *testing.T) {
|
||||
validValues := []string{
|
||||
"0", "0.", "0.952", "1", "123", "123.", "123.45", ".1",
|
||||
"-0", "-0.", "-0.952", "-1", "-123", "-123.", "-123.45", "-.1",
|
||||
}
|
||||
|
||||
for _, value := range validValues {
|
||||
assert.True(t, valueCheck.MatchString(value), "'%s' is a valid value string", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValueCheckInvalid(t *testing.T) {
|
||||
invalidValues := []string{".", "-.", "a", " ", "", "~"}
|
||||
|
||||
for _, value := range invalidValues {
|
||||
assert.False(t, valueCheck.MatchString(value), "'%s' is an invalid value string", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinCheckValid(t *testing.T) {
|
||||
validValues := []string{
|
||||
"0", "0.", "0.952", "1", "123", "123.", "123.45", ".1",
|
||||
"-0", "-0.", "-0.952", "-1", "-123", "-123.", "-123.45", "-.1",
|
||||
"~",
|
||||
}
|
||||
|
||||
for _, value := range validValues {
|
||||
if !rangeMinCheck.MatchString(value) {
|
||||
t.Errorf("'%s' is a valid value string", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinCheckInvalid(t *testing.T) {
|
||||
invalidValues := []string{".", "-.", "a", " ", ""}
|
||||
|
||||
for _, value := range invalidValues {
|
||||
if rangeMinCheck.MatchString(value) {
|
||||
t.Errorf("'%s' is an invalid value string", value)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,108 +1,261 @@
|
|||
package perfdata // import nocternity.net/gomonop/pkg/perfdata
|
||||
package perfdata
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func assert(t *testing.T, check bool, msg string) {
|
||||
if !check {
|
||||
t.Errorf(msg)
|
||||
}
|
||||
func TestNewNoValue(t *testing.T) {
|
||||
const label = "label"
|
||||
const units = UomNone
|
||||
|
||||
out := New(label, units, "")
|
||||
|
||||
assert.Equal(t, label, out.Label)
|
||||
assert.Equal(t, units, out.units)
|
||||
assert.Equal(t, perfDataBits(0), out.bits)
|
||||
assert.Equal(t, "U", out.value)
|
||||
// min, max, warn and crit are meaningless here
|
||||
}
|
||||
|
||||
func assertPanic(t *testing.T, f func(), msg string) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Errorf(msg)
|
||||
}
|
||||
}()
|
||||
f()
|
||||
func TestNewValidValue(t *testing.T) {
|
||||
const label = "label"
|
||||
const units = UomNone
|
||||
const value = "1234"
|
||||
|
||||
out := New(label, units, value)
|
||||
|
||||
assert.Equal(t, label, out.Label)
|
||||
assert.Equal(t, units, out.units)
|
||||
assert.Equal(t, perfDataBits(0), out.bits)
|
||||
assert.Equal(t, value, out.value)
|
||||
// min, max, warn and crit are meaningless here
|
||||
}
|
||||
|
||||
func TestValueCheckValid(t *testing.T) {
|
||||
validValues := []string{
|
||||
"0", "0.", "0.952", "1", "123", "123.", "123.45", ".1",
|
||||
"-0", "-0.", "-0.952", "-1", "-123", "-123.", "-123.45", "-.1",
|
||||
}
|
||||
|
||||
for _, value := range validValues {
|
||||
if !valueCheck.MatchString(value) {
|
||||
t.Errorf("'%s' is a valid value string", value)
|
||||
}
|
||||
}
|
||||
func TestNewInvalidValue(t *testing.T) {
|
||||
assert.Panics(t, func() { New("label", UomNone, "nope") })
|
||||
}
|
||||
|
||||
func TestValueCheckInvalid(t *testing.T) {
|
||||
invalidValues := []string{".", "-.", "a", " ", ""}
|
||||
func TestSetWarn(t *testing.T) {
|
||||
rangeValue := PDRange{start: "A", end: "B"}
|
||||
rangeStr := rangeValue.String()
|
||||
|
||||
for _, value := range invalidValues {
|
||||
if valueCheck.MatchString(value) {
|
||||
t.Errorf("'%s' is an invalid value string", value)
|
||||
}
|
||||
}
|
||||
data := PerfData{}
|
||||
data.SetWarn(&rangeValue)
|
||||
|
||||
assert.True(t, data.bits&PDatWarn != 0)
|
||||
assert.Equal(t, rangeStr, data.warn.String())
|
||||
}
|
||||
|
||||
func TestPdrMaxInvalid(t *testing.T) {
|
||||
assertPanic(
|
||||
t, func() { PDRMax("") },
|
||||
"Created PerfDataRange with invalid max value",
|
||||
)
|
||||
func TestSetWarnTwice(t *testing.T) {
|
||||
range1Value := PDRange{start: "A", end: "B"}
|
||||
range2Value := PDRange{start: "C", end: "D"}
|
||||
range2Str := range2Value.String()
|
||||
require.NotEqual(t, range2Str, range1Value.String())
|
||||
|
||||
data := PerfData{}
|
||||
data.SetWarn(&range1Value)
|
||||
data.SetWarn(&range2Value)
|
||||
|
||||
assert.True(t, data.bits&PDatWarn != 0)
|
||||
assert.Equal(t, range2Str, data.warn.String())
|
||||
}
|
||||
|
||||
func TestPdrMax(t *testing.T) {
|
||||
value := "123"
|
||||
pdr := PDRMax(value)
|
||||
assert(t, pdr.start == "0", "Min value should be '0'")
|
||||
assert(t, pdr.end == value, "Max value not copied to PerfDataRange")
|
||||
assert(t, !pdr.inside, "Inside flag should not be set")
|
||||
func TestSetCrit(t *testing.T) {
|
||||
rangeValue := PDRange{start: "A", end: "B"}
|
||||
rangeStr := rangeValue.String()
|
||||
|
||||
data := PerfData{}
|
||||
data.SetCrit(&rangeValue)
|
||||
|
||||
assert.True(t, data.bits&PDatCrit != 0)
|
||||
assert.Equal(t, rangeStr, data.crit.String())
|
||||
}
|
||||
|
||||
func TestPdrMinMaxInvalid(t *testing.T) {
|
||||
assertPanic(
|
||||
t, func() { PDRMinMax("", "123") },
|
||||
"Created PerfDataRange with invalid min value",
|
||||
)
|
||||
assertPanic(
|
||||
t, func() { PDRMinMax("123", "") },
|
||||
"Created PerfDataRange with invalid max value",
|
||||
)
|
||||
func TestSetCritTwice(t *testing.T) {
|
||||
range1Value := PDRange{start: "A", end: "B"}
|
||||
range2Value := PDRange{start: "C", end: "D"}
|
||||
range2Str := range2Value.String()
|
||||
require.NotEqual(t, range2Str, range1Value.String())
|
||||
|
||||
data := PerfData{}
|
||||
data.SetCrit(&range1Value)
|
||||
data.SetCrit(&range2Value)
|
||||
|
||||
assert.True(t, data.bits&PDatCrit != 0)
|
||||
assert.Equal(t, range2Str, data.crit.String())
|
||||
}
|
||||
|
||||
func TestPdrMinMax(t *testing.T) {
|
||||
min, max := "123", "456"
|
||||
pdr := PDRMinMax(min, max)
|
||||
assert(t, pdr.start == min, "Min value not copied to PerfDataRange")
|
||||
assert(t, pdr.end == max, "Max value not copied to PerfDataRange")
|
||||
assert(t, !pdr.inside, "Inside flag should not be set")
|
||||
func TestSetMin(t *testing.T) {
|
||||
const min = "100"
|
||||
|
||||
data := PerfData{}
|
||||
data.SetMin(min)
|
||||
|
||||
assert.True(t, data.bits&PDatMin != 0)
|
||||
assert.Equal(t, min, data.min)
|
||||
}
|
||||
|
||||
func TestPdrInside(t *testing.T) {
|
||||
pdr := &PDRange{}
|
||||
pdr = pdr.Inside()
|
||||
assert(t, pdr.inside, "Inside flag should be set")
|
||||
pdr = pdr.Inside()
|
||||
assert(t, pdr.inside, "Inside flag should still be set")
|
||||
func TestSetMinInvalid(t *testing.T) {
|
||||
data := PerfData{}
|
||||
assert.Panics(t, func() { data.SetMin("nope") })
|
||||
}
|
||||
|
||||
func TestPdrString(t *testing.T) {
|
||||
func TestSetMinTwice(t *testing.T) {
|
||||
data := PerfData{}
|
||||
data.SetMin("100")
|
||||
data.SetMin("200")
|
||||
assert.Equal(t, "200", data.min)
|
||||
assert.True(t, data.bits&PDatMin != 0)
|
||||
}
|
||||
|
||||
func TestSetMax(t *testing.T) {
|
||||
const max = "100"
|
||||
|
||||
data := PerfData{}
|
||||
data.SetMax(max)
|
||||
|
||||
assert.True(t, data.bits&PDatMax != 0)
|
||||
assert.Equal(t, max, data.max)
|
||||
}
|
||||
|
||||
func TestSetMaxInvalid(t *testing.T) {
|
||||
data := PerfData{}
|
||||
assert.Panics(t, func() { data.SetMax("nope") })
|
||||
}
|
||||
|
||||
func TestSetMaxTwice(t *testing.T) {
|
||||
data := PerfData{}
|
||||
data.SetMax("100")
|
||||
data.SetMax("200")
|
||||
assert.Equal(t, "200", data.max)
|
||||
assert.True(t, data.bits&PDatMax != 0)
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
type Test struct {
|
||||
pdr PDRange
|
||||
out string
|
||||
PerfData
|
||||
expected string
|
||||
}
|
||||
|
||||
range1 := PDRange{start: "A", end: "B"}
|
||||
range2 := PDRange{start: "C", end: "D"}
|
||||
|
||||
tests := []Test{
|
||||
{pdr: PDRange{start: "Y", end: "X"}, out: "Y:X"},
|
||||
{pdr: PDRange{end: "X"}, out: "~:X"},
|
||||
{pdr: PDRange{start: "0", end: "X"}, out: ":X"},
|
||||
{pdr: PDRange{inside: true, start: "Y", end: "X"}, out: "@Y:X"},
|
||||
{
|
||||
PerfData{
|
||||
Label: "label",
|
||||
units: UomNone,
|
||||
bits: perfDataBits(0),
|
||||
value: "1234",
|
||||
},
|
||||
"label=1234;;;;",
|
||||
},
|
||||
{
|
||||
PerfData{
|
||||
Label: "la=bel",
|
||||
units: UomNone,
|
||||
bits: perfDataBits(0),
|
||||
value: "1234",
|
||||
},
|
||||
"'la=bel'=1234;;;;",
|
||||
},
|
||||
{
|
||||
PerfData{
|
||||
Label: "la bel",
|
||||
units: UomNone,
|
||||
bits: perfDataBits(0),
|
||||
value: "1234",
|
||||
},
|
||||
"'la bel'=1234;;;;",
|
||||
},
|
||||
{
|
||||
PerfData{
|
||||
Label: "la\"bel",
|
||||
units: UomNone,
|
||||
bits: perfDataBits(0),
|
||||
value: "1234",
|
||||
},
|
||||
"'la\"bel'=1234;;;;",
|
||||
},
|
||||
{
|
||||
PerfData{
|
||||
Label: "la'bel",
|
||||
units: UomNone,
|
||||
bits: perfDataBits(0),
|
||||
value: "1234",
|
||||
},
|
||||
"'la''bel'=1234;;;;",
|
||||
},
|
||||
{
|
||||
PerfData{
|
||||
Label: "label",
|
||||
units: UomNone,
|
||||
bits: PDatWarn,
|
||||
value: "1234",
|
||||
warn: range1,
|
||||
},
|
||||
"label=1234;" + range1.String() + ";;;",
|
||||
},
|
||||
{
|
||||
PerfData{
|
||||
Label: "label",
|
||||
units: UomNone,
|
||||
bits: PDatCrit,
|
||||
value: "1234",
|
||||
crit: range1,
|
||||
},
|
||||
"label=1234;;" + range1.String() + ";;",
|
||||
},
|
||||
{
|
||||
PerfData{
|
||||
Label: "label",
|
||||
units: UomNone,
|
||||
bits: PDatWarn | PDatCrit,
|
||||
value: "1234",
|
||||
warn: range1,
|
||||
crit: range2,
|
||||
},
|
||||
"label=1234;" + range1.String() + ";" + range2.String() + ";;",
|
||||
},
|
||||
{
|
||||
PerfData{
|
||||
Label: "label",
|
||||
units: UomNone,
|
||||
bits: PDatMin,
|
||||
value: "1234",
|
||||
min: "X",
|
||||
},
|
||||
"label=1234;;;X;",
|
||||
},
|
||||
{
|
||||
PerfData{
|
||||
Label: "label",
|
||||
units: UomNone,
|
||||
bits: PDatMax,
|
||||
value: "1234",
|
||||
max: "Y",
|
||||
},
|
||||
"label=1234;;;;Y",
|
||||
},
|
||||
}
|
||||
|
||||
for _, units := range []UnitOfMeasurement{UomSeconds, UomPercent, UomBytes, UomKilobytes, UomMegabytes, UomGigabytes, UomTerabytes, UomCounter} {
|
||||
tests = append(tests, Test{
|
||||
PerfData{
|
||||
Label: "label",
|
||||
units: units,
|
||||
bits: perfDataBits(0),
|
||||
value: "1234",
|
||||
},
|
||||
fmt.Sprintf("label=1234%s;;;;", units),
|
||||
})
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := test.pdr.String()
|
||||
assert(
|
||||
t,
|
||||
result == test.out,
|
||||
fmt.Sprintf("Expected '%s', got '%s'", test.out, result),
|
||||
)
|
||||
assert.Equal(t, test.expected, test.PerfData.String())
|
||||
}
|
||||
}
|
||||
|
|
67
pkg/perfdata/range_test.go
Normal file
67
pkg/perfdata/range_test.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package perfdata // import nocternity.net/gomonop/pkg/perfdata
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRangeMaxInvalid(t *testing.T) {
|
||||
assert.Panics(
|
||||
t, func() { PDRMax("") },
|
||||
"Created PerfDataRange with invalid max value",
|
||||
)
|
||||
}
|
||||
|
||||
func TestRangeMax(t *testing.T) {
|
||||
value := "123"
|
||||
pdr := PDRMax(value)
|
||||
assert.Equal(t, "0", pdr.start, "Min value should be '0'")
|
||||
assert.Equal(t, value, pdr.end, "Max value not copied to PerfDataRange")
|
||||
assert.False(t, pdr.inside, "Inside flag should not be set")
|
||||
}
|
||||
|
||||
func TestRangeMinMaxInvalid(t *testing.T) {
|
||||
assert.Panics(
|
||||
t, func() { PDRMinMax("", "123") },
|
||||
"Created PerfDataRange with invalid min value",
|
||||
)
|
||||
assert.Panics(
|
||||
t, func() { PDRMinMax("123", "") },
|
||||
"Created PerfDataRange with invalid max value",
|
||||
)
|
||||
}
|
||||
|
||||
func TestRangeMinMax(t *testing.T) {
|
||||
min, max := "123", "456"
|
||||
pdr := PDRMinMax(min, max)
|
||||
assert.Equal(t, min, pdr.start, "Min value not copied to PerfDataRange")
|
||||
assert.Equal(t, max, pdr.end, "Max value not copied to PerfDataRange")
|
||||
assert.False(t, pdr.inside, "Inside flag should not be set")
|
||||
}
|
||||
|
||||
func TestRangeInside(t *testing.T) {
|
||||
pdr := &PDRange{}
|
||||
pdr = pdr.Inside()
|
||||
assert.True(t, pdr.inside, "Inside flag should be set")
|
||||
pdr = pdr.Inside()
|
||||
assert.True(t, pdr.inside, "Inside flag should still be set")
|
||||
}
|
||||
|
||||
func TestRangeString(t *testing.T) {
|
||||
type Test struct {
|
||||
pdr PDRange
|
||||
out string
|
||||
}
|
||||
tests := []Test{
|
||||
{pdr: PDRange{start: "Y", end: "X"}, out: "Y:X"},
|
||||
{pdr: PDRange{end: "X"}, out: "~:X"},
|
||||
{pdr: PDRange{start: "0", end: "X"}, out: ":X"},
|
||||
{pdr: PDRange{inside: true, start: "Y", end: "X"}, out: "@Y:X"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := test.pdr.String()
|
||||
assert.Equal(t, test.out, result, "Expected '%s', got '%s'", test.out, result)
|
||||
}
|
||||
}
|
26
pkg/perfdata/units_test.go
Normal file
26
pkg/perfdata/units_test.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package perfdata // import nocternity.net/gomonop/pkg/perfdata
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUnitsToString(t *testing.T) {
|
||||
checks := map[UnitOfMeasurement]string{
|
||||
UomNone: "",
|
||||
UomSeconds: "s",
|
||||
UomPercent: "%",
|
||||
UomBytes: "B",
|
||||
UomKilobytes: "KB",
|
||||
UomMegabytes: "MB",
|
||||
UomGigabytes: "GB",
|
||||
UomTerabytes: "TB",
|
||||
UomCounter: "c",
|
||||
}
|
||||
|
||||
for u, s := range checks {
|
||||
result := u.String()
|
||||
assert.Equal(t, s, result, "Expected '%s', got '%s'", s, result)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue