Browse code

Completely remove `d.NewClient` from testing tools

Favor `d.NewClientT` instead.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

Brian Goff authored on 2019/02/07 08:33:07
Showing 7 changed files
... ...
@@ -47,7 +47,7 @@ func (s *DockerSwarmSuite) TestAPISwarmInit(c *check.C) {
47 47
 	c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
48 48
 
49 49
 	// Leaving cluster
50
-	c.Assert(d2.SwarmLeave(false), checker.IsNil)
50
+	c.Assert(d2.SwarmLeave(c, false), checker.IsNil)
51 51
 
52 52
 	info = d2.SwarmInfo(c)
53 53
 	c.Assert(info.ControlAvailable, checker.False)
... ...
@@ -115,7 +115,7 @@ func (s *DockerSwarmSuite) TestAPISwarmJoinToken(c *check.C) {
115 115
 	})
116 116
 	info = d2.SwarmInfo(c)
117 117
 	c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
118
-	c.Assert(d2.SwarmLeave(false), checker.IsNil)
118
+	c.Assert(d2.SwarmLeave(c, false), checker.IsNil)
119 119
 	info = d2.SwarmInfo(c)
120 120
 	c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateInactive)
121 121
 
... ...
@@ -137,7 +137,7 @@ func (s *DockerSwarmSuite) TestAPISwarmJoinToken(c *check.C) {
137 137
 	d2.SwarmJoin(c, swarm.JoinRequest{JoinToken: workerToken, RemoteAddrs: []string{d1.SwarmListenAddr()}})
138 138
 	info = d2.SwarmInfo(c)
139 139
 	c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
140
-	c.Assert(d2.SwarmLeave(false), checker.IsNil)
140
+	c.Assert(d2.SwarmLeave(c, false), checker.IsNil)
141 141
 	info = d2.SwarmInfo(c)
142 142
 	c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateInactive)
143 143
 
... ...
@@ -156,7 +156,7 @@ func (s *DockerSwarmSuite) TestAPISwarmJoinToken(c *check.C) {
156 156
 	d2.SwarmJoin(c, swarm.JoinRequest{JoinToken: workerToken, RemoteAddrs: []string{d1.SwarmListenAddr()}})
157 157
 	info = d2.SwarmInfo(c)
158 158
 	c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
159
-	c.Assert(d2.SwarmLeave(false), checker.IsNil)
159
+	c.Assert(d2.SwarmLeave(c, false), checker.IsNil)
160 160
 	info = d2.SwarmInfo(c)
161 161
 	c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateInactive)
162 162
 }
... ...
@@ -423,8 +423,8 @@ func (s *DockerSwarmSuite) TestAPISwarmLeaveRemovesContainer(c *check.C) {
423 423
 
424 424
 	waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, instances+1)
425 425
 
426
-	c.Assert(d.SwarmLeave(false), checker.NotNil)
427
-	c.Assert(d.SwarmLeave(true), checker.IsNil)
426
+	c.Assert(d.SwarmLeave(c, false), checker.NotNil)
427
+	c.Assert(d.SwarmLeave(c, true), checker.IsNil)
428 428
 
429 429
 	waitAndAssert(c, defaultReconciliationTimeout, d.CheckActiveContainerCount, checker.Equals, 1)
430 430
 
... ...
@@ -454,7 +454,7 @@ func (s *DockerSwarmSuite) TestAPISwarmLeaveOnPendingJoin(c *check.C) {
454 454
 	info := d2.SwarmInfo(c)
455 455
 	c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStatePending)
456 456
 
457
-	c.Assert(d2.SwarmLeave(true), checker.IsNil)
457
+	c.Assert(d2.SwarmLeave(c, true), checker.IsNil)
458 458
 
459 459
 	waitAndAssert(c, defaultReconciliationTimeout, d2.CheckActiveContainerCount, checker.Equals, 1)
460 460
 
... ...
@@ -874,7 +874,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesUpdateWithName(c *check.C) {
874 874
 // Unlocking an unlocked swarm results in an error
875 875
 func (s *DockerSwarmSuite) TestAPISwarmUnlockNotLocked(c *check.C) {
876 876
 	d := s.AddDaemon(c, true, true)
877
-	err := d.SwarmUnlock(swarm.UnlockRequest{UnlockKey: "wrong-key"})
877
+	err := d.SwarmUnlock(c, swarm.UnlockRequest{UnlockKey: "wrong-key"})
878 878
 	c.Assert(err, checker.NotNil)
879 879
 	c.Assert(err.Error(), checker.Contains, "swarm is not locked")
880 880
 }
... ...
@@ -116,7 +116,7 @@ func (s *DockerSwarmSuite) TestSwarmInit(c *check.C) {
116 116
 	c.Assert(spec.CAConfig.ExternalCAs[0].CACert, checker.Equals, "")
117 117
 	c.Assert(spec.CAConfig.ExternalCAs[1].CACert, checker.Equals, string(expected))
118 118
 
119
-	c.Assert(d.SwarmLeave(true), checker.IsNil)
119
+	c.Assert(d.SwarmLeave(c, true), checker.IsNil)
120 120
 	cli.Docker(cli.Args("swarm", "init"), cli.Daemon(d)).Assert(c, icmd.Success)
121 121
 
122 122
 	spec = getSpec()
... ...
@@ -426,7 +426,7 @@ func (s *DockerSwarmSuite) TestOverlayAttachableOnSwarmLeave(c *check.C) {
426 426
 	c.Assert(err, checker.IsNil, check.Commentf("%s", out))
427 427
 
428 428
 	// Leave the swarm
429
-	c.Assert(d.SwarmLeave(true), checker.IsNil)
429
+	c.Assert(d.SwarmLeave(c, true), checker.IsNil)
430 430
 
431 431
 	// Check the container is disconnected
432 432
 	out, err = d.Cmd("inspect", "c1", "--format", "{{.NetworkSettings.Networks."+nwName+"}}")
... ...
@@ -356,7 +356,7 @@ func TestServiceWithDataPathPortInit(t *testing.T) {
356 356
 	assert.Equal(t, info.Swarm.Cluster.DataPathPort, datapathPort)
357 357
 	err := c.ServiceRemove(context.Background(), serviceID)
358 358
 	assert.NilError(t, err)
359
-	d.SwarmLeave(true)
359
+	d.SwarmLeave(t, true)
360 360
 	d.Stop(t)
361 361
 
362 362
 	// Clean up , set it back to original one to make sure other tests don't fail
... ...
@@ -382,7 +382,7 @@ func TestServiceWithDataPathPortInit(t *testing.T) {
382 382
 	assert.Equal(t, info.Swarm.Cluster.DataPathPort, defaultDataPathPort)
383 383
 	err = c.ServiceRemove(context.Background(), serviceID)
384 384
 	assert.NilError(t, err)
385
-	d.SwarmLeave(true)
385
+	d.SwarmLeave(t, true)
386 386
 	defer d.Stop(t)
387 387
 }
388 388
 
... ...
@@ -424,7 +424,7 @@ func TestServiceWithDefaultAddressPoolInit(t *testing.T) {
424 424
 
425 425
 	err = cli.ServiceRemove(context.Background(), serviceID)
426 426
 	assert.NilError(t, err)
427
-	d.SwarmLeave(true)
427
+	d.SwarmLeave(t, true)
428 428
 	d.Stop(t)
429 429
 
430 430
 	// Clean up , set it back to original one to make sure other tests don't fail
... ...
@@ -432,6 +432,6 @@ func TestServiceWithDefaultAddressPoolInit(t *testing.T) {
432 432
 	ops = append(ops, daemon.WithSwarmDefaultAddrPool(ipAddr))
433 433
 	ops = append(ops, daemon.WithSwarmDefaultAddrPoolSubnetSize(24))
434 434
 	d = swarm.NewSwarm(t, testEnv, ops...)
435
-	d.SwarmLeave(true)
435
+	d.SwarmLeave(t, true)
436 436
 	defer d.Stop(t)
437 437
 }
... ...
@@ -64,45 +64,45 @@ func TestServicePlugin(t *testing.T) {
64 64
 	defer d3.Stop(t)
65 65
 
66 66
 	id := d1.CreateService(t, makePlugin(repo, name, nil))
67
-	poll.WaitOn(t, d1.PluginIsRunning(name), swarm.ServicePoll)
68
-	poll.WaitOn(t, d2.PluginIsRunning(name), swarm.ServicePoll)
69
-	poll.WaitOn(t, d3.PluginIsRunning(name), swarm.ServicePoll)
67
+	poll.WaitOn(t, d1.PluginIsRunning(t, name), swarm.ServicePoll)
68
+	poll.WaitOn(t, d2.PluginIsRunning(t, name), swarm.ServicePoll)
69
+	poll.WaitOn(t, d3.PluginIsRunning(t, name), swarm.ServicePoll)
70 70
 
71 71
 	service := d1.GetService(t, id)
72 72
 	d1.UpdateService(t, service, makePlugin(repo2, name, nil))
73
-	poll.WaitOn(t, d1.PluginReferenceIs(name, repo2), swarm.ServicePoll)
74
-	poll.WaitOn(t, d2.PluginReferenceIs(name, repo2), swarm.ServicePoll)
75
-	poll.WaitOn(t, d3.PluginReferenceIs(name, repo2), swarm.ServicePoll)
76
-	poll.WaitOn(t, d1.PluginIsRunning(name), swarm.ServicePoll)
77
-	poll.WaitOn(t, d2.PluginIsRunning(name), swarm.ServicePoll)
78
-	poll.WaitOn(t, d3.PluginIsRunning(name), swarm.ServicePoll)
73
+	poll.WaitOn(t, d1.PluginReferenceIs(t, name, repo2), swarm.ServicePoll)
74
+	poll.WaitOn(t, d2.PluginReferenceIs(t, name, repo2), swarm.ServicePoll)
75
+	poll.WaitOn(t, d3.PluginReferenceIs(t, name, repo2), swarm.ServicePoll)
76
+	poll.WaitOn(t, d1.PluginIsRunning(t, name), swarm.ServicePoll)
77
+	poll.WaitOn(t, d2.PluginIsRunning(t, name), swarm.ServicePoll)
78
+	poll.WaitOn(t, d3.PluginIsRunning(t, name), swarm.ServicePoll)
79 79
 
80 80
 	d1.RemoveService(t, id)
81
-	poll.WaitOn(t, d1.PluginIsNotPresent(name), swarm.ServicePoll)
82
-	poll.WaitOn(t, d2.PluginIsNotPresent(name), swarm.ServicePoll)
83
-	poll.WaitOn(t, d3.PluginIsNotPresent(name), swarm.ServicePoll)
81
+	poll.WaitOn(t, d1.PluginIsNotPresent(t, name), swarm.ServicePoll)
82
+	poll.WaitOn(t, d2.PluginIsNotPresent(t, name), swarm.ServicePoll)
83
+	poll.WaitOn(t, d3.PluginIsNotPresent(t, name), swarm.ServicePoll)
84 84
 
85 85
 	// constrain to managers only
86 86
 	id = d1.CreateService(t, makePlugin(repo, name, []string{"node.role==manager"}))
87
-	poll.WaitOn(t, d1.PluginIsRunning(name), swarm.ServicePoll)
88
-	poll.WaitOn(t, d2.PluginIsRunning(name), swarm.ServicePoll)
89
-	poll.WaitOn(t, d3.PluginIsNotPresent(name), swarm.ServicePoll)
87
+	poll.WaitOn(t, d1.PluginIsRunning(t, name), swarm.ServicePoll)
88
+	poll.WaitOn(t, d2.PluginIsRunning(t, name), swarm.ServicePoll)
89
+	poll.WaitOn(t, d3.PluginIsNotPresent(t, name), swarm.ServicePoll)
90 90
 
91 91
 	d1.RemoveService(t, id)
92
-	poll.WaitOn(t, d1.PluginIsNotPresent(name), swarm.ServicePoll)
93
-	poll.WaitOn(t, d2.PluginIsNotPresent(name), swarm.ServicePoll)
94
-	poll.WaitOn(t, d3.PluginIsNotPresent(name), swarm.ServicePoll)
92
+	poll.WaitOn(t, d1.PluginIsNotPresent(t, name), swarm.ServicePoll)
93
+	poll.WaitOn(t, d2.PluginIsNotPresent(t, name), swarm.ServicePoll)
94
+	poll.WaitOn(t, d3.PluginIsNotPresent(t, name), swarm.ServicePoll)
95 95
 
96 96
 	// with no name
97 97
 	id = d1.CreateService(t, makePlugin(repo, "", nil))
98
-	poll.WaitOn(t, d1.PluginIsRunning(repo), swarm.ServicePoll)
99
-	poll.WaitOn(t, d2.PluginIsRunning(repo), swarm.ServicePoll)
100
-	poll.WaitOn(t, d3.PluginIsRunning(repo), swarm.ServicePoll)
98
+	poll.WaitOn(t, d1.PluginIsRunning(t, repo), swarm.ServicePoll)
99
+	poll.WaitOn(t, d2.PluginIsRunning(t, repo), swarm.ServicePoll)
100
+	poll.WaitOn(t, d3.PluginIsRunning(t, repo), swarm.ServicePoll)
101 101
 
102 102
 	d1.RemoveService(t, id)
103
-	poll.WaitOn(t, d1.PluginIsNotPresent(repo), swarm.ServicePoll)
104
-	poll.WaitOn(t, d2.PluginIsNotPresent(repo), swarm.ServicePoll)
105
-	poll.WaitOn(t, d3.PluginIsNotPresent(repo), swarm.ServicePoll)
103
+	poll.WaitOn(t, d1.PluginIsNotPresent(t, repo), swarm.ServicePoll)
104
+	poll.WaitOn(t, d2.PluginIsNotPresent(t, repo), swarm.ServicePoll)
105
+	poll.WaitOn(t, d3.PluginIsNotPresent(t, repo), swarm.ServicePoll)
106 106
 }
107 107
 
108 108
 func makePlugin(repo, name string, constraints []string) func(*swarmtypes.Service) {
... ...
@@ -167,16 +167,7 @@ func (d *Daemon) ReadLogFile() ([]byte, error) {
167 167
 	return ioutil.ReadFile(d.logFile.Name())
168 168
 }
169 169
 
170
-// NewClient creates new client based on daemon's socket path
171
-// FIXME(vdemeester): replace NewClient with NewClientT
172
-func (d *Daemon) NewClient() (*client.Client, error) {
173
-	return client.NewClientWithOpts(
174
-		client.FromEnv,
175
-		client.WithHost(d.Sock()))
176
-}
177
-
178 170
 // NewClientT creates new client based on daemon's socket path
179
-// FIXME(vdemeester): replace NewClient with NewClientT
180 171
 func (d *Daemon) NewClientT(t assert.TestingT) *client.Client {
181 172
 	if ht, ok := t.(test.HelperT); ok {
182 173
 		ht.Helper()
... ...
@@ -5,12 +5,13 @@ import (
5 5
 
6 6
 	"github.com/docker/docker/api/types"
7 7
 	"github.com/docker/docker/client"
8
+	"gotest.tools/assert"
8 9
 	"gotest.tools/poll"
9 10
 )
10 11
 
11 12
 // PluginIsRunning provides a poller to check if the specified plugin is running
12
-func (d *Daemon) PluginIsRunning(name string) func(poll.LogT) poll.Result {
13
-	return withClient(d, withPluginInspect(name, func(plugin *types.Plugin, t poll.LogT) poll.Result {
13
+func (d *Daemon) PluginIsRunning(t assert.TestingT, name string) func(poll.LogT) poll.Result {
14
+	return withClient(t, d, withPluginInspect(name, func(plugin *types.Plugin, t poll.LogT) poll.Result {
14 15
 		if plugin.Enabled {
15 16
 			return poll.Success()
16 17
 		}
... ...
@@ -19,8 +20,8 @@ func (d *Daemon) PluginIsRunning(name string) func(poll.LogT) poll.Result {
19 19
 }
20 20
 
21 21
 // PluginIsNotRunning provides a poller to check if the specified plugin is not running
22
-func (d *Daemon) PluginIsNotRunning(name string) func(poll.LogT) poll.Result {
23
-	return withClient(d, withPluginInspect(name, func(plugin *types.Plugin, t poll.LogT) poll.Result {
22
+func (d *Daemon) PluginIsNotRunning(t assert.TestingT, name string) func(poll.LogT) poll.Result {
23
+	return withClient(t, d, withPluginInspect(name, func(plugin *types.Plugin, t poll.LogT) poll.Result {
24 24
 		if !plugin.Enabled {
25 25
 			return poll.Success()
26 26
 		}
... ...
@@ -29,8 +30,8 @@ func (d *Daemon) PluginIsNotRunning(name string) func(poll.LogT) poll.Result {
29 29
 }
30 30
 
31 31
 // PluginIsNotPresent provides a poller to check if the specified plugin is not present
32
-func (d *Daemon) PluginIsNotPresent(name string) func(poll.LogT) poll.Result {
33
-	return withClient(d, func(c client.APIClient, t poll.LogT) poll.Result {
32
+func (d *Daemon) PluginIsNotPresent(t assert.TestingT, name string) func(poll.LogT) poll.Result {
33
+	return withClient(t, d, func(c client.APIClient, t poll.LogT) poll.Result {
34 34
 		_, _, err := c.PluginInspectWithRaw(context.Background(), name)
35 35
 		if client.IsErrNotFound(err) {
36 36
 			return poll.Success()
... ...
@@ -43,8 +44,8 @@ func (d *Daemon) PluginIsNotPresent(name string) func(poll.LogT) poll.Result {
43 43
 }
44 44
 
45 45
 // PluginReferenceIs provides a poller to check if the specified plugin has the specified reference
46
-func (d *Daemon) PluginReferenceIs(name, expectedRef string) func(poll.LogT) poll.Result {
47
-	return withClient(d, withPluginInspect(name, func(plugin *types.Plugin, t poll.LogT) poll.Result {
46
+func (d *Daemon) PluginReferenceIs(t assert.TestingT, name, expectedRef string) func(poll.LogT) poll.Result {
47
+	return withClient(t, d, withPluginInspect(name, func(plugin *types.Plugin, t poll.LogT) poll.Result {
48 48
 		if plugin.PluginReference == expectedRef {
49 49
 			return poll.Success()
50 50
 		}
... ...
@@ -66,12 +67,9 @@ func withPluginInspect(name string, f func(*types.Plugin, poll.LogT) poll.Result
66 66
 
67 67
 }
68 68
 
69
-func withClient(d *Daemon, f func(client.APIClient, poll.LogT) poll.Result) func(poll.LogT) poll.Result {
70
-	return func(t poll.LogT) poll.Result {
71
-		c, err := d.NewClient()
72
-		if err != nil {
73
-			poll.Error(err)
74
-		}
75
-		return f(c, t)
69
+func withClient(t assert.TestingT, d *Daemon, f func(client.APIClient, poll.LogT) poll.Result) func(poll.LogT) poll.Result {
70
+	return func(pt poll.LogT) poll.Result {
71
+		c := d.NewClientT(t)
72
+		return f(c, pt)
76 73
 	}
77 74
 }
... ...
@@ -111,17 +111,14 @@ func (d *Daemon) SwarmJoin(t assert.TestingT, req swarm.JoinRequest) {
111 111
 }
112 112
 
113 113
 // SwarmLeave forces daemon to leave current cluster.
114
-func (d *Daemon) SwarmLeave(force bool) error {
115
-	cli, err := d.NewClient()
116
-	if err != nil {
117
-		return fmt.Errorf("leaving swarm: failed to create client %v", err)
118
-	}
114
+//
115
+// The passed in TestingT is only used to validate that the client was successfully created
116
+// Some tests rely on error checking the result of the actual unlock, so allow
117
+// the error to be returned.
118
+func (d *Daemon) SwarmLeave(t assert.TestingT, force bool) error {
119
+	cli := d.NewClientT(t)
119 120
 	defer cli.Close()
120
-	err = cli.SwarmLeave(context.Background(), force)
121
-	if err != nil {
122
-		err = fmt.Errorf("leaving swarm: %v", err)
123
-	}
124
-	return err
121
+	return cli.SwarmLeave(context.Background(), force)
125 122
 }
126 123
 
127 124
 // SwarmInfo returns the swarm information of the daemon
... ...
@@ -136,13 +133,15 @@ func (d *Daemon) SwarmInfo(t assert.TestingT) swarm.Info {
136 136
 }
137 137
 
138 138
 // SwarmUnlock tries to unlock a locked swarm
139
-func (d *Daemon) SwarmUnlock(req swarm.UnlockRequest) error {
140
-	cli, err := d.NewClient()
141
-	if err != nil {
142
-		return fmt.Errorf("unlocking swarm: failed to create client %v", err)
143
-	}
139
+//
140
+// The passed in TestingT is only used to validate that the client was successfully created
141
+// Some tests rely on error checking the result of the actual unlock, so allow
142
+// the error to be returned.
143
+func (d *Daemon) SwarmUnlock(t assert.TestingT, req swarm.UnlockRequest) error {
144
+	cli := d.NewClientT(t)
144 145
 	defer cli.Close()
145
-	err = cli.SwarmUnlock(context.Background(), req)
146
+
147
+	err := cli.SwarmUnlock(context.Background(), req)
146 148
 	if err != nil {
147 149
 		err = errors.Wrap(err, "unlocking swarm")
148 150
 	}