2017-06-07 19:03:52 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-08-31 17:07:16 -04:00
|
|
|
"io"
|
|
|
|
|
2017-06-07 19:03:52 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-08-31 17:07:16 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2024-02-21 10:36:17 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2024-01-24 08:32:07 -05:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2017-08-31 17:07:16 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2023-07-14 17:42:40 -04:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2017-06-07 19:03:52 -04:00
|
|
|
"github.com/docker/docker/client"
|
2020-05-27 14:32:22 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-06-07 19:03:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeClient struct {
|
|
|
|
client.Client
|
2018-11-21 08:41:51 -05:00
|
|
|
inspectFunc func(string) (types.ContainerJSON, error)
|
|
|
|
execInspectFunc func(execID string) (types.ContainerExecInspect, error)
|
2023-11-20 11:38:50 -05:00
|
|
|
execCreateFunc func(containerID string, config types.ExecConfig) (types.IDResponse, error)
|
2018-11-21 08:41:51 -05:00
|
|
|
createContainerFunc func(config *container.Config,
|
|
|
|
hostConfig *container.HostConfig,
|
|
|
|
networkingConfig *network.NetworkingConfig,
|
2020-05-27 14:32:22 -04:00
|
|
|
platform *specs.Platform,
|
2022-04-29 13:26:50 -04:00
|
|
|
containerName string) (container.CreateResponse, error)
|
2023-11-20 11:38:50 -05:00
|
|
|
containerStartFunc func(containerID string, options container.StartOptions) error
|
2024-01-24 08:32:07 -05:00
|
|
|
imageCreateFunc func(parentReference string, options image.CreateOptions) (io.ReadCloser, error)
|
2023-07-14 17:42:40 -04:00
|
|
|
infoFunc func() (system.Info, error)
|
2023-11-20 11:38:50 -05:00
|
|
|
containerStatPathFunc func(containerID, path string) (types.ContainerPathStat, error)
|
|
|
|
containerCopyFromFunc func(containerID, srcPath string) (io.ReadCloser, types.ContainerPathStat, error)
|
2023-10-13 14:34:32 -04:00
|
|
|
logFunc func(string, container.LogsOptions) (io.ReadCloser, error)
|
2022-04-29 13:26:50 -04:00
|
|
|
waitFunc func(string) (<-chan container.WaitResponse, <-chan error)
|
2023-10-13 14:34:32 -04:00
|
|
|
containerListFunc func(container.ListOptions) ([]types.Container, error)
|
2018-11-21 08:41:51 -05:00
|
|
|
containerExportFunc func(string) (io.ReadCloser, error)
|
2023-10-13 14:34:32 -04:00
|
|
|
containerExecResizeFunc func(id string, options container.ResizeOptions) error
|
2023-11-20 11:38:50 -05:00
|
|
|
containerRemoveFunc func(ctx context.Context, containerID string, options container.RemoveOptions) error
|
|
|
|
containerKillFunc func(ctx context.Context, containerID, signal string) error
|
2024-02-21 10:36:17 -05:00
|
|
|
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error)
|
2024-04-08 04:11:09 -04:00
|
|
|
containerAttachFunc func(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error)
|
2018-11-21 08:41:51 -05:00
|
|
|
Version string
|
2017-06-07 19:03:52 -04:00
|
|
|
}
|
|
|
|
|
2023-10-13 14:34:32 -04:00
|
|
|
func (f *fakeClient) ContainerList(_ context.Context, options container.ListOptions) ([]types.Container, error) {
|
2018-02-15 11:58:17 -05:00
|
|
|
if f.containerListFunc != nil {
|
|
|
|
return f.containerListFunc(options)
|
|
|
|
}
|
|
|
|
return []types.Container{}, nil
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
func (f *fakeClient) ContainerInspect(_ context.Context, containerID string) (types.ContainerJSON, error) {
|
|
|
|
if f.inspectFunc != nil {
|
|
|
|
return f.inspectFunc(containerID)
|
2017-06-07 19:03:52 -04:00
|
|
|
}
|
|
|
|
return types.ContainerJSON{}, nil
|
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func (f *fakeClient) ContainerExecCreate(_ context.Context, containerID string, config types.ExecConfig) (types.IDResponse, error) {
|
2017-05-09 18:35:25 -04:00
|
|
|
if f.execCreateFunc != nil {
|
2023-11-20 11:38:50 -05:00
|
|
|
return f.execCreateFunc(containerID, config)
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:26:53 -04:00
|
|
|
func (f *fakeClient) ContainerExecStart(context.Context, string, types.ExecStartCheck) error {
|
2017-05-09 18:35:25 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-31 17:07:16 -04:00
|
|
|
|
|
|
|
func (f *fakeClient) ContainerCreate(
|
|
|
|
_ context.Context,
|
|
|
|
config *container.Config,
|
|
|
|
hostConfig *container.HostConfig,
|
|
|
|
networkingConfig *network.NetworkingConfig,
|
2020-05-27 14:32:22 -04:00
|
|
|
platform *specs.Platform,
|
2017-08-31 17:07:16 -04:00
|
|
|
containerName string,
|
2022-04-29 13:26:50 -04:00
|
|
|
) (container.CreateResponse, error) {
|
2017-08-31 17:07:16 -04:00
|
|
|
if f.createContainerFunc != nil {
|
2020-05-27 14:32:22 -04:00
|
|
|
return f.createContainerFunc(config, hostConfig, networkingConfig, platform, containerName)
|
2017-08-31 17:07:16 -04:00
|
|
|
}
|
2022-04-29 13:26:50 -04:00
|
|
|
return container.CreateResponse{}, nil
|
2017-08-31 17:07:16 -04:00
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
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 {
|
2023-11-20 11:38:50 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-24 08:32:07 -05:00
|
|
|
func (f *fakeClient) ImageCreate(_ context.Context, parentReference string, options image.CreateOptions) (io.ReadCloser, error) {
|
2017-08-31 17:07:16 -04:00
|
|
|
if f.imageCreateFunc != nil {
|
|
|
|
return f.imageCreateFunc(parentReference, options)
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 17:42:40 -04:00
|
|
|
func (f *fakeClient) Info(_ context.Context) (system.Info, error) {
|
2017-08-31 17:07:16 -04:00
|
|
|
if f.infoFunc != nil {
|
|
|
|
return f.infoFunc()
|
|
|
|
}
|
2023-07-14 17:42:40 -04:00
|
|
|
return system.Info{}, nil
|
2017-08-31 17:07:16 -04:00
|
|
|
}
|
2017-10-30 17:52:08 -04:00
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func (f *fakeClient) ContainerStatPath(_ context.Context, containerID, path string) (types.ContainerPathStat, error) {
|
2017-10-30 17:52:08 -04:00
|
|
|
if f.containerStatPathFunc != nil {
|
2023-11-20 11:38:50 -05:00
|
|
|
return f.containerStatPathFunc(containerID, path)
|
2017-10-30 17:52:08 -04:00
|
|
|
}
|
|
|
|
return types.ContainerPathStat{}, nil
|
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func (f *fakeClient) CopyFromContainer(_ context.Context, containerID, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
|
2017-10-30 17:52:08 -04:00
|
|
|
if f.containerCopyFromFunc != nil {
|
2023-11-20 11:38:50 -05:00
|
|
|
return f.containerCopyFromFunc(containerID, srcPath)
|
2017-10-30 17:52:08 -04:00
|
|
|
}
|
|
|
|
return nil, types.ContainerPathStat{}, nil
|
|
|
|
}
|
2017-11-13 04:53:13 -05:00
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func (f *fakeClient) ContainerLogs(_ context.Context, containerID string, options container.LogsOptions) (io.ReadCloser, error) {
|
2017-11-13 04:53:13 -05:00
|
|
|
if f.logFunc != nil {
|
2023-11-20 11:38:50 -05:00
|
|
|
return f.logFunc(containerID, options)
|
2017-11-13 04:53:13 -05:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-19 18:36:50 -05:00
|
|
|
|
|
|
|
func (f *fakeClient) ClientVersion() string {
|
|
|
|
return f.Version
|
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func (f *fakeClient) ContainerWait(_ context.Context, containerID string, _ container.WaitCondition) (<-chan container.WaitResponse, <-chan error) {
|
2017-12-19 18:36:50 -05:00
|
|
|
if f.waitFunc != nil {
|
2023-11-20 11:38:50 -05:00
|
|
|
return f.waitFunc(containerID)
|
2017-12-19 18:36:50 -05:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-02-27 06:14:07 -05:00
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func (f *fakeClient) ContainerStart(_ context.Context, containerID string, options container.StartOptions) error {
|
2018-02-27 06:14:07 -05:00
|
|
|
if f.containerStartFunc != nil {
|
2023-11-20 11:38:50 -05:00
|
|
|
return f.containerStartFunc(containerID, options)
|
2018-02-27 06:14:07 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-18 12:29:56 -04:00
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func (f *fakeClient) ContainerExport(_ context.Context, containerID string) (io.ReadCloser, error) {
|
2018-09-18 12:29:56 -04:00
|
|
|
if f.containerExportFunc != nil {
|
2023-11-20 11:38:50 -05:00
|
|
|
return f.containerExportFunc(containerID)
|
2018-09-18 12:29:56 -04:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-11-21 08:41:51 -05:00
|
|
|
|
2023-10-13 14:34:32 -04:00
|
|
|
func (f *fakeClient) ContainerExecResize(_ context.Context, id string, options container.ResizeOptions) error {
|
2018-11-21 08:41:51 -05:00
|
|
|
if f.containerExecResizeFunc != nil {
|
|
|
|
return f.containerExecResizeFunc(id, options)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-13 12:45:35 -05:00
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func (f *fakeClient) ContainerKill(ctx context.Context, containerID, signal string) error {
|
2021-01-13 12:45:35 -05:00
|
|
|
if f.containerKillFunc != nil {
|
2023-11-20 11:38:50 -05:00
|
|
|
return f.containerKillFunc(ctx, containerID, signal)
|
2021-01-13 12:45:35 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-21 10:36:17 -05:00
|
|
|
|
|
|
|
func (f *fakeClient) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error) {
|
|
|
|
if f.containerPruneFunc != nil {
|
|
|
|
return f.containerPruneFunc(ctx, pruneFilters)
|
|
|
|
}
|
|
|
|
return types.ContainersPruneReport{}, nil
|
|
|
|
}
|
2024-04-08 04:11:09 -04:00
|
|
|
|
|
|
|
func (f *fakeClient) ContainerAttach(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error) {
|
|
|
|
if f.containerAttachFunc != nil {
|
|
|
|
return f.containerAttachFunc(ctx, containerID, options)
|
|
|
|
}
|
|
|
|
return types.HijackedResponse{}, nil
|
|
|
|
}
|