Browse code

Move serviceRunningTasksCount to integration/internal/swarm

This fix moves multiple places of serviceRunningTasksCount
to one location in integration/internal/swarm, so that
code duplication could be removed.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2019/01/20 03:54:32
Showing 3 changed files
... ...
@@ -5,6 +5,7 @@ import (
5 5
 
6 6
 	"github.com/docker/docker/api/types"
7 7
 	"github.com/docker/docker/api/types/filters"
8
+	swarmtypes "github.com/docker/docker/api/types/swarm"
8 9
 	"github.com/docker/docker/client"
9 10
 	"gotest.tools/poll"
10 11
 )
... ...
@@ -45,3 +46,27 @@ func NoTasks(ctx context.Context, client client.ServiceAPIClient) func(log poll.
45 45
 		}
46 46
 	}
47 47
 }
48
+
49
+// RunningTasksCount verifies there are `instances` tasks running for `serviceID`
50
+func RunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result {
51
+	return func(log poll.LogT) poll.Result {
52
+		filter := filters.NewArgs()
53
+		filter.Add("service", serviceID)
54
+		tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
55
+			Filters: filter,
56
+		})
57
+		switch {
58
+		case err != nil:
59
+			return poll.Error(err)
60
+		case len(tasks) == int(instances):
61
+			for _, task := range tasks {
62
+				if task.Status.State != swarmtypes.TaskStateRunning {
63
+					return poll.Continue("waiting for tasks to enter run state")
64
+				}
65
+			}
66
+			return poll.Success()
67
+		default:
68
+			return poll.Continue("task count at %d waiting for %d", len(tasks), instances)
69
+		}
70
+	}
71
+}
... ...
@@ -5,9 +5,6 @@ import (
5 5
 	"testing"
6 6
 
7 7
 	"github.com/docker/docker/api/types"
8
-	"github.com/docker/docker/api/types/filters"
9
-	swarmtypes "github.com/docker/docker/api/types/swarm"
10
-	"github.com/docker/docker/client"
11 8
 	"github.com/docker/docker/integration/internal/network"
12 9
 	"github.com/docker/docker/integration/internal/swarm"
13 10
 	"gotest.tools/assert"
... ...
@@ -38,7 +35,7 @@ func TestInspectNetwork(t *testing.T) {
38 38
 		swarm.ServiceWithNetwork(networkName),
39 39
 	)
40 40
 
41
-	poll.WaitOn(t, serviceRunningTasksCount(c, serviceID, instances), swarm.ServicePoll)
41
+	poll.WaitOn(t, swarm.RunningTasksCount(c, serviceID, instances), swarm.ServicePoll)
42 42
 
43 43
 	tests := []struct {
44 44
 		name    string
... ...
@@ -103,30 +100,3 @@ func TestInspectNetwork(t *testing.T) {
103 103
 	assert.NilError(t, err)
104 104
 	poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll)
105 105
 }
106
-
107
-func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result {
108
-	return func(log poll.LogT) poll.Result {
109
-		tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
110
-			Filters: filters.NewArgs(
111
-				filters.Arg("service", serviceID),
112
-				filters.Arg("desired-state", string(swarmtypes.TaskStateRunning)),
113
-			),
114
-		})
115
-		switch {
116
-		case err != nil:
117
-			return poll.Error(err)
118
-		case len(tasks) == int(instances):
119
-			for _, task := range tasks {
120
-				if task.Status.Err != "" {
121
-					log.Log("task error:", task.Status.Err)
122
-				}
123
-				if task.Status.State != swarmtypes.TaskStateRunning {
124
-					return poll.Continue("waiting for tasks to enter run state (current status: %s)", task.Status.State)
125
-				}
126
-			}
127
-			return poll.Success()
128
-		default:
129
-			return poll.Continue("task count for service %s at %d waiting for %d", serviceID, len(tasks), instances)
130
-		}
131
-	}
132
-}
... ...
@@ -45,18 +45,18 @@ func testServiceCreateInit(daemonEnabled bool) func(t *testing.T) {
45 45
 		booleanFalse := false
46 46
 
47 47
 		serviceID := swarm.CreateService(t, d)
48
-		poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, 1), swarm.ServicePoll)
48
+		poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, 1), swarm.ServicePoll)
49 49
 		i := inspectServiceContainer(t, client, serviceID)
50 50
 		// HostConfig.Init == nil means that it delegates to daemon configuration
51 51
 		assert.Check(t, i.HostConfig.Init == nil)
52 52
 
53 53
 		serviceID = swarm.CreateService(t, d, swarm.ServiceWithInit(&booleanTrue))
54
-		poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, 1), swarm.ServicePoll)
54
+		poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, 1), swarm.ServicePoll)
55 55
 		i = inspectServiceContainer(t, client, serviceID)
56 56
 		assert.Check(t, is.Equal(true, *i.HostConfig.Init))
57 57
 
58 58
 		serviceID = swarm.CreateService(t, d, swarm.ServiceWithInit(&booleanFalse))
59
-		poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, 1), swarm.ServicePoll)
59
+		poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, 1), swarm.ServicePoll)
60 60
 		i = inspectServiceContainer(t, client, serviceID)
61 61
 		assert.Check(t, is.Equal(false, *i.HostConfig.Init))
62 62
 	}
... ...
@@ -100,7 +100,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
100 100
 	}
101 101
 
102 102
 	serviceID := swarm.CreateService(t, d, serviceSpec...)
103
-	poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
103
+	poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
104 104
 
105 105
 	_, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
106 106
 	assert.NilError(t, err)
... ...
@@ -111,7 +111,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
111 111
 	poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID), swarm.ServicePoll)
112 112
 
113 113
 	serviceID2 := swarm.CreateService(t, d, serviceSpec...)
114
-	poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll)
114
+	poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID2, instances), swarm.ServicePoll)
115 115
 
116 116
 	err = client.ServiceRemove(context.Background(), serviceID2)
117 117
 	assert.NilError(t, err)
... ...
@@ -166,7 +166,7 @@ func TestCreateServiceMaxReplicas(t *testing.T) {
166 166
 	}
167 167
 
168 168
 	serviceID := swarm.CreateService(t, d, serviceSpec...)
169
-	poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, maxReplicas), swarm.ServicePoll)
169
+	poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, maxReplicas), swarm.ServicePoll)
170 170
 
171 171
 	_, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
172 172
 	assert.NilError(t, err)
... ...
@@ -198,7 +198,7 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
198 198
 		swarm.ServiceWithNetwork(name),
199 199
 	)
200 200
 
201
-	poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
201
+	poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
202 202
 
203 203
 	resp, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
204 204
 	assert.NilError(t, err)
... ...
@@ -261,7 +261,7 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
261 261
 		}),
262 262
 	)
263 263
 
264
-	poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
264
+	poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
265 265
 
266 266
 	filter := filters.NewArgs()
267 267
 	filter.Add("service", serviceID)
... ...
@@ -325,7 +325,7 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
325 325
 		}),
326 326
 	)
327 327
 
328
-	poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances))
328
+	poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances))
329 329
 
330 330
 	filter := filters.NewArgs()
331 331
 	filter.Add("service", serviceID)
... ...
@@ -404,7 +404,7 @@ func TestCreateServiceSysctls(t *testing.T) {
404 404
 		)
405 405
 
406 406
 		// wait for the service to converge to 1 running task as expected
407
-		poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances))
407
+		poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances))
408 408
 
409 409
 		// we're going to check 3 things:
410 410
 		//
... ...
@@ -447,26 +447,3 @@ func TestCreateServiceSysctls(t *testing.T) {
447 447
 		)
448 448
 	}
449 449
 }
450
-
451
-func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result {
452
-	return func(log poll.LogT) poll.Result {
453
-		filter := filters.NewArgs()
454
-		filter.Add("service", serviceID)
455
-		tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
456
-			Filters: filter,
457
-		})
458
-		switch {
459
-		case err != nil:
460
-			return poll.Error(err)
461
-		case len(tasks) == int(instances):
462
-			for _, task := range tasks {
463
-				if task.Status.State != swarmtypes.TaskStateRunning {
464
-					return poll.Continue("waiting for tasks to enter run state")
465
-				}
466
-			}
467
-			return poll.Success()
468
-		default:
469
-			return poll.Continue("task count at %d waiting for %d", len(tasks), instances)
470
-		}
471
-	}
472
-}