feat: add the check_output_matches plugin #5

Merged
Emmanuel BENOîT merged 16 commits from tseeker/gomonop:20240720-line-matches into master 2024-07-20 22:57:10 +02:00
2 changed files with 74 additions and 0 deletions
Showing only changes of commit 7209591e08 - Show all commits

View file

@ -2,6 +2,7 @@ package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"fmt"
"strconv"
"strings"
)
@ -66,6 +67,29 @@ func (r *Range) String() string {
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.
type rangeParserState int

View file

@ -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) {
type Test struct {
in string