refactor: make internals easier to test and add unit tests #2

Merged
Emmanuel BENOîT merged 13 commits from tseeker/gomonop:20240719-more-tests into master 2024-07-20 10:01:05 +02:00
4 changed files with 371 additions and 75 deletions
Showing only changes of commit db47981d23 - Show all commits

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

View file

@ -1,108 +1,261 @@
package perfdata // import nocternity.net/gomonop/pkg/perfdata package perfdata
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func assert(t *testing.T, check bool, msg string) { func TestNewNoValue(t *testing.T) {
if !check { const label = "label"
t.Errorf(msg) 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) { func TestNewValidValue(t *testing.T) {
defer func() { const label = "label"
if r := recover(); r == nil { const units = UomNone
t.Errorf(msg) const value = "1234"
}
}() out := New(label, units, value)
f()
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) { func TestNewInvalidValue(t *testing.T) {
validValues := []string{ assert.Panics(t, func() { New("label", UomNone, "nope") })
"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 TestValueCheckInvalid(t *testing.T) { func TestSetWarn(t *testing.T) {
invalidValues := []string{".", "-.", "a", " ", ""} rangeValue := PDRange{start: "A", end: "B"}
rangeStr := rangeValue.String()
for _, value := range invalidValues { data := PerfData{}
if valueCheck.MatchString(value) { data.SetWarn(&rangeValue)
t.Errorf("'%s' is an invalid value string", value)
} assert.True(t, data.bits&PDatWarn != 0)
} assert.Equal(t, rangeStr, data.warn.String())
} }
func TestPdrMaxInvalid(t *testing.T) { func TestSetWarnTwice(t *testing.T) {
assertPanic( range1Value := PDRange{start: "A", end: "B"}
t, func() { PDRMax("") }, range2Value := PDRange{start: "C", end: "D"}
"Created PerfDataRange with invalid max value", 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) { func TestSetCrit(t *testing.T) {
value := "123" rangeValue := PDRange{start: "A", end: "B"}
pdr := PDRMax(value) rangeStr := rangeValue.String()
assert(t, pdr.start == "0", "Min value should be '0'")
assert(t, pdr.end == value, "Max value not copied to PerfDataRange") data := PerfData{}
assert(t, !pdr.inside, "Inside flag should not be set") data.SetCrit(&rangeValue)
assert.True(t, data.bits&PDatCrit != 0)
assert.Equal(t, rangeStr, data.crit.String())
} }
func TestPdrMinMaxInvalid(t *testing.T) { func TestSetCritTwice(t *testing.T) {
assertPanic( range1Value := PDRange{start: "A", end: "B"}
t, func() { PDRMinMax("", "123") }, range2Value := PDRange{start: "C", end: "D"}
"Created PerfDataRange with invalid min value", range2Str := range2Value.String()
) require.NotEqual(t, range2Str, range1Value.String())
assertPanic(
t, func() { PDRMinMax("123", "") }, data := PerfData{}
"Created PerfDataRange with invalid max value", 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) { func TestSetMin(t *testing.T) {
min, max := "123", "456" const min = "100"
pdr := PDRMinMax(min, max)
assert(t, pdr.start == min, "Min value not copied to PerfDataRange") data := PerfData{}
assert(t, pdr.end == max, "Max value not copied to PerfDataRange") data.SetMin(min)
assert(t, !pdr.inside, "Inside flag should not be set")
assert.True(t, data.bits&PDatMin != 0)
assert.Equal(t, min, data.min)
} }
func TestPdrInside(t *testing.T) { func TestSetMinInvalid(t *testing.T) {
pdr := &PDRange{} data := PerfData{}
pdr = pdr.Inside() assert.Panics(t, func() { data.SetMin("nope") })
assert(t, pdr.inside, "Inside flag should be set")
pdr = pdr.Inside()
assert(t, pdr.inside, "Inside flag should still be set")
} }
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 { type Test struct {
pdr PDRange PerfData
out string expected string
} }
range1 := PDRange{start: "A", end: "B"}
range2 := PDRange{start: "C", end: "D"}
tests := []Test{ tests := []Test{
{pdr: PDRange{start: "Y", end: "X"}, out: "Y:X"}, {
{pdr: PDRange{end: "X"}, out: "~:X"}, PerfData{
{pdr: PDRange{start: "0", end: "X"}, out: ":X"}, Label: "label",
{pdr: PDRange{inside: true, start: "Y", end: "X"}, out: "@Y:X"}, 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 { for _, test := range tests {
result := test.pdr.String() assert.Equal(t, test.expected, test.PerfData.String())
assert(
t,
result == test.out,
fmt.Sprintf("Expected '%s', got '%s'", test.out, result),
)
} }
} }

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

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