feat: add the check_output_matches
plugin #5
2 changed files with 74 additions and 0 deletions
|
@ -2,6 +2,7 @@ package perfdata // import nocternity.net/gomonop/pkg/perfdata
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -66,6 +67,29 @@ func (r *Range) String() string {
|
||||||
return inside + start + ":" + r.end
|
return inside + start + ":" + r.end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Contains checks whether a numeric value is within the range.
|
||||||
|
func (r *Range) Contains(value float64) bool {
|
||||||
|
var inStart, inEnd bool
|
||||||
|
|
||||||
|
if r.start != "" {
|
||||||
|
startValue, err := strconv.ParseFloat(r.start, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("invalid performance data range start value: %v", err))
|
||||||
|
}
|
||||||
|
inStart = value < startValue
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.end != "" {
|
||||||
|
endValue, err := strconv.ParseFloat(r.end, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("invalid performance data range end value: %v", err))
|
||||||
|
}
|
||||||
|
inEnd = value > endValue
|
||||||
|
}
|
||||||
|
|
||||||
|
return (inStart || inEnd) != r.inside
|
||||||
|
}
|
||||||
|
|
||||||
// A state of the range parser.
|
// A state of the range parser.
|
||||||
type rangeParserState int
|
type rangeParserState int
|
||||||
|
|
||||||
|
|
|
@ -69,6 +69,56 @@ func TestRangeString(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRangeContains(t *testing.T) {
|
||||||
|
type Test struct {
|
||||||
|
pdr Range
|
||||||
|
value float64
|
||||||
|
result bool
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []Test{
|
||||||
|
{pdr: Range{start: "0", end: "10"}, value: 0, result: false},
|
||||||
|
{pdr: Range{start: "0", end: "10"}, value: 10, result: false},
|
||||||
|
{pdr: Range{start: "0", end: "10"}, value: -1, result: true},
|
||||||
|
{pdr: Range{start: "0", end: "10"}, value: 11, result: true},
|
||||||
|
{pdr: Range{start: "", end: "10"}, value: -1000, result: false},
|
||||||
|
{pdr: Range{start: "", end: "10"}, value: 10, result: false},
|
||||||
|
{pdr: Range{start: "", end: "10"}, value: 11, result: true},
|
||||||
|
{pdr: Range{start: "10", end: ""}, value: -1000, result: true},
|
||||||
|
{pdr: Range{start: "10", end: ""}, value: 9, result: true},
|
||||||
|
{pdr: Range{start: "10", end: ""}, value: 10, result: false},
|
||||||
|
{pdr: Range{start: "10", end: "20"}, value: 9, result: true},
|
||||||
|
{pdr: Range{start: "10", end: "20"}, value: 10, result: false},
|
||||||
|
{pdr: Range{start: "10", end: "20"}, value: 20, result: false},
|
||||||
|
{pdr: Range{start: "10", end: "20"}, value: 21, result: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test cases with the inside flag set and the opposite result
|
||||||
|
n := len(tests)
|
||||||
|
for i := range n {
|
||||||
|
tests = append(tests, Test{
|
||||||
|
pdr: Range{
|
||||||
|
start: tests[i].pdr.start,
|
||||||
|
end: tests[i].pdr.end,
|
||||||
|
inside: !tests[i].pdr.inside,
|
||||||
|
},
|
||||||
|
value: tests[i].value,
|
||||||
|
result: !tests[i].result,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(fmt.Sprintf("%v", test), func(t *testing.T) {
|
||||||
|
result := test.pdr.Contains(test.value)
|
||||||
|
assert.Equal(
|
||||||
|
t, test.result, result,
|
||||||
|
"Expected '%v', got '%v' for value '%f' and range '%s'",
|
||||||
|
test.result, result, test.value, test.pdr.String(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRangeParserOk(t *testing.T) {
|
func TestRangeParserOk(t *testing.T) {
|
||||||
type Test struct {
|
type Test struct {
|
||||||
in string
|
in string
|
||||||
|
|
Loading…
Reference in a new issue