DockerCLI/cli/command/container/client_test.go

166 lines
5.7 KiB
Go
Raw Normal View History

package container
import (
"context"
"io"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/system"
"github.com/docker/docker/client"
specs "github.com/opencontainers/image-spec/specs-go/v1"
)
type fakeClient struct {
client.Client
inspectFunc func(string) (types.ContainerJSON, error)
execInspectFunc func(execID string) (types.ContainerExecInspect, error)
execCreateFunc func(containerID string, config types.ExecConfig) (types.IDResponse, error)
createContainerFunc func(config *container.Config,
hostConfig *container.HostConfig,
networkingConfig *network.NetworkingConfig,
platform *specs.Platform,
containerName string) (container.CreateResponse, error)
containerStartFunc func(containerID string, options container.StartOptions) error
imageCreateFunc func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error)
infoFunc func() (system.Info, error)
containerStatPathFunc func(containerID, path string) (types.ContainerPathStat, error)
containerCopyFromFunc func(containerID, srcPath string) (io.ReadCloser, types.ContainerPathStat, error)
logFunc func(string, container.LogsOptions) (io.ReadCloser, error)
waitFunc func(string) (<-chan container.WaitResponse, <-chan error)
containerListFunc func(container.ListOptions) ([]types.Container, error)
containerExportFunc func(string) (io.ReadCloser, error)
containerExecResizeFunc func(id string, options container.ResizeOptions) error
containerRemoveFunc func(ctx context.Context, containerID string, options container.RemoveOptions) error
containerKillFunc func(ctx context.Context, containerID, signal string) error
Version string
}
func (f *fakeClient) ContainerList(_ context.Context, options container.ListOptions) ([]types.Container, error) {
if f.containerListFunc != nil {
return f.containerListFunc(options)
}
return []types.Container{}, nil
}
func (f *fakeClient) ContainerInspect(_ context.Context, containerID string) (types.ContainerJSON, error) {
if f.inspectFunc != nil {
return f.inspectFunc(containerID)
}
return types.ContainerJSON{}, nil
}
func (f *fakeClient) ContainerExecCreate(_ context.Context, containerID string, config types.ExecConfig) (types.IDResponse, error) {
if f.execCreateFunc != nil {
return f.execCreateFunc(containerID, config)
}
return types.IDResponse{}, nil
}
func (f *fakeClient) ContainerExecInspect(_ context.Context, execID string) (types.ContainerExecInspect, error) {
if f.execInspectFunc != nil {
return f.execInspectFunc(execID)
}
return types.ContainerExecInspect{}, nil
}
func (f *fakeClient) ContainerExecStart(context.Context, string, types.ExecStartCheck) error {
return nil
}
func (f *fakeClient) ContainerCreate(
_ context.Context,
config *container.Config,
hostConfig *container.HostConfig,
networkingConfig *network.NetworkingConfig,
platform *specs.Platform,
containerName string,
) (container.CreateResponse, error) {
if f.createContainerFunc != nil {
return f.createContainerFunc(config, hostConfig, networkingConfig, platform, containerName)
}
return container.CreateResponse{}, nil
}
func (f *fakeClient) ContainerRemove(ctx context.Context, containerID string, options container.RemoveOptions) error {
Return zero exit-code when force-removing non-existing containers When using `docker rm` / `docker container rm` with the `-f` / `--force` option, attempts to remove non-existing containers should print a warning, but should return a zero exit code ("successful"). Currently, a non-zero exit code is returned, marking the removal as "failed"; $ docker rm -fv 798c9471b695 Error: No such container: 798c9471b695 $ echo $? 1 The command should match the behavior of `rm` / `rm -f`, with the exception that a warning is printed (instead of silently ignored): Running `rm` with `-f` silences output and returns a zero exit code: touch some-file && rm -f no-such-file some-file; echo exit code: $?; ls -la # exit code: 0 # total 0 # drwxr-xr-x 2 sebastiaan staff 64 Aug 14 12:17 . # drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 .. mkdir some-directory && rm -rf no-such-directory some-directory; echo exit code: $?; ls -la # exit code: 0 # total 0 # drwxr-xr-x 2 sebastiaan staff 64 Aug 14 12:17 . # drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 .. Note that other reasons for a delete to fail should still result in a non-zero exit code, matching the behavior of `rm`. For instance, in the example below, the `rm` failed because directories can only be removed if the `-r` option is used; touch some-file && mkdir some-directory && rm -f some-directory no-such-file some-file; echo exit code: $?; ls -la # rm: some-directory: is a directory # exit code: 1 # total 0 # drwxr-xr-x 3 sebastiaan staff 96 Aug 14 14:15 . # drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 .. # drwxr-xr-x 2 sebastiaan staff 64 Aug 14 14:15 some-directory This patch updates the `docker rm` / `docker container rm` command to not produce an error when attempting to remove a missing containers, and instead only print the error, but return a zero (0) exit code. With this patch applied: docker create --name mycontainer busybox \ && docker rm nosuchcontainer mycontainer; \ echo exit code: $?; \ docker ps -a --filter name=mycontainer # df23cc8573f00e97d6e948b48d9ea7d75ce3b4faaab4fe1d3458d3bfa451f39d # mycontainer # Error: No such container: nosuchcontainer # exit code: 0 # CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-14 10:04:32 -04:00
if f.containerRemoveFunc != nil {
return f.containerRemoveFunc(ctx, containerID, options)
Return zero exit-code when force-removing non-existing containers When using `docker rm` / `docker container rm` with the `-f` / `--force` option, attempts to remove non-existing containers should print a warning, but should return a zero exit code ("successful"). Currently, a non-zero exit code is returned, marking the removal as "failed"; $ docker rm -fv 798c9471b695 Error: No such container: 798c9471b695 $ echo $? 1 The command should match the behavior of `rm` / `rm -f`, with the exception that a warning is printed (instead of silently ignored): Running `rm` with `-f` silences output and returns a zero exit code: touch some-file && rm -f no-such-file some-file; echo exit code: $?; ls -la # exit code: 0 # total 0 # drwxr-xr-x 2 sebastiaan staff 64 Aug 14 12:17 . # drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 .. mkdir some-directory && rm -rf no-such-directory some-directory; echo exit code: $?; ls -la # exit code: 0 # total 0 # drwxr-xr-x 2 sebastiaan staff 64 Aug 14 12:17 . # drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 .. Note that other reasons for a delete to fail should still result in a non-zero exit code, matching the behavior of `rm`. For instance, in the example below, the `rm` failed because directories can only be removed if the `-r` option is used; touch some-file && mkdir some-directory && rm -f some-directory no-such-file some-file; echo exit code: $?; ls -la # rm: some-directory: is a directory # exit code: 1 # total 0 # drwxr-xr-x 3 sebastiaan staff 96 Aug 14 14:15 . # drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 .. # drwxr-xr-x 2 sebastiaan staff 64 Aug 14 14:15 some-directory This patch updates the `docker rm` / `docker container rm` command to not produce an error when attempting to remove a missing containers, and instead only print the error, but return a zero (0) exit code. With this patch applied: docker create --name mycontainer busybox \ && docker rm nosuchcontainer mycontainer; \ echo exit code: $?; \ docker ps -a --filter name=mycontainer # df23cc8573f00e97d6e948b48d9ea7d75ce3b4faaab4fe1d3458d3bfa451f39d # mycontainer # Error: No such container: nosuchcontainer # exit code: 0 # CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-14 10:04:32 -04:00
}
return nil
}
func (f *fakeClient) ImageCreate(_ context.Context, parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
if f.imageCreateFunc != nil {
return f.imageCreateFunc(parentReference, options)
}
return nil, nil
}
func (f *fakeClient) Info(_ context.Context) (system.Info, error) {
if f.infoFunc != nil {
return f.infoFunc()
}
return system.Info{}, nil
}
func (f *fakeClient) ContainerStatPath(_ context.Context, containerID, path string) (types.ContainerPathStat, error) {
if f.containerStatPathFunc != nil {
return f.containerStatPathFunc(containerID, path)
}
return types.ContainerPathStat{}, nil
}
func (f *fakeClient) CopyFromContainer(_ context.Context, containerID, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
if f.containerCopyFromFunc != nil {
return f.containerCopyFromFunc(containerID, srcPath)
}
return nil, types.ContainerPathStat{}, nil
}
func (f *fakeClient) ContainerLogs(_ context.Context, containerID string, options container.LogsOptions) (io.ReadCloser, error) {
if f.logFunc != nil {
return f.logFunc(containerID, options)
}
return nil, nil
}
func (f *fakeClient) ClientVersion() string {
return f.Version
}
func (f *fakeClient) ContainerWait(_ context.Context, containerID string, _ container.WaitCondition) (<-chan container.WaitResponse, <-chan error) {
if f.waitFunc != nil {
return f.waitFunc(containerID)
}
return nil, nil
}
func (f *fakeClient) ContainerStart(_ context.Context, containerID string, options container.StartOptions) error {
if f.containerStartFunc != nil {
return f.containerStartFunc(containerID, options)
}
return nil
}
func (f *fakeClient) ContainerExport(_ context.Context, containerID string) (io.ReadCloser, error) {
if f.containerExportFunc != nil {
return f.containerExportFunc(containerID)
}
return nil, nil
}
func (f *fakeClient) ContainerExecResize(_ context.Context, id string, options container.ResizeOptions) error {
if f.containerExecResizeFunc != nil {
return f.containerExecResizeFunc(id, options)
}
return nil
}
func (f *fakeClient) ContainerKill(ctx context.Context, containerID, signal string) error {
if f.containerKillFunc != nil {
return f.containerKillFunc(ctx, containerID, signal)
}
return nil
}