2024-07-19 23:15:40 +02:00
|
|
|
package perfdata
|
2024-07-19 22:01:34 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2024-07-19 23:15:40 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-07-19 22:01:34 +02:00
|
|
|
)
|
|
|
|
|
2024-07-19 23:15:40 +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-19 23:15:40 +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-19 23:15:40 +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-19 23:15:40 +02:00
|
|
|
func TestSetWarn(t *testing.T) {
|
2024-07-19 23:16:57 +02:00
|
|
|
rangeValue := Range{start: "A", end: "B"}
|
2024-07-19 23:15:40 +02:00
|
|
|
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-19 23:15:40 +02:00
|
|
|
func TestSetWarnTwice(t *testing.T) {
|
2024-07-19 23:16:57 +02:00
|
|
|
range1Value := Range{start: "A", end: "B"}
|
|
|
|
range2Value := Range{start: "C", end: "D"}
|
2024-07-19 23:15:40 +02:00
|
|
|
range2Str := range2Value.String()
|
|
|
|
require.NotEqual(t, range2Str, range1Value.String())
|
2024-07-19 22:01:34 +02:00
|
|
|
|
2024-07-19 23:15:40 +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-19 23:15:40 +02:00
|
|
|
func TestSetCrit(t *testing.T) {
|
2024-07-19 23:16:57 +02:00
|
|
|
rangeValue := Range{start: "A", end: "B"}
|
2024-07-19 23:15:40 +02:00
|
|
|
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-19 23:15:40 +02:00
|
|
|
func TestSetCritTwice(t *testing.T) {
|
2024-07-19 23:16:57 +02:00
|
|
|
range1Value := Range{start: "A", end: "B"}
|
|
|
|
range2Value := Range{start: "C", end: "D"}
|
2024-07-19 23:15:40 +02:00
|
|
|
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 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-19 23:15:40 +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-19 23:15:40 +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-19 23:15:40 +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-19 23:15:40 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestString(t *testing.T) {
|
2024-07-19 22:01:34 +02:00
|
|
|
type Test struct {
|
2024-07-19 23:15:40 +02:00
|
|
|
PerfData
|
|
|
|
expected string
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
2024-07-19 23:15:40 +02:00
|
|
|
|
2024-07-19 23:16:57 +02:00
|
|
|
range1 := Range{start: "A", end: "B"}
|
|
|
|
range2 := Range{start: "C", end: "D"}
|
2024-07-19 23:15:40 +02:00
|
|
|
|
2024-07-19 22:01:34 +02:00
|
|
|
tests := []Test{
|
2024-07-19 23:15:40 +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-19 23:15:40 +02:00
|
|
|
assert.Equal(t, test.expected, test.PerfData.String())
|
2024-07-19 22:01:34 +02:00
|
|
|
}
|
|
|
|
}
|