mirror of
https://github.com/moby/moby.git
synced 2026-08-04 15:11:00 +00:00
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:
@@ -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, ", "))}
|
||||
}
|
||||
}
|
||||
|
||||
31
api/types/container/health_test.go
Normal file
31
api/types/container/health_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user