Browse code

Stop using deprecated SockRequest

Signed-off-by: Stanislav Bondarenko <stanislav.bondarenko@gmail.com>

Stanislav Bondarenko authored on 2017/05/24 12:56:26
Showing 32 changed files
... ...
@@ -14,8 +14,10 @@ import (
14 14
 	"strings"
15 15
 	"time"
16 16
 
17
+	"github.com/docker/docker/api"
17 18
 	"github.com/docker/docker/api/types"
18 19
 	"github.com/docker/docker/api/types/events"
20
+	"github.com/docker/docker/client"
19 21
 	"github.com/docker/docker/integration-cli/checker"
20 22
 	"github.com/docker/docker/integration-cli/request"
21 23
 	"github.com/docker/docker/opts"
... ...
@@ -752,6 +754,16 @@ func (d *Daemon) ReloadConfig() error {
752 752
 	return nil
753 753
 }
754 754
 
755
+// NewClient creates new client based on daemon's socket path
756
+func (d *Daemon) NewClient() (*client.Client, error) {
757
+	httpClient, err := request.NewHTTPClient(d.Sock())
758
+	if err != nil {
759
+		return nil, err
760
+	}
761
+
762
+	return client.NewClient(d.Sock(), api.DefaultVersion, httpClient, nil)
763
+}
764
+
755 765
 // WaitInspectWithArgs waits for the specified expression to be equals to the specified expected string in the given time.
756 766
 // Deprecated: use cli.WaitCmd instead
757 767
 func WaitInspectWithArgs(dockerBinary, name, expr, expected string, timeout time.Duration, arg ...string) error {
... ...
@@ -1,7 +1,6 @@
1 1
 package daemon
2 2
 
3 3
 import (
4
-	"context"
5 4
 	"encoding/json"
6 5
 	"fmt"
7 6
 	"net/http"
... ...
@@ -11,10 +10,10 @@ import (
11 11
 	"github.com/docker/docker/api/types"
12 12
 	"github.com/docker/docker/api/types/filters"
13 13
 	"github.com/docker/docker/api/types/swarm"
14
-	"github.com/docker/docker/client"
15 14
 	"github.com/docker/docker/integration-cli/checker"
16 15
 	"github.com/go-check/check"
17 16
 	"github.com/pkg/errors"
17
+	"golang.org/x/net/context"
18 18
 )
19 19
 
20 20
 // Swarm is a test daemon with helpers for participating in a swarm.
... ...
@@ -30,10 +29,12 @@ func (d *Swarm) Init(req swarm.InitRequest) error {
30 30
 	if req.ListenAddr == "" {
31 31
 		req.ListenAddr = d.ListenAddr
32 32
 	}
33
-	status, out, err := d.SockRequest("POST", "/swarm/init", req)
34
-	if status != http.StatusOK {
35
-		return fmt.Errorf("initializing swarm: invalid statuscode %v, %q", status, out)
33
+	cli, err := d.NewClient()
34
+	if err != nil {
35
+		return fmt.Errorf("initializing swarm: failed to create client %v", err)
36 36
 	}
37
+	defer cli.Close()
38
+	_, err = cli.SwarmInit(context.Background(), req)
37 39
 	if err != nil {
38 40
 		return fmt.Errorf("initializing swarm: %v", err)
39 41
 	}
... ...
@@ -50,10 +51,12 @@ func (d *Swarm) Join(req swarm.JoinRequest) error {
50 50
 	if req.ListenAddr == "" {
51 51
 		req.ListenAddr = d.ListenAddr
52 52
 	}
53
-	status, out, err := d.SockRequest("POST", "/swarm/join", req)
54
-	if status != http.StatusOK {
55
-		return fmt.Errorf("joining swarm: invalid statuscode %v, %q", status, out)
53
+	cli, err := d.NewClient()
54
+	if err != nil {
55
+		return fmt.Errorf("joining swarm: failed to create client %v", err)
56 56
 	}
57
+	defer cli.Close()
58
+	err = cli.SwarmJoin(context.Background(), req)
57 59
 	if err != nil {
58 60
 		return fmt.Errorf("joining swarm: %v", err)
59 61
 	}
... ...
@@ -67,14 +70,12 @@ func (d *Swarm) Join(req swarm.JoinRequest) error {
67 67
 
68 68
 // Leave forces daemon to leave current cluster.
69 69
 func (d *Swarm) Leave(force bool) error {
70
-	url := "/swarm/leave"
71
-	if force {
72
-		url += "?force=1"
73
-	}
74
-	status, out, err := d.SockRequest("POST", url, nil)
75
-	if status != http.StatusOK {
76
-		return fmt.Errorf("leaving swarm: invalid statuscode %v, %q", status, out)
70
+	cli, err := d.NewClient()
71
+	if err != nil {
72
+		return fmt.Errorf("leaving swarm: failed to create client %v", err)
77 73
 	}
74
+	defer cli.Close()
75
+	err = cli.SwarmLeave(context.Background(), force)
78 76
 	if err != nil {
79 77
 		err = fmt.Errorf("leaving swarm: %v", err)
80 78
 	}
... ...
@@ -83,28 +84,27 @@ func (d *Swarm) Leave(force bool) error {
83 83
 
84 84
 // SwarmInfo returns the swarm information of the daemon
85 85
 func (d *Swarm) SwarmInfo() (swarm.Info, error) {
86
-	var info struct {
87
-		Swarm swarm.Info
88
-	}
89
-	status, dt, err := d.SockRequest("GET", "/info", nil)
90
-	if status != http.StatusOK {
91
-		return info.Swarm, fmt.Errorf("get swarm info: invalid statuscode %v", status)
92
-	}
86
+	cli, err := d.NewClient()
93 87
 	if err != nil {
94
-		return info.Swarm, fmt.Errorf("get swarm info: %v", err)
88
+		return swarm.Info{}, fmt.Errorf("get swarm info: %v", err)
95 89
 	}
96
-	if err := json.Unmarshal(dt, &info); err != nil {
97
-		return info.Swarm, err
90
+
91
+	info, err := cli.Info(context.Background())
92
+	if err != nil {
93
+		return swarm.Info{}, fmt.Errorf("get swarm info: %v", err)
98 94
 	}
95
+
99 96
 	return info.Swarm, nil
100 97
 }
101 98
 
102 99
 // Unlock tries to unlock a locked swarm
103 100
 func (d *Swarm) Unlock(req swarm.UnlockRequest) error {
104
-	status, out, err := d.SockRequest("POST", "/swarm/unlock", req)
105
-	if status != http.StatusOK {
106
-		return fmt.Errorf("unlocking swarm: invalid statuscode %v, %q", status, out)
101
+	cli, err := d.NewClient()
102
+	if err != nil {
103
+		return fmt.Errorf("unlocking swarm: failed to create client %v", err)
107 104
 	}
105
+	defer cli.Close()
106
+	err = cli.SwarmUnlock(context.Background(), req)
108 107
 	if err != nil {
109 108
 		err = errors.Wrap(err, "unlocking swarm")
110 109
 	}
... ...
@@ -129,19 +129,19 @@ type SpecConstructor func(*swarm.Spec)
129 129
 // CreateServiceWithOptions creates a swarm service given the specified service constructors
130 130
 // and auth config
131 131
 func (d *Swarm) CreateServiceWithOptions(c *check.C, opts types.ServiceCreateOptions, f ...ServiceConstructor) string {
132
-	cl, err := client.NewClient(d.Sock(), "", nil, nil)
133
-	c.Assert(err, checker.IsNil, check.Commentf("failed to create client"))
134
-	defer cl.Close()
135
-
136 132
 	var service swarm.Service
137 133
 	for _, fn := range f {
138 134
 		fn(&service)
139 135
 	}
140 136
 
137
+	cli, err := d.NewClient()
138
+	c.Assert(err, checker.IsNil)
139
+	defer cli.Close()
140
+
141 141
 	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
142 142
 	defer cancel()
143 143
 
144
-	res, err := cl.ServiceCreate(ctx, service.Spec, opts)
144
+	res, err := cli.ServiceCreate(ctx, service.Spec, opts)
145 145
 	c.Assert(err, checker.IsNil)
146 146
 	return res.ID
147 147
 }
... ...
@@ -153,28 +153,31 @@ func (d *Swarm) CreateService(c *check.C, f ...ServiceConstructor) string {
153 153
 
154 154
 // GetService returns the swarm service corresponding to the specified id
155 155
 func (d *Swarm) GetService(c *check.C, id string) *swarm.Service {
156
-	var service swarm.Service
157
-	status, out, err := d.SockRequest("GET", "/services/"+id, nil)
158
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
159
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
160
-	c.Assert(json.Unmarshal(out, &service), checker.IsNil)
156
+	cli, err := d.NewClient()
157
+	c.Assert(err, checker.IsNil)
158
+	defer cli.Close()
159
+
160
+	service, _, err := cli.ServiceInspectWithRaw(context.Background(), id, types.ServiceInspectOptions{})
161
+	c.Assert(err, checker.IsNil)
161 162
 	return &service
162 163
 }
163 164
 
164 165
 // GetServiceTasks returns the swarm tasks for the specified service
165 166
 func (d *Swarm) GetServiceTasks(c *check.C, service string) []swarm.Task {
166
-	var tasks []swarm.Task
167
+	cli, err := d.NewClient()
168
+	c.Assert(err, checker.IsNil)
169
+	defer cli.Close()
167 170
 
168 171
 	filterArgs := filters.NewArgs()
169 172
 	filterArgs.Add("desired-state", "running")
170 173
 	filterArgs.Add("service", service)
171
-	filters, err := filters.ToParam(filterArgs)
172
-	c.Assert(err, checker.IsNil)
173 174
 
174
-	status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
175
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
176
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
177
-	c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
175
+	options := types.TaskListOptions{
176
+		Filters: filterArgs,
177
+	}
178
+
179
+	tasks, err := cli.TaskList(context.Background(), options)
180
+	c.Assert(err, checker.IsNil)
178 181
 	return tasks
179 182
 }
180 183
 
... ...
@@ -252,17 +255,19 @@ func (d *Swarm) CheckServiceTasks(service string) func(*check.C) (interface{}, c
252 252
 
253 253
 // CheckRunningTaskNetworks returns the number of times each network is referenced from a task.
254 254
 func (d *Swarm) CheckRunningTaskNetworks(c *check.C) (interface{}, check.CommentInterface) {
255
-	var tasks []swarm.Task
255
+	cli, err := d.NewClient()
256
+	c.Assert(err, checker.IsNil)
257
+	defer cli.Close()
256 258
 
257 259
 	filterArgs := filters.NewArgs()
258 260
 	filterArgs.Add("desired-state", "running")
259
-	filters, err := filters.ToParam(filterArgs)
260
-	c.Assert(err, checker.IsNil)
261 261
 
262
-	status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
263
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
264
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
265
-	c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
262
+	options := types.TaskListOptions{
263
+		Filters: filterArgs,
264
+	}
265
+
266
+	tasks, err := cli.TaskList(context.Background(), options)
267
+	c.Assert(err, checker.IsNil)
266 268
 
267 269
 	result := make(map[string]int)
268 270
 	for _, task := range tasks {
... ...
@@ -275,17 +280,19 @@ func (d *Swarm) CheckRunningTaskNetworks(c *check.C) (interface{}, check.Comment
275 275
 
276 276
 // CheckRunningTaskImages returns the times each image is running as a task.
277 277
 func (d *Swarm) CheckRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) {
278
-	var tasks []swarm.Task
278
+	cli, err := d.NewClient()
279
+	c.Assert(err, checker.IsNil)
280
+	defer cli.Close()
279 281
 
280 282
 	filterArgs := filters.NewArgs()
281 283
 	filterArgs.Add("desired-state", "running")
282
-	filters, err := filters.ToParam(filterArgs)
283
-	c.Assert(err, checker.IsNil)
284 284
 
285
-	status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
286
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
287
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
288
-	c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
285
+	options := types.TaskListOptions{
286
+		Filters: filterArgs,
287
+	}
288
+
289
+	tasks, err := cli.TaskList(context.Background(), options)
290
+	c.Assert(err, checker.IsNil)
289 291
 
290 292
 	result := make(map[string]int)
291 293
 	for _, task := range tasks {
... ...
@@ -310,246 +317,281 @@ func (d *Swarm) CheckNodeReadyCount(c *check.C) (interface{}, check.CommentInter
310 310
 
311 311
 // GetTask returns the swarm task identified by the specified id
312 312
 func (d *Swarm) GetTask(c *check.C, id string) swarm.Task {
313
-	var task swarm.Task
313
+	cli, err := d.NewClient()
314
+	c.Assert(err, checker.IsNil)
315
+	defer cli.Close()
314 316
 
315
-	status, out, err := d.SockRequest("GET", "/tasks/"+id, nil)
316
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
317
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
318
-	c.Assert(json.Unmarshal(out, &task), checker.IsNil)
317
+	task, _, err := cli.TaskInspectWithRaw(context.Background(), id)
318
+	c.Assert(err, checker.IsNil)
319 319
 	return task
320 320
 }
321 321
 
322 322
 // UpdateService updates a swarm service with the specified service constructor
323 323
 func (d *Swarm) UpdateService(c *check.C, service *swarm.Service, f ...ServiceConstructor) {
324
+	cli, err := d.NewClient()
325
+	c.Assert(err, checker.IsNil)
326
+	defer cli.Close()
327
+
324 328
 	for _, fn := range f {
325 329
 		fn(service)
326 330
 	}
327
-	url := fmt.Sprintf("/services/%s/update?version=%d", service.ID, service.Version.Index)
328
-	status, out, err := d.SockRequest("POST", url, service.Spec)
329
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
330
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
331
+
332
+	_, err = cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
333
+	c.Assert(err, checker.IsNil)
331 334
 }
332 335
 
333 336
 // RemoveService removes the specified service
334 337
 func (d *Swarm) RemoveService(c *check.C, id string) {
335
-	status, out, err := d.SockRequest("DELETE", "/services/"+id, nil)
336
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
337
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
338
+	cli, err := d.NewClient()
339
+	c.Assert(err, checker.IsNil)
340
+	defer cli.Close()
341
+
342
+	err = cli.ServiceRemove(context.Background(), id)
343
+	c.Assert(err, checker.IsNil)
338 344
 }
339 345
 
340 346
 // GetNode returns a swarm node identified by the specified id
341 347
 func (d *Swarm) GetNode(c *check.C, id string) *swarm.Node {
342
-	var node swarm.Node
343
-	status, out, err := d.SockRequest("GET", "/nodes/"+id, nil)
344
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
345
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
346
-	c.Assert(json.Unmarshal(out, &node), checker.IsNil)
348
+	cli, err := d.NewClient()
349
+	c.Assert(err, checker.IsNil)
350
+	defer cli.Close()
351
+
352
+	node, _, err := cli.NodeInspectWithRaw(context.Background(), id)
353
+	c.Assert(err, checker.IsNil)
347 354
 	c.Assert(node.ID, checker.Equals, id)
348 355
 	return &node
349 356
 }
350 357
 
351 358
 // RemoveNode removes the specified node
352 359
 func (d *Swarm) RemoveNode(c *check.C, id string, force bool) {
353
-	url := "/nodes/" + id
354
-	if force {
355
-		url += "?force=1"
356
-	}
360
+	cli, err := d.NewClient()
361
+	c.Assert(err, checker.IsNil)
362
+	defer cli.Close()
357 363
 
358
-	status, out, err := d.SockRequest("DELETE", url, nil)
359
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
360
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
364
+	options := types.NodeRemoveOptions{
365
+		Force: force,
366
+	}
367
+	err = cli.NodeRemove(context.Background(), id, options)
368
+	c.Assert(err, checker.IsNil)
361 369
 }
362 370
 
363 371
 // UpdateNode updates a swarm node with the specified node constructor
364 372
 func (d *Swarm) UpdateNode(c *check.C, id string, f ...NodeConstructor) {
373
+	cli, err := d.NewClient()
374
+	c.Assert(err, checker.IsNil)
375
+	defer cli.Close()
376
+
365 377
 	for i := 0; ; i++ {
366 378
 		node := d.GetNode(c, id)
367 379
 		for _, fn := range f {
368 380
 			fn(node)
369 381
 		}
370
-		url := fmt.Sprintf("/nodes/%s/update?version=%d", node.ID, node.Version.Index)
371
-		status, out, err := d.SockRequest("POST", url, node.Spec)
372
-		if i < 10 && strings.Contains(string(out), "update out of sequence") {
382
+
383
+		err = cli.NodeUpdate(context.Background(), node.ID, node.Version, node.Spec)
384
+		if i < 10 && err != nil && strings.Contains(err.Error(), "update out of sequence") {
373 385
 			time.Sleep(100 * time.Millisecond)
374 386
 			continue
375 387
 		}
376
-		c.Assert(err, checker.IsNil, check.Commentf(string(out)))
377
-		c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
388
+		c.Assert(err, checker.IsNil)
378 389
 		return
379 390
 	}
380 391
 }
381 392
 
382 393
 // ListNodes returns the list of the current swarm nodes
383 394
 func (d *Swarm) ListNodes(c *check.C) []swarm.Node {
384
-	status, out, err := d.SockRequest("GET", "/nodes", nil)
385
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
386
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
395
+	cli, err := d.NewClient()
396
+	c.Assert(err, checker.IsNil)
397
+	defer cli.Close()
398
+
399
+	nodes, err := cli.NodeList(context.Background(), types.NodeListOptions{})
400
+	c.Assert(err, checker.IsNil)
387 401
 
388
-	nodes := []swarm.Node{}
389
-	c.Assert(json.Unmarshal(out, &nodes), checker.IsNil)
390 402
 	return nodes
391 403
 }
392 404
 
393 405
 // ListServices returns the list of the current swarm services
394 406
 func (d *Swarm) ListServices(c *check.C) []swarm.Service {
395
-	status, out, err := d.SockRequest("GET", "/services", nil)
396
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
397
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
407
+	cli, err := d.NewClient()
408
+	c.Assert(err, checker.IsNil)
409
+	defer cli.Close()
398 410
 
399
-	services := []swarm.Service{}
400
-	c.Assert(json.Unmarshal(out, &services), checker.IsNil)
411
+	services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
412
+	c.Assert(err, checker.IsNil)
401 413
 	return services
402 414
 }
403 415
 
404 416
 // CreateSecret creates a secret given the specified spec
405 417
 func (d *Swarm) CreateSecret(c *check.C, secretSpec swarm.SecretSpec) string {
406
-	status, out, err := d.SockRequest("POST", "/secrets/create", secretSpec)
418
+	cli, err := d.NewClient()
419
+	c.Assert(err, checker.IsNil)
420
+	defer cli.Close()
407 421
 
408
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
409
-	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
422
+	scr, err := cli.SecretCreate(context.Background(), secretSpec)
423
+	c.Assert(err, checker.IsNil)
410 424
 
411
-	var scr types.SecretCreateResponse
412
-	c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
413 425
 	return scr.ID
414 426
 }
415 427
 
416 428
 // ListSecrets returns the list of the current swarm secrets
417 429
 func (d *Swarm) ListSecrets(c *check.C) []swarm.Secret {
418
-	status, out, err := d.SockRequest("GET", "/secrets", nil)
419
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
420
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
430
+	cli, err := d.NewClient()
431
+	c.Assert(err, checker.IsNil)
432
+	defer cli.Close()
421 433
 
422
-	secrets := []swarm.Secret{}
423
-	c.Assert(json.Unmarshal(out, &secrets), checker.IsNil)
434
+	secrets, err := cli.SecretList(context.Background(), types.SecretListOptions{})
435
+	c.Assert(err, checker.IsNil)
424 436
 	return secrets
425 437
 }
426 438
 
427 439
 // GetSecret returns a swarm secret identified by the specified id
428 440
 func (d *Swarm) GetSecret(c *check.C, id string) *swarm.Secret {
429
-	var secret swarm.Secret
430
-	status, out, err := d.SockRequest("GET", "/secrets/"+id, nil)
431
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
432
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
433
-	c.Assert(json.Unmarshal(out, &secret), checker.IsNil)
441
+	cli, err := d.NewClient()
442
+	c.Assert(err, checker.IsNil)
443
+	defer cli.Close()
444
+
445
+	secret, _, err := cli.SecretInspectWithRaw(context.Background(), id)
446
+	c.Assert(err, checker.IsNil)
434 447
 	return &secret
435 448
 }
436 449
 
437 450
 // DeleteSecret removes the swarm secret identified by the specified id
438 451
 func (d *Swarm) DeleteSecret(c *check.C, id string) {
439
-	status, out, err := d.SockRequest("DELETE", "/secrets/"+id, nil)
440
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
441
-	c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf("output: %q", string(out)))
452
+	cli, err := d.NewClient()
453
+	c.Assert(err, checker.IsNil)
454
+	defer cli.Close()
455
+
456
+	err = cli.SecretRemove(context.Background(), id)
457
+	c.Assert(err, checker.IsNil)
442 458
 }
443 459
 
444 460
 // UpdateSecret updates the swarm secret identified by the specified id
445 461
 // Currently, only label update is supported.
446 462
 func (d *Swarm) UpdateSecret(c *check.C, id string, f ...SecretConstructor) {
463
+	cli, err := d.NewClient()
464
+	c.Assert(err, checker.IsNil)
465
+	defer cli.Close()
466
+
447 467
 	secret := d.GetSecret(c, id)
448 468
 	for _, fn := range f {
449 469
 		fn(secret)
450 470
 	}
451
-	url := fmt.Sprintf("/secrets/%s/update?version=%d", secret.ID, secret.Version.Index)
452
-	status, out, err := d.SockRequest("POST", url, secret.Spec)
453
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
454
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
471
+
472
+	err = cli.SecretUpdate(context.Background(), secret.ID, secret.Version, secret.Spec)
473
+
474
+	c.Assert(err, checker.IsNil)
455 475
 }
456 476
 
457 477
 // CreateConfig creates a config given the specified spec
458 478
 func (d *Swarm) CreateConfig(c *check.C, configSpec swarm.ConfigSpec) string {
459
-	status, out, err := d.SockRequest("POST", "/configs/create", configSpec)
460
-
461
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
462
-	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
479
+	cli, err := d.NewClient()
480
+	c.Assert(err, checker.IsNil)
481
+	defer cli.Close()
463 482
 
464
-	var scr types.ConfigCreateResponse
465
-	c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
483
+	scr, err := cli.ConfigCreate(context.Background(), configSpec)
484
+	c.Assert(err, checker.IsNil)
466 485
 	return scr.ID
467 486
 }
468 487
 
469 488
 // ListConfigs returns the list of the current swarm configs
470 489
 func (d *Swarm) ListConfigs(c *check.C) []swarm.Config {
471
-	status, out, err := d.SockRequest("GET", "/configs", nil)
472
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
473
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
490
+	cli, err := d.NewClient()
491
+	c.Assert(err, checker.IsNil)
492
+	defer cli.Close()
474 493
 
475
-	configs := []swarm.Config{}
476
-	c.Assert(json.Unmarshal(out, &configs), checker.IsNil)
494
+	configs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{})
495
+	c.Assert(err, checker.IsNil)
477 496
 	return configs
478 497
 }
479 498
 
480 499
 // GetConfig returns a swarm config identified by the specified id
481 500
 func (d *Swarm) GetConfig(c *check.C, id string) *swarm.Config {
482
-	var config swarm.Config
483
-	status, out, err := d.SockRequest("GET", "/configs/"+id, nil)
484
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
485
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
486
-	c.Assert(json.Unmarshal(out, &config), checker.IsNil)
501
+	cli, err := d.NewClient()
502
+	c.Assert(err, checker.IsNil)
503
+	defer cli.Close()
504
+
505
+	config, _, err := cli.ConfigInspectWithRaw(context.Background(), id)
506
+	c.Assert(err, checker.IsNil)
487 507
 	return &config
488 508
 }
489 509
 
490 510
 // DeleteConfig removes the swarm config identified by the specified id
491 511
 func (d *Swarm) DeleteConfig(c *check.C, id string) {
492
-	status, out, err := d.SockRequest("DELETE", "/configs/"+id, nil)
493
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
494
-	c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf("output: %q", string(out)))
512
+	cli, err := d.NewClient()
513
+	c.Assert(err, checker.IsNil)
514
+	defer cli.Close()
515
+
516
+	err = cli.ConfigRemove(context.Background(), id)
517
+	c.Assert(err, checker.IsNil)
495 518
 }
496 519
 
497 520
 // UpdateConfig updates the swarm config identified by the specified id
498 521
 // Currently, only label update is supported.
499 522
 func (d *Swarm) UpdateConfig(c *check.C, id string, f ...ConfigConstructor) {
523
+	cli, err := d.NewClient()
524
+	c.Assert(err, checker.IsNil)
525
+	defer cli.Close()
526
+
500 527
 	config := d.GetConfig(c, id)
501 528
 	for _, fn := range f {
502 529
 		fn(config)
503 530
 	}
504
-	url := fmt.Sprintf("/configs/%s/update?version=%d", config.ID, config.Version.Index)
505
-	status, out, err := d.SockRequest("POST", url, config.Spec)
506
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
507
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
531
+
532
+	err = cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
533
+	c.Assert(err, checker.IsNil)
508 534
 }
509 535
 
510 536
 // GetSwarm returns the current swarm object
511 537
 func (d *Swarm) GetSwarm(c *check.C) swarm.Swarm {
512
-	var sw swarm.Swarm
513
-	status, out, err := d.SockRequest("GET", "/swarm", nil)
514
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
515
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
516
-	c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
538
+	cli, err := d.NewClient()
539
+	c.Assert(err, checker.IsNil)
540
+	defer cli.Close()
541
+
542
+	sw, err := cli.SwarmInspect(context.Background())
543
+	c.Assert(err, checker.IsNil)
517 544
 	return sw
518 545
 }
519 546
 
520 547
 // UpdateSwarm updates the current swarm object with the specified spec constructors
521 548
 func (d *Swarm) UpdateSwarm(c *check.C, f ...SpecConstructor) {
549
+	cli, err := d.NewClient()
550
+	c.Assert(err, checker.IsNil)
551
+	defer cli.Close()
552
+
522 553
 	sw := d.GetSwarm(c)
523 554
 	for _, fn := range f {
524 555
 		fn(&sw.Spec)
525 556
 	}
526
-	url := fmt.Sprintf("/swarm/update?version=%d", sw.Version.Index)
527
-	status, out, err := d.SockRequest("POST", url, sw.Spec)
528
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
529
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
557
+
558
+	err = cli.SwarmUpdate(context.Background(), sw.Version, sw.Spec, swarm.UpdateFlags{})
559
+	c.Assert(err, checker.IsNil)
530 560
 }
531 561
 
532 562
 // RotateTokens update the swarm to rotate tokens
533 563
 func (d *Swarm) RotateTokens(c *check.C) {
534
-	var sw swarm.Swarm
535
-	status, out, err := d.SockRequest("GET", "/swarm", nil)
536
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
537
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
538
-	c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
564
+	cli, err := d.NewClient()
565
+	c.Assert(err, checker.IsNil)
566
+	defer cli.Close()
567
+
568
+	sw, err := cli.SwarmInspect(context.Background())
569
+	c.Assert(err, checker.IsNil)
539 570
 
540
-	url := fmt.Sprintf("/swarm/update?version=%d&rotateWorkerToken=true&rotateManagerToken=true", sw.Version.Index)
541
-	status, out, err = d.SockRequest("POST", url, sw.Spec)
542
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
543
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
571
+	flags := swarm.UpdateFlags{
572
+		RotateManagerToken: true,
573
+		RotateWorkerToken:  true,
574
+	}
575
+
576
+	err = cli.SwarmUpdate(context.Background(), sw.Version, sw.Spec, flags)
577
+	c.Assert(err, checker.IsNil)
544 578
 }
545 579
 
546 580
 // JoinTokens returns the current swarm join tokens
547 581
 func (d *Swarm) JoinTokens(c *check.C) swarm.JoinTokens {
548
-	var sw swarm.Swarm
549
-	status, out, err := d.SockRequest("GET", "/swarm", nil)
550
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
551
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
552
-	c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
582
+	cli, err := d.NewClient()
583
+	c.Assert(err, checker.IsNil)
584
+	defer cli.Close()
585
+
586
+	sw, err := cli.SwarmInspect(context.Background())
587
+	c.Assert(err, checker.IsNil)
553 588
 	return sw.JoinTokens
554 589
 }
555 590
 
... ...
@@ -570,17 +612,14 @@ func (d *Swarm) CheckControlAvailable(c *check.C) (interface{}, check.CommentInt
570 570
 
571 571
 // CheckLeader returns whether there is a leader on the swarm or not
572 572
 func (d *Swarm) CheckLeader(c *check.C) (interface{}, check.CommentInterface) {
573
+	cli, err := d.NewClient()
574
+	c.Assert(err, checker.IsNil)
575
+	defer cli.Close()
576
+
573 577
 	errList := check.Commentf("could not get node list")
574
-	status, out, err := d.SockRequest("GET", "/nodes", nil)
575
-	if err != nil {
576
-		return err, errList
577
-	}
578
-	if status != http.StatusOK {
579
-		return fmt.Errorf("expected http status OK, got: %d", status), errList
580
-	}
581 578
 
582
-	var ls []swarm.Node
583
-	if err := json.Unmarshal(out, &ls); err != nil {
579
+	ls, err := cli.NodeList(context.Background(), types.NodeListOptions{})
580
+	if err != nil {
584 581
 		return err, errList
585 582
 	}
586 583
 
... ...
@@ -86,11 +86,13 @@ func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
86 86
 }
87 87
 
88 88
 func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
89
-	status, body, err := request.SockRequest("GET", "/containers/doesnotexist/attach/ws", nil, daemonHost())
90
-	c.Assert(status, checker.Equals, http.StatusNotFound)
89
+	res, body, err := request.Get("/containers/doesnotexist/attach/ws")
90
+	c.Assert(res.StatusCode, checker.Equals, http.StatusNotFound)
91
+	c.Assert(err, checker.IsNil)
92
+	b, err := request.ReadBody(body)
91 93
 	c.Assert(err, checker.IsNil)
92 94
 	expected := "No such container: doesnotexist"
93
-	c.Assert(getErrorMessage(c, body), checker.Contains, expected)
95
+	c.Assert(getErrorMessage(c, b), checker.Contains, expected)
94 96
 }
95 97
 
96 98
 func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
... ...
@@ -177,6 +179,7 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
177 177
 	// Make sure we don't see "hello" if Logs is false
178 178
 	client, err := client.NewEnvClient()
179 179
 	c.Assert(err, checker.IsNil)
180
+	defer client.Close()
180 181
 
181 182
 	cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "echo hello; cat")
182 183
 	cid = strings.TrimSpace(cid)
... ...
@@ -1,12 +1,11 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"net/http"
5
-
6 4
 	"github.com/docker/docker/api/types"
5
+	"github.com/docker/docker/client"
7 6
 	"github.com/docker/docker/integration-cli/checker"
8
-	"github.com/docker/docker/integration-cli/request"
9 7
 	"github.com/go-check/check"
8
+	"golang.org/x/net/context"
10 9
 )
11 10
 
12 11
 // Test case for #22244
... ...
@@ -16,11 +15,11 @@ func (s *DockerSuite) TestAuthAPI(c *check.C) {
16 16
 		Username: "no-user",
17 17
 		Password: "no-password",
18 18
 	}
19
+	cli, err := client.NewEnvClient()
20
+	c.Assert(err, checker.IsNil)
21
+	defer cli.Close()
19 22
 
23
+	_, err = cli.RegistryLogin(context.Background(), config)
20 24
 	expected := "Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password"
21
-	status, body, err := request.SockRequest("POST", "/auth", config, daemonHost())
22
-	c.Assert(err, check.IsNil)
23
-	c.Assert(status, check.Equals, http.StatusUnauthorized)
24
-	msg := getErrorMessage(c, body)
25
-	c.Assert(msg, checker.Contains, expected, check.Commentf("Expected: %v, got: %v", expected, msg))
25
+	c.Assert(err.Error(), checker.Contains, expected)
26 26
 }
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"io"
9 9
 	"io/ioutil"
10 10
 	"net/http"
11
-	"net/url"
12 11
 	"os"
13 12
 	"path/filepath"
14 13
 	"regexp"
... ...
@@ -20,6 +19,7 @@ import (
20 20
 	containertypes "github.com/docker/docker/api/types/container"
21 21
 	mounttypes "github.com/docker/docker/api/types/mount"
22 22
 	networktypes "github.com/docker/docker/api/types/network"
23
+	"github.com/docker/docker/client"
23 24
 	"github.com/docker/docker/integration-cli/checker"
24 25
 	"github.com/docker/docker/integration-cli/cli"
25 26
 	"github.com/docker/docker/integration-cli/cli/build"
... ...
@@ -28,7 +28,9 @@ import (
28 28
 	"github.com/docker/docker/pkg/mount"
29 29
 	"github.com/docker/docker/pkg/stringid"
30 30
 	"github.com/docker/docker/volume"
31
+	"github.com/docker/go-connections/nat"
31 32
 	"github.com/go-check/check"
33
+	"golang.org/x/net/context"
32 34
 )
33 35
 
34 36
 func (s *DockerSuite) TestContainerAPIGetAll(c *check.C) {
... ...
@@ -36,35 +38,43 @@ func (s *DockerSuite) TestContainerAPIGetAll(c *check.C) {
36 36
 	name := "getall"
37 37
 	dockerCmd(c, "run", "--name", name, "busybox", "true")
38 38
 
39
-	status, body, err := request.SockRequest("GET", "/containers/json?all=1", nil, daemonHost())
39
+	cli, err := client.NewEnvClient()
40 40
 	c.Assert(err, checker.IsNil)
41
-	c.Assert(status, checker.Equals, http.StatusOK)
41
+	defer cli.Close()
42 42
 
43
-	var inspectJSON []struct {
44
-		Names []string
43
+	options := types.ContainerListOptions{
44
+		All: true,
45 45
 	}
46
-	err = json.Unmarshal(body, &inspectJSON)
47
-	c.Assert(err, checker.IsNil, check.Commentf("unable to unmarshal response body"))
48
-
49
-	c.Assert(inspectJSON, checker.HasLen, startCount+1)
50
-
51
-	actual := inspectJSON[0].Names[0]
46
+	containers, err := cli.ContainerList(context.Background(), options)
47
+	c.Assert(err, checker.IsNil)
48
+	c.Assert(containers, checker.HasLen, startCount+1)
49
+	actual := containers[0].Names[0]
52 50
 	c.Assert(actual, checker.Equals, "/"+name)
53 51
 }
54 52
 
55 53
 // regression test for empty json field being omitted #13691
56 54
 func (s *DockerSuite) TestContainerAPIGetJSONNoFieldsOmitted(c *check.C) {
55
+	startCount := getContainerCount(c)
57 56
 	dockerCmd(c, "run", "busybox", "true")
58 57
 
59
-	status, body, err := request.SockRequest("GET", "/containers/json?all=1", nil, daemonHost())
58
+	cli, err := client.NewEnvClient()
59
+	c.Assert(err, checker.IsNil)
60
+	defer cli.Close()
61
+
62
+	options := types.ContainerListOptions{
63
+		All: true,
64
+	}
65
+	containers, err := cli.ContainerList(context.Background(), options)
60 66
 	c.Assert(err, checker.IsNil)
61
-	c.Assert(status, checker.Equals, http.StatusOK)
67
+	c.Assert(containers, checker.HasLen, startCount+1)
68
+	actual := fmt.Sprintf("%+v", containers[0])
69
+	fmt.Println(actual)
62 70
 
63 71
 	// empty Labels field triggered this bug, make sense to check for everything
64 72
 	// cause even Ports for instance can trigger this bug
65 73
 	// better safe than sorry..
66 74
 	fields := []string{
67
-		"Id",
75
+		"ID",
68 76
 		"Names",
69 77
 		"Image",
70 78
 		"Command",
... ...
@@ -78,7 +88,7 @@ func (s *DockerSuite) TestContainerAPIGetJSONNoFieldsOmitted(c *check.C) {
78 78
 	// decoding into types.Container do not work since it eventually unmarshal
79 79
 	// and empty field to an empty go map, so we just check for a string
80 80
 	for _, f := range fields {
81
-		if !strings.Contains(string(body), f) {
81
+		if !strings.Contains(actual, f) {
82 82
 			c.Fatalf("Field %s is missing and it shouldn't", f)
83 83
 		}
84 84
 	}
... ...
@@ -86,7 +96,7 @@ func (s *DockerSuite) TestContainerAPIGetJSONNoFieldsOmitted(c *check.C) {
86 86
 
87 87
 type containerPs struct {
88 88
 	Names []string
89
-	Ports []map[string]interface{}
89
+	Ports []types.Port
90 90
 }
91 91
 
92 92
 // regression test for non-empty fields from #13901
... ...
@@ -97,30 +107,30 @@ func (s *DockerSuite) TestContainerAPIPsOmitFields(c *check.C) {
97 97
 	port := 80
98 98
 	runSleepingContainer(c, "--name", name, "--expose", strconv.Itoa(port))
99 99
 
100
-	status, body, err := request.SockRequest("GET", "/containers/json?all=1", nil, daemonHost())
100
+	cli, err := client.NewEnvClient()
101 101
 	c.Assert(err, checker.IsNil)
102
-	c.Assert(status, checker.Equals, http.StatusOK)
102
+	defer cli.Close()
103 103
 
104
-	var resp []containerPs
105
-	err = json.Unmarshal(body, &resp)
104
+	options := types.ContainerListOptions{
105
+		All: true,
106
+	}
107
+	containers, err := cli.ContainerList(context.Background(), options)
106 108
 	c.Assert(err, checker.IsNil)
107
-
108
-	var foundContainer *containerPs
109
-	for _, container := range resp {
110
-		for _, testName := range container.Names {
109
+	var foundContainer containerPs
110
+	for _, c := range containers {
111
+		for _, testName := range c.Names {
111 112
 			if "/"+name == testName {
112
-				foundContainer = &container
113
+				foundContainer.Names = c.Names
114
+				foundContainer.Ports = c.Ports
113 115
 				break
114 116
 			}
115 117
 		}
116 118
 	}
117 119
 
118 120
 	c.Assert(foundContainer.Ports, checker.HasLen, 1)
119
-	c.Assert(foundContainer.Ports[0]["PrivatePort"], checker.Equals, float64(port))
120
-	_, ok := foundContainer.Ports[0]["PublicPort"]
121
-	c.Assert(ok, checker.Not(checker.Equals), true)
122
-	_, ok = foundContainer.Ports[0]["IP"]
123
-	c.Assert(ok, checker.Not(checker.Equals), true)
121
+	c.Assert(foundContainer.Ports[0].PrivatePort, checker.Equals, uint16(port))
122
+	c.Assert(foundContainer.Ports[0].PublicPort, checker.NotNil)
123
+	c.Assert(foundContainer.Ports[0].IP, checker.NotNil)
124 124
 }
125 125
 
126 126
 func (s *DockerSuite) TestContainerAPIGetExport(c *check.C) {
... ...
@@ -129,12 +139,15 @@ func (s *DockerSuite) TestContainerAPIGetExport(c *check.C) {
129 129
 	name := "exportcontainer"
130 130
 	dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test")
131 131
 
132
-	status, body, err := request.SockRequest("GET", "/containers/"+name+"/export", nil, daemonHost())
132
+	cli, err := client.NewEnvClient()
133 133
 	c.Assert(err, checker.IsNil)
134
-	c.Assert(status, checker.Equals, http.StatusOK)
134
+	defer cli.Close()
135 135
 
136
+	body, err := cli.ContainerExport(context.Background(), name)
137
+	c.Assert(err, checker.IsNil)
138
+	defer body.Close()
136 139
 	found := false
137
-	for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
140
+	for tarReader := tar.NewReader(body); ; {
138 141
 		h, err := tarReader.Next()
139 142
 		if err != nil && err == io.EOF {
140 143
 			break
... ...
@@ -153,15 +166,12 @@ func (s *DockerSuite) TestContainerAPIGetChanges(c *check.C) {
153 153
 	name := "changescontainer"
154 154
 	dockerCmd(c, "run", "--name", name, "busybox", "rm", "/etc/passwd")
155 155
 
156
-	status, body, err := request.SockRequest("GET", "/containers/"+name+"/changes", nil, daemonHost())
156
+	cli, err := client.NewEnvClient()
157 157
 	c.Assert(err, checker.IsNil)
158
-	c.Assert(status, checker.Equals, http.StatusOK)
158
+	defer cli.Close()
159 159
 
160
-	changes := []struct {
161
-		Kind int
162
-		Path string
163
-	}{}
164
-	c.Assert(json.Unmarshal(body, &changes), checker.IsNil, check.Commentf("unable to unmarshal response body"))
160
+	changes, err := cli.ContainerDiff(context.Background(), name)
161
+	c.Assert(err, checker.IsNil)
165 162
 
166 163
 	// Check the changelog for removal of /etc/passwd
167 164
 	success := false
... ...
@@ -180,14 +190,19 @@ func (s *DockerSuite) TestGetContainerStats(c *check.C) {
180 180
 	runSleepingContainer(c, "--name", name)
181 181
 
182 182
 	type b struct {
183
-		status int
184
-		body   []byte
185
-		err    error
183
+		stats types.ContainerStats
184
+		err   error
186 185
 	}
186
+
187 187
 	bc := make(chan b, 1)
188 188
 	go func() {
189
-		status, body, err := request.SockRequest("GET", "/containers/"+name+"/stats", nil, daemonHost())
190
-		bc <- b{status, body, err}
189
+		cli, err := client.NewEnvClient()
190
+		c.Assert(err, checker.IsNil)
191
+		defer cli.Close()
192
+
193
+		stats, err := cli.ContainerStats(context.Background(), name, true)
194
+		c.Assert(err, checker.IsNil)
195
+		bc <- b{stats, err}
191 196
 	}()
192 197
 
193 198
 	// allow some time to stream the stats from the container
... ...
@@ -200,10 +215,8 @@ func (s *DockerSuite) TestGetContainerStats(c *check.C) {
200 200
 	case <-time.After(2 * time.Second):
201 201
 		c.Fatal("stream was not closed after container was removed")
202 202
 	case sr := <-bc:
203
-		c.Assert(sr.err, checker.IsNil)
204
-		c.Assert(sr.status, checker.Equals, http.StatusOK)
205
-
206
-		dec := json.NewDecoder(bytes.NewBuffer(sr.body))
203
+		dec := json.NewDecoder(sr.stats.Body)
204
+		defer sr.stats.Body.Close()
207 205
 		var s *types.Stats
208 206
 		// decode only one object from the stream
209 207
 		c.Assert(dec.Decode(&s), checker.IsNil)
... ...
@@ -217,13 +230,17 @@ func (s *DockerSuite) TestGetContainerStatsRmRunning(c *check.C) {
217 217
 	buf := &ChannelBuffer{C: make(chan []byte, 1)}
218 218
 	defer buf.Close()
219 219
 
220
-	_, body, err := request.Get("/containers/"+id+"/stats?stream=1", request.JSON)
220
+	cli, err := client.NewEnvClient()
221 221
 	c.Assert(err, checker.IsNil)
222
-	defer body.Close()
222
+	defer cli.Close()
223
+
224
+	stats, err := cli.ContainerStats(context.Background(), id, true)
225
+	c.Assert(err, checker.IsNil)
226
+	defer stats.Body.Close()
223 227
 
224 228
 	chErr := make(chan error, 1)
225 229
 	go func() {
226
-		_, err = io.Copy(buf, body)
230
+		_, err = io.Copy(buf, stats.Body)
227 231
 		chErr <- err
228 232
 	}()
229 233
 
... ...
@@ -278,14 +295,19 @@ func (s *DockerSuite) TestGetContainerStatsStream(c *check.C) {
278 278
 	runSleepingContainer(c, "--name", name)
279 279
 
280 280
 	type b struct {
281
-		status int
282
-		body   io.ReadCloser
283
-		err    error
281
+		stats types.ContainerStats
282
+		err   error
284 283
 	}
284
+
285 285
 	bc := make(chan b, 1)
286 286
 	go func() {
287
-		status, body, err := request.Get("/containers/" + name + "/stats")
288
-		bc <- b{status.StatusCode, body, err}
287
+		cli, err := client.NewEnvClient()
288
+		c.Assert(err, checker.IsNil)
289
+		defer cli.Close()
290
+
291
+		stats, err := cli.ContainerStats(context.Background(), name, true)
292
+		c.Assert(err, checker.IsNil)
293
+		bc <- b{stats, err}
289 294
 	}()
290 295
 
291 296
 	// allow some time to stream the stats from the container
... ...
@@ -298,10 +320,8 @@ func (s *DockerSuite) TestGetContainerStatsStream(c *check.C) {
298 298
 	case <-time.After(2 * time.Second):
299 299
 		c.Fatal("stream was not closed after container was removed")
300 300
 	case sr := <-bc:
301
-		c.Assert(sr.err, checker.IsNil)
302
-		c.Assert(sr.status, checker.Equals, http.StatusOK)
303
-
304
-		b, err := ioutil.ReadAll(sr.body)
301
+		b, err := ioutil.ReadAll(sr.stats.Body)
302
+		defer sr.stats.Body.Close()
305 303
 		c.Assert(err, checker.IsNil)
306 304
 		s := string(b)
307 305
 		// count occurrences of "read" of types.Stats
... ...
@@ -316,14 +336,20 @@ func (s *DockerSuite) TestGetContainerStatsNoStream(c *check.C) {
316 316
 	runSleepingContainer(c, "--name", name)
317 317
 
318 318
 	type b struct {
319
-		status int
320
-		body   []byte
321
-		err    error
319
+		stats types.ContainerStats
320
+		err   error
322 321
 	}
322
+
323 323
 	bc := make(chan b, 1)
324
+
324 325
 	go func() {
325
-		status, body, err := request.SockRequest("GET", "/containers/"+name+"/stats?stream=0", nil, daemonHost())
326
-		bc <- b{status, body, err}
326
+		cli, err := client.NewEnvClient()
327
+		c.Assert(err, checker.IsNil)
328
+		defer cli.Close()
329
+
330
+		stats, err := cli.ContainerStats(context.Background(), name, false)
331
+		c.Assert(err, checker.IsNil)
332
+		bc <- b{stats, err}
327 333
 	}()
328 334
 
329 335
 	// allow some time to stream the stats from the container
... ...
@@ -336,10 +362,10 @@ func (s *DockerSuite) TestGetContainerStatsNoStream(c *check.C) {
336 336
 	case <-time.After(2 * time.Second):
337 337
 		c.Fatal("stream was not closed after container was removed")
338 338
 	case sr := <-bc:
339
-		c.Assert(sr.err, checker.IsNil)
340
-		c.Assert(sr.status, checker.Equals, http.StatusOK)
341
-
342
-		s := string(sr.body)
339
+		b, err := ioutil.ReadAll(sr.stats.Body)
340
+		defer sr.stats.Body.Close()
341
+		c.Assert(err, checker.IsNil)
342
+		s := string(b)
343 343
 		// count occurrences of `"read"` of types.Stats
344 344
 		c.Assert(strings.Count(s, `"read"`), checker.Equals, 1, check.Commentf("Expected only one stat streamed, got %d", strings.Count(s, `"read"`)))
345 345
 	}
... ...
@@ -349,24 +375,23 @@ func (s *DockerSuite) TestGetStoppedContainerStats(c *check.C) {
349 349
 	name := "statscontainer"
350 350
 	dockerCmd(c, "create", "--name", name, "busybox", "ps")
351 351
 
352
-	type stats struct {
353
-		status int
354
-		err    error
355
-	}
356
-	chResp := make(chan stats)
352
+	chResp := make(chan error)
357 353
 
358 354
 	// We expect an immediate response, but if it's not immediate, the test would hang, so put it in a goroutine
359 355
 	// below we'll check this on a timeout.
360 356
 	go func() {
361
-		resp, body, err := request.Get("/containers/" + name + "/stats")
362
-		body.Close()
363
-		chResp <- stats{resp.StatusCode, err}
357
+		cli, err := client.NewEnvClient()
358
+		c.Assert(err, checker.IsNil)
359
+		defer cli.Close()
360
+
361
+		resp, err := cli.ContainerStats(context.Background(), name, false)
362
+		defer resp.Body.Close()
363
+		chResp <- err
364 364
 	}()
365 365
 
366 366
 	select {
367
-	case r := <-chResp:
368
-		c.Assert(r.err, checker.IsNil)
369
-		c.Assert(r.status, checker.Equals, http.StatusOK)
367
+	case err := <-chResp:
368
+		c.Assert(err, checker.IsNil)
370 369
 	case <-time.After(10 * time.Second):
371 370
 		c.Fatal("timeout waiting for stats response for stopped container")
372 371
 	}
... ...
@@ -383,9 +408,12 @@ func (s *DockerSuite) TestContainerAPIPause(c *check.C) {
383 383
 	out := cli.DockerCmd(c, "run", "-d", "busybox", "sleep", "30").Combined()
384 384
 	ContainerID := strings.TrimSpace(out)
385 385
 
386
-	resp, _, err := request.Post("/containers/" + ContainerID + "/pause")
386
+	cli, err := client.NewEnvClient()
387
+	c.Assert(err, checker.IsNil)
388
+	defer cli.Close()
389
+
390
+	err = cli.ContainerPause(context.Background(), ContainerID)
387 391
 	c.Assert(err, checker.IsNil)
388
-	c.Assert(resp.StatusCode, checker.Equals, http.StatusNoContent)
389 392
 
390 393
 	pausedContainers := getPaused(c)
391 394
 
... ...
@@ -393,9 +421,8 @@ func (s *DockerSuite) TestContainerAPIPause(c *check.C) {
393 393
 		c.Fatalf("there should be one paused container and not %d", len(pausedContainers))
394 394
 	}
395 395
 
396
-	resp, _, err = request.Post("/containers/" + ContainerID + "/unpause")
396
+	err = cli.ContainerUnpause(context.Background(), ContainerID)
397 397
 	c.Assert(err, checker.IsNil)
398
-	c.Assert(resp.StatusCode, checker.Equals, http.StatusNoContent)
399 398
 
400 399
 	pausedContainers = getPaused(c)
401 400
 	c.Assert(pausedContainers, checker.HasLen, 0, check.Commentf("There should be no paused container."))
... ...
@@ -407,15 +434,12 @@ func (s *DockerSuite) TestContainerAPITop(c *check.C) {
407 407
 	id := strings.TrimSpace(string(out))
408 408
 	c.Assert(waitRun(id), checker.IsNil)
409 409
 
410
-	type topResp struct {
411
-		Titles    []string
412
-		Processes [][]string
413
-	}
414
-	var top topResp
415
-	status, b, err := request.SockRequest("GET", "/containers/"+id+"/top?ps_args=aux", nil, daemonHost())
410
+	cli, err := client.NewEnvClient()
411
+	c.Assert(err, checker.IsNil)
412
+	defer cli.Close()
413
+
414
+	top, err := cli.ContainerTop(context.Background(), id, []string{"aux"})
416 415
 	c.Assert(err, checker.IsNil)
417
-	c.Assert(status, checker.Equals, http.StatusOK)
418
-	c.Assert(json.Unmarshal(b, &top), checker.IsNil)
419 416
 	c.Assert(top.Titles, checker.HasLen, 11, check.Commentf("expected 11 titles, found %d: %v", len(top.Titles), top.Titles))
420 417
 
421 418
 	if top.Titles[0] != "USER" || top.Titles[10] != "COMMAND" {
... ...
@@ -432,15 +456,12 @@ func (s *DockerSuite) TestContainerAPITopWindows(c *check.C) {
432 432
 	id := strings.TrimSpace(string(out))
433 433
 	c.Assert(waitRun(id), checker.IsNil)
434 434
 
435
-	type topResp struct {
436
-		Titles    []string
437
-		Processes [][]string
438
-	}
439
-	var top topResp
440
-	status, b, err := request.SockRequest("GET", "/containers/"+id+"/top", nil, daemonHost())
435
+	cli, err := client.NewEnvClient()
436
+	c.Assert(err, checker.IsNil)
437
+	defer cli.Close()
438
+
439
+	top, err := cli.ContainerTop(context.Background(), id, nil)
441 440
 	c.Assert(err, checker.IsNil)
442
-	c.Assert(status, checker.Equals, http.StatusOK)
443
-	c.Assert(json.Unmarshal(b, &top), checker.IsNil)
444 441
 	c.Assert(top.Titles, checker.HasLen, 4, check.Commentf("expected 4 titles, found %d: %v", len(top.Titles), top.Titles))
445 442
 
446 443
 	if top.Titles[0] != "Name" || top.Titles[3] != "Private Working Set" {
... ...
@@ -464,16 +485,16 @@ func (s *DockerSuite) TestContainerAPICommit(c *check.C) {
464 464
 	cName := "testapicommit"
465 465
 	dockerCmd(c, "run", "--name="+cName, "busybox", "/bin/sh", "-c", "touch /test")
466 466
 
467
-	name := "testcontainerapicommit"
468
-	status, b, err := request.SockRequest("POST", "/commit?repo="+name+"&testtag=tag&container="+cName, nil, daemonHost())
467
+	cli, err := client.NewEnvClient()
469 468
 	c.Assert(err, checker.IsNil)
470
-	c.Assert(status, checker.Equals, http.StatusCreated)
469
+	defer cli.Close()
471 470
 
472
-	type resp struct {
473
-		ID string
471
+	options := types.ContainerCommitOptions{
472
+		Reference: "testcontainerapicommit:testtag",
474 473
 	}
475
-	var img resp
476
-	c.Assert(json.Unmarshal(b, &img), checker.IsNil)
474
+
475
+	img, err := cli.ContainerCommit(context.Background(), cName, options)
476
+	c.Assert(err, checker.IsNil)
477 477
 
478 478
 	cmd := inspectField(c, img.ID, "Config.Cmd")
479 479
 	c.Assert(cmd, checker.Equals, "[/bin/sh -c touch /test]", check.Commentf("got wrong Cmd from commit: %q", cmd))
... ...
@@ -486,20 +507,20 @@ func (s *DockerSuite) TestContainerAPICommitWithLabelInConfig(c *check.C) {
486 486
 	cName := "testapicommitwithconfig"
487 487
 	dockerCmd(c, "run", "--name="+cName, "busybox", "/bin/sh", "-c", "touch /test")
488 488
 
489
-	config := map[string]interface{}{
490
-		"Labels": map[string]string{"key1": "value1", "key2": "value2"},
491
-	}
492
-
493
-	name := "testcontainerapicommitwithconfig"
494
-	status, b, err := request.SockRequest("POST", "/commit?repo="+name+"&container="+cName, config, daemonHost())
489
+	cli, err := client.NewEnvClient()
495 490
 	c.Assert(err, checker.IsNil)
496
-	c.Assert(status, checker.Equals, http.StatusCreated)
491
+	defer cli.Close()
497 492
 
498
-	type resp struct {
499
-		ID string
493
+	config := containertypes.Config{
494
+		Labels: map[string]string{"key1": "value1", "key2": "value2"}}
495
+
496
+	options := types.ContainerCommitOptions{
497
+		Reference: "testcontainerapicommitwithconfig",
498
+		Config:    &config,
500 499
 	}
501
-	var img resp
502
-	c.Assert(json.Unmarshal(b, &img), checker.IsNil)
500
+
501
+	img, err := cli.ContainerCommit(context.Background(), cName, options)
502
+	c.Assert(err, checker.IsNil)
503 503
 
504 504
 	label1 := inspectFieldMap(c, img.ID, "Config.Labels", "key1")
505 505
 	c.Assert(label1, checker.Equals, "value1")
... ...
@@ -517,76 +538,79 @@ func (s *DockerSuite) TestContainerAPICommitWithLabelInConfig(c *check.C) {
517 517
 func (s *DockerSuite) TestContainerAPIBadPort(c *check.C) {
518 518
 	// TODO Windows to Windows CI - Port this test
519 519
 	testRequires(c, DaemonIsLinux)
520
-	config := map[string]interface{}{
521
-		"Image": "busybox",
522
-		"Cmd":   []string{"/bin/sh", "-c", "echo test"},
523
-		"PortBindings": map[string]interface{}{
524
-			"8080/tcp": []map[string]interface{}{
520
+
521
+	config := containertypes.Config{
522
+		Image: "busybox",
523
+		Cmd:   []string{"/bin/sh", "-c", "echo test"},
524
+	}
525
+
526
+	hostConfig := containertypes.HostConfig{
527
+		PortBindings: nat.PortMap{
528
+			"8080/tcp": []nat.PortBinding{
525 529
 				{
526
-					"HostIP":   "",
527
-					"HostPort": "aa80",
528
-				},
530
+					HostIP:   "",
531
+					HostPort: "aa80"},
529 532
 			},
530 533
 		},
531 534
 	}
532 535
 
533
-	jsonData := bytes.NewBuffer(nil)
534
-	json.NewEncoder(jsonData).Encode(config)
535
-
536
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
536
+	cli, err := client.NewEnvClient()
537 537
 	c.Assert(err, checker.IsNil)
538
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
539
-	c.Assert(getErrorMessage(c, body), checker.Equals, `invalid port specification: "aa80"`, check.Commentf("Incorrect error msg: %s", body))
538
+	defer cli.Close()
539
+
540
+	_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
541
+	c.Assert(err.Error(), checker.Contains, `invalid port specification: "aa80"`)
540 542
 }
541 543
 
542 544
 func (s *DockerSuite) TestContainerAPICreate(c *check.C) {
543
-	config := map[string]interface{}{
544
-		"Image": "busybox",
545
-		"Cmd":   []string{"/bin/sh", "-c", "touch /test && ls /test"},
545
+	config := containertypes.Config{
546
+		Image: "busybox",
547
+		Cmd:   []string{"/bin/sh", "-c", "touch /test && ls /test"},
546 548
 	}
547 549
 
548
-	status, b, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
550
+	cli, err := client.NewEnvClient()
549 551
 	c.Assert(err, checker.IsNil)
550
-	c.Assert(status, checker.Equals, http.StatusCreated)
552
+	defer cli.Close()
551 553
 
552
-	type createResp struct {
553
-		ID string
554
-	}
555
-	var container createResp
556
-	c.Assert(json.Unmarshal(b, &container), checker.IsNil)
554
+	container, err := cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
555
+	c.Assert(err, checker.IsNil)
557 556
 
558 557
 	out, _ := dockerCmd(c, "start", "-a", container.ID)
559 558
 	c.Assert(strings.TrimSpace(out), checker.Equals, "/test")
560 559
 }
561 560
 
562 561
 func (s *DockerSuite) TestContainerAPICreateEmptyConfig(c *check.C) {
563
-	config := map[string]interface{}{}
564 562
 
565
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
563
+	cli, err := client.NewEnvClient()
566 564
 	c.Assert(err, checker.IsNil)
567
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
565
+	defer cli.Close()
568 566
 
569
-	expected := "Config cannot be empty in order to create a container"
570
-	c.Assert(getErrorMessage(c, body), checker.Equals, expected)
567
+	_, err = cli.ContainerCreate(context.Background(), &containertypes.Config{}, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
568
+
569
+	expected := "No command specified"
570
+	c.Assert(err.Error(), checker.Contains, expected)
571 571
 }
572 572
 
573 573
 func (s *DockerSuite) TestContainerAPICreateMultipleNetworksConfig(c *check.C) {
574 574
 	// Container creation must fail if client specified configurations for more than one network
575
-	config := map[string]interface{}{
576
-		"Image": "busybox",
577
-		"NetworkingConfig": networktypes.NetworkingConfig{
578
-			EndpointsConfig: map[string]*networktypes.EndpointSettings{
579
-				"net1": {},
580
-				"net2": {},
581
-				"net3": {},
582
-			},
575
+	config := containertypes.Config{
576
+		Image: "busybox",
577
+	}
578
+
579
+	networkingConfig := networktypes.NetworkingConfig{
580
+		EndpointsConfig: map[string]*networktypes.EndpointSettings{
581
+			"net1": {},
582
+			"net2": {},
583
+			"net3": {},
583 584
 		},
584 585
 	}
585 586
 
586
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
587
+	cli, err := client.NewEnvClient()
587 588
 	c.Assert(err, checker.IsNil)
588
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
589
-	msg := getErrorMessage(c, body)
589
+	defer cli.Close()
590
+
591
+	_, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networkingConfig, "")
592
+	msg := err.Error()
590 593
 	// network name order in error message is not deterministic
591 594
 	c.Assert(msg, checker.Contains, "Container cannot be connected to network endpoints")
592 595
 	c.Assert(msg, checker.Contains, "net1")
... ...
@@ -597,25 +621,22 @@ func (s *DockerSuite) TestContainerAPICreateMultipleNetworksConfig(c *check.C) {
597 597
 func (s *DockerSuite) TestContainerAPICreateWithHostName(c *check.C) {
598 598
 	domainName := "test-domain"
599 599
 	hostName := "test-hostname"
600
-	config := map[string]interface{}{
601
-		"Image":      "busybox",
602
-		"Hostname":   hostName,
603
-		"Domainname": domainName,
600
+	config := containertypes.Config{
601
+		Image:      "busybox",
602
+		Hostname:   hostName,
603
+		Domainname: domainName,
604 604
 	}
605 605
 
606
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
606
+	cli, err := client.NewEnvClient()
607 607
 	c.Assert(err, checker.IsNil)
608
-	c.Assert(status, checker.Equals, http.StatusCreated)
608
+	defer cli.Close()
609 609
 
610
-	var container containertypes.ContainerCreateCreatedBody
611
-	c.Assert(json.Unmarshal(body, &container), checker.IsNil)
610
+	container, err := cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
611
+	c.Assert(err, checker.IsNil)
612 612
 
613
-	status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost())
613
+	containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
614 614
 	c.Assert(err, checker.IsNil)
615
-	c.Assert(status, checker.Equals, http.StatusOK)
616 615
 
617
-	var containerJSON types.ContainerJSON
618
-	c.Assert(json.Unmarshal(body, &containerJSON), checker.IsNil)
619 616
 	c.Assert(containerJSON.Config.Hostname, checker.Equals, hostName, check.Commentf("Mismatched Hostname"))
620 617
 	c.Assert(containerJSON.Config.Domainname, checker.Equals, domainName, check.Commentf("Mismatched Domainname"))
621 618
 }
... ...
@@ -633,51 +654,51 @@ func (s *DockerSuite) TestContainerAPICreateOtherNetworkModes(c *check.C) {
633 633
 	UtilCreateNetworkMode(c, "container:web1")
634 634
 }
635 635
 
636
-func UtilCreateNetworkMode(c *check.C, networkMode string) {
637
-	config := map[string]interface{}{
638
-		"Image":      "busybox",
639
-		"HostConfig": map[string]interface{}{"NetworkMode": networkMode},
636
+func UtilCreateNetworkMode(c *check.C, networkMode containertypes.NetworkMode) {
637
+	config := containertypes.Config{
638
+		Image: "busybox",
640 639
 	}
641 640
 
642
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
641
+	hostConfig := containertypes.HostConfig{
642
+		NetworkMode: networkMode,
643
+	}
644
+
645
+	cli, err := client.NewEnvClient()
643 646
 	c.Assert(err, checker.IsNil)
644
-	c.Assert(status, checker.Equals, http.StatusCreated)
647
+	defer cli.Close()
645 648
 
646
-	var container containertypes.ContainerCreateCreatedBody
647
-	c.Assert(json.Unmarshal(body, &container), checker.IsNil)
649
+	container, err := cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
650
+	c.Assert(err, checker.IsNil)
648 651
 
649
-	status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost())
652
+	containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
650 653
 	c.Assert(err, checker.IsNil)
651
-	c.Assert(status, checker.Equals, http.StatusOK)
652 654
 
653
-	var containerJSON types.ContainerJSON
654
-	c.Assert(json.Unmarshal(body, &containerJSON), checker.IsNil)
655 655
 	c.Assert(containerJSON.HostConfig.NetworkMode, checker.Equals, containertypes.NetworkMode(networkMode), check.Commentf("Mismatched NetworkMode"))
656 656
 }
657 657
 
658 658
 func (s *DockerSuite) TestContainerAPICreateWithCpuSharesCpuset(c *check.C) {
659 659
 	// TODO Windows to Windows CI. The CpuShares part could be ported.
660 660
 	testRequires(c, DaemonIsLinux)
661
-	config := map[string]interface{}{
662
-		"Image":      "busybox",
663
-		"CpuShares":  512,
664
-		"CpusetCpus": "0",
661
+	config := containertypes.Config{
662
+		Image: "busybox",
665 663
 	}
666 664
 
667
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
668
-	c.Assert(err, checker.IsNil)
669
-	c.Assert(status, checker.Equals, http.StatusCreated)
670
-
671
-	var container containertypes.ContainerCreateCreatedBody
672
-	c.Assert(json.Unmarshal(body, &container), checker.IsNil)
665
+	hostConfig := containertypes.HostConfig{
666
+		Resources: containertypes.Resources{
667
+			CPUShares:  512,
668
+			CpusetCpus: "0",
669
+		},
670
+	}
673 671
 
674
-	status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost())
672
+	cli, err := client.NewEnvClient()
675 673
 	c.Assert(err, checker.IsNil)
676
-	c.Assert(status, checker.Equals, http.StatusOK)
674
+	defer cli.Close()
677 675
 
678
-	var containerJSON types.ContainerJSON
676
+	container, err := cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
677
+	c.Assert(err, checker.IsNil)
679 678
 
680
-	c.Assert(json.Unmarshal(body, &containerJSON), checker.IsNil)
679
+	containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
680
+	c.Assert(err, checker.IsNil)
681 681
 
682 682
 	out := inspectField(c, containerJSON.ID, "HostConfig.CpuShares")
683 683
 	c.Assert(out, checker.Equals, "512")
... ...
@@ -886,10 +907,13 @@ func (s *DockerSuite) TestContainerAPIRename(c *check.C) {
886 886
 
887 887
 	containerID := strings.TrimSpace(out)
888 888
 	newName := "TestContainerAPIRenameNew"
889
-	statusCode, _, err := request.SockRequest("POST", "/containers/"+containerID+"/rename?name="+newName, nil, daemonHost())
889
+
890
+	cli, err := client.NewEnvClient()
891
+	c.Assert(err, checker.IsNil)
892
+	defer cli.Close()
893
+
894
+	err = cli.ContainerRename(context.Background(), containerID, newName)
890 895
 	c.Assert(err, checker.IsNil)
891
-	// 204 No Content is expected, not 200
892
-	c.Assert(statusCode, checker.Equals, http.StatusNoContent)
893 896
 
894 897
 	name := inspectField(c, containerID, "Name")
895 898
 	c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container"))
... ...
@@ -899,9 +923,12 @@ func (s *DockerSuite) TestContainerAPIKill(c *check.C) {
899 899
 	name := "test-api-kill"
900 900
 	runSleepingContainer(c, "-i", "--name", name)
901 901
 
902
-	status, _, err := request.SockRequest("POST", "/containers/"+name+"/kill", nil, daemonHost())
902
+	cli, err := client.NewEnvClient()
903
+	c.Assert(err, checker.IsNil)
904
+	defer cli.Close()
905
+
906
+	err = cli.ContainerKill(context.Background(), name, "SIGKILL")
903 907
 	c.Assert(err, checker.IsNil)
904
-	c.Assert(status, checker.Equals, http.StatusNoContent)
905 908
 
906 909
 	state := inspectField(c, name, "State.Running")
907 910
 	c.Assert(state, checker.Equals, "false", check.Commentf("got wrong State from container %s: %q", name, state))
... ...
@@ -910,10 +937,14 @@ func (s *DockerSuite) TestContainerAPIKill(c *check.C) {
910 910
 func (s *DockerSuite) TestContainerAPIRestart(c *check.C) {
911 911
 	name := "test-api-restart"
912 912
 	runSleepingContainer(c, "-di", "--name", name)
913
+	cli, err := client.NewEnvClient()
914
+	c.Assert(err, checker.IsNil)
915
+	defer cli.Close()
913 916
 
914
-	status, _, err := request.SockRequest("POST", "/containers/"+name+"/restart?t=1", nil, daemonHost())
917
+	timeout := 1 * time.Second
918
+	err = cli.ContainerRestart(context.Background(), name, &timeout)
915 919
 	c.Assert(err, checker.IsNil)
916
-	c.Assert(status, checker.Equals, http.StatusNoContent)
920
+
917 921
 	c.Assert(waitInspect(name, "{{ .State.Restarting  }} {{ .State.Running  }}", "false true", 15*time.Second), checker.IsNil)
918 922
 }
919 923
 
... ...
@@ -923,51 +954,59 @@ func (s *DockerSuite) TestContainerAPIRestartNotimeoutParam(c *check.C) {
923 923
 	id := strings.TrimSpace(out)
924 924
 	c.Assert(waitRun(id), checker.IsNil)
925 925
 
926
-	status, _, err := request.SockRequest("POST", "/containers/"+name+"/restart", nil, daemonHost())
926
+	cli, err := client.NewEnvClient()
927
+	c.Assert(err, checker.IsNil)
928
+	defer cli.Close()
929
+
930
+	err = cli.ContainerRestart(context.Background(), name, nil)
927 931
 	c.Assert(err, checker.IsNil)
928
-	c.Assert(status, checker.Equals, http.StatusNoContent)
932
+
929 933
 	c.Assert(waitInspect(name, "{{ .State.Restarting  }} {{ .State.Running  }}", "false true", 15*time.Second), checker.IsNil)
930 934
 }
931 935
 
932 936
 func (s *DockerSuite) TestContainerAPIStart(c *check.C) {
933 937
 	name := "testing-start"
934
-	config := map[string]interface{}{
935
-		"Image":     "busybox",
936
-		"Cmd":       append([]string{"/bin/sh", "-c"}, sleepCommandForDaemonPlatform()...),
937
-		"OpenStdin": true,
938
+	config := containertypes.Config{
939
+		Image:     "busybox",
940
+		Cmd:       append([]string{"/bin/sh", "-c"}, sleepCommandForDaemonPlatform()...),
941
+		OpenStdin: true,
938 942
 	}
939 943
 
940
-	status, _, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
944
+	cli, err := client.NewEnvClient()
945
+	c.Assert(err, checker.IsNil)
946
+	defer cli.Close()
947
+
948
+	_, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, name)
941 949
 	c.Assert(err, checker.IsNil)
942
-	c.Assert(status, checker.Equals, http.StatusCreated)
943 950
 
944
-	status, _, err = request.SockRequest("POST", "/containers/"+name+"/start", nil, daemonHost())
951
+	err = cli.ContainerStart(context.Background(), name, types.ContainerStartOptions{})
945 952
 	c.Assert(err, checker.IsNil)
946
-	c.Assert(status, checker.Equals, http.StatusNoContent)
947 953
 
948 954
 	// second call to start should give 304
949
-	status, _, err = request.SockRequest("POST", "/containers/"+name+"/start", nil, daemonHost())
955
+	// maybe add ContainerStartWithRaw to test it
956
+	err = cli.ContainerStart(context.Background(), name, types.ContainerStartOptions{})
950 957
 	c.Assert(err, checker.IsNil)
951 958
 
952 959
 	// TODO(tibor): figure out why this doesn't work on windows
953
-	if testEnv.LocalDaemon() {
954
-		c.Assert(status, checker.Equals, http.StatusNotModified)
955
-	}
956 960
 }
957 961
 
958 962
 func (s *DockerSuite) TestContainerAPIStop(c *check.C) {
959 963
 	name := "test-api-stop"
960 964
 	runSleepingContainer(c, "-i", "--name", name)
965
+	timeout := 30 * time.Second
961 966
 
962
-	status, _, err := request.SockRequest("POST", "/containers/"+name+"/stop?t=30", nil, daemonHost())
967
+	cli, err := client.NewEnvClient()
968
+	c.Assert(err, checker.IsNil)
969
+	defer cli.Close()
970
+
971
+	err = cli.ContainerStop(context.Background(), name, &timeout)
963 972
 	c.Assert(err, checker.IsNil)
964
-	c.Assert(status, checker.Equals, http.StatusNoContent)
965 973
 	c.Assert(waitInspect(name, "{{ .State.Running  }}", "false", 60*time.Second), checker.IsNil)
966 974
 
967 975
 	// second call to start should give 304
968
-	status, _, err = request.SockRequest("POST", "/containers/"+name+"/stop?t=30", nil, daemonHost())
976
+	// maybe add ContainerStartWithRaw to test it
977
+	err = cli.ContainerStop(context.Background(), name, &timeout)
969 978
 	c.Assert(err, checker.IsNil)
970
-	c.Assert(status, checker.Equals, http.StatusNotModified)
971 979
 }
972 980
 
973 981
 func (s *DockerSuite) TestContainerAPIWait(c *check.C) {
... ...
@@ -979,14 +1018,18 @@ func (s *DockerSuite) TestContainerAPIWait(c *check.C) {
979 979
 	}
980 980
 	dockerCmd(c, "run", "--name", name, "busybox", sleepCmd, "2")
981 981
 
982
-	status, body, err := request.SockRequest("POST", "/containers/"+name+"/wait", nil, daemonHost())
982
+	cli, err := client.NewEnvClient()
983 983
 	c.Assert(err, checker.IsNil)
984
-	c.Assert(status, checker.Equals, http.StatusOK)
985
-	c.Assert(waitInspect(name, "{{ .State.Running  }}", "false", 60*time.Second), checker.IsNil)
984
+	defer cli.Close()
986 985
 
987
-	var waitres containertypes.ContainerWaitOKBody
988
-	c.Assert(json.Unmarshal(body, &waitres), checker.IsNil)
989
-	c.Assert(waitres.StatusCode, checker.Equals, int64(0))
986
+	waitresC, errC := cli.ContainerWait(context.Background(), name, "")
987
+
988
+	select {
989
+	case err = <-errC:
990
+		c.Assert(err, checker.IsNil)
991
+	case waitres := <-waitresC:
992
+		c.Assert(waitres.StatusCode, checker.Equals, int64(0))
993
+	}
990 994
 }
991 995
 
992 996
 func (s *DockerSuite) TestContainerAPICopyNotExistsAnyMore(c *check.C) {
... ...
@@ -996,10 +1039,10 @@ func (s *DockerSuite) TestContainerAPICopyNotExistsAnyMore(c *check.C) {
996 996
 	postData := types.CopyConfig{
997 997
 		Resource: "/test.txt",
998 998
 	}
999
-
1000
-	status, _, err := request.SockRequest("POST", "/containers/"+name+"/copy", postData, daemonHost())
999
+	// no copy in client/
1000
+	res, _, err := request.Post("/containers/"+name+"/copy", request.JSONBody(postData))
1001 1001
 	c.Assert(err, checker.IsNil)
1002
-	c.Assert(status, checker.Equals, http.StatusNotFound)
1002
+	c.Assert(res.StatusCode, checker.Equals, http.StatusNotFound)
1003 1003
 }
1004 1004
 
1005 1005
 func (s *DockerSuite) TestContainerAPICopyPre124(c *check.C) {
... ...
@@ -1011,12 +1054,12 @@ func (s *DockerSuite) TestContainerAPICopyPre124(c *check.C) {
1011 1011
 		Resource: "/test.txt",
1012 1012
 	}
1013 1013
 
1014
-	status, body, err := request.SockRequest("POST", "/v1.23/containers/"+name+"/copy", postData, daemonHost())
1014
+	res, body, err := request.Post("/v1.23/containers/"+name+"/copy", request.JSONBody(postData))
1015 1015
 	c.Assert(err, checker.IsNil)
1016
-	c.Assert(status, checker.Equals, http.StatusOK)
1016
+	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
1017 1017
 
1018 1018
 	found := false
1019
-	for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
1019
+	for tarReader := tar.NewReader(body); ; {
1020 1020
 		h, err := tarReader.Next()
1021 1021
 		if err != nil {
1022 1022
 			if err == io.EOF {
... ...
@@ -1041,10 +1084,12 @@ func (s *DockerSuite) TestContainerAPICopyResourcePathEmptyPre124(c *check.C) {
1041 1041
 		Resource: "",
1042 1042
 	}
1043 1043
 
1044
-	status, body, err := request.SockRequest("POST", "/v1.23/containers/"+name+"/copy", postData, daemonHost())
1044
+	res, body, err := request.Post("/v1.23/containers/"+name+"/copy", request.JSONBody(postData))
1045 1045
 	c.Assert(err, checker.IsNil)
1046
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
1047
-	c.Assert(string(body), checker.Matches, "Path cannot be empty\n")
1046
+	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
1047
+	b, err := request.ReadBody(body)
1048
+	c.Assert(err, checker.IsNil)
1049
+	c.Assert(string(b), checker.Matches, "Path cannot be empty\n")
1048 1050
 }
1049 1051
 
1050 1052
 func (s *DockerSuite) TestContainerAPICopyResourcePathNotFoundPre124(c *check.C) {
... ...
@@ -1056,10 +1101,13 @@ func (s *DockerSuite) TestContainerAPICopyResourcePathNotFoundPre124(c *check.C)
1056 1056
 		Resource: "/notexist",
1057 1057
 	}
1058 1058
 
1059
-	status, body, err := request.SockRequest("POST", "/v1.23/containers/"+name+"/copy", postData, daemonHost())
1059
+	res, body, err := request.Post("/v1.23/containers/"+name+"/copy", request.JSONBody(postData))
1060 1060
 	c.Assert(err, checker.IsNil)
1061
-	c.Assert(status, checker.Equals, http.StatusNotFound)
1062
-	c.Assert(string(body), checker.Matches, "Could not find the file /notexist in container "+name+"\n")
1061
+	c.Assert(res.StatusCode, checker.Equals, http.StatusNotFound)
1062
+
1063
+	b, err := request.ReadBody(body)
1064
+	c.Assert(err, checker.IsNil)
1065
+	c.Assert(string(b), checker.Matches, "Could not find the file /notexist in container "+name+"\n")
1063 1066
 }
1064 1067
 
1065 1068
 func (s *DockerSuite) TestContainerAPICopyContainerNotFoundPr124(c *check.C) {
... ...
@@ -1068,9 +1116,9 @@ func (s *DockerSuite) TestContainerAPICopyContainerNotFoundPr124(c *check.C) {
1068 1068
 		Resource: "/something",
1069 1069
 	}
1070 1070
 
1071
-	status, _, err := request.SockRequest("POST", "/v1.23/containers/notexists/copy", postData, daemonHost())
1071
+	res, _, err := request.Post("/v1.23/containers/notexists/copy", request.JSONBody(postData))
1072 1072
 	c.Assert(err, checker.IsNil)
1073
-	c.Assert(status, checker.Equals, http.StatusNotFound)
1073
+	c.Assert(res.StatusCode, checker.Equals, http.StatusNotFound)
1074 1074
 }
1075 1075
 
1076 1076
 func (s *DockerSuite) TestContainerAPIDelete(c *check.C) {
... ...
@@ -1081,27 +1129,38 @@ func (s *DockerSuite) TestContainerAPIDelete(c *check.C) {
1081 1081
 
1082 1082
 	dockerCmd(c, "stop", id)
1083 1083
 
1084
-	status, _, err := request.SockRequest("DELETE", "/containers/"+id, nil, daemonHost())
1084
+	cli, err := client.NewEnvClient()
1085
+	c.Assert(err, checker.IsNil)
1086
+	defer cli.Close()
1087
+
1088
+	err = cli.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{})
1085 1089
 	c.Assert(err, checker.IsNil)
1086
-	c.Assert(status, checker.Equals, http.StatusNoContent)
1087 1090
 }
1088 1091
 
1089 1092
 func (s *DockerSuite) TestContainerAPIDeleteNotExist(c *check.C) {
1090
-	status, body, err := request.SockRequest("DELETE", "/containers/doesnotexist", nil, daemonHost())
1093
+	cli, err := client.NewEnvClient()
1091 1094
 	c.Assert(err, checker.IsNil)
1092
-	c.Assert(status, checker.Equals, http.StatusNotFound)
1093
-	c.Assert(getErrorMessage(c, body), checker.Matches, "No such container: doesnotexist")
1095
+	defer cli.Close()
1096
+
1097
+	err = cli.ContainerRemove(context.Background(), "doesnotexist", types.ContainerRemoveOptions{})
1098
+	c.Assert(err.Error(), checker.Contains, "No such container: doesnotexist")
1094 1099
 }
1095 1100
 
1096 1101
 func (s *DockerSuite) TestContainerAPIDeleteForce(c *check.C) {
1097 1102
 	out := runSleepingContainer(c)
1098
-
1099 1103
 	id := strings.TrimSpace(out)
1100 1104
 	c.Assert(waitRun(id), checker.IsNil)
1101 1105
 
1102
-	status, _, err := request.SockRequest("DELETE", "/containers/"+id+"?force=1", nil, daemonHost())
1106
+	removeOptions := types.ContainerRemoveOptions{
1107
+		Force: true,
1108
+	}
1109
+
1110
+	cli, err := client.NewEnvClient()
1111
+	c.Assert(err, checker.IsNil)
1112
+	defer cli.Close()
1113
+
1114
+	err = cli.ContainerRemove(context.Background(), id, removeOptions)
1103 1115
 	c.Assert(err, checker.IsNil)
1104
-	c.Assert(status, checker.Equals, http.StatusNoContent)
1105 1116
 }
1106 1117
 
1107 1118
 func (s *DockerSuite) TestContainerAPIDeleteRemoveLinks(c *check.C) {
... ...
@@ -1120,9 +1179,16 @@ func (s *DockerSuite) TestContainerAPIDeleteRemoveLinks(c *check.C) {
1120 1120
 	links := inspectFieldJSON(c, id2, "HostConfig.Links")
1121 1121
 	c.Assert(links, checker.Equals, "[\"/tlink1:/tlink2/tlink1\"]", check.Commentf("expected to have links between containers"))
1122 1122
 
1123
-	status, b, err := request.SockRequest("DELETE", "/containers/tlink2/tlink1?link=1", nil, daemonHost())
1123
+	removeOptions := types.ContainerRemoveOptions{
1124
+		RemoveLinks: true,
1125
+	}
1126
+
1127
+	cli, err := client.NewEnvClient()
1128
+	c.Assert(err, checker.IsNil)
1129
+	defer cli.Close()
1130
+
1131
+	err = cli.ContainerRemove(context.Background(), "tlink2/tlink1", removeOptions)
1124 1132
 	c.Assert(err, check.IsNil)
1125
-	c.Assert(status, check.Equals, http.StatusNoContent, check.Commentf(string(b)))
1126 1133
 
1127 1134
 	linksPostRm := inspectFieldJSON(c, id2, "HostConfig.Links")
1128 1135
 	c.Assert(linksPostRm, checker.Equals, "null", check.Commentf("call to api deleteContainer links should have removed the specified links"))
... ...
@@ -1134,9 +1200,13 @@ func (s *DockerSuite) TestContainerAPIDeleteConflict(c *check.C) {
1134 1134
 	id := strings.TrimSpace(out)
1135 1135
 	c.Assert(waitRun(id), checker.IsNil)
1136 1136
 
1137
-	status, _, err := request.SockRequest("DELETE", "/containers/"+id, nil, daemonHost())
1137
+	cli, err := client.NewEnvClient()
1138 1138
 	c.Assert(err, checker.IsNil)
1139
-	c.Assert(status, checker.Equals, http.StatusConflict)
1139
+	defer cli.Close()
1140
+
1141
+	err = cli.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{})
1142
+	expected := "cannot remove a running container"
1143
+	c.Assert(err.Error(), checker.Contains, expected)
1140 1144
 }
1141 1145
 
1142 1146
 func (s *DockerSuite) TestContainerAPIDeleteRemoveVolume(c *check.C) {
... ...
@@ -1156,9 +1226,18 @@ func (s *DockerSuite) TestContainerAPIDeleteRemoveVolume(c *check.C) {
1156 1156
 	_, err = os.Stat(source)
1157 1157
 	c.Assert(err, checker.IsNil)
1158 1158
 
1159
-	status, _, err := request.SockRequest("DELETE", "/containers/"+id+"?v=1&force=1", nil, daemonHost())
1159
+	removeOptions := types.ContainerRemoveOptions{
1160
+		Force:         true,
1161
+		RemoveVolumes: true,
1162
+	}
1163
+
1164
+	cli, err := client.NewEnvClient()
1160 1165
 	c.Assert(err, checker.IsNil)
1161
-	c.Assert(status, checker.Equals, http.StatusNoContent)
1166
+	defer cli.Close()
1167
+
1168
+	err = cli.ContainerRemove(context.Background(), id, removeOptions)
1169
+	c.Assert(err, check.IsNil)
1170
+
1162 1171
 	_, err = os.Stat(source)
1163 1172
 	c.Assert(os.IsNotExist(err), checker.True, check.Commentf("expected to get ErrNotExist error, got %v", err))
1164 1173
 }
... ...
@@ -1190,31 +1269,38 @@ func (s *DockerSuite) TestContainerAPIPostContainerStop(c *check.C) {
1190 1190
 	containerID := strings.TrimSpace(out)
1191 1191
 	c.Assert(waitRun(containerID), checker.IsNil)
1192 1192
 
1193
-	statusCode, _, err := request.SockRequest("POST", "/containers/"+containerID+"/stop", nil, daemonHost())
1193
+	cli, err := client.NewEnvClient()
1194
+	c.Assert(err, checker.IsNil)
1195
+	defer cli.Close()
1196
+
1197
+	err = cli.ContainerStop(context.Background(), containerID, nil)
1194 1198
 	c.Assert(err, checker.IsNil)
1195
-	// 204 No Content is expected, not 200
1196
-	c.Assert(statusCode, checker.Equals, http.StatusNoContent)
1197 1199
 	c.Assert(waitInspect(containerID, "{{ .State.Running  }}", "false", 60*time.Second), checker.IsNil)
1198 1200
 }
1199 1201
 
1200 1202
 // #14170
1201 1203
 func (s *DockerSuite) TestPostContainerAPICreateWithStringOrSliceEntrypoint(c *check.C) {
1202
-	config := struct {
1203
-		Image      string
1204
-		Entrypoint string
1205
-		Cmd        []string
1206
-	}{"busybox", "echo", []string{"hello", "world"}}
1207
-	_, _, err := request.SockRequest("POST", "/containers/create?name=echotest", config, daemonHost())
1204
+	config := containertypes.Config{
1205
+		Image:      "busybox",
1206
+		Entrypoint: []string{"echo"},
1207
+		Cmd:        []string{"hello", "world"},
1208
+	}
1209
+
1210
+	cli, err := client.NewEnvClient()
1211
+	c.Assert(err, checker.IsNil)
1212
+	defer cli.Close()
1213
+
1214
+	_, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "echotest")
1208 1215
 	c.Assert(err, checker.IsNil)
1209 1216
 	out, _ := dockerCmd(c, "start", "-a", "echotest")
1210 1217
 	c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
1211 1218
 
1212 1219
 	config2 := struct {
1213 1220
 		Image      string
1214
-		Entrypoint []string
1221
+		Entrypoint string
1215 1222
 		Cmd        []string
1216
-	}{"busybox", []string{"echo"}, []string{"hello", "world"}}
1217
-	_, _, err = request.SockRequest("POST", "/containers/create?name=echotest2", config2, daemonHost())
1223
+	}{"busybox", "echo", []string{"hello", "world"}}
1224
+	_, _, err = request.Post("/containers/create?name=echotest2", request.JSONBody(config2))
1218 1225
 	c.Assert(err, checker.IsNil)
1219 1226
 	out, _ = dockerCmd(c, "start", "-a", "echotest2")
1220 1227
 	c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
... ...
@@ -1222,21 +1308,26 @@ func (s *DockerSuite) TestPostContainerAPICreateWithStringOrSliceEntrypoint(c *c
1222 1222
 
1223 1223
 // #14170
1224 1224
 func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCmd(c *check.C) {
1225
-	config := struct {
1226
-		Image      string
1227
-		Entrypoint string
1228
-		Cmd        string
1229
-	}{"busybox", "echo", "hello world"}
1230
-	_, _, err := request.SockRequest("POST", "/containers/create?name=echotest", config, daemonHost())
1225
+	config := containertypes.Config{
1226
+		Image: "busybox",
1227
+		Cmd:   []string{"echo", "hello", "world"},
1228
+	}
1229
+
1230
+	cli, err := client.NewEnvClient()
1231
+	c.Assert(err, checker.IsNil)
1232
+	defer cli.Close()
1233
+
1234
+	_, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "echotest")
1231 1235
 	c.Assert(err, checker.IsNil)
1232 1236
 	out, _ := dockerCmd(c, "start", "-a", "echotest")
1233 1237
 	c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
1234 1238
 
1235 1239
 	config2 := struct {
1236
-		Image string
1237
-		Cmd   []string
1238
-	}{"busybox", []string{"echo", "hello", "world"}}
1239
-	_, _, err = request.SockRequest("POST", "/containers/create?name=echotest2", config2, daemonHost())
1240
+		Image      string
1241
+		Entrypoint string
1242
+		Cmd        string
1243
+	}{"busybox", "echo", "hello world"}
1244
+	_, _, err = request.Post("/containers/create?name=echotest2", request.JSONBody(config2))
1240 1245
 	c.Assert(err, checker.IsNil)
1241 1246
 	out, _ = dockerCmd(c, "start", "-a", "echotest2")
1242 1247
 	c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
... ...
@@ -1251,29 +1342,38 @@ func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCapAddDrop(c *che
1251 1251
 		CapAdd  string
1252 1252
 		CapDrop string
1253 1253
 	}{"busybox", "NET_ADMIN", "SYS_ADMIN"}
1254
-	status, _, err := request.SockRequest("POST", "/containers/create?name=capaddtest0", config, daemonHost())
1254
+	res, _, err := request.Post("/containers/create?name=capaddtest0", request.JSONBody(config))
1255 1255
 	c.Assert(err, checker.IsNil)
1256
-	c.Assert(status, checker.Equals, http.StatusCreated)
1256
+	c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
1257 1257
 
1258
-	config2 := struct {
1259
-		Image   string
1260
-		CapAdd  []string
1261
-		CapDrop []string
1262
-	}{"busybox", []string{"NET_ADMIN", "SYS_ADMIN"}, []string{"SETGID"}}
1263
-	status, _, err = request.SockRequest("POST", "/containers/create?name=capaddtest1", config2, daemonHost())
1258
+	config2 := containertypes.Config{
1259
+		Image: "busybox",
1260
+	}
1261
+	hostConfig := containertypes.HostConfig{
1262
+		CapAdd:  []string{"NET_ADMIN", "SYS_ADMIN"},
1263
+		CapDrop: []string{"SETGID"},
1264
+	}
1265
+
1266
+	cli, err := client.NewEnvClient()
1267
+	c.Assert(err, checker.IsNil)
1268
+	defer cli.Close()
1269
+
1270
+	_, err = cli.ContainerCreate(context.Background(), &config2, &hostConfig, &networktypes.NetworkingConfig{}, "capaddtest1")
1264 1271
 	c.Assert(err, checker.IsNil)
1265
-	c.Assert(status, checker.Equals, http.StatusCreated)
1266 1272
 }
1267 1273
 
1268 1274
 // #14915
1269 1275
 func (s *DockerSuite) TestContainerAPICreateNoHostConfig118(c *check.C) {
1270 1276
 	testRequires(c, DaemonIsLinux) // Windows only support 1.25 or later
1271
-	config := struct {
1272
-		Image string
1273
-	}{"busybox"}
1274
-	status, _, err := request.SockRequest("POST", "/v1.18/containers/create", config, daemonHost())
1277
+	config := containertypes.Config{
1278
+		Image: "busybox",
1279
+	}
1280
+
1281
+	var httpClient *http.Client
1282
+	cli, err := client.NewClient(daemonHost(), "v1.18", httpClient, map[string]string{})
1283
+
1284
+	_, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
1275 1285
 	c.Assert(err, checker.IsNil)
1276
-	c.Assert(status, checker.Equals, http.StatusCreated)
1277 1286
 }
1278 1287
 
1279 1288
 // Ensure an error occurs when you have a container read-only rootfs but you
... ...
@@ -1298,88 +1398,93 @@ func (s *DockerSuite) TestPutContainerArchiveErrSymlinkInVolumeToReadOnlyRootfs(
1298 1298
 	// Attempt to extract to a symlink in the volume which points to a
1299 1299
 	// directory outside the volume. This should cause an error because the
1300 1300
 	// rootfs is read-only.
1301
-	query := make(url.Values, 1)
1302
-	query.Set("path", "/vol2/symlinkToAbsDir")
1303
-	urlPath := fmt.Sprintf("/v1.20/containers/%s/archive?%s", cID, query.Encode())
1304
-
1305
-	statusCode, body, err := request.SockRequest("PUT", urlPath, nil, daemonHost())
1301
+	var httpClient *http.Client
1302
+	cli, err := client.NewClient(daemonHost(), "v1.20", httpClient, map[string]string{})
1306 1303
 	c.Assert(err, checker.IsNil)
1307 1304
 
1308
-	if !isCpCannotCopyReadOnly(fmt.Errorf(string(body))) {
1309
-		c.Fatalf("expected ErrContainerRootfsReadonly error, but got %d: %s", statusCode, string(body))
1310
-	}
1305
+	err = cli.CopyToContainer(context.Background(), cID, "/vol2/symlinkToAbsDir", nil, types.CopyToContainerOptions{})
1306
+	c.Assert(err.Error(), checker.Contains, "container rootfs is marked read-only")
1311 1307
 }
1312 1308
 
1313 1309
 func (s *DockerSuite) TestContainerAPIGetContainersJSONEmpty(c *check.C) {
1314
-	status, body, err := request.SockRequest("GET", "/containers/json?all=1", nil, daemonHost())
1310
+	cli, err := client.NewEnvClient()
1311
+	c.Assert(err, checker.IsNil)
1312
+	defer cli.Close()
1313
+
1314
+	containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
1315 1315
 	c.Assert(err, checker.IsNil)
1316
-	c.Assert(status, checker.Equals, http.StatusOK)
1317
-	c.Assert(string(body), checker.Equals, "[]\n")
1316
+	c.Assert(containers, checker.HasLen, 0)
1318 1317
 }
1319 1318
 
1320 1319
 func (s *DockerSuite) TestPostContainersCreateWithWrongCpusetValues(c *check.C) {
1321 1320
 	// Not supported on Windows
1322 1321
 	testRequires(c, DaemonIsLinux)
1323 1322
 
1324
-	c1 := struct {
1325
-		Image      string
1326
-		CpusetCpus string
1327
-	}{"busybox", "1-42,,"}
1328
-	name := "wrong-cpuset-cpus"
1329
-	status, body, err := request.SockRequest("POST", "/containers/create?name="+name, c1, daemonHost())
1323
+	cli, err := client.NewEnvClient()
1330 1324
 	c.Assert(err, checker.IsNil)
1331
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
1325
+	defer cli.Close()
1326
+
1327
+	config := containertypes.Config{
1328
+		Image: "busybox",
1329
+	}
1330
+	hostConfig1 := containertypes.HostConfig{
1331
+		Resources: containertypes.Resources{
1332
+			CpusetCpus: "1-42,,",
1333
+		},
1334
+	}
1335
+	name := "wrong-cpuset-cpus"
1336
+
1337
+	_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig1, &networktypes.NetworkingConfig{}, name)
1332 1338
 	expected := "Invalid value 1-42,, for cpuset cpus"
1333
-	c.Assert(getErrorMessage(c, body), checker.Equals, expected)
1339
+	c.Assert(err.Error(), checker.Contains, expected)
1334 1340
 
1335
-	c2 := struct {
1336
-		Image      string
1337
-		CpusetMems string
1338
-	}{"busybox", "42-3,1--"}
1341
+	hostConfig2 := containertypes.HostConfig{
1342
+		Resources: containertypes.Resources{
1343
+			CpusetMems: "42-3,1--",
1344
+		},
1345
+	}
1339 1346
 	name = "wrong-cpuset-mems"
1340
-	status, body, err = request.SockRequest("POST", "/containers/create?name="+name, c2, daemonHost())
1341
-	c.Assert(err, checker.IsNil)
1342
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
1347
+	_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig2, &networktypes.NetworkingConfig{}, name)
1343 1348
 	expected = "Invalid value 42-3,1-- for cpuset mems"
1344
-	c.Assert(getErrorMessage(c, body), checker.Equals, expected)
1349
+	c.Assert(err.Error(), checker.Contains, expected)
1345 1350
 }
1346 1351
 
1347 1352
 func (s *DockerSuite) TestPostContainersCreateShmSizeNegative(c *check.C) {
1348 1353
 	// ShmSize is not supported on Windows
1349 1354
 	testRequires(c, DaemonIsLinux)
1350
-	config := map[string]interface{}{
1351
-		"Image":      "busybox",
1352
-		"HostConfig": map[string]interface{}{"ShmSize": -1},
1355
+	config := containertypes.Config{
1356
+		Image: "busybox",
1357
+	}
1358
+	hostConfig := containertypes.HostConfig{
1359
+		ShmSize: -1,
1353 1360
 	}
1354 1361
 
1355
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
1356
-	c.Assert(err, check.IsNil)
1357
-	c.Assert(status, check.Equals, http.StatusBadRequest)
1358
-	c.Assert(getErrorMessage(c, body), checker.Contains, "SHM size can not be less than 0")
1362
+	cli, err := client.NewEnvClient()
1363
+	c.Assert(err, checker.IsNil)
1364
+	defer cli.Close()
1365
+
1366
+	_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
1367
+	c.Assert(err.Error(), checker.Contains, "SHM size can not be less than 0")
1359 1368
 }
1360 1369
 
1361 1370
 func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *check.C) {
1362 1371
 	// ShmSize is not supported on Windows
1363 1372
 	testRequires(c, DaemonIsLinux)
1364 1373
 	var defaultSHMSize int64 = 67108864
1365
-	config := map[string]interface{}{
1366
-		"Image": "busybox",
1367
-		"Cmd":   "mount",
1374
+	config := containertypes.Config{
1375
+		Image: "busybox",
1376
+		Cmd:   []string{"mount"},
1368 1377
 	}
1369 1378
 
1370
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
1371
-	c.Assert(err, check.IsNil)
1372
-	c.Assert(status, check.Equals, http.StatusCreated)
1373
-
1374
-	var container containertypes.ContainerCreateCreatedBody
1375
-	c.Assert(json.Unmarshal(body, &container), check.IsNil)
1379
+	cli, err := client.NewEnvClient()
1380
+	c.Assert(err, checker.IsNil)
1381
+	defer cli.Close()
1376 1382
 
1377
-	status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost())
1383
+	container, err := cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
1378 1384
 	c.Assert(err, check.IsNil)
1379
-	c.Assert(status, check.Equals, http.StatusOK)
1380 1385
 
1381
-	var containerJSON types.ContainerJSON
1382
-	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
1386
+	containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
1387
+	c.Assert(err, check.IsNil)
1383 1388
 
1384 1389
 	c.Assert(containerJSON.HostConfig.ShmSize, check.Equals, defaultSHMSize)
1385 1390
 
... ...
@@ -1393,25 +1498,20 @@ func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *check.
1393 1393
 func (s *DockerSuite) TestPostContainersCreateShmSizeOmitted(c *check.C) {
1394 1394
 	// ShmSize is not supported on Windows
1395 1395
 	testRequires(c, DaemonIsLinux)
1396
-	config := map[string]interface{}{
1397
-		"Image":      "busybox",
1398
-		"HostConfig": map[string]interface{}{},
1399
-		"Cmd":        "mount",
1396
+	config := containertypes.Config{
1397
+		Image: "busybox",
1398
+		Cmd:   []string{"mount"},
1400 1399
 	}
1401 1400
 
1402
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
1403
-	c.Assert(err, check.IsNil)
1404
-	c.Assert(status, check.Equals, http.StatusCreated)
1405
-
1406
-	var container containertypes.ContainerCreateCreatedBody
1407
-	c.Assert(json.Unmarshal(body, &container), check.IsNil)
1401
+	cli, err := client.NewEnvClient()
1402
+	c.Assert(err, checker.IsNil)
1403
+	defer cli.Close()
1408 1404
 
1409
-	status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost())
1405
+	container, err := cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
1410 1406
 	c.Assert(err, check.IsNil)
1411
-	c.Assert(status, check.Equals, http.StatusOK)
1412 1407
 
1413
-	var containerJSON types.ContainerJSON
1414
-	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
1408
+	containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
1409
+	c.Assert(err, check.IsNil)
1415 1410
 
1416 1411
 	c.Assert(containerJSON.HostConfig.ShmSize, check.Equals, int64(67108864))
1417 1412
 
... ...
@@ -1425,25 +1525,24 @@ func (s *DockerSuite) TestPostContainersCreateShmSizeOmitted(c *check.C) {
1425 1425
 func (s *DockerSuite) TestPostContainersCreateWithShmSize(c *check.C) {
1426 1426
 	// ShmSize is not supported on Windows
1427 1427
 	testRequires(c, DaemonIsLinux)
1428
-	config := map[string]interface{}{
1429
-		"Image":      "busybox",
1430
-		"Cmd":        "mount",
1431
-		"HostConfig": map[string]interface{}{"ShmSize": 1073741824},
1428
+	config := containertypes.Config{
1429
+		Image: "busybox",
1430
+		Cmd:   []string{"mount"},
1432 1431
 	}
1433 1432
 
1434
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
1435
-	c.Assert(err, check.IsNil)
1436
-	c.Assert(status, check.Equals, http.StatusCreated)
1433
+	hostConfig := containertypes.HostConfig{
1434
+		ShmSize: 1073741824,
1435
+	}
1437 1436
 
1438
-	var container containertypes.ContainerCreateCreatedBody
1439
-	c.Assert(json.Unmarshal(body, &container), check.IsNil)
1437
+	cli, err := client.NewEnvClient()
1438
+	c.Assert(err, checker.IsNil)
1439
+	defer cli.Close()
1440 1440
 
1441
-	status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost())
1441
+	container, err := cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
1442 1442
 	c.Assert(err, check.IsNil)
1443
-	c.Assert(status, check.Equals, http.StatusOK)
1444 1443
 
1445
-	var containerJSON types.ContainerJSON
1446
-	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
1444
+	containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
1445
+	c.Assert(err, check.IsNil)
1447 1446
 
1448 1447
 	c.Assert(containerJSON.HostConfig.ShmSize, check.Equals, int64(1073741824))
1449 1448
 
... ...
@@ -1457,23 +1556,19 @@ func (s *DockerSuite) TestPostContainersCreateWithShmSize(c *check.C) {
1457 1457
 func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(c *check.C) {
1458 1458
 	// Swappiness is not supported on Windows
1459 1459
 	testRequires(c, DaemonIsLinux)
1460
-	config := map[string]interface{}{
1461
-		"Image": "busybox",
1460
+	config := containertypes.Config{
1461
+		Image: "busybox",
1462 1462
 	}
1463 1463
 
1464
-	status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost())
1465
-	c.Assert(err, check.IsNil)
1466
-	c.Assert(status, check.Equals, http.StatusCreated)
1467
-
1468
-	var container containertypes.ContainerCreateCreatedBody
1469
-	c.Assert(json.Unmarshal(body, &container), check.IsNil)
1464
+	cli, err := client.NewEnvClient()
1465
+	c.Assert(err, checker.IsNil)
1466
+	defer cli.Close()
1470 1467
 
1471
-	status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost())
1468
+	container, err := cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
1472 1469
 	c.Assert(err, check.IsNil)
1473
-	c.Assert(status, check.Equals, http.StatusOK)
1474 1470
 
1475
-	var containerJSON types.ContainerJSON
1476
-	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
1471
+	containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
1472
+	c.Assert(err, check.IsNil)
1477 1473
 
1478 1474
 	c.Assert(containerJSON.HostConfig.MemorySwappiness, check.IsNil)
1479 1475
 }
... ...
@@ -1483,41 +1578,43 @@ func (s *DockerSuite) TestPostContainersCreateWithOomScoreAdjInvalidRange(c *che
1483 1483
 	// OomScoreAdj is not supported on Windows
1484 1484
 	testRequires(c, DaemonIsLinux)
1485 1485
 
1486
-	config := struct {
1487
-		Image       string
1488
-		OomScoreAdj int
1489
-	}{"busybox", 1001}
1486
+	config := containertypes.Config{
1487
+		Image: "busybox",
1488
+	}
1489
+
1490
+	hostConfig := containertypes.HostConfig{
1491
+		OomScoreAdj: 1001,
1492
+	}
1493
+
1494
+	cli, err := client.NewEnvClient()
1495
+	c.Assert(err, checker.IsNil)
1496
+	defer cli.Close()
1497
+
1490 1498
 	name := "oomscoreadj-over"
1491
-	status, b, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
1492
-	c.Assert(err, check.IsNil)
1493
-	c.Assert(status, check.Equals, http.StatusBadRequest)
1499
+	_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, name)
1494 1500
 
1495 1501
 	expected := "Invalid value 1001, range for oom score adj is [-1000, 1000]"
1496
-	msg := getErrorMessage(c, b)
1497
-	if !strings.Contains(msg, expected) {
1498
-		c.Fatalf("Expected output to contain %q, got %q", expected, msg)
1502
+	c.Assert(err.Error(), checker.Contains, expected)
1503
+
1504
+	hostConfig = containertypes.HostConfig{
1505
+		OomScoreAdj: -1001,
1499 1506
 	}
1500 1507
 
1501
-	config = struct {
1502
-		Image       string
1503
-		OomScoreAdj int
1504
-	}{"busybox", -1001}
1505 1508
 	name = "oomscoreadj-low"
1506
-	status, b, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
1507
-	c.Assert(err, check.IsNil)
1508
-	c.Assert(status, check.Equals, http.StatusBadRequest)
1509
+	_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, name)
1510
+
1509 1511
 	expected = "Invalid value -1001, range for oom score adj is [-1000, 1000]"
1510
-	msg = getErrorMessage(c, b)
1511
-	if !strings.Contains(msg, expected) {
1512
-		c.Fatalf("Expected output to contain %q, got %q", expected, msg)
1513
-	}
1512
+	c.Assert(err.Error(), checker.Contains, expected)
1514 1513
 }
1515 1514
 
1516 1515
 // test case for #22210 where an empty container name caused panic.
1517 1516
 func (s *DockerSuite) TestContainerAPIDeleteWithEmptyName(c *check.C) {
1518
-	status, _, err := request.SockRequest("DELETE", "/containers/", nil, daemonHost())
1517
+	cli, err := client.NewEnvClient()
1519 1518
 	c.Assert(err, checker.IsNil)
1520
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
1519
+	defer cli.Close()
1520
+
1521
+	err = cli.ContainerRemove(context.Background(), "", types.ContainerRemoveOptions{})
1522
+	c.Assert(err.Error(), checker.Contains, "Error response from daemon: page not found")
1521 1523
 }
1522 1524
 
1523 1525
 func (s *DockerSuite) TestContainerAPIStatsWithNetworkDisabled(c *check.C) {
... ...
@@ -1525,31 +1622,33 @@ func (s *DockerSuite) TestContainerAPIStatsWithNetworkDisabled(c *check.C) {
1525 1525
 	testRequires(c, DaemonIsLinux)
1526 1526
 
1527 1527
 	name := "testing-network-disabled"
1528
-	config := map[string]interface{}{
1529
-		"Image":           "busybox",
1530
-		"Cmd":             []string{"top"},
1531
-		"NetworkDisabled": true,
1528
+
1529
+	config := containertypes.Config{
1530
+		Image:           "busybox",
1531
+		Cmd:             []string{"top"},
1532
+		NetworkDisabled: true,
1532 1533
 	}
1533 1534
 
1534
-	status, _, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
1535
+	cli, err := client.NewEnvClient()
1536
+	c.Assert(err, checker.IsNil)
1537
+	defer cli.Close()
1538
+
1539
+	_, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, name)
1535 1540
 	c.Assert(err, checker.IsNil)
1536
-	c.Assert(status, checker.Equals, http.StatusCreated)
1537 1541
 
1538
-	status, _, err = request.SockRequest("POST", "/containers/"+name+"/start", nil, daemonHost())
1542
+	err = cli.ContainerStart(context.Background(), name, types.ContainerStartOptions{})
1539 1543
 	c.Assert(err, checker.IsNil)
1540
-	c.Assert(status, checker.Equals, http.StatusNoContent)
1541 1544
 
1542 1545
 	c.Assert(waitRun(name), check.IsNil)
1543 1546
 
1544 1547
 	type b struct {
1545
-		status int
1546
-		body   []byte
1547
-		err    error
1548
+		stats types.ContainerStats
1549
+		err   error
1548 1550
 	}
1549 1551
 	bc := make(chan b, 1)
1550 1552
 	go func() {
1551
-		status, body, err := request.SockRequest("GET", "/containers/"+name+"/stats", nil, daemonHost())
1552
-		bc <- b{status, body, err}
1553
+		stats, err := cli.ContainerStats(context.Background(), name, false)
1554
+		bc <- b{stats, err}
1553 1555
 	}()
1554 1556
 
1555 1557
 	// allow some time to stream the stats from the container
... ...
@@ -1563,26 +1662,15 @@ func (s *DockerSuite) TestContainerAPIStatsWithNetworkDisabled(c *check.C) {
1563 1563
 		c.Fatal("stream was not closed after container was removed")
1564 1564
 	case sr := <-bc:
1565 1565
 		c.Assert(sr.err, checker.IsNil)
1566
-		c.Assert(sr.status, checker.Equals, http.StatusOK)
1567
-
1568
-		// decode only one object from the stream
1569
-		var s *types.Stats
1570
-		dec := json.NewDecoder(bytes.NewBuffer(sr.body))
1571
-		c.Assert(dec.Decode(&s), checker.IsNil)
1566
+		sr.stats.Body.Close()
1572 1567
 	}
1573 1568
 }
1574 1569
 
1575 1570
 func (s *DockerSuite) TestContainersAPICreateMountsValidation(c *check.C) {
1576
-	type m mounttypes.Mount
1577
-	type hc struct{ Mounts []m }
1578
-	type cfg struct {
1579
-		Image      string
1580
-		HostConfig hc
1581
-	}
1582 1571
 	type testCase struct {
1583
-		config cfg
1584
-		status int
1585
-		msg    string
1572
+		config     containertypes.Config
1573
+		hostConfig containertypes.HostConfig
1574
+		msg        string
1586 1575
 	}
1587 1576
 
1588 1577
 	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
... ...
@@ -1591,78 +1679,82 @@ func (s *DockerSuite) TestContainersAPICreateMountsValidation(c *check.C) {
1591 1591
 
1592 1592
 	cases := []testCase{
1593 1593
 		{
1594
-			config: cfg{
1594
+			config: containertypes.Config{
1595 1595
 				Image: "busybox",
1596
-				HostConfig: hc{
1597
-					Mounts: []m{{
1598
-						Type:   "notreal",
1599
-						Target: destPath}}}},
1600
-			status: http.StatusBadRequest,
1601
-			msg:    "mount type unknown",
1596
+			},
1597
+			hostConfig: containertypes.HostConfig{
1598
+				Mounts: []mounttypes.Mount{{
1599
+					Type:   "notreal",
1600
+					Target: destPath,
1601
+				},
1602
+				},
1603
+			},
1604
+
1605
+			msg: "mount type unknown",
1602 1606
 		},
1603 1607
 		{
1604
-			config: cfg{
1608
+			config: containertypes.Config{
1605 1609
 				Image: "busybox",
1606
-				HostConfig: hc{
1607
-					Mounts: []m{{
1608
-						Type: "bind"}}}},
1609
-			status: http.StatusBadRequest,
1610
-			msg:    "Target must not be empty",
1610
+			},
1611
+			hostConfig: containertypes.HostConfig{
1612
+				Mounts: []mounttypes.Mount{{
1613
+					Type: "bind"}}},
1614
+			msg: "Target must not be empty",
1611 1615
 		},
1612 1616
 		{
1613
-			config: cfg{
1617
+			config: containertypes.Config{
1614 1618
 				Image: "busybox",
1615
-				HostConfig: hc{
1616
-					Mounts: []m{{
1617
-						Type:   "bind",
1618
-						Target: destPath}}}},
1619
-			status: http.StatusBadRequest,
1620
-			msg:    "Source must not be empty",
1619
+			},
1620
+			hostConfig: containertypes.HostConfig{
1621
+				Mounts: []mounttypes.Mount{{
1622
+					Type:   "bind",
1623
+					Target: destPath}}},
1624
+			msg: "Source must not be empty",
1621 1625
 		},
1622 1626
 		{
1623
-			config: cfg{
1627
+			config: containertypes.Config{
1624 1628
 				Image: "busybox",
1625
-				HostConfig: hc{
1626
-					Mounts: []m{{
1627
-						Type:   "bind",
1628
-						Source: notExistPath,
1629
-						Target: destPath}}}},
1630
-			status: http.StatusBadRequest,
1631
-			msg:    "bind source path does not exist",
1629
+			},
1630
+			hostConfig: containertypes.HostConfig{
1631
+				Mounts: []mounttypes.Mount{{
1632
+					Type:   "bind",
1633
+					Source: notExistPath,
1634
+					Target: destPath}}},
1635
+			msg: "bind source path does not exist",
1632 1636
 		},
1633 1637
 		{
1634
-			config: cfg{
1638
+			config: containertypes.Config{
1635 1639
 				Image: "busybox",
1636
-				HostConfig: hc{
1637
-					Mounts: []m{{
1638
-						Type: "volume"}}}},
1639
-			status: http.StatusBadRequest,
1640
-			msg:    "Target must not be empty",
1640
+			},
1641
+			hostConfig: containertypes.HostConfig{
1642
+				Mounts: []mounttypes.Mount{{
1643
+					Type: "volume"}}},
1644
+			msg: "Target must not be empty",
1641 1645
 		},
1642 1646
 		{
1643
-			config: cfg{
1647
+			config: containertypes.Config{
1644 1648
 				Image: "busybox",
1645
-				HostConfig: hc{
1646
-					Mounts: []m{{
1647
-						Type:   "volume",
1648
-						Source: "hello",
1649
-						Target: destPath}}}},
1650
-			status: http.StatusCreated,
1651
-			msg:    "",
1649
+			},
1650
+			hostConfig: containertypes.HostConfig{
1651
+				Mounts: []mounttypes.Mount{{
1652
+					Type:   "volume",
1653
+					Source: "hello",
1654
+					Target: destPath}}},
1655
+			msg: "",
1652 1656
 		},
1653 1657
 		{
1654
-			config: cfg{
1658
+			config: containertypes.Config{
1655 1659
 				Image: "busybox",
1656
-				HostConfig: hc{
1657
-					Mounts: []m{{
1658
-						Type:   "volume",
1659
-						Source: "hello2",
1660
-						Target: destPath,
1661
-						VolumeOptions: &mounttypes.VolumeOptions{
1662
-							DriverConfig: &mounttypes.Driver{
1663
-								Name: "local"}}}}}},
1664
-			status: http.StatusCreated,
1665
-			msg:    "",
1660
+			},
1661
+			hostConfig: containertypes.HostConfig{
1662
+				Mounts: []mounttypes.Mount{{
1663
+					Type:   "volume",
1664
+					Source: "hello2",
1665
+					Target: destPath,
1666
+					VolumeOptions: &mounttypes.VolumeOptions{
1667
+						DriverConfig: &mounttypes.Driver{
1668
+							Name: "local"}}}}},
1669
+			msg: "",
1666 1670
 		},
1667 1671
 	}
1668 1672
 
... ...
@@ -1672,27 +1764,27 @@ func (s *DockerSuite) TestContainersAPICreateMountsValidation(c *check.C) {
1672 1672
 		defer os.RemoveAll(tmpDir)
1673 1673
 		cases = append(cases, []testCase{
1674 1674
 			{
1675
-				config: cfg{
1675
+				config: containertypes.Config{
1676 1676
 					Image: "busybox",
1677
-					HostConfig: hc{
1678
-						Mounts: []m{{
1679
-							Type:   "bind",
1680
-							Source: tmpDir,
1681
-							Target: destPath}}}},
1682
-				status: http.StatusCreated,
1683
-				msg:    "",
1677
+				},
1678
+				hostConfig: containertypes.HostConfig{
1679
+					Mounts: []mounttypes.Mount{{
1680
+						Type:   "bind",
1681
+						Source: tmpDir,
1682
+						Target: destPath}}},
1683
+				msg: "",
1684 1684
 			},
1685 1685
 			{
1686
-				config: cfg{
1686
+				config: containertypes.Config{
1687 1687
 					Image: "busybox",
1688
-					HostConfig: hc{
1689
-						Mounts: []m{{
1690
-							Type:          "bind",
1691
-							Source:        tmpDir,
1692
-							Target:        destPath,
1693
-							VolumeOptions: &mounttypes.VolumeOptions{}}}}},
1694
-				status: http.StatusBadRequest,
1695
-				msg:    "VolumeOptions must not be specified",
1688
+				},
1689
+				hostConfig: containertypes.HostConfig{
1690
+					Mounts: []mounttypes.Mount{{
1691
+						Type:          "bind",
1692
+						Source:        tmpDir,
1693
+						Target:        destPath,
1694
+						VolumeOptions: &mounttypes.VolumeOptions{}}}},
1695
+				msg: "VolumeOptions must not be specified",
1696 1696
 			},
1697 1697
 		}...)
1698 1698
 	}
... ...
@@ -1700,67 +1792,70 @@ func (s *DockerSuite) TestContainersAPICreateMountsValidation(c *check.C) {
1700 1700
 	if DaemonIsLinux() {
1701 1701
 		cases = append(cases, []testCase{
1702 1702
 			{
1703
-				config: cfg{
1703
+				config: containertypes.Config{
1704 1704
 					Image: "busybox",
1705
-					HostConfig: hc{
1706
-						Mounts: []m{{
1707
-							Type:   "volume",
1708
-							Source: "hello3",
1709
-							Target: destPath,
1710
-							VolumeOptions: &mounttypes.VolumeOptions{
1711
-								DriverConfig: &mounttypes.Driver{
1712
-									Name:    "local",
1713
-									Options: map[string]string{"o": "size=1"}}}}}}},
1714
-				status: http.StatusCreated,
1715
-				msg:    "",
1705
+				},
1706
+				hostConfig: containertypes.HostConfig{
1707
+					Mounts: []mounttypes.Mount{{
1708
+						Type:   "volume",
1709
+						Source: "hello3",
1710
+						Target: destPath,
1711
+						VolumeOptions: &mounttypes.VolumeOptions{
1712
+							DriverConfig: &mounttypes.Driver{
1713
+								Name:    "local",
1714
+								Options: map[string]string{"o": "size=1"}}}}}},
1715
+				msg: "",
1716 1716
 			},
1717 1717
 			{
1718
-				config: cfg{
1718
+				config: containertypes.Config{
1719 1719
 					Image: "busybox",
1720
-					HostConfig: hc{
1721
-						Mounts: []m{{
1722
-							Type:   "tmpfs",
1723
-							Target: destPath}}}},
1724
-				status: http.StatusCreated,
1725
-				msg:    "",
1720
+				},
1721
+				hostConfig: containertypes.HostConfig{
1722
+					Mounts: []mounttypes.Mount{{
1723
+						Type:   "tmpfs",
1724
+						Target: destPath}}},
1725
+				msg: "",
1726 1726
 			},
1727 1727
 			{
1728
-				config: cfg{
1728
+				config: containertypes.Config{
1729 1729
 					Image: "busybox",
1730
-					HostConfig: hc{
1731
-						Mounts: []m{{
1732
-							Type:   "tmpfs",
1733
-							Target: destPath,
1734
-							TmpfsOptions: &mounttypes.TmpfsOptions{
1735
-								SizeBytes: 4096 * 1024,
1736
-								Mode:      0700,
1737
-							}}}}},
1738
-				status: http.StatusCreated,
1739
-				msg:    "",
1730
+				},
1731
+				hostConfig: containertypes.HostConfig{
1732
+					Mounts: []mounttypes.Mount{{
1733
+						Type:   "tmpfs",
1734
+						Target: destPath,
1735
+						TmpfsOptions: &mounttypes.TmpfsOptions{
1736
+							SizeBytes: 4096 * 1024,
1737
+							Mode:      0700,
1738
+						}}}},
1739
+				msg: "",
1740 1740
 			},
1741 1741
 
1742 1742
 			{
1743
-				config: cfg{
1743
+				config: containertypes.Config{
1744 1744
 					Image: "busybox",
1745
-					HostConfig: hc{
1746
-						Mounts: []m{{
1747
-							Type:   "tmpfs",
1748
-							Source: "/shouldnotbespecified",
1749
-							Target: destPath}}}},
1750
-				status: http.StatusBadRequest,
1751
-				msg:    "Source must not be specified",
1745
+				},
1746
+				hostConfig: containertypes.HostConfig{
1747
+					Mounts: []mounttypes.Mount{{
1748
+						Type:   "tmpfs",
1749
+						Source: "/shouldnotbespecified",
1750
+						Target: destPath}}},
1751
+				msg: "Source must not be specified",
1752 1752
 			},
1753 1753
 		}...)
1754 1754
 
1755 1755
 	}
1756
+	cli, err := client.NewEnvClient()
1757
+	c.Assert(err, checker.IsNil)
1758
+	defer cli.Close()
1756 1759
 
1757 1760
 	for i, x := range cases {
1758 1761
 		c.Logf("case %d", i)
1759
-		status, b, err := request.SockRequest("POST", "/containers/create", x.config, daemonHost())
1760
-		c.Assert(err, checker.IsNil)
1761
-		c.Assert(status, checker.Equals, x.status, check.Commentf("%s\n%v", string(b), cases[i].config))
1762
+		_, err = cli.ContainerCreate(context.Background(), &x.config, &x.hostConfig, &networktypes.NetworkingConfig{}, "")
1762 1763
 		if len(x.msg) > 0 {
1763
-			c.Assert(string(b), checker.Contains, x.msg, check.Commentf("%v", cases[i].config))
1764
+			c.Assert(err.Error(), checker.Contains, x.msg, check.Commentf("%v", cases[i].config))
1765
+		} else {
1766
+			c.Assert(err, checker.IsNil)
1764 1767
 		}
1765 1768
 	}
1766 1769
 }
... ...
@@ -1775,15 +1870,21 @@ func (s *DockerSuite) TestContainerAPICreateMountsBindRead(c *check.C) {
1775 1775
 	defer os.RemoveAll(tmpDir)
1776 1776
 	err = ioutil.WriteFile(filepath.Join(tmpDir, "bar"), []byte("hello"), 666)
1777 1777
 	c.Assert(err, checker.IsNil)
1778
-
1779
-	data := map[string]interface{}{
1780
-		"Image":      "busybox",
1781
-		"Cmd":        []string{"/bin/sh", "-c", "cat /foo/bar"},
1782
-		"HostConfig": map[string]interface{}{"Mounts": []map[string]interface{}{{"Type": "bind", "Source": tmpDir, "Target": destPath}}},
1778
+	config := containertypes.Config{
1779
+		Image: "busybox",
1780
+		Cmd:   []string{"/bin/sh", "-c", "cat /foo/bar"},
1783 1781
 	}
1784
-	status, resp, err := request.SockRequest("POST", "/containers/create?name=test", data, daemonHost())
1785
-	c.Assert(err, checker.IsNil, check.Commentf(string(resp)))
1786
-	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(resp)))
1782
+	hostConfig := containertypes.HostConfig{
1783
+		Mounts: []mounttypes.Mount{
1784
+			{Type: "bind", Source: tmpDir, Target: destPath},
1785
+		},
1786
+	}
1787
+	cli, err := client.NewEnvClient()
1788
+	c.Assert(err, checker.IsNil)
1789
+	defer cli.Close()
1790
+
1791
+	_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "test")
1792
+	c.Assert(err, checker.IsNil)
1787 1793
 
1788 1794
 	out, _ := dockerCmd(c, "start", "-a", "test")
1789 1795
 	c.Assert(out, checker.Equals, "hello")
... ...
@@ -1866,16 +1967,17 @@ func (s *DockerSuite) TestContainersAPICreateMountsCreate(c *check.C) {
1866 1866
 	type createResp struct {
1867 1867
 		ID string `json:"Id"`
1868 1868
 	}
1869
+
1870
+	cli, err := client.NewEnvClient()
1871
+	c.Assert(err, checker.IsNil)
1872
+	defer cli.Close()
1873
+
1869 1874
 	for i, x := range cases {
1870 1875
 		c.Logf("case %d - config: %v", i, x.cfg)
1871
-		status, data, err := request.SockRequest("POST", "/containers/create", wrapper{containertypes.Config{Image: testImg}, containertypes.HostConfig{Mounts: []mounttypes.Mount{x.cfg}}}, daemonHost())
1872
-		c.Assert(err, checker.IsNil, check.Commentf(string(data)))
1873
-		c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(data)))
1876
+		container, err := cli.ContainerCreate(context.Background(), &containertypes.Config{Image: testImg}, &containertypes.HostConfig{Mounts: []mounttypes.Mount{x.cfg}}, &networktypes.NetworkingConfig{}, "")
1877
+		c.Assert(err, checker.IsNil)
1874 1878
 
1875
-		var resp createResp
1876
-		err = json.Unmarshal(data, &resp)
1877
-		c.Assert(err, checker.IsNil, check.Commentf(string(data)))
1878
-		id := resp.ID
1879
+		id := container.ID
1879 1880
 
1880 1881
 		var mps []types.MountPoint
1881 1882
 		err = json.NewDecoder(strings.NewReader(inspectFieldJSON(c, id, "Mounts"))).Decode(&mps)
... ...
@@ -1921,38 +2023,43 @@ func (s *DockerSuite) TestContainersAPICreateMountsCreate(c *check.C) {
1921 1921
 func (s *DockerSuite) TestContainersAPICreateMountsTmpfs(c *check.C) {
1922 1922
 	testRequires(c, DaemonIsLinux)
1923 1923
 	type testCase struct {
1924
-		cfg             map[string]interface{}
1924
+		cfg             mounttypes.Mount
1925 1925
 		expectedOptions []string
1926 1926
 	}
1927 1927
 	target := "/foo"
1928 1928
 	cases := []testCase{
1929 1929
 		{
1930
-			cfg: map[string]interface{}{
1931
-				"Type":   "tmpfs",
1932
-				"Target": target},
1930
+			cfg: mounttypes.Mount{
1931
+				Type:   "tmpfs",
1932
+				Target: target},
1933 1933
 			expectedOptions: []string{"rw", "nosuid", "nodev", "noexec", "relatime"},
1934 1934
 		},
1935 1935
 		{
1936
-			cfg: map[string]interface{}{
1937
-				"Type":   "tmpfs",
1938
-				"Target": target,
1939
-				"TmpfsOptions": map[string]interface{}{
1940
-					"SizeBytes": 4096 * 1024, "Mode": 0700}},
1936
+			cfg: mounttypes.Mount{
1937
+				Type:   "tmpfs",
1938
+				Target: target,
1939
+				TmpfsOptions: &mounttypes.TmpfsOptions{
1940
+					SizeBytes: 4096 * 1024, Mode: 0700}},
1941 1941
 			expectedOptions: []string{"rw", "nosuid", "nodev", "noexec", "relatime", "size=4096k", "mode=700"},
1942 1942
 		},
1943 1943
 	}
1944 1944
 
1945
+	cli, err := client.NewEnvClient()
1946
+	c.Assert(err, checker.IsNil)
1947
+	defer cli.Close()
1948
+
1949
+	config := containertypes.Config{
1950
+		Image: "busybox",
1951
+		Cmd:   []string{"/bin/sh", "-c", fmt.Sprintf("mount | grep 'tmpfs on %s'", target)},
1952
+	}
1945 1953
 	for i, x := range cases {
1946 1954
 		cName := fmt.Sprintf("test-tmpfs-%d", i)
1947
-		data := map[string]interface{}{
1948
-			"Image": "busybox",
1949
-			"Cmd": []string{"/bin/sh", "-c",
1950
-				fmt.Sprintf("mount | grep 'tmpfs on %s'", target)},
1951
-			"HostConfig": map[string]interface{}{"Mounts": []map[string]interface{}{x.cfg}},
1955
+		hostConfig := containertypes.HostConfig{
1956
+			Mounts: []mounttypes.Mount{x.cfg},
1952 1957
 		}
1953
-		status, resp, err := request.SockRequest("POST", "/containers/create?name="+cName, data, daemonHost())
1954
-		c.Assert(err, checker.IsNil, check.Commentf(string(resp)))
1955
-		c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(resp)))
1958
+
1959
+		_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, cName)
1960
+		c.Assert(err, checker.IsNil)
1956 1961
 		out, _ := dockerCmd(c, "start", "-a", cName)
1957 1962
 		for _, option := range x.expectedOptions {
1958 1963
 			c.Assert(out, checker.Contains, option)
... ...
@@ -23,11 +23,15 @@ func (s *DockerSuite) TestAPICreateWithInvalidHealthcheckParams(c *check.C) {
23 23
 		},
24 24
 	}
25 25
 
26
-	status, body, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
26
+	res, body, err := request.Post("/containers/create?name="+name, request.JSONBody(config))
27 27
 	c.Assert(err, check.IsNil)
28
-	c.Assert(status, check.Equals, http.StatusBadRequest)
28
+	c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
29
+
30
+	buf, err := request.ReadBody(body)
31
+	c.Assert(err, checker.IsNil)
32
+
29 33
 	expected := fmt.Sprintf("Interval in Healthcheck cannot be less than %s", container.MinimumDuration)
30
-	c.Assert(getErrorMessage(c, body), checker.Contains, expected)
34
+	c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
31 35
 
32 36
 	// test invalid Interval in Healthcheck: larger than 0s but less than 1ms
33 37
 	name = "test2"
... ...
@@ -39,10 +43,14 @@ func (s *DockerSuite) TestAPICreateWithInvalidHealthcheckParams(c *check.C) {
39 39
 			"Retries":  int(1000),
40 40
 		},
41 41
 	}
42
-	status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
42
+	res, body, err = request.Post("/containers/create?name="+name, request.JSONBody(config))
43 43
 	c.Assert(err, check.IsNil)
44
-	c.Assert(status, check.Equals, http.StatusBadRequest)
45
-	c.Assert(getErrorMessage(c, body), checker.Contains, expected)
44
+
45
+	buf, err = request.ReadBody(body)
46
+	c.Assert(err, checker.IsNil)
47
+
48
+	c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
49
+	c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
46 50
 
47 51
 	// test invalid Timeout in Healthcheck: less than 1ms
48 52
 	name = "test3"
... ...
@@ -54,11 +62,15 @@ func (s *DockerSuite) TestAPICreateWithInvalidHealthcheckParams(c *check.C) {
54 54
 			"Retries":  int(1000),
55 55
 		},
56 56
 	}
57
-	status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
57
+	res, body, err = request.Post("/containers/create?name="+name, request.JSONBody(config))
58 58
 	c.Assert(err, check.IsNil)
59
-	c.Assert(status, check.Equals, http.StatusBadRequest)
59
+	c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
60
+
61
+	buf, err = request.ReadBody(body)
62
+	c.Assert(err, checker.IsNil)
63
+
60 64
 	expected = fmt.Sprintf("Timeout in Healthcheck cannot be less than %s", container.MinimumDuration)
61
-	c.Assert(getErrorMessage(c, body), checker.Contains, expected)
65
+	c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
62 66
 
63 67
 	// test invalid Retries in Healthcheck: less than 0
64 68
 	name = "test4"
... ...
@@ -70,11 +82,15 @@ func (s *DockerSuite) TestAPICreateWithInvalidHealthcheckParams(c *check.C) {
70 70
 			"Retries":  int(-10),
71 71
 		},
72 72
 	}
73
-	status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
73
+	res, body, err = request.Post("/containers/create?name="+name, request.JSONBody(config))
74 74
 	c.Assert(err, check.IsNil)
75
-	c.Assert(status, check.Equals, http.StatusBadRequest)
75
+	c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
76
+
77
+	buf, err = request.ReadBody(body)
78
+	c.Assert(err, checker.IsNil)
79
+
76 80
 	expected = "Retries in Healthcheck cannot be negative"
77
-	c.Assert(getErrorMessage(c, body), checker.Contains, expected)
81
+	c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
78 82
 
79 83
 	// test invalid StartPeriod in Healthcheck: not 0 and less than 1ms
80 84
 	name = "test3"
... ...
@@ -87,9 +103,13 @@ func (s *DockerSuite) TestAPICreateWithInvalidHealthcheckParams(c *check.C) {
87 87
 			"StartPeriod": 100 * time.Microsecond,
88 88
 		},
89 89
 	}
90
-	status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
90
+	res, body, err = request.Post("/containers/create?name="+name, request.JSONBody(config))
91 91
 	c.Assert(err, check.IsNil)
92
-	c.Assert(status, check.Equals, http.StatusBadRequest)
92
+	c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
93
+
94
+	buf, err = request.ReadBody(body)
95
+	c.Assert(err, checker.IsNil)
96
+
93 97
 	expected = fmt.Sprintf("StartPeriod in Healthcheck cannot be less than %s", container.MinimumDuration)
94
-	c.Assert(getErrorMessage(c, body), checker.Contains, expected)
98
+	c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
95 99
 }
... ...
@@ -20,9 +20,9 @@ func (s *DockerSuite) TestExecResizeAPIHeightWidthNoInt(c *check.C) {
20 20
 	cleanedContainerID := strings.TrimSpace(out)
21 21
 
22 22
 	endpoint := "/exec/" + cleanedContainerID + "/resize?h=foo&w=bar"
23
-	status, _, err := request.SockRequest("POST", endpoint, nil, daemonHost())
23
+	res, _, err := request.Post(endpoint)
24 24
 	c.Assert(err, checker.IsNil)
25
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
25
+	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
26 26
 }
27 27
 
28 28
 // Part of #14845
... ...
@@ -36,16 +36,19 @@ func (s *DockerSuite) TestExecResizeImmediatelyAfterExecStart(c *check.C) {
36 36
 			"Cmd":         []string{"/bin/sh"},
37 37
 		}
38 38
 		uri := fmt.Sprintf("/containers/%s/exec", name)
39
-		status, body, err := request.SockRequest("POST", uri, data, daemonHost())
39
+		res, body, err := request.Post(uri, request.JSONBody(data))
40 40
 		if err != nil {
41 41
 			return err
42 42
 		}
43
-		if status != http.StatusCreated {
44
-			return fmt.Errorf("POST %s is expected to return %d, got %d", uri, http.StatusCreated, status)
43
+		if res.StatusCode != http.StatusCreated {
44
+			return fmt.Errorf("POST %s is expected to return %d, got %d", uri, http.StatusCreated, res.StatusCode)
45 45
 		}
46 46
 
47
+		buf, err := request.ReadBody(body)
48
+		c.Assert(err, checker.IsNil)
49
+
47 50
 		out := map[string]string{}
48
-		err = json.Unmarshal(body, &out)
51
+		err = json.Unmarshal(buf, &out)
49 52
 		if err != nil {
50 53
 			return fmt.Errorf("ExecCreate returned invalid json. Error: %q", err.Error())
51 54
 		}
... ...
@@ -10,9 +10,12 @@ import (
10 10
 	"net/http"
11 11
 	"time"
12 12
 
13
+	"github.com/docker/docker/api/types"
14
+	"github.com/docker/docker/client"
13 15
 	"github.com/docker/docker/integration-cli/checker"
14 16
 	"github.com/docker/docker/integration-cli/request"
15 17
 	"github.com/go-check/check"
18
+	"golang.org/x/net/context"
16 19
 )
17 20
 
18 21
 // Regression test for #9414
... ...
@@ -20,12 +23,15 @@ func (s *DockerSuite) TestExecAPICreateNoCmd(c *check.C) {
20 20
 	name := "exec_test"
21 21
 	dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
22 22
 
23
-	status, body, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil}, daemonHost())
23
+	res, body, err := request.Post(fmt.Sprintf("/containers/%s/exec", name), request.JSONBody(map[string]interface{}{"Cmd": nil}))
24
+	c.Assert(err, checker.IsNil)
25
+	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
26
+
27
+	b, err := request.ReadBody(body)
24 28
 	c.Assert(err, checker.IsNil)
25
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
26 29
 
27 30
 	comment := check.Commentf("Expected message when creating exec command with no Cmd specified")
28
-	c.Assert(getErrorMessage(c, body), checker.Contains, "No exec command specified", comment)
31
+	c.Assert(getErrorMessage(c, b), checker.Contains, "No exec command specified", comment)
29 32
 }
30 33
 
31 34
 func (s *DockerSuite) TestExecAPICreateNoValidContentType(c *check.C) {
... ...
@@ -55,12 +61,18 @@ func (s *DockerSuite) TestExecAPICreateContainerPaused(c *check.C) {
55 55
 	dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
56 56
 
57 57
 	dockerCmd(c, "pause", name)
58
-	status, body, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}}, daemonHost())
58
+
59
+	cli, err := client.NewEnvClient()
59 60
 	c.Assert(err, checker.IsNil)
60
-	c.Assert(status, checker.Equals, http.StatusConflict)
61
+	defer cli.Close()
62
+
63
+	config := types.ExecConfig{
64
+		Cmd: []string{"true"},
65
+	}
66
+	_, err = cli.ContainerExecCreate(context.Background(), name, config)
61 67
 
62 68
 	comment := check.Commentf("Expected message when creating exec command with Container %s is paused", name)
63
-	c.Assert(getErrorMessage(c, body), checker.Contains, "Container "+name+" is paused, unpause the container before exec", comment)
69
+	c.Assert(err.Error(), checker.Contains, "Container "+name+" is paused, unpause the container before exec", comment)
64 70
 }
65 71
 
66 72
 func (s *DockerSuite) TestExecAPIStart(c *check.C) {
... ...
@@ -128,22 +140,23 @@ func (s *DockerSuite) TestExecAPIStartMultipleTimesError(c *check.C) {
128 128
 func (s *DockerSuite) TestExecAPIStartWithDetach(c *check.C) {
129 129
 	name := "foo"
130 130
 	runSleepingContainer(c, "-d", "-t", "--name", name)
131
-	data := map[string]interface{}{
132
-		"cmd":         []string{"true"},
133
-		"AttachStdin": true,
131
+
132
+	config := types.ExecConfig{
133
+		Cmd:          []string{"true"},
134
+		AttachStderr: true,
134 135
 	}
135
-	_, b, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data, daemonHost())
136
-	c.Assert(err, checker.IsNil, check.Commentf(string(b)))
137 136
 
138
-	createResp := struct {
139
-		ID string `json:"Id"`
140
-	}{}
141
-	c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
137
+	cli, err := client.NewEnvClient()
138
+	c.Assert(err, checker.IsNil)
139
+	defer cli.Close()
140
+
141
+	createResp, err := cli.ContainerExecCreate(context.Background(), name, config)
142
+	c.Assert(err, checker.IsNil)
142 143
 
143 144
 	_, body, err := request.Post(fmt.Sprintf("/exec/%s/start", createResp.ID), request.RawString(`{"Detach": true}`), request.JSON)
144 145
 	c.Assert(err, checker.IsNil)
145 146
 
146
-	b, err = request.ReadBody(body)
147
+	b, err := request.ReadBody(body)
147 148
 	comment := check.Commentf("response body: %s", b)
148 149
 	c.Assert(err, checker.IsNil, comment)
149 150
 
... ...
@@ -1,38 +1,40 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"encoding/json"
5 4
 	"net/http"
6 5
 	"net/http/httptest"
7
-	"net/url"
8 6
 	"strings"
9 7
 
10 8
 	"github.com/docker/docker/api/types"
11
-	"github.com/docker/docker/api/types/image"
9
+	"github.com/docker/docker/api/types/filters"
10
+	"github.com/docker/docker/client"
12 11
 	"github.com/docker/docker/integration-cli/checker"
13 12
 	"github.com/docker/docker/integration-cli/cli"
14 13
 	"github.com/docker/docker/integration-cli/cli/build"
15 14
 	"github.com/docker/docker/integration-cli/request"
16 15
 	"github.com/go-check/check"
16
+	"golang.org/x/net/context"
17 17
 )
18 18
 
19 19
 func (s *DockerSuite) TestAPIImagesFilter(c *check.C) {
20
+	cli, err := client.NewEnvClient()
21
+	c.Assert(err, checker.IsNil)
22
+	defer cli.Close()
23
+
20 24
 	name := "utest:tag1"
21 25
 	name2 := "utest/docker:tag2"
22 26
 	name3 := "utest:5000/docker:tag3"
23 27
 	for _, n := range []string{name, name2, name3} {
24 28
 		dockerCmd(c, "tag", "busybox", n)
25 29
 	}
26
-	type image types.ImageSummary
27
-	getImages := func(filter string) []image {
28
-		v := url.Values{}
29
-		v.Set("filter", filter)
30
-		status, b, err := request.SockRequest("GET", "/images/json?"+v.Encode(), nil, daemonHost())
31
-		c.Assert(err, checker.IsNil)
32
-		c.Assert(status, checker.Equals, http.StatusOK)
33
-
34
-		var images []image
35
-		err = json.Unmarshal(b, &images)
30
+	getImages := func(filter string) []types.ImageSummary {
31
+		filters := filters.NewArgs()
32
+		filters.Add("reference", filter)
33
+		options := types.ImageListOptions{
34
+			All:     false,
35
+			Filters: filters,
36
+		}
37
+		images, err := cli.ImageList(context.Background(), options)
36 38
 		c.Assert(err, checker.IsNil)
37 39
 
38 40
 		return images
... ...
@@ -74,6 +76,10 @@ func (s *DockerSuite) TestAPIImagesSaveAndLoad(c *check.C) {
74 74
 }
75 75
 
76 76
 func (s *DockerSuite) TestAPIImagesDelete(c *check.C) {
77
+	cli, err := client.NewEnvClient()
78
+	c.Assert(err, checker.IsNil)
79
+	defer cli.Close()
80
+
77 81
 	if testEnv.DaemonPlatform() != "windows" {
78 82
 		testRequires(c, Network)
79 83
 	}
... ...
@@ -83,20 +89,21 @@ func (s *DockerSuite) TestAPIImagesDelete(c *check.C) {
83 83
 
84 84
 	dockerCmd(c, "tag", name, "test:tag1")
85 85
 
86
-	status, _, err := request.SockRequest("DELETE", "/images/"+id, nil, daemonHost())
87
-	c.Assert(err, checker.IsNil)
88
-	c.Assert(status, checker.Equals, http.StatusConflict)
86
+	_, err = cli.ImageRemove(context.Background(), id, types.ImageRemoveOptions{})
87
+	c.Assert(err.Error(), checker.Contains, "unable to delete")
89 88
 
90
-	status, _, err = request.SockRequest("DELETE", "/images/test:noexist", nil, daemonHost())
91
-	c.Assert(err, checker.IsNil)
92
-	c.Assert(status, checker.Equals, http.StatusNotFound) //Status Codes:404 – no such image
89
+	_, err = cli.ImageRemove(context.Background(), "test:noexist", types.ImageRemoveOptions{})
90
+	c.Assert(err.Error(), checker.Contains, "No such image")
93 91
 
94
-	status, _, err = request.SockRequest("DELETE", "/images/test:tag1", nil, daemonHost())
92
+	_, err = cli.ImageRemove(context.Background(), "test:tag1", types.ImageRemoveOptions{})
95 93
 	c.Assert(err, checker.IsNil)
96
-	c.Assert(status, checker.Equals, http.StatusOK)
97 94
 }
98 95
 
99 96
 func (s *DockerSuite) TestAPIImagesHistory(c *check.C) {
97
+	cli, err := client.NewEnvClient()
98
+	c.Assert(err, checker.IsNil)
99
+	defer cli.Close()
100
+
100 101
 	if testEnv.DaemonPlatform() != "windows" {
101 102
 		testRequires(c, Network)
102 103
 	}
... ...
@@ -104,13 +111,8 @@ func (s *DockerSuite) TestAPIImagesHistory(c *check.C) {
104 104
 	buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
105 105
 	id := getIDByName(c, name)
106 106
 
107
-	status, body, err := request.SockRequest("GET", "/images/"+id+"/history", nil, daemonHost())
107
+	historydata, err := cli.ImageHistory(context.Background(), id)
108 108
 	c.Assert(err, checker.IsNil)
109
-	c.Assert(status, checker.Equals, http.StatusOK)
110
-
111
-	var historydata []image.HistoryResponseItem
112
-	err = json.Unmarshal(body, &historydata)
113
-	c.Assert(err, checker.IsNil, check.Commentf("Error on unmarshal"))
114 109
 
115 110
 	c.Assert(historydata, checker.Not(checker.HasLen), 0)
116 111
 	c.Assert(historydata[0].Tags[0], checker.Equals, "test-api-images-history:latest")
... ...
@@ -133,9 +135,8 @@ func (s *DockerSuite) TestAPIImagesImportBadSrc(c *check.C) {
133 133
 	}
134 134
 
135 135
 	for _, te := range tt {
136
-		res, b, err := request.SockRequestRaw("POST", strings.Join([]string{"/images/create?fromSrc=", te.fromSrc}, ""), nil, "application/json", daemonHost())
136
+		res, _, err := request.Post(strings.Join([]string{"/images/create?fromSrc=", te.fromSrc}, ""), request.JSON)
137 137
 		c.Assert(err, check.IsNil)
138
-		b.Close()
139 138
 		c.Assert(res.StatusCode, checker.Equals, te.statusExp)
140 139
 		c.Assert(res.Header.Get("Content-Type"), checker.Equals, "application/json")
141 140
 	}
... ...
@@ -156,11 +157,11 @@ func (s *DockerSuite) TestAPIImagesSearchJSONContentType(c *check.C) {
156 156
 // Test case for 30027: image size reported as -1 in v1.12 client against v1.13 daemon.
157 157
 // This test checks to make sure both v1.12 and v1.13 client against v1.13 daemon get correct `Size` after the fix.
158 158
 func (s *DockerSuite) TestAPIImagesSizeCompatibility(c *check.C) {
159
-	status, b, err := request.SockRequest("GET", "/images/json", nil, daemonHost())
159
+	cli, err := client.NewEnvClient()
160 160
 	c.Assert(err, checker.IsNil)
161
-	c.Assert(status, checker.Equals, http.StatusOK)
162
-	var images []types.ImageSummary
163
-	err = json.Unmarshal(b, &images)
161
+	defer cli.Close()
162
+
163
+	images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
164 164
 	c.Assert(err, checker.IsNil)
165 165
 	c.Assert(len(images), checker.Not(checker.Equals), 0)
166 166
 	for _, image := range images {
... ...
@@ -177,11 +178,13 @@ func (s *DockerSuite) TestAPIImagesSizeCompatibility(c *check.C) {
177 177
 		VirtualSize int64
178 178
 		Labels      map[string]string
179 179
 	}
180
-	status, b, err = request.SockRequest("GET", "/v1.24/images/json", nil, daemonHost())
180
+
181
+	var httpClient *http.Client
182
+	cli, err = client.NewClient(daemonHost(), "v1.24", httpClient, nil)
181 183
 	c.Assert(err, checker.IsNil)
182
-	c.Assert(status, checker.Equals, http.StatusOK)
183
-	var v124Images []v124Image
184
-	err = json.Unmarshal(b, &v124Images)
184
+	defer cli.Close()
185
+
186
+	v124Images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
185 187
 	c.Assert(err, checker.IsNil)
186 188
 	c.Assert(len(v124Images), checker.Not(checker.Equals), 0)
187 189
 	for _, image := range v124Images {
... ...
@@ -4,17 +4,23 @@ import (
4 4
 	"encoding/json"
5 5
 	"net/http"
6 6
 
7
+	"fmt"
8
+
7 9
 	"github.com/docker/docker/api/types"
10
+
11
+	"github.com/docker/docker/client"
8 12
 	"github.com/docker/docker/integration-cli/checker"
9 13
 	"github.com/docker/docker/integration-cli/request"
10 14
 	"github.com/go-check/check"
15
+	"golang.org/x/net/context"
11 16
 )
12 17
 
13 18
 func (s *DockerSuite) TestInfoAPI(c *check.C) {
14
-	endpoint := "/info"
19
+	cli, err := client.NewEnvClient()
20
+	c.Assert(err, checker.IsNil)
21
+	defer cli.Close()
15 22
 
16
-	status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost())
17
-	c.Assert(status, checker.Equals, http.StatusOK)
23
+	info, err := cli.Info(context.Background())
18 24
 	c.Assert(err, checker.IsNil)
19 25
 
20 26
 	// always shown fields
... ...
@@ -36,7 +42,7 @@ func (s *DockerSuite) TestInfoAPI(c *check.C) {
36 36
 		"ServerVersion",
37 37
 		"SecurityOptions"}
38 38
 
39
-	out := string(body)
39
+	out := fmt.Sprintf("%+v", info)
40 40
 	for _, linePrefix := range stringsToCheck {
41 41
 		c.Assert(out, checker.Contains, linePrefix)
42 42
 	}
... ...
@@ -63,13 +69,15 @@ func (s *DockerSuite) TestInfoAPIRuncCommit(c *check.C) {
63 63
 
64 64
 func (s *DockerSuite) TestInfoAPIVersioned(c *check.C) {
65 65
 	testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
66
-	endpoint := "/v1.20/info"
67 66
 
68
-	status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost())
69
-	c.Assert(status, checker.Equals, http.StatusOK)
67
+	res, body, err := request.Get("/v1.20/info")
68
+	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
69
+	c.Assert(err, checker.IsNil)
70
+
71
+	b, err := request.ReadBody(body)
70 72
 	c.Assert(err, checker.IsNil)
71 73
 
72
-	out := string(body)
74
+	out := string(b)
73 75
 	c.Assert(out, checker.Contains, "ExecutionDriver")
74 76
 	c.Assert(out, checker.Contains, "not supported")
75 77
 }
... ...
@@ -2,13 +2,14 @@ package main
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"net/http"
6 5
 	"strings"
7 6
 
7
+	"golang.org/x/net/context"
8
+
8 9
 	"github.com/docker/docker/api/types"
9 10
 	"github.com/docker/docker/api/types/versions/v1p20"
11
+	"github.com/docker/docker/client"
10 12
 	"github.com/docker/docker/integration-cli/checker"
11
-	"github.com/docker/docker/integration-cli/request"
12 13
 	"github.com/docker/docker/pkg/stringutils"
13 14
 	"github.com/go-check/check"
14 15
 )
... ...
@@ -106,18 +107,14 @@ func (s *DockerSuite) TestInspectAPIContainerVolumeDriver(c *check.C) {
106 106
 
107 107
 func (s *DockerSuite) TestInspectAPIImageResponse(c *check.C) {
108 108
 	dockerCmd(c, "tag", "busybox:latest", "busybox:mytag")
109
+	cli, err := client.NewEnvClient()
110
+	c.Assert(err, checker.IsNil)
111
+	defer cli.Close()
109 112
 
110
-	endpoint := "/images/busybox/json"
111
-	status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost())
112
-
113
+	imageJSON, _, err := cli.ImageInspectWithRaw(context.Background(), "busybox")
113 114
 	c.Assert(err, checker.IsNil)
114
-	c.Assert(status, checker.Equals, http.StatusOK)
115 115
 
116
-	var imageJSON types.ImageInspect
117
-	err = json.Unmarshal(body, &imageJSON)
118
-	c.Assert(err, checker.IsNil, check.Commentf("Unable to unmarshal body for latest version"))
119 116
 	c.Assert(imageJSON.RepoTags, checker.HasLen, 2)
120
-
121 117
 	c.Assert(stringutils.InSlice(imageJSON.RepoTags, "busybox:latest"), checker.Equals, true)
122 118
 	c.Assert(stringutils.InSlice(imageJSON.RepoTags, "busybox:mytag"), checker.Equals, true)
123 119
 }
... ...
@@ -4,12 +4,12 @@ package main
4 4
 
5 5
 import (
6 6
 	"encoding/json"
7
-	"fmt"
8 7
 	"net/http"
9 8
 
9
+	"github.com/docker/docker/client"
10 10
 	"github.com/docker/docker/integration-cli/checker"
11
-	"github.com/docker/docker/integration-cli/request"
12 11
 	"github.com/go-check/check"
12
+	"golang.org/x/net/context"
13 13
 )
14 14
 
15 15
 // #16665
... ...
@@ -19,9 +19,11 @@ func (s *DockerSuite) TestInspectAPICpusetInConfigPre120(c *check.C) {
19 19
 
20 20
 	name := "cpusetinconfig-pre120"
21 21
 	dockerCmd(c, "run", "--name", name, "--cpuset-cpus", "0", "busybox", "true")
22
-
23
-	status, body, err := request.SockRequest("GET", fmt.Sprintf("/v1.19/containers/%s/json", name), nil, daemonHost())
24
-	c.Assert(status, check.Equals, http.StatusOK)
22
+	var httpClient *http.Client
23
+	cli, err := client.NewClient(daemonHost(), "v1.19", httpClient, nil)
24
+	c.Assert(err, checker.IsNil)
25
+	defer cli.Close()
26
+	_, body, err := cli.ContainerInspectWithRaw(context.Background(), name, false)
25 27
 	c.Assert(err, check.IsNil)
26 28
 
27 29
 	var inspectJSON map[string]interface{}
... ...
@@ -7,9 +7,12 @@ import (
7 7
 	"strings"
8 8
 	"time"
9 9
 
10
+	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/client"
10 12
 	"github.com/docker/docker/integration-cli/checker"
11 13
 	"github.com/docker/docker/integration-cli/request"
12 14
 	"github.com/go-check/check"
15
+	"golang.org/x/net/context"
13 16
 )
14 17
 
15 18
 func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) {
... ...
@@ -51,13 +54,13 @@ func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) {
51 51
 func (s *DockerSuite) TestLogsAPINoStdoutNorStderr(c *check.C) {
52 52
 	name := "logs_test"
53 53
 	dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
54
-
55
-	status, body, err := request.SockRequest("GET", fmt.Sprintf("/containers/%s/logs", name), nil, daemonHost())
56
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
54
+	cli, err := client.NewEnvClient()
57 55
 	c.Assert(err, checker.IsNil)
56
+	defer cli.Close()
58 57
 
58
+	_, err = cli.ContainerLogs(context.Background(), name, types.ContainerLogsOptions{})
59 59
 	expected := "Bad parameters: you must choose at least one stream"
60
-	c.Assert(getErrorMessage(c, body), checker.Contains, expected)
60
+	c.Assert(err.Error(), checker.Contains, expected)
61 61
 }
62 62
 
63 63
 // Regression test for #12704
... ...
@@ -1,9 +1,12 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"context"
4 5
 	"net/http"
5 6
 	"strings"
6 7
 
8
+	"github.com/docker/docker/api/types"
9
+	"github.com/docker/docker/client"
7 10
 	"github.com/docker/docker/integration-cli/checker"
8 11
 	"github.com/docker/docker/integration-cli/request"
9 12
 	"github.com/go-check/check"
... ...
@@ -12,10 +15,15 @@ import (
12 12
 func (s *DockerSuite) TestResizeAPIResponse(c *check.C) {
13 13
 	out := runSleepingContainer(c, "-d")
14 14
 	cleanedContainerID := strings.TrimSpace(out)
15
-
16
-	endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40"
17
-	status, _, err := request.SockRequest("POST", endpoint, nil, daemonHost())
18
-	c.Assert(status, check.Equals, http.StatusOK)
15
+	cli, err := client.NewEnvClient()
16
+	c.Assert(err, checker.IsNil)
17
+	defer cli.Close()
18
+
19
+	options := types.ResizeOptions{
20
+		Height: 40,
21
+		Width:  40,
22
+	}
23
+	err = cli.ContainerResize(context.Background(), cleanedContainerID, options)
19 24
 	c.Assert(err, check.IsNil)
20 25
 }
21 26
 
... ...
@@ -24,8 +32,8 @@ func (s *DockerSuite) TestResizeAPIHeightWidthNoInt(c *check.C) {
24 24
 	cleanedContainerID := strings.TrimSpace(out)
25 25
 
26 26
 	endpoint := "/containers/" + cleanedContainerID + "/resize?h=foo&w=bar"
27
-	status, _, err := request.SockRequest("POST", endpoint, nil, daemonHost())
28
-	c.Assert(status, check.Equals, http.StatusBadRequest)
27
+	res, _, err := request.Post(endpoint)
28
+	c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
29 29
 	c.Assert(err, check.IsNil)
30 30
 }
31 31
 
... ...
@@ -36,10 +44,15 @@ func (s *DockerSuite) TestResizeAPIResponseWhenContainerNotStarted(c *check.C) {
36 36
 	// make sure the exited container is not running
37 37
 	dockerCmd(c, "wait", cleanedContainerID)
38 38
 
39
-	endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40"
40
-	status, body, err := request.SockRequest("POST", endpoint, nil, daemonHost())
41
-	c.Assert(status, check.Equals, http.StatusConflict)
42
-	c.Assert(err, check.IsNil)
39
+	cli, err := client.NewEnvClient()
40
+	c.Assert(err, checker.IsNil)
41
+	defer cli.Close()
42
+
43
+	options := types.ResizeOptions{
44
+		Height: 40,
45
+		Width:  40,
46
+	}
43 47
 
44
-	c.Assert(getErrorMessage(c, body), checker.Contains, "is not running", check.Commentf("resize should fail with message 'Container is not running'"))
48
+	err = cli.ContainerResize(context.Background(), cleanedContainerID, options)
49
+	c.Assert(err.Error(), checker.Contains, "is not running")
45 50
 }
... ...
@@ -13,9 +13,11 @@ import (
13 13
 
14 14
 	"github.com/docker/docker/api/types"
15 15
 	"github.com/docker/docker/api/types/versions"
16
+	"github.com/docker/docker/client"
16 17
 	"github.com/docker/docker/integration-cli/checker"
17 18
 	"github.com/docker/docker/integration-cli/request"
18 19
 	"github.com/go-check/check"
20
+	"golang.org/x/net/context"
19 21
 )
20 22
 
21 23
 var expectedNetworkInterfaceStats = strings.Split("rx_bytes rx_dropped rx_errors rx_packets tx_bytes tx_dropped tx_errors tx_packets", " ")
... ...
@@ -260,14 +262,16 @@ func jsonBlobHasGTE121NetworkStats(blob map[string]interface{}) bool {
260 260
 
261 261
 func (s *DockerSuite) TestAPIStatsContainerNotFound(c *check.C) {
262 262
 	testRequires(c, DaemonIsLinux)
263
-
264
-	status, _, err := request.SockRequest("GET", "/containers/nonexistent/stats", nil, daemonHost())
263
+	cli, err := client.NewEnvClient()
265 264
 	c.Assert(err, checker.IsNil)
266
-	c.Assert(status, checker.Equals, http.StatusNotFound)
265
+	defer cli.Close()
267 266
 
268
-	status, _, err = request.SockRequest("GET", "/containers/nonexistent/stats?stream=0", nil, daemonHost())
269
-	c.Assert(err, checker.IsNil)
270
-	c.Assert(status, checker.Equals, http.StatusNotFound)
267
+	expected := "No such container: nonexistent"
268
+
269
+	_, err = cli.ContainerStats(context.Background(), "nonexistent", true)
270
+	c.Assert(err.Error(), checker.Contains, expected)
271
+	_, err = cli.ContainerStats(context.Background(), "nonexistent", false)
272
+	c.Assert(err.Error(), checker.Contains, expected)
271 273
 }
272 274
 
273 275
 func (s *DockerSuite) TestAPIStatsNoStreamConnectedContainers(c *check.C) {
... ...
@@ -3,12 +3,10 @@
3 3
 package main
4 4
 
5 5
 import (
6
-	"fmt"
7
-	"net/http"
8
-
9 6
 	"github.com/docker/docker/api/types/swarm"
10 7
 	"github.com/docker/docker/integration-cli/checker"
11 8
 	"github.com/go-check/check"
9
+	"golang.org/x/net/context"
12 10
 )
13 11
 
14 12
 func (s *DockerSwarmSuite) TestAPISwarmConfigsEmptyList(c *check.C) {
... ...
@@ -52,9 +50,15 @@ func (s *DockerSwarmSuite) TestAPISwarmConfigsDelete(c *check.C) {
52 52
 	c.Assert(config.ID, checker.Equals, id, check.Commentf("config: %v", config))
53 53
 
54 54
 	d.DeleteConfig(c, config.ID)
55
-	status, out, err := d.SockRequest("GET", "/configs/"+id, nil)
55
+
56
+	cli, err := d.NewClient()
56 57
 	c.Assert(err, checker.IsNil)
57
-	c.Assert(status, checker.Equals, http.StatusNotFound, check.Commentf("config delete: %s", string(out)))
58
+	defer cli.Close()
59
+
60
+	expected := "no such config"
61
+
62
+	_, _, err = cli.ConfigInspectWithRaw(context.Background(), id)
63
+	c.Assert(err.Error(), checker.Contains, expected)
58 64
 }
59 65
 
60 66
 func (s *DockerSwarmSuite) TestAPISwarmConfigsUpdate(c *check.C) {
... ...
@@ -110,9 +114,12 @@ func (s *DockerSwarmSuite) TestAPISwarmConfigsUpdate(c *check.C) {
110 110
 	config = d.GetConfig(c, id)
111 111
 	config.Spec.Data = []byte("TESTINGDATA2")
112 112
 
113
-	url := fmt.Sprintf("/configs/%s/update?version=%d", config.ID, config.Version.Index)
114
-	status, out, err := d.SockRequest("POST", url, config.Spec)
113
+	cli, err := d.NewClient()
114
+	c.Assert(err, checker.IsNil)
115
+	defer cli.Close()
116
+
117
+	expected := "only updates to Labels are allowed"
115 118
 
116
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
117
-	c.Assert(status, checker.Equals, http.StatusBadRequest, check.Commentf("output: %q", string(out)))
119
+	err = cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
120
+	c.Assert(err.Error(), checker.Contains, expected)
118 121
 }
... ...
@@ -3,12 +3,12 @@
3 3
 package main
4 4
 
5 5
 import (
6
-	"fmt"
7 6
 	"net/http"
8 7
 
9 8
 	"github.com/docker/docker/api/types/swarm"
10 9
 	"github.com/docker/docker/integration-cli/checker"
11 10
 	"github.com/go-check/check"
11
+	"golang.org/x/net/context"
12 12
 )
13 13
 
14 14
 func (s *DockerSwarmSuite) TestAPISwarmSecretsEmptyList(c *check.C) {
... ...
@@ -59,16 +59,19 @@ func (s *DockerSwarmSuite) TestAPISwarmSecretsDelete(c *check.C) {
59 59
 	c.Assert(secret.ID, checker.Equals, id, check.Commentf("secret: %v", secret))
60 60
 
61 61
 	d.DeleteSecret(c, secret.ID)
62
-	status, out, err := d.SockRequest("GET", "/secrets/"+id, nil)
63
-	c.Assert(err, checker.IsNil)
64
-	c.Assert(status, checker.Equals, http.StatusNotFound, check.Commentf("secret delete: %s", string(out)))
65 62
 
66
-	// delete non-existing secret, daemon should return a status code of 404
67
-	id = "non-existing"
68
-	status, out, err = d.SockRequest("DELETE", "/secrets/"+id, nil)
63
+	cli, err := d.NewClient()
69 64
 	c.Assert(err, checker.IsNil)
70
-	c.Assert(status, checker.Equals, http.StatusNotFound, check.Commentf("secret delete: %s", string(out)))
65
+	defer cli.Close()
66
+
67
+	expected := "no such secret"
68
+	_, _, err = cli.SecretInspectWithRaw(context.Background(), id)
69
+	c.Assert(err.Error(), checker.Contains, expected)
71 70
 
71
+	id = "non-existing"
72
+	expected = "secret non-existing not found"
73
+	err = cli.SecretRemove(context.Background(), id)
74
+	c.Assert(err.Error(), checker.Contains, expected)
72 75
 }
73 76
 
74 77
 func (s *DockerSwarmSuite) TestAPISwarmSecretsUpdate(c *check.C) {
... ...
@@ -124,9 +127,12 @@ func (s *DockerSwarmSuite) TestAPISwarmSecretsUpdate(c *check.C) {
124 124
 	secret = d.GetSecret(c, id)
125 125
 	secret.Spec.Data = []byte("TESTINGDATA2")
126 126
 
127
-	url := fmt.Sprintf("/secrets/%s/update?version=%d", secret.ID, secret.Version.Index)
128
-	status, out, err := d.SockRequest("POST", url, secret.Spec)
127
+	cli, err := d.NewClient()
128
+	c.Assert(err, checker.IsNil)
129
+	defer cli.Close()
130
+
131
+	expected := "only updates to Labels are allowed"
129 132
 
130
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
131
-	c.Assert(status, checker.Equals, http.StatusBadRequest, check.Commentf("output: %q", string(out)))
133
+	err = cli.SecretUpdate(context.Background(), secret.ID, secret.Version, secret.Spec)
134
+	c.Assert(err.Error(), checker.Contains, expected)
132 135
 }
... ...
@@ -9,6 +9,7 @@ import (
9 9
 	"strings"
10 10
 	"time"
11 11
 
12
+	"github.com/docker/docker/api/types"
12 13
 	"github.com/docker/docker/api/types/swarm"
13 14
 	"github.com/docker/docker/api/types/swarm/runtime"
14 15
 	"github.com/docker/docker/integration-cli/checker"
... ...
@@ -64,14 +65,24 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesCreate(c *check.C) {
64 64
 	id := d.CreateService(c, simpleTestService, setInstances(instances))
65 65
 	waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, instances)
66 66
 
67
+	cli, err := d.NewClient()
68
+	c.Assert(err, checker.IsNil)
69
+	defer cli.Close()
70
+
71
+	options := types.ServiceInspectOptions{
72
+		InsertDefaults: true,
73
+	}
74
+
67 75
 	// insertDefaults inserts UpdateConfig when service is fetched by ID
68
-	_, out, err := d.SockRequest("GET", "/services/"+id+"?insertDefaults=true", nil)
69
-	c.Assert(err, checker.IsNil, check.Commentf("%s", out))
70
-	c.Assert(string(out), checker.Contains, "UpdateConfig")
76
+	resp, _, err := cli.ServiceInspectWithRaw(context.Background(), id, options)
77
+	out := fmt.Sprintf("%+v", resp)
78
+	c.Assert(err, checker.IsNil)
79
+	c.Assert(out, checker.Contains, "UpdateConfig")
71 80
 
72 81
 	// insertDefaults inserts UpdateConfig when service is fetched by ID
73
-	_, out, err = d.SockRequest("GET", "/services/top?insertDefaults=true", nil)
74
-	c.Assert(err, checker.IsNil, check.Commentf("%s", out))
82
+	resp, _, err = cli.ServiceInspectWithRaw(context.Background(), "top", options)
83
+	out = fmt.Sprintf("%+v", resp)
84
+	c.Assert(err, checker.IsNil)
75 85
 	c.Assert(string(out), checker.Contains, "UpdateConfig")
76 86
 
77 87
 	service := d.GetService(c, id)
... ...
@@ -195,7 +206,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesUpdateStartFirst(c *check.C) {
195 195
 	// service image at start
196 196
 	image1 := "busybox:latest"
197 197
 	// target image in update
198
-	image2 := "testhealth"
198
+	image2 := "testhealth:latest"
199 199
 
200 200
 	// service started from this image won't pass health check
201 201
 	_, _, err := d.BuildImageWithOut(image2,
... ...
@@ -23,8 +23,10 @@ import (
23 23
 	"github.com/docker/docker/api/types/swarm"
24 24
 	"github.com/docker/docker/integration-cli/checker"
25 25
 	"github.com/docker/docker/integration-cli/daemon"
26
+	"github.com/docker/docker/integration-cli/request"
26 27
 	"github.com/docker/swarmkit/ca"
27 28
 	"github.com/go-check/check"
29
+	"golang.org/x/net/context"
28 30
 )
29 31
 
30 32
 var defaultReconciliationTimeout = 30 * time.Second
... ...
@@ -228,17 +230,20 @@ func (s *DockerSwarmSuite) TestAPISwarmPromoteDemote(c *check.C) {
228 228
 	node := d1.GetNode(c, d1.NodeID)
229 229
 	node.Spec.Role = swarm.NodeRoleWorker
230 230
 	url := fmt.Sprintf("/nodes/%s/update?version=%d", node.ID, node.Version.Index)
231
-	status, out, err := d1.SockRequest("POST", url, node.Spec)
231
+	res, body, err := request.DoOnHost(d1.Sock(), url, request.Method("POST"), request.JSONBody(node.Spec))
232 232
 	c.Assert(err, checker.IsNil)
233
-	c.Assert(status, checker.Equals, http.StatusBadRequest, check.Commentf("output: %q", string(out)))
233
+	b, err := request.ReadBody(body)
234
+	c.Assert(err, checker.IsNil)
235
+	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest, check.Commentf("output: %q", string(b)))
236
+
234 237
 	// The warning specific to demoting the last manager is best-effort and
235 238
 	// won't appear until the Role field of the demoted manager has been
236 239
 	// updated.
237 240
 	// Yes, I know this looks silly, but checker.Matches is broken, since
238 241
 	// it anchors the regexp contrary to the documentation, and this makes
239 242
 	// it impossible to match something that includes a line break.
240
-	if !strings.Contains(string(out), "last manager of the swarm") {
241
-		c.Assert(string(out), checker.Contains, "this would result in a loss of quorum")
243
+	if !strings.Contains(string(b), "last manager of the swarm") {
244
+		c.Assert(string(b), checker.Contains, "this would result in a loss of quorum")
242 245
 	}
243 246
 	info, err = d1.SwarmInfo()
244 247
 	c.Assert(err, checker.IsNil)
... ...
@@ -362,9 +367,11 @@ func (s *DockerSwarmSuite) TestAPISwarmRaftQuorum(c *check.C) {
362 362
 	var service swarm.Service
363 363
 	simpleTestService(&service)
364 364
 	service.Spec.Name = "top2"
365
-	status, out, err := d1.SockRequest("POST", "/services/create", service.Spec)
365
+	cli, err := d1.NewClient()
366 366
 	c.Assert(err, checker.IsNil)
367
-	c.Assert(status, checker.Equals, http.StatusInternalServerError, check.Commentf("deadline exceeded", string(out)))
367
+	defer cli.Close()
368
+	_, err = cli.ServiceCreate(context.Background(), service.Spec, types.ServiceCreateOptions{})
369
+	c.Assert(err.Error(), checker.Contains, "deadline exceeded")
368 370
 
369 371
 	d2.Start(c)
370 372
 
... ...
@@ -505,17 +512,17 @@ func (s *DockerSwarmSuite) TestAPISwarmInvalidAddress(c *check.C) {
505 505
 	req := swarm.InitRequest{
506 506
 		ListenAddr: "",
507 507
 	}
508
-	status, _, err := d.SockRequest("POST", "/swarm/init", req)
508
+	res, _, err := request.DoOnHost(d.Sock(), "/swarm/init", request.Method("POST"), request.JSONBody(req))
509 509
 	c.Assert(err, checker.IsNil)
510
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
510
+	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
511 511
 
512 512
 	req2 := swarm.JoinRequest{
513 513
 		ListenAddr:  "0.0.0.0:2377",
514 514
 		RemoteAddrs: []string{""},
515 515
 	}
516
-	status, _, err = d.SockRequest("POST", "/swarm/join", req2)
516
+	res, _, err = request.DoOnHost(d.Sock(), "/swarm/join", request.Method("POST"), request.JSONBody(req2))
517 517
 	c.Assert(err, checker.IsNil)
518
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
518
+	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
519 519
 }
520 520
 
521 521
 func (s *DockerSwarmSuite) TestAPISwarmForceNewCluster(c *check.C) {
... ...
@@ -836,10 +843,11 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesUpdateWithName(c *check.C) {
836 836
 	instances = 5
837 837
 
838 838
 	setInstances(instances)(service)
839
-	url := fmt.Sprintf("/services/%s/update?version=%d", service.Spec.Name, service.Version.Index)
840
-	status, out, err := d.SockRequest("POST", url, service.Spec)
839
+	cli, err := d.NewClient()
840
+	c.Assert(err, checker.IsNil)
841
+	defer cli.Close()
842
+	_, err = cli.ServiceUpdate(context.Background(), service.Spec.Name, service.Version, service.Spec, types.ServiceUpdateOptions{})
841 843
 	c.Assert(err, checker.IsNil)
842
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
843 844
 	waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, instances)
844 845
 }
845 846
 
... ...
@@ -867,51 +875,31 @@ func (s *DockerSwarmSuite) TestAPISwarmErrorHandling(c *check.C) {
867 867
 // This test makes sure the fixes correctly output scopes instead.
868 868
 func (s *DockerSwarmSuite) TestAPIDuplicateNetworks(c *check.C) {
869 869
 	d := s.AddDaemon(c, true, true)
870
+	cli, err := d.NewClient()
871
+	c.Assert(err, checker.IsNil)
872
+	defer cli.Close()
870 873
 
871 874
 	name := "foo"
872
-	networkCreateRequest := types.NetworkCreateRequest{
873
-		Name: name,
874
-		NetworkCreate: types.NetworkCreate{
875
-			CheckDuplicate: false,
876
-		},
875
+	networkCreate := types.NetworkCreate{
876
+		CheckDuplicate: false,
877 877
 	}
878 878
 
879
-	var n1 types.NetworkCreateResponse
880
-	networkCreateRequest.NetworkCreate.Driver = "bridge"
879
+	networkCreate.Driver = "bridge"
881 880
 
882
-	status, out, err := d.SockRequest("POST", "/networks/create", networkCreateRequest)
883
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
884
-	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(out)))
885
-
886
-	c.Assert(json.Unmarshal(out, &n1), checker.IsNil)
887
-
888
-	var n2 types.NetworkCreateResponse
889
-	networkCreateRequest.NetworkCreate.Driver = "overlay"
890
-
891
-	status, out, err = d.SockRequest("POST", "/networks/create", networkCreateRequest)
892
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
893
-	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(out)))
894
-
895
-	c.Assert(json.Unmarshal(out, &n2), checker.IsNil)
881
+	n1, err := cli.NetworkCreate(context.Background(), name, networkCreate)
882
+	c.Assert(err, checker.IsNil)
896 883
 
897
-	var r1 types.NetworkResource
884
+	networkCreate.Driver = "overlay"
898 885
 
899
-	status, out, err = d.SockRequest("GET", "/networks/"+n1.ID, nil)
900
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
901
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(out)))
902
-
903
-	c.Assert(json.Unmarshal(out, &r1), checker.IsNil)
886
+	n2, err := cli.NetworkCreate(context.Background(), name, networkCreate)
887
+	c.Assert(err, checker.IsNil)
904 888
 
889
+	r1, err := cli.NetworkInspect(context.Background(), n1.ID, types.NetworkInspectOptions{})
890
+	c.Assert(err, checker.IsNil)
905 891
 	c.Assert(r1.Scope, checker.Equals, "local")
906 892
 
907
-	var r2 types.NetworkResource
908
-
909
-	status, out, err = d.SockRequest("GET", "/networks/"+n2.ID, nil)
910
-	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
911
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(out)))
912
-
913
-	c.Assert(json.Unmarshal(out, &r2), checker.IsNil)
914
-
893
+	r2, err := cli.NetworkInspect(context.Background(), n2.ID, types.NetworkInspectOptions{})
894
+	c.Assert(err, checker.IsNil)
915 895
 	c.Assert(r2.Scope, checker.Equals, "swarm")
916 896
 }
917 897
 
... ...
@@ -5,9 +5,11 @@ package main
5 5
 import (
6 6
 	"strings"
7 7
 
8
+	"github.com/docker/docker/api/types/container"
9
+	"github.com/docker/docker/client"
8 10
 	"github.com/docker/docker/integration-cli/checker"
9
-	"github.com/docker/docker/integration-cli/request"
10 11
 	"github.com/go-check/check"
12
+	"golang.org/x/net/context"
11 13
 )
12 14
 
13 15
 func (s *DockerSuite) TestAPIUpdateContainer(c *check.C) {
... ...
@@ -16,12 +18,19 @@ func (s *DockerSuite) TestAPIUpdateContainer(c *check.C) {
16 16
 	testRequires(c, swapMemorySupport)
17 17
 
18 18
 	name := "apiUpdateContainer"
19
-	hostConfig := map[string]interface{}{
20
-		"Memory":     314572800,
21
-		"MemorySwap": 524288000,
19
+	updateConfig := container.UpdateConfig{
20
+		Resources: container.Resources{
21
+			Memory:     314572800,
22
+			MemorySwap: 524288000,
23
+		},
22 24
 	}
23 25
 	dockerCmd(c, "run", "-d", "--name", name, "-m", "200M", "busybox", "top")
24
-	_, _, err := request.SockRequest("POST", "/containers/"+name+"/update", hostConfig, daemonHost())
26
+	cli, err := client.NewEnvClient()
27
+	c.Assert(err, check.IsNil)
28
+	defer cli.Close()
29
+
30
+	_, err = cli.ContainerUpdate(context.Background(), name, updateConfig)
31
+
25 32
 	c.Assert(err, check.IsNil)
26 33
 
27 34
 	c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "314572800")
... ...
@@ -1,24 +1,18 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"encoding/json"
5
-	"net/http"
6
-
7
-	"github.com/docker/docker/api/types"
4
+	"github.com/docker/docker/client"
8 5
 	"github.com/docker/docker/dockerversion"
9 6
 	"github.com/docker/docker/integration-cli/checker"
10
-	"github.com/docker/docker/integration-cli/request"
11 7
 	"github.com/go-check/check"
8
+	"golang.org/x/net/context"
12 9
 )
13 10
 
14 11
 func (s *DockerSuite) TestGetVersion(c *check.C) {
15
-	status, body, err := request.SockRequest("GET", "/version", nil, daemonHost())
16
-	c.Assert(status, checker.Equals, http.StatusOK)
12
+	cli, err := client.NewEnvClient()
17 13
 	c.Assert(err, checker.IsNil)
14
+	defer cli.Close()
18 15
 
19
-	var v types.Version
20
-
21
-	c.Assert(json.Unmarshal(body, &v), checker.IsNil)
22
-
16
+	v, err := cli.ServerVersion(context.Background())
23 17
 	c.Assert(v.Version, checker.Equals, dockerversion.Version, check.Commentf("Version mismatch"))
24 18
 }
... ...
@@ -1,30 +1,29 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"encoding/json"
5 4
 	"fmt"
6
-	"net/http"
7 5
 	"path/filepath"
8 6
 	"strings"
9 7
 	"time"
10 8
 
11
-	"github.com/docker/docker/api/types"
9
+	"github.com/docker/docker/api/types/filters"
12 10
 	volumetypes "github.com/docker/docker/api/types/volume"
11
+	"github.com/docker/docker/client"
13 12
 	"github.com/docker/docker/integration-cli/checker"
14
-	"github.com/docker/docker/integration-cli/request"
15 13
 	"github.com/go-check/check"
14
+	"golang.org/x/net/context"
16 15
 )
17 16
 
18 17
 func (s *DockerSuite) TestVolumesAPIList(c *check.C) {
19 18
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
20 19
 	dockerCmd(c, "run", "-v", prefix+"/foo", "busybox")
21 20
 
22
-	status, b, err := request.SockRequest("GET", "/volumes", nil, daemonHost())
21
+	cli, err := client.NewEnvClient()
23 22
 	c.Assert(err, checker.IsNil)
24
-	c.Assert(status, checker.Equals, http.StatusOK)
23
+	defer cli.Close()
25 24
 
26
-	var volumes volumetypes.VolumesListOKBody
27
-	c.Assert(json.Unmarshal(b, &volumes), checker.IsNil)
25
+	volumes, err := cli.VolumeList(context.Background(), filters.Args{})
26
+	c.Assert(err, checker.IsNil)
28 27
 
29 28
 	c.Assert(len(volumes.Volumes), checker.Equals, 1, check.Commentf("\n%v", volumes.Volumes))
30 29
 }
... ...
@@ -33,13 +32,13 @@ func (s *DockerSuite) TestVolumesAPICreate(c *check.C) {
33 33
 	config := volumetypes.VolumesCreateBody{
34 34
 		Name: "test",
35 35
 	}
36
-	status, b, err := request.SockRequest("POST", "/volumes/create", config, daemonHost())
37
-	c.Assert(err, check.IsNil)
38
-	c.Assert(status, check.Equals, http.StatusCreated, check.Commentf(string(b)))
39 36
 
40
-	var vol types.Volume
41
-	err = json.Unmarshal(b, &vol)
37
+	cli, err := client.NewEnvClient()
42 38
 	c.Assert(err, checker.IsNil)
39
+	defer cli.Close()
40
+
41
+	vol, err := cli.VolumeCreate(context.Background(), config)
42
+	c.Assert(err, check.IsNil)
43 43
 
44 44
 	c.Assert(filepath.Base(filepath.Dir(vol.Mountpoint)), checker.Equals, config.Name)
45 45
 }
... ...
@@ -48,49 +47,43 @@ func (s *DockerSuite) TestVolumesAPIRemove(c *check.C) {
48 48
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
49 49
 	dockerCmd(c, "run", "-v", prefix+"/foo", "--name=test", "busybox")
50 50
 
51
-	status, b, err := request.SockRequest("GET", "/volumes", nil, daemonHost())
51
+	cli, err := client.NewEnvClient()
52 52
 	c.Assert(err, checker.IsNil)
53
-	c.Assert(status, checker.Equals, http.StatusOK)
53
+	defer cli.Close()
54 54
 
55
-	var volumes volumetypes.VolumesListOKBody
56
-	c.Assert(json.Unmarshal(b, &volumes), checker.IsNil)
57
-	c.Assert(len(volumes.Volumes), checker.Equals, 1, check.Commentf("\n%v", volumes.Volumes))
55
+	volumes, err := cli.VolumeList(context.Background(), filters.Args{})
56
+	c.Assert(err, checker.IsNil)
58 57
 
59 58
 	v := volumes.Volumes[0]
60
-	status, _, err = request.SockRequest("DELETE", "/volumes/"+v.Name, nil, daemonHost())
61
-	c.Assert(err, checker.IsNil)
62
-	c.Assert(status, checker.Equals, http.StatusConflict, check.Commentf("Should not be able to remove a volume that is in use"))
59
+	err = cli.VolumeRemove(context.Background(), v.Name, false)
60
+	c.Assert(err.Error(), checker.Contains, "volume is in use")
63 61
 
64 62
 	dockerCmd(c, "rm", "-f", "test")
65
-	status, data, err := request.SockRequest("DELETE", "/volumes/"+v.Name, nil, daemonHost())
63
+	err = cli.VolumeRemove(context.Background(), v.Name, false)
66 64
 	c.Assert(err, checker.IsNil)
67
-	c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf(string(data)))
68
-
69 65
 }
70 66
 
71 67
 func (s *DockerSuite) TestVolumesAPIInspect(c *check.C) {
72 68
 	config := volumetypes.VolumesCreateBody{
73 69
 		Name: "test",
74 70
 	}
71
+
75 72
 	// sampling current time minus a minute so to now have false positive in case of delays
76 73
 	now := time.Now().Truncate(time.Minute)
77
-	status, b, err := request.SockRequest("POST", "/volumes/create", config, daemonHost())
78
-	c.Assert(err, check.IsNil)
79
-	c.Assert(status, check.Equals, http.StatusCreated, check.Commentf(string(b)))
80 74
 
81
-	status, b, err = request.SockRequest("GET", "/volumes", nil, daemonHost())
75
+	cli, err := client.NewEnvClient()
82 76
 	c.Assert(err, checker.IsNil)
83
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(b)))
77
+	defer cli.Close()
84 78
 
85
-	var volumes volumetypes.VolumesListOKBody
86
-	c.Assert(json.Unmarshal(b, &volumes), checker.IsNil)
79
+	_, err = cli.VolumeCreate(context.Background(), config)
80
+	c.Assert(err, check.IsNil)
81
+
82
+	volumes, err := cli.VolumeList(context.Background(), filters.Args{})
83
+	c.Assert(err, checker.IsNil)
87 84
 	c.Assert(len(volumes.Volumes), checker.Equals, 1, check.Commentf("\n%v", volumes.Volumes))
88 85
 
89
-	var vol types.Volume
90
-	status, b, err = request.SockRequest("GET", "/volumes/"+config.Name, nil, daemonHost())
86
+	vol, err := cli.VolumeInspect(context.Background(), config.Name)
91 87
 	c.Assert(err, checker.IsNil)
92
-	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(b)))
93
-	c.Assert(json.Unmarshal(b, &vol), checker.IsNil)
94 88
 	c.Assert(vol.Name, checker.Equals, config.Name)
95 89
 
96 90
 	// comparing CreatedAt field time for the new volume to now. Removing a minute from both to avoid false positive
... ...
@@ -6,20 +6,22 @@ import (
6 6
 	"fmt"
7 7
 	"io"
8 8
 	"io/ioutil"
9
-	"net/http"
10 9
 	"os"
11 10
 	"os/exec"
12 11
 	"strings"
13 12
 	"time"
14 13
 
14
+	"github.com/docker/docker/api/types"
15 15
 	eventtypes "github.com/docker/docker/api/types/events"
16
+	"github.com/docker/docker/client"
16 17
 	eventstestutils "github.com/docker/docker/daemon/events/testutils"
17 18
 	"github.com/docker/docker/integration-cli/checker"
18 19
 	"github.com/docker/docker/integration-cli/cli"
19 20
 	"github.com/docker/docker/integration-cli/cli/build"
20
-	"github.com/docker/docker/integration-cli/request"
21
+
21 22
 	icmd "github.com/docker/docker/pkg/testutil/cmd"
22 23
 	"github.com/go-check/check"
24
+	"golang.org/x/net/context"
23 25
 )
24 26
 
25 27
 func (s *DockerSuite) TestEventsTimestampFormats(c *check.C) {
... ...
@@ -498,9 +500,15 @@ func (s *DockerSuite) TestEventsResize(c *check.C) {
498 498
 	cID := strings.TrimSpace(out)
499 499
 	c.Assert(waitRun(cID), checker.IsNil)
500 500
 
501
-	endpoint := "/containers/" + cID + "/resize?h=80&w=24"
502
-	status, _, err := request.SockRequest("POST", endpoint, nil, daemonHost())
503
-	c.Assert(status, checker.Equals, http.StatusOK)
501
+	cli, err := client.NewEnvClient()
502
+	c.Assert(err, checker.IsNil)
503
+	defer cli.Close()
504
+
505
+	options := types.ResizeOptions{
506
+		Height: 80,
507
+		Width:  24,
508
+	}
509
+	err = cli.ContainerResize(context.Background(), cID, options)
504 510
 	c.Assert(err, checker.IsNil)
505 511
 
506 512
 	dockerCmd(c, "stop", cID)
... ...
@@ -5,7 +5,6 @@ package main
5 5
 import (
6 6
 	"bufio"
7 7
 	"fmt"
8
-	"net/http"
9 8
 	"os"
10 9
 	"os/exec"
11 10
 	"reflect"
... ...
@@ -15,12 +14,13 @@ import (
15 15
 	"sync"
16 16
 	"time"
17 17
 
18
+	"github.com/docker/docker/client"
18 19
 	"github.com/docker/docker/integration-cli/checker"
19 20
 	"github.com/docker/docker/integration-cli/cli"
20 21
 	"github.com/docker/docker/integration-cli/cli/build"
21
-	"github.com/docker/docker/integration-cli/request"
22 22
 	icmd "github.com/docker/docker/pkg/testutil/cmd"
23 23
 	"github.com/go-check/check"
24
+	"golang.org/x/net/context"
24 25
 )
25 26
 
26 27
 func (s *DockerSuite) TestExec(c *check.C) {
... ...
@@ -357,16 +357,21 @@ func (s *DockerSuite) TestExecInspectID(c *check.C) {
357 357
 	}
358 358
 
359 359
 	// But we should still be able to query the execID
360
-	sc, body, _ := request.SockRequest("GET", "/exec/"+execID+"/json", nil, daemonHost())
360
+	cli, err := client.NewEnvClient()
361
+	c.Assert(err, checker.IsNil)
362
+	defer cli.Close()
361 363
 
362
-	c.Assert(sc, checker.Equals, http.StatusOK, check.Commentf("received status != 200 OK: %d\n%s", sc, body))
364
+	_, err = cli.ContainerExecInspect(context.Background(), execID)
365
+	c.Assert(err, checker.IsNil)
363 366
 
364 367
 	// Now delete the container and then an 'inspect' on the exec should
365 368
 	// result in a 404 (not 'container not running')
366 369
 	out, ec := dockerCmd(c, "rm", "-f", id)
367 370
 	c.Assert(ec, checker.Equals, 0, check.Commentf("error removing container: %s", out))
368
-	sc, body, _ = request.SockRequest("GET", "/exec/"+execID+"/json", nil, daemonHost())
369
-	c.Assert(sc, checker.Equals, http.StatusNotFound, check.Commentf("received status != 404: %d\n%s", sc, body))
371
+
372
+	_, err = cli.ContainerExecInspect(context.Background(), execID)
373
+	expected := "No such exec instance"
374
+	c.Assert(err.Error(), checker.Contains, expected)
370 375
 }
371 376
 
372 377
 func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
... ...
@@ -1,16 +1,16 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"fmt"
5 4
 	"net/http"
6 5
 	"strings"
7 6
 	"time"
8 7
 
8
+	"github.com/docker/docker/client"
9 9
 	"github.com/docker/docker/integration-cli/checker"
10 10
 	"github.com/docker/docker/integration-cli/cli"
11
-	"github.com/docker/docker/integration-cli/request"
12 11
 	icmd "github.com/docker/docker/pkg/testutil/cmd"
13 12
 	"github.com/go-check/check"
13
+	"golang.org/x/net/context"
14 14
 )
15 15
 
16 16
 func (s *DockerSuite) TestKillContainer(c *check.C) {
... ...
@@ -131,8 +131,10 @@ func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
131 131
 	testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
132 132
 	runSleepingContainer(c, "--name", "docker-kill-test-api", "-d")
133 133
 	dockerCmd(c, "stop", "docker-kill-test-api")
134
-
135
-	status, _, err := request.SockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil, daemonHost())
134
+	var httpClient *http.Client
135
+	cli, err := client.NewClient(daemonHost(), "v1.19", httpClient, nil)
136
+	c.Assert(err, check.IsNil)
137
+	defer cli.Close()
138
+	err = cli.ContainerKill(context.Background(), "docker-kill-test-api", "SIGKILL")
136 139
 	c.Assert(err, check.IsNil)
137
-	c.Assert(status, check.Equals, http.StatusNoContent)
138 140
 }
... ...
@@ -1,14 +1,12 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"encoding/json"
5
-	"net/http"
6 4
 	"strings"
7 5
 
8
-	"github.com/docker/docker/api/types"
6
+	"github.com/docker/docker/client"
9 7
 	"github.com/docker/docker/integration-cli/checker"
10
-	"github.com/docker/docker/integration-cli/request"
11 8
 	"github.com/go-check/check"
9
+	"golang.org/x/net/context"
12 10
 )
13 11
 
14 12
 func (s *DockerSuite) TestPluginLogDriver(c *check.C) {
... ...
@@ -36,13 +34,14 @@ func (s *DockerSuite) TestPluginLogDriverInfoList(c *check.C) {
36 36
 	pluginName := "cpuguy83/docker-logdriver-test"
37 37
 
38 38
 	dockerCmd(c, "plugin", "install", pluginName)
39
-	status, body, err := request.SockRequest("GET", "/info", nil, daemonHost())
40
-	c.Assert(status, checker.Equals, http.StatusOK)
39
+
40
+	cli, err := client.NewEnvClient()
41 41
 	c.Assert(err, checker.IsNil)
42
+	defer cli.Close()
42 43
 
43
-	var info types.Info
44
-	err = json.Unmarshal(body, &info)
44
+	info, err := cli.Info(context.Background())
45 45
 	c.Assert(err, checker.IsNil)
46
+
46 47
 	drivers := strings.Join(info.Plugins.Log, " ")
47 48
 	c.Assert(drivers, checker.Contains, "json-file")
48 49
 	c.Assert(drivers, checker.Not(checker.Contains), pluginName)
... ...
@@ -29,6 +29,7 @@ import (
29 29
 	remoteipam "github.com/docker/libnetwork/ipams/remote/api"
30 30
 	"github.com/go-check/check"
31 31
 	"github.com/vishvananda/netlink"
32
+	"golang.org/x/net/context"
32 33
 )
33 34
 
34 35
 func (s *DockerSwarmSuite) TestSwarmUpdate(c *check.C) {
... ...
@@ -1843,19 +1844,17 @@ func (s *DockerSwarmSuite) TestNetworkInspectWithDuplicateNames(c *check.C) {
1843 1843
 	d := s.AddDaemon(c, true, true)
1844 1844
 
1845 1845
 	name := "foo"
1846
-	networkCreateRequest := types.NetworkCreateRequest{
1847
-		Name: name,
1848
-		NetworkCreate: types.NetworkCreate{
1849
-			CheckDuplicate: false,
1850
-			Driver:         "bridge",
1851
-		},
1846
+	options := types.NetworkCreate{
1847
+		CheckDuplicate: false,
1848
+		Driver:         "bridge",
1852 1849
 	}
1853 1850
 
1854
-	var n1 types.NetworkCreateResponse
1855
-	status, body, err := d.SockRequest("POST", "/networks/create", networkCreateRequest)
1856
-	c.Assert(err, checker.IsNil, check.Commentf(string(body)))
1857
-	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(body)))
1858
-	c.Assert(json.Unmarshal(body, &n1), checker.IsNil)
1851
+	cli, err := d.NewClient()
1852
+	c.Assert(err, checker.IsNil)
1853
+	defer cli.Close()
1854
+
1855
+	n1, err := cli.NetworkCreate(context.Background(), name, options)
1856
+	c.Assert(err, checker.IsNil)
1859 1857
 
1860 1858
 	// Full ID always works
1861 1859
 	out, err := d.Cmd("network", "inspect", "--format", "{{.ID}}", n1.ID)
... ...
@@ -1867,12 +1866,8 @@ func (s *DockerSwarmSuite) TestNetworkInspectWithDuplicateNames(c *check.C) {
1867 1867
 	c.Assert(err, checker.IsNil, check.Commentf(out))
1868 1868
 	c.Assert(strings.TrimSpace(out), checker.Equals, n1.ID)
1869 1869
 
1870
-	var n2 types.NetworkCreateResponse
1871
-	status, body, err = d.SockRequest("POST", "/networks/create", networkCreateRequest)
1872
-	c.Assert(err, checker.IsNil, check.Commentf(string(body)))
1873
-	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(body)))
1874
-	c.Assert(json.Unmarshal(body, &n2), checker.IsNil)
1875
-
1870
+	n2, err := cli.NetworkCreate(context.Background(), name, options)
1871
+	c.Assert(err, checker.IsNil)
1876 1872
 	// Full ID always works
1877 1873
 	out, err = d.Cmd("network", "inspect", "--format", "{{.ID}}", n1.ID)
1878 1874
 	c.Assert(err, checker.IsNil, check.Commentf(out))
... ...
@@ -1890,13 +1885,11 @@ func (s *DockerSwarmSuite) TestNetworkInspectWithDuplicateNames(c *check.C) {
1890 1890
 	out, err = d.Cmd("network", "rm", n2.ID)
1891 1891
 	c.Assert(err, checker.IsNil, check.Commentf(out))
1892 1892
 
1893
-	// Duplicates with name but with different driver
1894
-	networkCreateRequest.NetworkCreate.Driver = "overlay"
1893
+	// Dupliates with name but with different driver
1894
+	options.Driver = "overlay"
1895 1895
 
1896
-	status, body, err = d.SockRequest("POST", "/networks/create", networkCreateRequest)
1897
-	c.Assert(err, checker.IsNil, check.Commentf(string(body)))
1898
-	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(body)))
1899
-	c.Assert(json.Unmarshal(body, &n2), checker.IsNil)
1896
+	n2, err = cli.NetworkCreate(context.Background(), name, options)
1897
+	c.Assert(err, checker.IsNil)
1900 1898
 
1901 1899
 	// Full ID always works
1902 1900
 	out, err = d.Cmd("network", "inspect", "--format", "{{.ID}}", n1.ID)
... ...
@@ -3,17 +3,20 @@ package main
3 3
 import (
4 4
 	"fmt"
5 5
 	"io/ioutil"
6
-	"net/http"
7 6
 	"os"
8 7
 	"os/exec"
9 8
 	"path/filepath"
10 9
 	"strings"
11 10
 
11
+	"github.com/docker/docker/api/types/container"
12
+	"github.com/docker/docker/api/types/mount"
13
+	"github.com/docker/docker/api/types/network"
14
+	"github.com/docker/docker/client"
12 15
 	"github.com/docker/docker/integration-cli/checker"
13 16
 	"github.com/docker/docker/integration-cli/cli/build"
14
-	"github.com/docker/docker/integration-cli/request"
15 17
 	icmd "github.com/docker/docker/pkg/testutil/cmd"
16 18
 	"github.com/go-check/check"
19
+	"golang.org/x/net/context"
17 20
 )
18 21
 
19 22
 func (s *DockerSuite) TestVolumeCLICreate(c *check.C) {
... ...
@@ -607,25 +610,28 @@ func (s *DockerSuite) TestDuplicateMountpointsForVolumesFromAndMounts(c *check.C
607 607
 	err := os.MkdirAll("/tmp/data", 0755)
608 608
 	c.Assert(err, checker.IsNil)
609 609
 	// Mounts is available in API
610
-	status, body, err := request.SockRequest("POST", "/containers/create?name=app", map[string]interface{}{
611
-		"Image": "busybox",
612
-		"Cmd":   []string{"top"},
613
-		"HostConfig": map[string]interface{}{
614
-			"VolumesFrom": []string{
615
-				"data1",
616
-				"data2",
610
+	cli, err := client.NewEnvClient()
611
+	c.Assert(err, checker.IsNil)
612
+	defer cli.Close()
613
+
614
+	config := container.Config{
615
+		Cmd:   []string{"top"},
616
+		Image: "busybox",
617
+	}
618
+
619
+	hostConfig := container.HostConfig{
620
+		VolumesFrom: []string{"data1", "data2"},
621
+		Mounts: []mount.Mount{
622
+			{
623
+				Type:   "bind",
624
+				Source: "/tmp/data",
625
+				Target: "/tmp/data",
617 626
 			},
618
-			"Mounts": []map[string]interface{}{
619
-				{
620
-					"Type":   "bind",
621
-					"Source": "/tmp/data",
622
-					"Target": "/tmp/data",
623
-				},
624
-			}},
625
-	}, daemonHost())
626
-
627
-	c.Assert(err, checker.IsNil, check.Commentf(string(body)))
628
-	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(body)))
627
+		},
628
+	}
629
+	_, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &network.NetworkingConfig{}, "app")
630
+
631
+	c.Assert(err, checker.IsNil)
629 632
 
630 633
 	// No volume will be referenced (mount is /tmp/data), this is backward compatible
631 634
 	out, _ = dockerCmd(c, "inspect", "--format", "{{(index .Mounts 0).Name}}", "app")
... ...
@@ -22,9 +22,14 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartHostConfig(c *check.C) {
22 22
 	config := map[string]interface{}{
23 23
 		"Binds": []string{"/aa:/bb"},
24 24
 	}
25
-	status, _, err := request.SockRequest("POST", "/containers/"+name+"/start", config, daemonHost())
25
+	res, body, err := request.Post("/containers/"+name+"/start", request.JSONBody(config))
26 26
 	c.Assert(err, checker.IsNil)
27
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
27
+
28
+	buf, err := request.ReadBody(body)
29
+	c.Assert(err, checker.IsNil)
30
+
31
+	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
32
+	c.Assert(string(buf), checker.Contains, "was deprecated since API v1.22")
28 33
 }
29 34
 
30 35
 func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumeBinds(c *check.C) {
... ...
@@ -40,17 +45,17 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumeBinds(c *check.C) {
40 40
 		"Volumes": map[string]struct{}{path: {}},
41 41
 	}
42 42
 
43
-	status, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/create?name="+name), config, daemonHost())
43
+	res, _, err := request.Post(formatV123StartAPIURL("/containers/create?name="+name), request.JSONBody(config))
44 44
 	c.Assert(err, checker.IsNil)
45
-	c.Assert(status, checker.Equals, http.StatusCreated)
45
+	c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
46 46
 
47 47
 	bindPath := RandomTmpDirPath("test", testEnv.DaemonPlatform())
48 48
 	config = map[string]interface{}{
49 49
 		"Binds": []string{bindPath + ":" + path},
50 50
 	}
51
-	status, _, err = request.SockRequest("POST", formatV123StartAPIURL("/containers/"+name+"/start"), config, daemonHost())
51
+	res, _, err = request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.JSONBody(config))
52 52
 	c.Assert(err, checker.IsNil)
53
-	c.Assert(status, checker.Equals, http.StatusNoContent)
53
+	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
54 54
 
55 55
 	pth, err := inspectMountSourceField(name, path)
56 56
 	c.Assert(err, checker.IsNil)
... ...
@@ -67,9 +72,9 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartDupVolumeBinds(c *check.C)
67 67
 		"Volumes": map[string]struct{}{"/tmp": {}},
68 68
 	}
69 69
 
70
-	status, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/create?name="+name), config, daemonHost())
70
+	res, _, err := request.Post(formatV123StartAPIURL("/containers/create?name="+name), request.JSONBody(config))
71 71
 	c.Assert(err, checker.IsNil)
72
-	c.Assert(status, checker.Equals, http.StatusCreated)
72
+	c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
73 73
 
74 74
 	bindPath1 := RandomTmpDirPath("test1", testEnv.DaemonPlatform())
75 75
 	bindPath2 := RandomTmpDirPath("test2", testEnv.DaemonPlatform())
... ...
@@ -77,10 +82,14 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartDupVolumeBinds(c *check.C)
77 77
 	config = map[string]interface{}{
78 78
 		"Binds": []string{bindPath1 + ":/tmp", bindPath2 + ":/tmp"},
79 79
 	}
80
-	status, body, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/"+name+"/start"), config, daemonHost())
80
+	res, body, err := request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.JSONBody(config))
81 81
 	c.Assert(err, checker.IsNil)
82
-	c.Assert(status, checker.Equals, http.StatusBadRequest)
83
-	c.Assert(string(body), checker.Contains, "Duplicate mount point", check.Commentf("Expected failure due to duplicate bind mounts to same path, instead got: %q with error: %v", string(body), err))
82
+
83
+	buf, err := request.ReadBody(body)
84
+	c.Assert(err, checker.IsNil)
85
+
86
+	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
87
+	c.Assert(string(buf), checker.Contains, "Duplicate mount point", check.Commentf("Expected failure due to duplicate bind mounts to same path, instead got: %q with error: %v", string(buf), err))
84 88
 }
85 89
 
86 90
 func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumesFrom(c *check.C) {
... ...
@@ -97,16 +106,16 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumesFrom(c *check.C) {
97 97
 		"Volumes": map[string]struct{}{volPath: {}},
98 98
 	}
99 99
 
100
-	status, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/create?name="+name), config, daemonHost())
100
+	res, _, err := request.Post(formatV123StartAPIURL("/containers/create?name="+name), request.JSONBody(config))
101 101
 	c.Assert(err, checker.IsNil)
102
-	c.Assert(status, checker.Equals, http.StatusCreated)
102
+	c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
103 103
 
104 104
 	config = map[string]interface{}{
105 105
 		"VolumesFrom": []string{volName},
106 106
 	}
107
-	status, _, err = request.SockRequest("POST", formatV123StartAPIURL("/containers/"+name+"/start"), config, daemonHost())
107
+	res, _, err = request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.JSONBody(config))
108 108
 	c.Assert(err, checker.IsNil)
109
-	c.Assert(status, checker.Equals, http.StatusNoContent)
109
+	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
110 110
 
111 111
 	pth, err := inspectMountSourceField(name, volPath)
112 112
 	c.Assert(err, checker.IsNil)
... ...
@@ -127,9 +136,9 @@ func (s *DockerSuite) TestDeprecatedPostContainerBindNormalVolume(c *check.C) {
127 127
 	dockerCmd(c, "create", "-v", "/foo", "--name=two", "busybox")
128 128
 
129 129
 	bindSpec := map[string][]string{"Binds": {fooDir + ":/foo"}}
130
-	status, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/two/start"), bindSpec, daemonHost())
130
+	res, _, err := request.Post(formatV123StartAPIURL("/containers/two/start"), request.JSONBody(bindSpec))
131 131
 	c.Assert(err, checker.IsNil)
132
-	c.Assert(status, checker.Equals, http.StatusNoContent)
132
+	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
133 133
 
134 134
 	fooDir2, err := inspectMountSourceField("two", "/foo")
135 135
 	c.Assert(err, checker.IsNil)
... ...
@@ -22,7 +22,7 @@ func (s *DockerNetworkSuite) TestDeprecatedDockerNetworkStartAPIWithHostconfig(c
22 22
 			"NetworkMode": netName,
23 23
 		},
24 24
 	}
25
-	_, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/"+conName+"/start"), config, daemonHost())
25
+	_, _, err := request.Post(formatV123StartAPIURL("/containers/"+conName+"/start"), request.JSONBody(config))
26 26
 	c.Assert(err, checker.IsNil)
27 27
 	c.Assert(waitRun(conName), checker.IsNil)
28 28
 	networks := inspectField(c, conName, "NetworkSettings.Networks")
... ...
@@ -15,6 +15,7 @@ import (
15 15
 	"time"
16 16
 
17 17
 	"github.com/docker/docker/api/types"
18
+	"github.com/docker/docker/client"
18 19
 	"github.com/docker/docker/integration-cli/checker"
19 20
 	"github.com/docker/docker/integration-cli/cli"
20 21
 	"github.com/docker/docker/integration-cli/daemon"
... ...
@@ -22,6 +23,7 @@ import (
22 22
 	"github.com/docker/docker/integration-cli/request"
23 23
 	icmd "github.com/docker/docker/pkg/testutil/cmd"
24 24
 	"github.com/go-check/check"
25
+	"golang.org/x/net/context"
25 26
 )
26 27
 
27 28
 // Deprecated
... ...
@@ -267,17 +269,12 @@ func daemonTime(c *check.C) time.Time {
267 267
 	if testEnv.LocalDaemon() {
268 268
 		return time.Now()
269 269
 	}
270
-
271
-	status, body, err := request.SockRequest("GET", "/info", nil, daemonHost())
270
+	cli, err := client.NewEnvClient()
272 271
 	c.Assert(err, check.IsNil)
273
-	c.Assert(status, check.Equals, http.StatusOK)
272
+	defer cli.Close()
274 273
 
275
-	type infoJSON struct {
276
-		SystemTime string
277
-	}
278
-	var info infoJSON
279
-	err = json.Unmarshal(body, &info)
280
-	c.Assert(err, check.IsNil, check.Commentf("unable to unmarshal GET /info response"))
274
+	info, err := cli.Info(context.Background())
275
+	c.Assert(err, check.IsNil)
281 276
 
282 277
 	dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
283 278
 	c.Assert(err, check.IsNil, check.Commentf("invalid time format in GET /info response"))
... ...
@@ -376,10 +373,12 @@ func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg
376 376
 }
377 377
 
378 378
 func getInspectBody(c *check.C, version, id string) []byte {
379
-	endpoint := fmt.Sprintf("/%s/containers/%s/json", version, id)
380
-	status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost())
379
+	var httpClient *http.Client
380
+	cli, err := client.NewClient(daemonHost(), version, httpClient, nil)
381
+	c.Assert(err, check.IsNil)
382
+	defer cli.Close()
383
+	_, body, err := cli.ContainerInspectWithRaw(context.Background(), id, false)
381 384
 	c.Assert(err, check.IsNil)
382
-	c.Assert(status, check.Equals, http.StatusOK)
383 385
 	return body
384 386
 }
385 387
 
... ...
@@ -406,20 +405,17 @@ func minimalBaseImage() string {
406 406
 }
407 407
 
408 408
 func getGoroutineNumber() (int, error) {
409
-	i := struct {
410
-		NGoroutines int
411
-	}{}
412
-	status, b, err := request.SockRequest("GET", "/info", nil, daemonHost())
409
+	cli, err := client.NewEnvClient()
413 410
 	if err != nil {
414 411
 		return 0, err
415 412
 	}
416
-	if status != http.StatusOK {
417
-		return 0, fmt.Errorf("http status code: %d", status)
418
-	}
419
-	if err := json.Unmarshal(b, &i); err != nil {
413
+	defer cli.Close()
414
+
415
+	info, err := cli.Info(context.Background())
416
+	if err != nil {
420 417
 		return 0, err
421 418
 	}
422
-	return i.NGoroutines, nil
419
+	return info.NGoroutines, nil
423 420
 }
424 421
 
425 422
 func waitForGoroutines(expected int) error {
... ...
@@ -1,16 +1,14 @@
1 1
 package environment
2 2
 
3 3
 import (
4
-	"encoding/json"
5
-	"fmt"
6
-	"net/http"
7 4
 	"regexp"
8 5
 	"strings"
9 6
 
10 7
 	"github.com/docker/docker/api/types"
11
-	volumetypes "github.com/docker/docker/api/types/volume"
12
-	"github.com/docker/docker/integration-cli/request"
8
+	"github.com/docker/docker/api/types/filters"
9
+	"github.com/docker/docker/client"
13 10
 	icmd "github.com/docker/docker/pkg/testutil/cmd"
11
+	"golang.org/x/net/context"
14 12
 )
15 13
 
16 14
 type testingT interface {
... ...
@@ -26,15 +24,21 @@ type logT interface {
26 26
 // and removing everything else. It's meant to run after any tests so that they don't
27 27
 // depend on each others.
28 28
 func (e *Execution) Clean(t testingT, dockerBinary string) {
29
+	cli, err := client.NewEnvClient()
30
+	if err != nil {
31
+		t.Fatalf("%v", err)
32
+	}
33
+	defer cli.Close()
34
+
29 35
 	if (e.DaemonPlatform() != "windows") || (e.DaemonPlatform() == "windows" && e.Isolation() == "hyperv") {
30 36
 		unpauseAllContainers(t, dockerBinary)
31 37
 	}
32 38
 	deleteAllContainers(t, dockerBinary)
33 39
 	deleteAllImages(t, dockerBinary, e.protectedElements.images)
34
-	deleteAllVolumes(t, dockerBinary)
35
-	deleteAllNetworks(t, dockerBinary, e.DaemonPlatform())
40
+	deleteAllVolumes(t, cli)
41
+	deleteAllNetworks(t, cli, e.DaemonPlatform())
36 42
 	if e.DaemonPlatform() == "linux" {
37
-		deleteAllPlugins(t, dockerBinary)
43
+		deleteAllPlugins(t, cli, dockerBinary)
38 44
 	}
39 45
 }
40 46
 
... ...
@@ -108,41 +112,34 @@ func deleteAllImages(t testingT, dockerBinary string, protectedImages map[string
108 108
 	}
109 109
 }
110 110
 
111
-func deleteAllVolumes(t testingT, dockerBinary string) {
112
-	volumes, err := getAllVolumes()
111
+func deleteAllVolumes(t testingT, c client.APIClient) {
112
+	var errs []string
113
+	volumes, err := getAllVolumes(c)
113 114
 	if err != nil {
114 115
 		t.Fatalf("%v", err)
115 116
 	}
116
-	var errs []string
117 117
 	for _, v := range volumes {
118
-		status, b, err := request.SockRequest("DELETE", "/volumes/"+v.Name, nil, request.DaemonHost())
118
+		err := c.VolumeRemove(context.Background(), v.Name, true)
119 119
 		if err != nil {
120 120
 			errs = append(errs, err.Error())
121 121
 			continue
122 122
 		}
123
-		if status != http.StatusNoContent {
124
-			errs = append(errs, fmt.Sprintf("error deleting volume %s: %s", v.Name, string(b)))
125
-		}
126 123
 	}
127 124
 	if len(errs) > 0 {
128 125
 		t.Fatalf("%v", strings.Join(errs, "\n"))
129 126
 	}
130 127
 }
131 128
 
132
-func getAllVolumes() ([]*types.Volume, error) {
133
-	var volumes volumetypes.VolumesListOKBody
134
-	_, b, err := request.SockRequest("GET", "/volumes", nil, request.DaemonHost())
129
+func getAllVolumes(c client.APIClient) ([]*types.Volume, error) {
130
+	volumes, err := c.VolumeList(context.Background(), filters.Args{})
135 131
 	if err != nil {
136 132
 		return nil, err
137 133
 	}
138
-	if err := json.Unmarshal(b, &volumes); err != nil {
139
-		return nil, err
140
-	}
141 134
 	return volumes.Volumes, nil
142 135
 }
143 136
 
144
-func deleteAllNetworks(t testingT, dockerBinary string, daemonPlatform string) {
145
-	networks, err := getAllNetworks()
137
+func deleteAllNetworks(t testingT, c client.APIClient, daemonPlatform string) {
138
+	networks, err := getAllNetworks(c)
146 139
 	if err != nil {
147 140
 		t.Fatalf("%v", err)
148 141
 	}
... ...
@@ -155,62 +152,47 @@ func deleteAllNetworks(t testingT, dockerBinary string, daemonPlatform string) {
155 155
 			// nat is a pre-defined network on Windows and cannot be removed
156 156
 			continue
157 157
 		}
158
-		status, b, err := request.SockRequest("DELETE", "/networks/"+n.Name, nil, request.DaemonHost())
158
+		err := c.NetworkRemove(context.Background(), n.ID)
159 159
 		if err != nil {
160 160
 			errs = append(errs, err.Error())
161 161
 			continue
162 162
 		}
163
-		if status != http.StatusNoContent {
164
-			errs = append(errs, fmt.Sprintf("error deleting network %s: %s", n.Name, string(b)))
165
-		}
166 163
 	}
167 164
 	if len(errs) > 0 {
168 165
 		t.Fatalf("%v", strings.Join(errs, "\n"))
169 166
 	}
170 167
 }
171 168
 
172
-func getAllNetworks() ([]types.NetworkResource, error) {
173
-	var networks []types.NetworkResource
174
-	_, b, err := request.SockRequest("GET", "/networks", nil, request.DaemonHost())
169
+func getAllNetworks(c client.APIClient) ([]types.NetworkResource, error) {
170
+	networks, err := c.NetworkList(context.Background(), types.NetworkListOptions{})
175 171
 	if err != nil {
176 172
 		return nil, err
177 173
 	}
178
-	if err := json.Unmarshal(b, &networks); err != nil {
179
-		return nil, err
180
-	}
181 174
 	return networks, nil
182 175
 }
183 176
 
184
-func deleteAllPlugins(t testingT, dockerBinary string) {
185
-	plugins, err := getAllPlugins()
177
+func deleteAllPlugins(t testingT, c client.APIClient, dockerBinary string) {
178
+	plugins, err := getAllPlugins(c)
186 179
 	if err != nil {
187 180
 		t.Fatalf("%v", err)
188 181
 	}
189 182
 	var errs []string
190 183
 	for _, p := range plugins {
191
-		pluginName := p.Name
192
-		status, b, err := request.SockRequest("DELETE", "/plugins/"+pluginName+"?force=1", nil, request.DaemonHost())
184
+		err := c.PluginRemove(context.Background(), p.Name, types.PluginRemoveOptions{Force: true})
193 185
 		if err != nil {
194 186
 			errs = append(errs, err.Error())
195 187
 			continue
196 188
 		}
197
-		if status != http.StatusOK {
198
-			errs = append(errs, fmt.Sprintf("error deleting plugin %s: %s", p.Name, string(b)))
199
-		}
200 189
 	}
201 190
 	if len(errs) > 0 {
202 191
 		t.Fatalf("%v", strings.Join(errs, "\n"))
203 192
 	}
204 193
 }
205 194
 
206
-func getAllPlugins() (types.PluginsListResponse, error) {
207
-	var plugins types.PluginsListResponse
208
-	_, b, err := request.SockRequest("GET", "/plugins", nil, request.DaemonHost())
195
+func getAllPlugins(c client.APIClient) (types.PluginsListResponse, error) {
196
+	plugins, err := c.PluginList(context.Background(), filters.Args{})
209 197
 	if err != nil {
210 198
 		return nil, err
211 199
 	}
212
-	if err := json.Unmarshal(b, &plugins); err != nil {
213
-		return nil, err
214
-	}
215 200
 	return plugins, nil
216 201
 }