(t *testing.T)
| 14 | ) |
| 15 | |
| 16 | func Test_dockerClient_execOnContainer(t *testing.T) { |
| 17 | type args struct { |
| 18 | c *ctr.Container |
| 19 | ctx context.Context |
| 20 | execCmd string |
| 21 | execArgs []string |
| 22 | privileged bool |
| 23 | } |
| 24 | tests := []struct { |
| 25 | name string |
| 26 | args args |
| 27 | mockInit func(context.Context, *mocks.APIClient, string, string, []string) |
| 28 | wantErr bool |
| 29 | }{ |
| 30 | { |
| 31 | name: "run non-privileged command with args", |
| 32 | args: args{ |
| 33 | c: &ctr.Container{ContainerID: "abc123"}, |
| 34 | ctx: context.TODO(), |
| 35 | execCmd: "test-app", |
| 36 | execArgs: []string{"one", "two", "three"}, |
| 37 | }, |
| 38 | mockInit: func(ctx context.Context, engine *mocks.APIClient, cID, cmd string, args []string) { |
| 39 | checkConfig := ctypes.ExecOptions{AttachStdout: true, AttachStderr: true, Cmd: []string{"which", cmd}} |
| 40 | engine.EXPECT().ContainerExecCreate(ctx, cID, checkConfig).Return(ctypes.ExecCreateResponse{ID: "whichID"}, nil) |
| 41 | engine.EXPECT().ContainerExecAttach(ctx, "whichID", ctypes.ExecAttachOptions{}).Return(fakeExecAttach(), nil) |
| 42 | engine.EXPECT().ContainerExecInspect(ctx, "whichID").Return(ctypes.ExecInspect{}, nil) |
| 43 | execConfig := ctypes.ExecOptions{AttachStdout: true, AttachStderr: true, Cmd: append([]string{cmd}, args...), Privileged: false} |
| 44 | engine.EXPECT().ContainerExecCreate(ctx, cID, execConfig).Return(ctypes.ExecCreateResponse{ID: "cmdID"}, nil) |
| 45 | engine.EXPECT().ContainerExecAttach(ctx, "cmdID", ctypes.ExecAttachOptions{}).Return(fakeExecAttach(), nil) |
| 46 | engine.EXPECT().ContainerExecInspect(ctx, "cmdID").Return(ctypes.ExecInspect{}, nil) |
| 47 | }, |
| 48 | }, |
| 49 | { |
| 50 | name: "run privileged command with args", |
| 51 | args: args{ |
| 52 | c: &ctr.Container{ContainerID: "abc123"}, |
| 53 | ctx: context.TODO(), |
| 54 | execCmd: "test-app", |
| 55 | execArgs: []string{"one", "two", "three"}, |
| 56 | privileged: true, |
| 57 | }, |
| 58 | mockInit: func(ctx context.Context, engine *mocks.APIClient, cID, cmd string, args []string) { |
| 59 | checkConfig := ctypes.ExecOptions{AttachStdout: true, AttachStderr: true, Cmd: []string{"which", cmd}} |
| 60 | engine.EXPECT().ContainerExecCreate(ctx, cID, checkConfig).Return(ctypes.ExecCreateResponse{ID: "whichID"}, nil) |
| 61 | engine.EXPECT().ContainerExecAttach(ctx, "whichID", ctypes.ExecAttachOptions{}).Return(fakeExecAttach(), nil) |
| 62 | engine.EXPECT().ContainerExecInspect(ctx, "whichID").Return(ctypes.ExecInspect{}, nil) |
| 63 | execConfig := ctypes.ExecOptions{AttachStdout: true, AttachStderr: true, Cmd: append([]string{cmd}, args...), Privileged: true} |
| 64 | engine.EXPECT().ContainerExecCreate(ctx, cID, execConfig).Return(ctypes.ExecCreateResponse{ID: "cmdID"}, nil) |
| 65 | engine.EXPECT().ContainerExecAttach(ctx, "cmdID", ctypes.ExecAttachOptions{}).Return(fakeExecAttach(), nil) |
| 66 | engine.EXPECT().ContainerExecInspect(ctx, "cmdID").Return(ctypes.ExecInspect{}, nil) |
| 67 | }, |
| 68 | }, |
| 69 | { |
| 70 | name: "fail to find command", |
| 71 | args: args{ |
| 72 | c: &ctr.Container{ContainerID: "abc123"}, |
| 73 | ctx: context.TODO(), |
nothing calls this directly
no test coverage detected