1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
//go:build !integration
// +build !integration
package exec
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/stdcopy"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-runner/executors/docker/internal/wait"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker"
)
func TestDefaultDocker_Exec(t *testing.T) {
id := "container-id"
input := func(err error) io.Reader {
r := new(mockReader)
r.On("Read", mock.Anything).
Return(0, err).
Once()
return r
}
mockWorkingClient := func(
clientMock *docker.MockClient,
reader io.Reader,
expectedCtx context.Context,
) {
conn := new(mockConn)
conn.On("Close").Return(nil).Twice()
conn.On("Write", mock.Anything).Return(0, nil)
hijacked := types.HijackedResponse{
Conn: conn,
Reader: bufio.NewReader(reader),
}
clientMock.On("ContainerAttach", expectedCtx, id, attachOptions()).
Return(hijacked, nil).
Once()
clientMock.On("ContainerStart", expectedCtx, id, types.ContainerStartOptions{}).
Return(nil).
Once()
}
tests := map[string]struct {
input io.Reader
cancelContext bool
setupDockerClient func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context)
setupKillWaiter func(t *testing.T, waiterMock *wait.MockKillWaiter, expectedCtx context.Context)
assertLogOutput func(t *testing.T, logOutput string)
expectedError error
expectedStdOut string
expectedStdErr string
}{
"ContainerAttach error": {
cancelContext: false,
setupDockerClient: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
clientMock.On("ContainerAttach", expectedCtx, id, attachOptions()).
Return(types.HijackedResponse{}, assert.AnError).
Once()
},
setupKillWaiter: func(t *testing.T, waiterMock *wait.MockKillWaiter, expectedCtx context.Context) {},
assertLogOutput: func(t *testing.T, logOutput string) {},
expectedError: assert.AnError,
},
"ContainerStart error": {
cancelContext: false,
setupDockerClient: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
conn := new(mockConn)
conn.On("Close").Return(nil).Twice()
conn.On("Write", mock.Anything).Return(0, nil)
hijacked := types.HijackedResponse{
Conn: conn,
}
clientMock.On("ContainerAttach", expectedCtx, id, attachOptions()).
Return(hijacked, nil).
Once()
clientMock.On("ContainerStart", expectedCtx, id, types.ContainerStartOptions{}).
Return(assert.AnError).
Once()
},
setupKillWaiter: func(t *testing.T, waiterMock *wait.MockKillWaiter, expectedCtx context.Context) {},
assertLogOutput: func(t *testing.T, logOutput string) {},
expectedError: assert.AnError,
},
"context done": {
input: input(io.EOF),
cancelContext: true,
setupDockerClient: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
reader := new(mockReader)
reader.On("Read", mock.Anything).
Return(0, nil)
mockWorkingClient(clientMock, reader, expectedCtx)
},
setupKillWaiter: func(t *testing.T, waiterMock *wait.MockKillWaiter, expectedCtx context.Context) {
waiterMock.On("StopKillWait", expectedCtx, id, mock.Anything).Return(nil).Once()
},
assertLogOutput: func(t *testing.T, logOutput string) {
assert.Contains(t, logOutput, "finished with aborted")
},
expectedError: nil,
},
"input error": {
input: input(errors.New("input error")),
cancelContext: false,
setupDockerClient: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
reader := new(mockReader)
reader.On("Read", mock.Anything).
Return(0, nil)
mockWorkingClient(clientMock, reader, expectedCtx)
},
setupKillWaiter: func(t *testing.T, waiterMock *wait.MockKillWaiter, expectedCtx context.Context) {
waiterMock.On("StopKillWait", expectedCtx, id, mock.Anything).Return(nil).Once()
},
assertLogOutput: func(t *testing.T, logOutput string) {
assert.Contains(t, logOutput, "finished with input error")
},
expectedError: nil,
},
"output error": {
input: input(io.EOF),
cancelContext: false,
setupDockerClient: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
reader := new(mockReader)
reader.On("Read", mock.Anything).
Return(0, errors.New("output error"))
mockWorkingClient(clientMock, reader, expectedCtx)
},
setupKillWaiter: func(t *testing.T, waiterMock *wait.MockKillWaiter, expectedCtx context.Context) {
waiterMock.On("StopKillWait", expectedCtx, id, mock.Anything).Return(nil).Once()
},
assertLogOutput: func(t *testing.T, logOutput string) {
assert.Contains(t, logOutput, "finished with output error")
},
expectedError: nil,
},
"killWaiter error": {
input: input(io.EOF),
cancelContext: false,
setupDockerClient: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
reader := new(mockReader)
reader.On("Read", mock.Anything).
Return(0, io.EOF).
Once()
mockWorkingClient(clientMock, reader, expectedCtx)
},
setupKillWaiter: func(t *testing.T, waiterMock *wait.MockKillWaiter, expectedCtx context.Context) {
waiterMock.On("StopKillWait", expectedCtx, id, mock.Anything).Return(assert.AnError).Once()
},
assertLogOutput: func(t *testing.T, logOutput string) {},
expectedError: assert.AnError,
},
"output passed to the writers": {
input: input(io.EOF),
cancelContext: false,
setupDockerClient: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
pr, pw := io.Pipe()
outWriter := stdcopy.NewStdWriter(pw, stdcopy.Stdout)
errWriter := stdcopy.NewStdWriter(pw, stdcopy.Stderr)
go func() {
var err error
_, err = fmt.Fprintln(outWriter, "out line 1")
require.NoError(t, err)
_, err = fmt.Fprintln(errWriter, "err line 1")
require.NoError(t, err)
_, err = fmt.Fprintln(outWriter, "out line 2")
require.NoError(t, err)
_, err = fmt.Fprintln(errWriter, "err line 2")
require.NoError(t, err)
err = pw.Close()
require.NoError(t, err)
}()
mockWorkingClient(clientMock, pr, expectedCtx)
},
setupKillWaiter: func(t *testing.T, waiterMock *wait.MockKillWaiter, expectedCtx context.Context) {
waiterMock.On("StopKillWait", expectedCtx, id, mock.Anything).Return(nil).Once()
},
assertLogOutput: func(t *testing.T, logOutput string) {},
expectedError: nil,
expectedStdOut: "out line 1\nout line 2\n",
expectedStdErr: "err line 1\nerr line 2\n",
},
}
for tn, tt := range tests {
t.Run(tn, func(t *testing.T) {
clientMock := new(docker.MockClient)
defer clientMock.AssertExpectations(t)
waiterMock := new(wait.MockKillWaiter)
defer waiterMock.AssertExpectations(t)
logger, hook := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
executorCtx, executorCancelFn := context.WithCancel(context.Background())
defer executorCancelFn()
ctx, cancelFn := context.WithCancel(executorCtx)
defer cancelFn()
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
tt.setupDockerClient(t, clientMock, ctx)
tt.setupKillWaiter(t, waiterMock, executorCtx)
if tt.cancelContext {
cancelFn()
}
streams := IOStreams{
Stdin: tt.input,
Stdout: outBuf,
Stderr: errBuf,
}
dockerExec := NewDocker(executorCtx, clientMock, waiterMock, logger)
err := dockerExec.Exec(ctx, id, streams)
logOutput := ""
for _, entry := range hook.AllEntries() {
line, e := entry.String()
require.NoError(t, e)
logOutput += line
}
tt.assertLogOutput(t, logOutput)
if tt.expectedError != nil {
assert.ErrorIs(t, err, tt.expectedError)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.expectedStdOut, outBuf.String())
assert.Equal(t, tt.expectedStdErr, errBuf.String())
})
}
}
|