Skip to content

Commit

Permalink
Add helper function to transform string states into int states
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Dec 23, 2024
1 parent 2e424f3 commit e9ce24f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
23 changes: 23 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package check

import (
"strings"
)

const (
// OK means everything is fine
OK = 0
Expand Down Expand Up @@ -29,3 +33,22 @@ func StatusText(status int) string {

return UnknownString
}

// StatusText returns a state corresponding to its
// common string representation
func StatusInt(status string) int {
status = strings.ToUpper(status)

switch status {
case OKString, "0":
return OK
case WarningString, "1":
return Warning
case CriticalString, "2":
return Critical
case UnknownString, "3":
return Unknown
default:
return Unknown
}
}
38 changes: 38 additions & 0 deletions status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,41 @@ func TestStatusText(t *testing.T) {
})
}
}

func TestStatusInt(t *testing.T) {
testcases := map[string]struct {
input string
expected int
}{
"OK": {
expected: 0,
input: "OK",
},
"WARNING": {
expected: 1,
input: "warning",
},
"CRITICAL": {
expected: 2,
input: "Critical",
},
"UNKNOWN": {
expected: 3,
input: "unknown",
},
"Invalid-Input": {
expected: 3,
input: "Something else",
},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
actual := StatusInt(tc.input)

if actual != tc.expected {
t.Error("\nActual: ", actual, "\nExpected: ", tc.expected)
}
})
}
}

0 comments on commit e9ce24f

Please sign in to comment.