Skip to content

Commit df662eb

Browse files
committed
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>
1 parent e205701 commit df662eb

5 files changed

Lines changed: 57 additions & 33 deletions

File tree

api/types/container/health.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package container
22

3-
import "time"
3+
import (
4+
"fmt"
5+
"strings"
6+
"time"
7+
)
48

59
// HealthStatus is a string representation of the container's health.
610
//
@@ -29,3 +33,18 @@ type HealthcheckResult struct {
2933
ExitCode int // ExitCode meanings: 0=healthy, 1=unhealthy, 2=reserved (considered unhealthy), else=error running probe
3034
Output string // Output from last check
3135
}
36+
37+
var validHealths = []string{
38+
NoHealthcheck, Starting, Healthy, Unhealthy,
39+
}
40+
41+
// ValidateHealthStatus checks if the provided string is a valid
42+
// container [HealthStatus].
43+
func ValidateHealthStatus(s HealthStatus) error {
44+
switch s {
45+
case NoHealthcheck, Starting, Healthy, Unhealthy:
46+
return nil
47+
default:
48+
return errInvalidParameter{error: fmt.Errorf("invalid value for health (%s): must be one of %s", s, strings.Join(validHealths, ", "))}
49+
}
50+
}

api/types/container/health_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package container
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestValidateHealthStatus(t *testing.T) {
10+
tests := []struct {
11+
health HealthStatus
12+
expectedErr string
13+
}{
14+
{health: Healthy},
15+
{health: Unhealthy},
16+
{health: Starting},
17+
{health: NoHealthcheck},
18+
{health: "invalid-health-string", expectedErr: `invalid value for health (invalid-health-string): must be one of none, starting, healthy, unhealthy`},
19+
}
20+
21+
for _, tc := range tests {
22+
t.Run(tc.health, func(t *testing.T) {
23+
err := ValidateHealthStatus(tc.health)
24+
if tc.expectedErr == "" {
25+
assert.NilError(t, err)
26+
} else {
27+
assert.Error(t, err, tc.expectedErr)
28+
}
29+
})
30+
}
31+
}

container/state.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,10 @@ func (s *State) String() string {
102102

103103
// IsValidHealthString checks if the provided string is a valid
104104
// [container.HealthStatus].
105-
func IsValidHealthString(s container.HealthStatus) bool {
106-
switch s {
107-
case container.NoHealthcheck, container.Starting, container.Healthy, container.Unhealthy:
108-
return true
109-
default:
110-
return false
111-
}
105+
//
106+
// Deprecated: use [container.ValidateHealthStatus] and check for nil-errors.
107+
func IsValidHealthString(s string) bool {
108+
return container.ValidateHealthStatus(s) == nil
112109
}
113110

114111
// StateString returns a single string to describe state

container/state_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,6 @@ import (
99
libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
1010
)
1111

12-
func TestIsValidHealthString(t *testing.T) {
13-
tests := []struct {
14-
health container.HealthStatus
15-
expected bool
16-
}{
17-
{health: container.Healthy, expected: true},
18-
{health: container.Unhealthy, expected: true},
19-
{health: container.Starting, expected: true},
20-
{health: container.NoHealthcheck, expected: true},
21-
{health: "fail", expected: false},
22-
}
23-
24-
for _, tc := range tests {
25-
t.Run(tc.health, func(t *testing.T) {
26-
v := IsValidHealthString(tc.health)
27-
if v != tc.expected {
28-
t.Fatalf("Expected %t, but got %t", tc.expected, v)
29-
}
30-
})
31-
}
32-
}
33-
3412
type mockTask struct {
3513
libcontainerdtypes.Task
3614
pid uint32

daemon/list.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,9 @@ func (daemon *Daemon) foldFilter(ctx context.Context, view *container.View, conf
297297
}
298298

299299
err = psFilters.WalkValues("health", func(value string) error {
300-
if !container.IsValidHealthString(value) {
301-
return errdefs.InvalidParameter(fmt.Errorf("unrecognized filter value for health: %s", value))
300+
if err := containertypes.ValidateHealthStatus(value); err != nil {
301+
return errdefs.InvalidParameter(fmt.Errorf("invalid filter 'health=%s': %w", value, err))
302302
}
303-
304303
return nil
305304
})
306305
if err != nil {

0 commit comments

Comments
 (0)