Skip to content

Commit 44b653e

Browse files
committed
container: deprecate IsValidStateString
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent e477df3 commit 44b653e

5 files changed

Lines changed: 58 additions & 35 deletions

File tree

api/types/container/state.go

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

3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
38
// ContainerState is a string representation of the container's current state.
49
//
510
// It currently is an alias for string, but may become a distinct type in the future.
@@ -15,6 +20,21 @@ const (
1520
StateDead ContainerState = "dead" // StateDead indicates that the container failed to be deleted. Containers in this state are attempted to be cleaned up when the daemon restarts.
1621
)
1722

23+
var validStates = []ContainerState{
24+
StateCreated, StateRunning, StatePaused, StateRestarting, StateRemoving, StateExited, StateDead,
25+
}
26+
27+
// ValidateContainerState checks if the provided string is a valid
28+
// container [ContainerState].
29+
func ValidateContainerState(s ContainerState) error {
30+
switch s {
31+
case StateCreated, StateRunning, StatePaused, StateRestarting, StateRemoving, StateExited, StateDead:
32+
return nil
33+
default:
34+
return errInvalidParameter{error: fmt.Errorf("invalid value for state (%s): must be one of %s", s, strings.Join(validStates, ", "))}
35+
}
36+
}
37+
1838
// StateStatus is used to return container wait results.
1939
// Implements exec.ExitCode interface.
2040
// This type is needed as State include a sync.Mutex field which make

api/types/container/state_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package container
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestValidateContainerState(t *testing.T) {
10+
tests := []struct {
11+
state ContainerState
12+
expectedErr string
13+
}{
14+
{state: StatePaused},
15+
{state: StateRestarting},
16+
{state: StateRunning},
17+
{state: StateDead},
18+
{state: StateCreated},
19+
{state: StateExited},
20+
{state: StateRemoving},
21+
{state: "invalid-state-string", expectedErr: `invalid value for state (invalid-state-string): must be one of created, running, paused, restarting, removing, exited, dead`},
22+
}
23+
for _, tc := range tests {
24+
t.Run(tc.state, func(t *testing.T) {
25+
err := ValidateContainerState(tc.state)
26+
if tc.expectedErr == "" {
27+
assert.NilError(t, err)
28+
} else {
29+
assert.Error(t, err, tc.expectedErr)
30+
}
31+
})
32+
}
33+
}

container/state.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,10 @@ func (s *State) StateString() container.ContainerState {
147147
}
148148

149149
// IsValidStateString checks if the provided string is a valid container state.
150+
//
151+
// Deprecated: use [container.ValidateContainerState] instead.
150152
func IsValidStateString(s container.ContainerState) bool {
151-
switch s {
152-
case container.StateCreated, container.StateRunning, container.StatePaused,
153-
container.StateRestarting, container.StateRemoving, container.StateExited,
154-
container.StateDead:
155-
return true
156-
default:
157-
return false
158-
}
153+
return container.ValidateContainerState(s) == nil
159154
}
160155

161156
// WaitCondition is an enum type for different states to wait for.

container/state_test.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -181,27 +181,3 @@ func TestCorrectStateWaitResultAfterRestart(t *testing.T) {
181181
t.Fatalf("expected exit code %v, got %v", want.ExitCode, got.ExitCode())
182182
}
183183
}
184-
185-
func TestIsValidStateString(t *testing.T) {
186-
states := []struct {
187-
state container.ContainerState
188-
expected bool
189-
}{
190-
{state: container.StatePaused, expected: true},
191-
{state: container.StateRestarting, expected: true},
192-
{state: container.StateRunning, expected: true},
193-
{state: container.StateDead, expected: true},
194-
{state: "start"},
195-
{state: container.StateCreated, expected: true},
196-
{state: container.StateExited, expected: true},
197-
{state: container.StateRemoving, expected: true},
198-
{state: "stop"},
199-
}
200-
201-
for _, s := range states {
202-
v := IsValidStateString(s.state)
203-
if v != s.expected {
204-
t.Fatalf("Expected %t, but got %t", s.expected, v)
205-
}
206-
}
207-
}

daemon/list.go

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

281281
err = psFilters.WalkValues("status", func(value string) error {
282-
if !container.IsValidStateString(value) {
283-
return errdefs.InvalidParameter(fmt.Errorf("invalid filter 'status=%s'", value))
282+
if err := containertypes.ValidateContainerState(value); err != nil {
283+
return errdefs.InvalidParameter(fmt.Errorf("invalid filter 'status=%s': %w", value, err))
284284
}
285-
286285
config.All = true
287286
return nil
288287
})

0 commit comments

Comments
 (0)