|
1 | 1 | package container |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "context" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "strings" |
5 | 9 | "testing" |
| 10 | + "time" |
6 | 11 |
|
| 12 | + "github.com/docker/docker/api/types" |
| 13 | + "github.com/docker/docker/client" |
7 | 14 | "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/mock" |
8 | 16 | ) |
9 | 17 |
|
10 | 18 | func TestDocker(t *testing.T) { |
@@ -45,3 +53,113 @@ func TestDocker(t *testing.T) { |
45 | 53 | "CONFLICT_VAR": "I_EXIST_IN_MULTIPLE_PLACES", |
46 | 54 | }, env) |
47 | 55 | } |
| 56 | + |
| 57 | +type mockDockerClient struct { |
| 58 | + client.APIClient |
| 59 | + mock.Mock |
| 60 | +} |
| 61 | + |
| 62 | +func (m *mockDockerClient) ContainerExecCreate(ctx context.Context, id string, opts types.ExecConfig) (types.IDResponse, error) { |
| 63 | + args := m.Called(ctx, id, opts) |
| 64 | + return args.Get(0).(types.IDResponse), args.Error(1) |
| 65 | +} |
| 66 | + |
| 67 | +func (m *mockDockerClient) ContainerExecAttach(ctx context.Context, id string, opts types.ExecStartCheck) (types.HijackedResponse, error) { |
| 68 | + args := m.Called(ctx, id, opts) |
| 69 | + return args.Get(0).(types.HijackedResponse), args.Error(1) |
| 70 | +} |
| 71 | + |
| 72 | +func (m *mockDockerClient) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) { |
| 73 | + args := m.Called(ctx, execID) |
| 74 | + return args.Get(0).(types.ContainerExecInspect), args.Error(1) |
| 75 | +} |
| 76 | + |
| 77 | +type endlessReader struct { |
| 78 | + io.Reader |
| 79 | +} |
| 80 | + |
| 81 | +func (r endlessReader) Read(p []byte) (n int, err error) { |
| 82 | + return 1, nil |
| 83 | +} |
| 84 | + |
| 85 | +type mockConn struct { |
| 86 | + net.Conn |
| 87 | + mock.Mock |
| 88 | +} |
| 89 | + |
| 90 | +func (m *mockConn) Write(b []byte) (n int, err error) { |
| 91 | + args := m.Called(b) |
| 92 | + return args.Int(0), args.Error(1) |
| 93 | +} |
| 94 | + |
| 95 | +func (m *mockConn) Close() (err error) { |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func TestDockerExecAbort(t *testing.T) { |
| 100 | + ctx, cancel := context.WithCancel(context.Background()) |
| 101 | + |
| 102 | + conn := &mockConn{} |
| 103 | + conn.On("Write", mock.AnythingOfType("[]uint8")).Return(1, nil) |
| 104 | + |
| 105 | + client := &mockDockerClient{} |
| 106 | + client.On("ContainerExecCreate", ctx, "123", mock.AnythingOfType("types.ExecConfig")).Return(types.IDResponse{ID: "id"}, nil) |
| 107 | + client.On("ContainerExecAttach", ctx, "id", mock.AnythingOfType("types.ExecStartCheck")).Return(types.HijackedResponse{ |
| 108 | + Conn: conn, |
| 109 | + Reader: bufio.NewReader(endlessReader{}), |
| 110 | + }, nil) |
| 111 | + |
| 112 | + cr := &containerReference{ |
| 113 | + id: "123", |
| 114 | + cli: client, |
| 115 | + input: &NewContainerInput{ |
| 116 | + Image: "image", |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + channel := make(chan error) |
| 121 | + |
| 122 | + go func() { |
| 123 | + channel <- cr.exec([]string{""}, map[string]string{}, "user", "workdir")(ctx) |
| 124 | + }() |
| 125 | + |
| 126 | + time.Sleep(500 * time.Millisecond) |
| 127 | + |
| 128 | + cancel() |
| 129 | + |
| 130 | + err := <-channel |
| 131 | + assert.ErrorIs(t, err, context.Canceled) |
| 132 | + |
| 133 | + conn.AssertExpectations(t) |
| 134 | + client.AssertExpectations(t) |
| 135 | +} |
| 136 | + |
| 137 | +func TestDockerExecFailure(t *testing.T) { |
| 138 | + ctx := context.Background() |
| 139 | + |
| 140 | + conn := &mockConn{} |
| 141 | + |
| 142 | + client := &mockDockerClient{} |
| 143 | + client.On("ContainerExecCreate", ctx, "123", mock.AnythingOfType("types.ExecConfig")).Return(types.IDResponse{ID: "id"}, nil) |
| 144 | + client.On("ContainerExecAttach", ctx, "id", mock.AnythingOfType("types.ExecStartCheck")).Return(types.HijackedResponse{ |
| 145 | + Conn: conn, |
| 146 | + Reader: bufio.NewReader(strings.NewReader("output")), |
| 147 | + }, nil) |
| 148 | + client.On("ContainerExecInspect", ctx, "id").Return(types.ContainerExecInspect{ |
| 149 | + ExitCode: 1, |
| 150 | + }, nil) |
| 151 | + |
| 152 | + cr := &containerReference{ |
| 153 | + id: "123", |
| 154 | + cli: client, |
| 155 | + input: &NewContainerInput{ |
| 156 | + Image: "image", |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + err := cr.exec([]string{""}, map[string]string{}, "user", "workdir")(ctx) |
| 161 | + assert.Error(t, err, "exit with `FAILURE`: 1") |
| 162 | + |
| 163 | + conn.AssertExpectations(t) |
| 164 | + client.AssertExpectations(t) |
| 165 | +} |
0 commit comments