|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/docker/cli/cli/streams" |
| 11 | + "github.com/docker/docker/api/types/container" |
| 12 | + "github.com/docker/docker/api/types/image" |
| 13 | + "github.com/docker/docker/api/types/network" |
| 14 | + "github.com/docker/docker/pkg/jsonmessage" |
| 15 | + "github.com/opencontainers/image-spec/specs-go/v1" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestPullImageAndStartContainer(t *testing.T) { |
| 20 | + t.Run("successful pulling and container start", func(t *testing.T) { |
| 21 | + utils := utils{ |
| 22 | + imagePull: func(ctx context.Context, imageName string, opts image.PullOptions) (io.ReadCloser, error) { |
| 23 | + return io.NopCloser(bytes.NewReader([]byte(""))), nil |
| 24 | + }, |
| 25 | + containerCreate: func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (container.CreateResponse, error) { |
| 26 | + return container.CreateResponse{ID: "test-container"}, nil |
| 27 | + }, |
| 28 | + containerStart: func(ctx context.Context, containerID string, opts container.StartOptions) error { |
| 29 | + return nil |
| 30 | + }, |
| 31 | + displayJSON: func(r io.Reader, outStream *streams.Out) error { |
| 32 | + return jsonmessage.DisplayJSONMessagesToStream(r, outStream, nil) |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + opts := ContainerRunOpts{ |
| 37 | + ContainerName: "test", |
| 38 | + Image: "test-image", |
| 39 | + } |
| 40 | + |
| 41 | + id, err := pullImageAndStartContainer(context.Background(), opts, utils) |
| 42 | + require.NoError(t, err) |
| 43 | + require.Equal(t, "test-container", id) |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("error while pulling image", func(t *testing.T) { |
| 47 | + utils := utils{ |
| 48 | + imagePull: func(ctx context.Context, imageName string, opts image.PullOptions) (io.ReadCloser, error) { |
| 49 | + return nil, errors.New("pull error") |
| 50 | + }, |
| 51 | + containerCreate: func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (container.CreateResponse, error) { |
| 52 | + return container.CreateResponse{}, nil |
| 53 | + }, |
| 54 | + containerStart: func(ctx context.Context, containerID string, opts container.StartOptions) error { return nil }, |
| 55 | + displayJSON: func(r io.Reader, outStream *streams.Out) error { return nil }, |
| 56 | + } |
| 57 | + |
| 58 | + opts := ContainerRunOpts{ContainerName: "test", Image: "test-image"} |
| 59 | + |
| 60 | + _, err := pullImageAndStartContainer(context.Background(), opts, utils) |
| 61 | + require.ErrorContains(t, err, "pull error") |
| 62 | + }) |
| 63 | + |
| 64 | + t.Run("error while creating container", func(t *testing.T) { |
| 65 | + utils := utils{ |
| 66 | + imagePull: func(ctx context.Context, imageName string, opts image.PullOptions) (io.ReadCloser, error) { |
| 67 | + return io.NopCloser(bytes.NewReader([]byte(""))), nil |
| 68 | + }, |
| 69 | + containerCreate: func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (container.CreateResponse, error) { |
| 70 | + return container.CreateResponse{}, errors.New("create error") |
| 71 | + }, |
| 72 | + containerStart: func(ctx context.Context, containerID string, opts container.StartOptions) error { return nil }, |
| 73 | + displayJSON: func(r io.Reader, outStream *streams.Out) error { return nil }, |
| 74 | + } |
| 75 | + |
| 76 | + opts := ContainerRunOpts{ContainerName: "test", Image: "test-image"} |
| 77 | + |
| 78 | + _, err := pullImageAndStartContainer(context.Background(), opts, utils) |
| 79 | + require.ErrorContains(t, err, "create error") |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("error while starting container", func(t *testing.T) { |
| 83 | + utils := utils{ |
| 84 | + imagePull: func(ctx context.Context, imageName string, opts image.PullOptions) (io.ReadCloser, error) { |
| 85 | + return io.NopCloser(bytes.NewReader([]byte(""))), nil |
| 86 | + }, |
| 87 | + containerCreate: func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (container.CreateResponse, error) { |
| 88 | + return container.CreateResponse{ID: "test-container"}, nil |
| 89 | + }, |
| 90 | + containerStart: func(ctx context.Context, containerID string, opts container.StartOptions) error { |
| 91 | + return errors.New("start error") |
| 92 | + }, |
| 93 | + displayJSON: func(r io.Reader, outStream *streams.Out) error { return nil }, |
| 94 | + } |
| 95 | + |
| 96 | + opts := ContainerRunOpts{ContainerName: "test", Image: "test-image"} |
| 97 | + |
| 98 | + _, err := pullImageAndStartContainer(context.Background(), opts, utils) |
| 99 | + require.ErrorContains(t, err, "start error") |
| 100 | + }) |
| 101 | +} |
0 commit comments