2024-07-20 10:01:05 +02:00
|
|
|
package perfdata
|
2024-07-19 22:01:34 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2024-07-20 10:01:05 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-07-20 19:12:59 +02:00
|
|
|
"nocternity.net/gomonop/pkg/status"
|
2024-07-19 22:01:34 +02:00
|
|
|
)
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
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
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
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
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
func TestNewInvalidValue(t *testing.T) {
|
|
|
|
assert.Panics(t, func() { New("label", UomNone, "nope") })
|
|
|
|
}
|
2024-07-19 22:01:34 +02:00
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
func TestSetWarn(t *testing.T) {
|
|
|
|
rangeValue := Range{start: "A", end: "B"}
|
|
|
|
rangeStr := rangeValue.String()
|
|
|
|
|
|
|
|
data := PerfData{}
|
|
|
|
data.SetWarn(&rangeValue)
|
|
|
|
|
|
|
|
assert.True(t, data.bits&PDatWarn != 0)
|
|
|
|
assert.Equal(t, rangeStr, data.warn.String())
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 18:11:06 +02:00
|
|
|
func TestSetWarnNil(t *testing.T) {
|
|
|
|
data := PerfData{}
|
|
|
|
data.SetWarn(nil)
|
|
|
|
assert.Equal(t, perfDataBits(0), data.bits&PDatWarn)
|
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
func TestSetWarnTwice(t *testing.T) {
|
|
|
|
range1Value := Range{start: "A", end: "B"}
|
|
|
|
range2Value := Range{start: "C", end: "D"}
|
|
|
|
range2Str := range2Value.String()
|
|
|
|
require.NotEqual(t, range2Str, range1Value.String())
|
2024-07-19 22:01:34 +02:00
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
data := PerfData{}
|
|
|
|
data.SetWarn(&range1Value)
|
|
|
|
data.SetWarn(&range2Value)
|
|
|
|
|
|
|
|
assert.True(t, data.bits&PDatWarn != 0)
|
|
|
|
assert.Equal(t, range2Str, data.warn.String())
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 18:11:06 +02:00
|
|
|
func TestSetWarnClear(t *testing.T) {
|
|
|
|
range1Value := Range{start: "A", end: "B"}
|
|
|
|
|
|
|
|
data := PerfData{}
|
|
|
|
data.SetWarn(&range1Value)
|
|
|
|
data.SetWarn(nil)
|
|
|
|
|
|
|
|
assert.Equal(t, perfDataBits(0), data.bits&PDatWarn)
|
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
func TestSetCrit(t *testing.T) {
|
|
|
|
rangeValue := Range{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())
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 18:11:06 +02:00
|
|
|
func TestSetCritNil(t *testing.T) {
|
|
|
|
data := PerfData{}
|
|
|
|
data.SetCrit(nil)
|
|
|
|
assert.Equal(t, perfDataBits(0), data.bits&PDatCrit)
|
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
func TestSetCritTwice(t *testing.T) {
|
|
|
|
range1Value := Range{start: "A", end: "B"}
|
|
|
|
range2Value := Range{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())
|
|
|
|
}
|
2024-07-20 18:11:06 +02:00
|
|
|
|
|
|
|
func TestSetCritClear(t *testing.T) {
|
|
|
|
range1Value := Range{start: "A", end: "B"}
|
|
|
|
|
|
|
|
data := PerfData{}
|
|
|
|
data.SetCrit(&range1Value)
|
|
|
|
data.SetCrit(nil)
|
|
|
|
|
|
|
|
assert.Equal(t, perfDataBits(0), data.bits&PDatCrit)
|
|
|
|
}
|
2024-07-20 10:01:05 +02:00
|
|
|
|
|
|
|
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)
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
func TestSetMinInvalid(t *testing.T) {
|
|
|
|
data := PerfData{}
|
|
|
|
assert.Panics(t, func() { data.SetMin("nope") })
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
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)
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
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)
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:12:59 +02:00
|
|
|
func TestCheck(t *testing.T) {
|
|
|
|
warnRange := RangeMinMax("0", "10").Inside()
|
|
|
|
critRange := RangeMinMax("1", "9").Inside()
|
|
|
|
|
|
|
|
type Test struct {
|
|
|
|
in string
|
|
|
|
out status.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []Test{
|
|
|
|
{in: "moo", out: status.StatusUnknown},
|
|
|
|
{in: "-1", out: status.StatusOK},
|
|
|
|
{in: "0", out: status.StatusWarning},
|
|
|
|
{in: "1", out: status.StatusCritical},
|
|
|
|
{in: "9", out: status.StatusCritical},
|
|
|
|
{in: "10", out: status.StatusWarning},
|
|
|
|
{in: "11", out: status.StatusOK},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
pdat := PerfData{
|
|
|
|
value: test.in,
|
|
|
|
bits: PDatCrit | PDatWarn,
|
|
|
|
warn: *warnRange,
|
|
|
|
crit: *critRange,
|
|
|
|
}
|
|
|
|
assert.Equal(t, test.out, pdat.GetStatus())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-20 10:01:05 +02:00
|
|
|
func TestString(t *testing.T) {
|
2024-07-19 22:01:34 +02:00
|
|
|
type Test struct {
|
2024-07-20 10:01:05 +02:00
|
|
|
PerfData
|
|
|
|
expected string
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
2024-07-20 10:01:05 +02:00
|
|
|
|
|
|
|
range1 := Range{start: "A", end: "B"}
|
|
|
|
range2 := Range{start: "C", end: "D"}
|
|
|
|
|
2024-07-19 22:01:34 +02:00
|
|
|
tests := []Test{
|
2024-07-20 10:01:05 +02:00
|
|
|
{
|
|
|
|
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),
|
|
|
|
})
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2024-07-20 10:01:05 +02:00
|
|
|
assert.Equal(t, test.expected, test.PerfData.String())
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
}
|