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
5 changed files with 31 additions and 31 deletions
Showing only changes of commit 47b342b317 - Show all commits

View file

@ -338,10 +338,10 @@ func (program *checkProgram) checkCertificateExpiry(tlDays int) (plugin.Status,
func (program *checkProgram) setPerfData(tlDays int) {
pdat := perfdata.New("validity", perfdata.UomNone, strconv.Itoa(tlDays))
if program.crit > 0 {
pdat.SetCrit(perfdata.PDRMax(strconv.Itoa(program.crit)))
pdat.SetCrit(perfdata.RangeMax(strconv.Itoa(program.crit)))
}
if program.warn > 0 {
pdat.SetWarn(perfdata.PDRMax(strconv.Itoa(program.warn)))
pdat.SetWarn(perfdata.RangeMax(strconv.Itoa(program.warn)))
}
program.plugin.AddPerfData(pdat)
}

View file

@ -14,7 +14,7 @@ type PerfData struct {
units UnitOfMeasurement
bits perfDataBits
value string
warn, crit PDRange
warn, crit Range
min, max string
}
@ -35,13 +35,13 @@ func New(label string, units UnitOfMeasurement, value string) *PerfData {
}
// Set the warning range for the performance data record.
func (d *PerfData) SetWarn(r *PDRange) {
func (d *PerfData) SetWarn(r *Range) {
d.warn = *r
d.bits |= PDatWarn
}
// Set the critical range for the performance data record.
func (d *PerfData) SetCrit(r *PDRange) {
func (d *PerfData) SetCrit(r *Range) {
d.crit = *r
d.bits |= PDatCrit
}

View file

@ -40,7 +40,7 @@ func TestNewInvalidValue(t *testing.T) {
}
func TestSetWarn(t *testing.T) {
rangeValue := PDRange{start: "A", end: "B"}
rangeValue := Range{start: "A", end: "B"}
rangeStr := rangeValue.String()
data := PerfData{}
@ -51,8 +51,8 @@ func TestSetWarn(t *testing.T) {
}
func TestSetWarnTwice(t *testing.T) {
range1Value := PDRange{start: "A", end: "B"}
range2Value := PDRange{start: "C", end: "D"}
range1Value := Range{start: "A", end: "B"}
range2Value := Range{start: "C", end: "D"}
range2Str := range2Value.String()
require.NotEqual(t, range2Str, range1Value.String())
@ -65,7 +65,7 @@ func TestSetWarnTwice(t *testing.T) {
}
func TestSetCrit(t *testing.T) {
rangeValue := PDRange{start: "A", end: "B"}
rangeValue := Range{start: "A", end: "B"}
rangeStr := rangeValue.String()
data := PerfData{}
@ -76,8 +76,8 @@ func TestSetCrit(t *testing.T) {
}
func TestSetCritTwice(t *testing.T) {
range1Value := PDRange{start: "A", end: "B"}
range2Value := PDRange{start: "C", end: "D"}
range1Value := Range{start: "A", end: "B"}
range2Value := Range{start: "C", end: "D"}
range2Str := range2Value.String()
require.NotEqual(t, range2Str, range1Value.String())
@ -141,8 +141,8 @@ func TestString(t *testing.T) {
expected string
}
range1 := PDRange{start: "A", end: "B"}
range2 := PDRange{start: "C", end: "D"}
range1 := Range{start: "A", end: "B"}
range2 := Range{start: "C", end: "D"}
tests := []Test{
{

View file

@ -1,7 +1,7 @@
package perfdata // import nocternity.net/gomonop/pkg/perfdata
// Performance data range.
type PDRange struct {
type Range struct {
start string
end string
inside bool
@ -9,11 +9,11 @@ type PDRange struct {
// Creates a performance data range from -inf to 0 and from the specified
// value to +inf.
func PDRMax(max string) *PDRange {
func RangeMax(max string) *Range {
if !valueCheck.MatchString(max) {
panic("invalid performance data range maximum value")
}
pdRange := &PDRange{}
pdRange := &Range{}
pdRange.start = "0"
pdRange.end = max
return pdRange
@ -21,28 +21,28 @@ func PDRMax(max string) *PDRange {
// Creates a performance data range from -inf to the specified minimal value
// and from the specified maximal value to +inf.
func PDRMinMax(min, max string) *PDRange {
func RangeMinMax(min, max string) *Range {
if !valueCheck.MatchString(max) {
panic("invalid performance data range maximum value")
}
if !rangeMinCheck.MatchString(min) {
panic("invalid performance data range minimum value")
}
pdRange := &PDRange{}
pdRange := &Range{}
pdRange.start = min
pdRange.end = max
return pdRange
}
// Inverts the range.
func (r *PDRange) Inside() *PDRange {
func (r *Range) Inside() *Range {
r.inside = true
return r
}
// Generates the range's string representation so it can be sent to the
// monitoring system.
func (r *PDRange) String() string {
func (r *Range) String() string {
var start, inside string
switch r.start {

View file

@ -8,14 +8,14 @@ import (
func TestRangeMaxInvalid(t *testing.T) {
assert.Panics(
t, func() { PDRMax("") },
t, func() { RangeMax("") },
"Created PerfDataRange with invalid max value",
)
}
func TestRangeMax(t *testing.T) {
value := "123"
pdr := PDRMax(value)
pdr := RangeMax(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")
@ -23,25 +23,25 @@ func TestRangeMax(t *testing.T) {
func TestRangeMinMaxInvalid(t *testing.T) {
assert.Panics(
t, func() { PDRMinMax("", "123") },
t, func() { RangeMinMax("", "123") },
"Created PerfDataRange with invalid min value",
)
assert.Panics(
t, func() { PDRMinMax("123", "") },
t, func() { RangeMinMax("123", "") },
"Created PerfDataRange with invalid max value",
)
}
func TestRangeMinMax(t *testing.T) {
min, max := "123", "456"
pdr := PDRMinMax(min, max)
pdr := RangeMinMax(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 := &Range{}
pdr = pdr.Inside()
assert.True(t, pdr.inside, "Inside flag should be set")
pdr = pdr.Inside()
@ -50,14 +50,14 @@ func TestRangeInside(t *testing.T) {
func TestRangeString(t *testing.T) {
type Test struct {
pdr PDRange
pdr Range
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"},
{pdr: Range{start: "Y", end: "X"}, out: "Y:X"},
{pdr: Range{end: "X"}, out: "~:X"},
{pdr: Range{start: "0", end: "X"}, out: ":X"},
{pdr: Range{inside: true, start: "Y", end: "X"}, out: "@Y:X"},
}
for _, test := range tests {