package perfdata import ( "fmt" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) 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 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 TestNewInvalidValue(t *testing.T) { assert.Panics(t, func() { New("label", UomNone, "nope") }) } 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()) } 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()) data := PerfData{} data.SetWarn(&range1Value) data.SetWarn(&range2Value) assert.True(t, data.bits&PDatWarn != 0) assert.Equal(t, range2Str, data.warn.String()) } 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()) } 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()) } 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 TestSetMinInvalid(t *testing.T) { data := PerfData{} assert.Panics(t, func() { data.SetMin("nope") }) } 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 { PerfData expected string } range1 := Range{start: "A", end: "B"} range2 := Range{start: "C", end: "D"} tests := []Test{ { 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 { assert.Equal(t, test.expected, test.PerfData.String()) } }