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
|
//go:build !integration
// +build !integration
package user
import (
"context"
"fmt"
"io/ioutil"
"strconv"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-runner/executors/docker/internal/exec"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker"
)
func TestDefaultInspect_IsRoot(t *testing.T) {
containerID := "container-id"
tests := map[string]struct {
setupDockerClientMock func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context)
expectedIsRoot bool
expectedError error
}{
"ImageInspectWithRaw error": {
setupDockerClientMock: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
clientMock.On("ImageInspectWithRaw", expectedCtx, containerID).
Return(types.ImageInspect{}, nil, assert.AnError).
Once()
},
expectedIsRoot: true,
expectedError: assert.AnError,
},
"empty Config": {
setupDockerClientMock: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
clientMock.On("ImageInspectWithRaw", expectedCtx, containerID).
Return(types.ImageInspect{}, nil, nil).
Once()
},
expectedIsRoot: true,
expectedError: nil,
},
"empty user entry in Config": {
setupDockerClientMock: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
clientMock.On("ImageInspectWithRaw", expectedCtx, containerID).
Return(types.ImageInspect{Config: &container.Config{User: ""}}, nil, nil).
Once()
},
expectedIsRoot: true,
expectedError: nil,
},
"user entry in Config set to root": {
setupDockerClientMock: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
clientMock.On("ImageInspectWithRaw", expectedCtx, containerID).
Return(types.ImageInspect{Config: &container.Config{User: "root"}}, nil, nil).
Once()
},
expectedIsRoot: true,
expectedError: nil,
},
"user entry in Config set to non-root": {
setupDockerClientMock: func(t *testing.T, clientMock *docker.MockClient, expectedCtx context.Context) {
clientMock.On("ImageInspectWithRaw", expectedCtx, containerID).
Return(types.ImageInspect{Config: &container.Config{User: "non-root"}}, nil, nil).
Once()
},
expectedIsRoot: false,
expectedError: nil,
},
}
for tn, tt := range tests {
t.Run(tn, func(t *testing.T) {
clientMock := new(docker.MockClient)
defer clientMock.AssertExpectations(t)
execMock := new(exec.MockDocker)
defer execMock.AssertExpectations(t)
ctx := context.Background()
tt.setupDockerClientMock(t, clientMock, ctx)
inspect := NewInspect(clientMock, execMock)
isRoot, err := inspect.IsRoot(ctx, containerID)
if tt.expectedError != nil {
assert.ErrorIs(t, err, tt.expectedError)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.expectedIsRoot, isRoot, "user root-status mismatch")
})
}
}
type uidAndGidTestCase struct {
assertExecMock func(t *testing.T, clientMock *exec.MockDocker, expectedCtx context.Context)
expectedID int
assertError func(t *testing.T, err error)
}
func TestDefaultInspect_UID(t *testing.T) {
testDefaultInspectUIDandGID(
t,
commandIDU,
func(inspect Inspect, ctx context.Context, containerID string) (int, error) {
return inspect.UID(ctx, containerID)
},
)
}
func TestDefaultInspect_GID(t *testing.T) {
testDefaultInspectUIDandGID(
t,
commandIDG,
func(inspect Inspect, ctx context.Context, containerID string) (int, error) {
return inspect.GID(ctx, containerID)
},
)
}
func testDefaultInspectUIDandGID(
t *testing.T,
expectedCommand string,
testCall func(inspect Inspect, ctx context.Context, containerID string) (int, error),
) {
containerID := "container-id"
assertCommand := func(t *testing.T, args mock.Arguments) {
streams, ok := args.Get(2).(exec.IOStreams)
require.True(t, ok)
data, err := ioutil.ReadAll(streams.Stdin)
require.NoError(t, err)
assert.Equal(t, expectedCommand, string(data))
}
mockOutput := func(t *testing.T, args mock.Arguments, stdout string, stderr string) {
streams, ok := args.Get(2).(exec.IOStreams)
require.True(t, ok)
_, err := fmt.Fprintln(streams.Stdout, stdout)
require.NoError(t, err)
_, err = fmt.Fprintln(streams.Stderr, stderr)
require.NoError(t, err)
}
tests := map[string]uidAndGidTestCase{
"Exec error": {
assertExecMock: func(t *testing.T, clientMock *exec.MockDocker, expectedCtx context.Context) {
clientMock.On("Exec", expectedCtx, containerID, mock.Anything).
Run(func(args mock.Arguments) {
assertCommand(t, args)
}).
Return(assert.AnError).
Once()
},
expectedID: 0,
assertError: func(t *testing.T, err error) {
assert.ErrorIs(t, err, assert.AnError)
},
},
"ID parsing error": {
assertExecMock: func(t *testing.T, clientMock *exec.MockDocker, expectedCtx context.Context) {
clientMock.On("Exec", expectedCtx, containerID, mock.Anything).
Run(func(args mock.Arguments) {
assertCommand(t, args)
mockOutput(t, args, "\n\ntest\n\n", "")
}).
Return(nil).
Once()
},
expectedID: 0,
assertError: func(t *testing.T, err error) {
var e *strconv.NumError
assert.ErrorAs(t, err, &e)
},
},
"err output mixed with expected stdout output": {
assertExecMock: func(t *testing.T, clientMock *exec.MockDocker, expectedCtx context.Context) {
clientMock.On("Exec", expectedCtx, containerID, mock.Anything).
Run(func(args mock.Arguments) {
assertCommand(t, args)
mockOutput(t, args, "\n\n123\n\n", "Some mixed error output")
}).
Return(nil).
Once()
},
expectedID: 123,
assertError: nil,
},
"empty output of the id command": {
assertExecMock: func(t *testing.T, clientMock *exec.MockDocker, expectedCtx context.Context) {
clientMock.On("Exec", expectedCtx, containerID, mock.Anything).
Run(func(args mock.Arguments) {
assertCommand(t, args)
mockOutput(t, args, "\n\n\n\n", "")
}).
Return(nil).
Once()
},
expectedID: 0,
assertError: func(t *testing.T, err error) {
assert.ErrorIs(t, err, errIDNoOutput)
},
},
"proper ID received from output": {
assertExecMock: func(t *testing.T, clientMock *exec.MockDocker, expectedCtx context.Context) {
clientMock.On("Exec", expectedCtx, containerID, mock.Anything).
Run(func(args mock.Arguments) {
assertCommand(t, args)
mockOutput(t, args, "\n\n123\n\n", "")
}).
Return(nil).
Once()
},
expectedID: 123,
assertError: nil,
},
}
for tn, tt := range tests {
t.Run(tn, func(t *testing.T) {
clientMock := new(docker.MockClient)
defer clientMock.AssertExpectations(t)
execMock := new(exec.MockDocker)
defer execMock.AssertExpectations(t)
ctx := context.Background()
tt.assertExecMock(t, execMock, ctx)
inspect := NewInspect(clientMock, execMock)
id, err := testCall(inspect, ctx, containerID)
assert.Equal(t, tt.expectedID, id)
if tt.assertError != nil {
tt.assertError(t, err)
return
}
assert.NoError(t, err)
})
}
}
|