container: deprecate IsValidHealthString

Introduce a ValidateHealthStatus utility in api/types/container to
validate if a given HealthState is valid.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-04-26 15:33:41 +02:00
parent e205701266
commit df662ebc59
5 changed files with 57 additions and 33 deletions

View File

@@ -1,6 +1,10 @@
package container
import "time"
import (
"fmt"
"strings"
"time"
)
// HealthStatus is a string representation of the container's health.
//
@@ -29,3 +33,18 @@ type HealthcheckResult struct {
ExitCode int // ExitCode meanings: 0=healthy, 1=unhealthy, 2=reserved (considered unhealthy), else=error running probe
Output string // Output from last check
}
var validHealths = []string{
NoHealthcheck, Starting, Healthy, Unhealthy,
}
// ValidateHealthStatus checks if the provided string is a valid
// container [HealthStatus].
func ValidateHealthStatus(s HealthStatus) error {
switch s {
case NoHealthcheck, Starting, Healthy, Unhealthy:
return nil
default:
return errInvalidParameter{error: fmt.Errorf("invalid value for health (%s): must be one of %s", s, strings.Join(validHealths, ", "))}
}
}

View File

@@ -0,0 +1,31 @@
package container
import (
"testing"
"gotest.tools/v3/assert"
)
func TestValidateHealthStatus(t *testing.T) {
tests := []struct {
health HealthStatus
expectedErr string
}{
{health: Healthy},
{health: Unhealthy},
{health: Starting},
{health: NoHealthcheck},
{health: "invalid-health-string", expectedErr: `invalid value for health (invalid-health-string): must be one of none, starting, healthy, unhealthy`},
}
for _, tc := range tests {
t.Run(tc.health, func(t *testing.T) {
err := ValidateHealthStatus(tc.health)
if tc.expectedErr == "" {
assert.NilError(t, err)
} else {
assert.Error(t, err, tc.expectedErr)
}
})
}
}