opnsense-exporter/opnsense/utils_test.go
ihatemodels 24e8161262 Add initial project structure
- add base structure
 - unify the proto metrics creation and propagation
 - implement arp and openvpn
 - refactor to meet the prom exporter standart
 - add instance label to the metrics
 - refactor the call chain
 - add gateway, unbound_dns and openvpn implementations
 - add gateway stuff
 - structure refactor; mod clean; cron implementation
 - implement cron in the collector; refactor utils in the opnsense package

refactor names and implement option functions to disable collectorInstances

add GH action workflows

Create codeql.yml

- clean

fix stuff
2023-11-26 16:06:03 +02:00

70 lines
1.7 KiB
Go

package opnsense
import (
"regexp"
"testing"
"github.com/go-kit/log"
)
func TestParsePercentage(t *testing.T) {
logger := log.NewNopLogger()
testRegex := regexp.MustCompile(`\d\.\d %`)
tests := []struct {
name string
value string
regex *regexp.Regexp
replacePattern string
valueTypeName string
gatewayName string
expected float64
}{
{
name: "Valid percentage with space",
value: "50.5 %",
regex: testRegex,
replacePattern: " %",
valueTypeName: "loss",
gatewayName: "Gateway1",
expected: 50.5,
},
{
name: "Valid percentage with space",
value: "5.5 %",
regex: testRegex,
replacePattern: " %",
valueTypeName: "loss",
gatewayName: "Gateway1",
expected: 5.5,
},
{
name: "Invalid percentage format",
value: "invalid %",
regex: testRegex,
replacePattern: " %",
valueTypeName: "loss",
gatewayName: "Gateway1",
expected: -1.0,
},
{
name: "Invalid regex match (no space)",
value: "50.5%",
regex: testRegex,
replacePattern: " %",
valueTypeName: "loss",
gatewayName: "Gateway1",
expected: -1,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := parseStringToFloatWithReplace(tc.value, tc.regex, tc.replacePattern, tc.valueTypeName, logger)
if result != tc.expected {
t.Errorf("parsePercentage(%s, %v, %s, %s, logger, %s) = %v; want %v",
tc.value, tc.regex, tc.replacePattern, tc.valueTypeName, tc.gatewayName, result, tc.expected)
}
})
}
}