github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/internal/container/states.go (about) 1 package container 2 3 import ( 4 "context" 5 "strings" 6 7 "github.com/Prakhar-Agarwal-byte/moby/client" 8 "github.com/Prakhar-Agarwal-byte/moby/errdefs" 9 "github.com/pkg/errors" 10 "gotest.tools/v3/poll" 11 ) 12 13 // IsStopped verifies the container is in stopped state. 14 func IsStopped(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result { 15 return func(log poll.LogT) poll.Result { 16 inspect, err := apiClient.ContainerInspect(ctx, containerID) 17 18 switch { 19 case err != nil: 20 return poll.Error(err) 21 case !inspect.State.Running: 22 return poll.Success() 23 default: 24 return poll.Continue("waiting for container to be stopped") 25 } 26 } 27 } 28 29 // IsInState verifies the container is in one of the specified state, e.g., "running", "exited", etc. 30 func IsInState(ctx context.Context, apiClient client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result { 31 return func(log poll.LogT) poll.Result { 32 inspect, err := apiClient.ContainerInspect(ctx, containerID) 33 if err != nil { 34 return poll.Error(err) 35 } 36 for _, v := range state { 37 if inspect.State.Status == v { 38 return poll.Success() 39 } 40 } 41 return poll.Continue("waiting for container to be one of (%s), currently %s", strings.Join(state, ", "), inspect.State.Status) 42 } 43 } 44 45 // IsSuccessful verifies state.Status == "exited" && state.ExitCode == 0 46 func IsSuccessful(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result { 47 return func(log poll.LogT) poll.Result { 48 inspect, err := apiClient.ContainerInspect(ctx, containerID) 49 if err != nil { 50 return poll.Error(err) 51 } 52 if inspect.State.Status == "exited" { 53 if inspect.State.ExitCode == 0 { 54 return poll.Success() 55 } 56 return poll.Error(errors.Errorf("expected exit code 0, got %d", inspect.State.ExitCode)) 57 } 58 return poll.Continue("waiting for container to be \"exited\", currently %s", inspect.State.Status) 59 } 60 } 61 62 // IsRemoved verifies the container has been removed 63 func IsRemoved(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result { 64 return func(log poll.LogT) poll.Result { 65 inspect, err := apiClient.ContainerInspect(ctx, containerID) 66 if err != nil { 67 if errdefs.IsNotFound(err) { 68 return poll.Success() 69 } 70 return poll.Error(err) 71 } 72 return poll.Continue("waiting for container to be removed, currently %s", inspect.State.Status) 73 } 74 }