forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_exec_test.go
More file actions
206 lines (185 loc) · 5.97 KB
/
Copy pathcontainer_exec_test.go
File metadata and controls
206 lines (185 loc) · 5.97 KB
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
package client
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"testing"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/container"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestExecCreateError(t *testing.T) {
client, err := New(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ExecCreate(t.Context(), "container_id", ExecCreateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ExecCreate(t.Context(), "", ExecCreateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
_, err = client.ExecCreate(t.Context(), " ", ExecCreateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
// TestExecCreateConnectionError verifies that connection errors occurring
// during API-version negotiation are not shadowed by API-version errors.
//
// Regression test for https://github.com/docker/cli/issues/4890
func TestExecCreateConnectionError(t *testing.T) {
client, err := New(WithHost("tcp://no-such-host.invalid"))
assert.NilError(t, err)
_, err = client.ExecCreate(t.Context(), "container_id", ExecCreateOptions{})
assert.Check(t, is.ErrorType(err, IsErrConnectionFailed))
}
func TestExecCreate(t *testing.T) {
const expectedURL = "/containers/container_id/exec"
client, err := New(
WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
// FIXME validate the content is the given ExecConfig ?
if err := req.ParseForm(); err != nil {
return nil, err
}
execConfig := &container.ExecCreateRequest{}
if err := json.NewDecoder(req.Body).Decode(execConfig); err != nil {
return nil, err
}
if execConfig.User != "user" {
return nil, fmt.Errorf("expected an execConfig with User == 'user', got %v", execConfig)
}
return mockJSONResponse(http.StatusOK, nil, container.ExecCreateResponse{
ID: "exec_id",
})(req)
}),
)
assert.NilError(t, err)
res, err := client.ExecCreate(t.Context(), "container_id", ExecCreateOptions{
User: "user",
})
assert.NilError(t, err)
assert.Check(t, is.Equal(res.ID, "exec_id"))
}
func TestExecStartError(t *testing.T) {
client, err := New(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ExecStart(t.Context(), "nothing", ExecStartOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestExecStart(t *testing.T) {
const expectedURL = "/exec/exec_id/start"
client, err := New(
WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
if err := req.ParseForm(); err != nil {
return nil, err
}
request := &container.ExecStartRequest{}
if err := json.NewDecoder(req.Body).Decode(request); err != nil {
return nil, err
}
if request.Tty || !request.Detach {
return nil, fmt.Errorf("expected ExecStartOptions{Detach:true,Tty:false}, got %v", request)
}
return mockResponse(http.StatusOK, nil, "")(req)
}),
)
assert.NilError(t, err)
_, err = client.ExecStart(t.Context(), "exec_id", ExecStartOptions{
Detach: true,
TTY: false,
})
assert.NilError(t, err)
}
func TestExecStartConsoleSize(t *testing.T) {
tests := []struct {
doc string
options ExecStartOptions
expErr string
expReq container.ExecStartRequest
}{
{
doc: "without TTY",
options: ExecStartOptions{
Detach: true,
TTY: false,
ConsoleSize: ConsoleSize{Height: 100, Width: 200},
},
expErr: "console size is only supported when TTY is enabled",
},
{
doc: "with TTY",
options: ExecStartOptions{
Detach: true,
TTY: true,
ConsoleSize: ConsoleSize{Height: 100, Width: 200},
},
expReq: container.ExecStartRequest{
Detach: true,
Tty: true,
ConsoleSize: &[2]uint{100, 200},
},
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
var actualReq container.ExecStartRequest
client, err := New(
WithMockClient(func(req *http.Request) (*http.Response, error) {
if tc.expErr != "" {
return nil, errors.New("should not have made API request")
}
if err := json.NewDecoder(req.Body).Decode(&actualReq); err != nil {
return nil, err
}
return mockJSONResponse(http.StatusOK, nil, ExecStartResult{})(req)
}),
)
assert.NilError(t, err)
_, err = client.ExecStart(t.Context(), "exec_id", tc.options)
if tc.expErr != "" {
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, tc.expErr))
assert.Check(t, is.DeepEqual(actualReq, tc.expReq))
} else {
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(actualReq, tc.expReq))
}
})
}
}
func TestExecInspectError(t *testing.T) {
client, err := New(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ExecInspect(t.Context(), "nothing", ExecInspectOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestExecInspect(t *testing.T) {
const expectedURL = "/exec/exec_id/json"
client, err := New(
WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
return mockJSONResponse(http.StatusOK, nil, container.ExecInspectResponse{
ID: "exec_id",
ContainerID: "container_id",
})(req)
}),
)
assert.NilError(t, err)
inspect, err := client.ExecInspect(t.Context(), "exec_id", ExecInspectOptions{})
assert.NilError(t, err)
assert.Check(t, is.Equal(inspect.ID, "exec_id"))
assert.Check(t, is.Equal(inspect.ContainerID, "container_id"))
}