This introduces `NoTasksForService` and `NoTasks` poller checks, that
can be used to check if no tasks are left in general, or for a specific
service.
Some redundant checks were also removed from some tests.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,47 @@ |
| 0 |
+package swarm |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "context" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/docker/docker/api/types" |
|
| 6 |
+ "github.com/docker/docker/api/types/filters" |
|
| 7 |
+ "github.com/docker/docker/client" |
|
| 8 |
+ "gotest.tools/poll" |
|
| 9 |
+) |
|
| 10 |
+ |
|
| 11 |
+// NoTasksForService verifies that there are no more tasks for the given service |
|
| 12 |
+func NoTasksForService(ctx context.Context, client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result {
|
|
| 13 |
+ return func(log poll.LogT) poll.Result {
|
|
| 14 |
+ tasks, err := client.TaskList(ctx, types.TaskListOptions{
|
|
| 15 |
+ Filters: filters.NewArgs( |
|
| 16 |
+ filters.Arg("service", serviceID),
|
|
| 17 |
+ ), |
|
| 18 |
+ }) |
|
| 19 |
+ if err == nil {
|
|
| 20 |
+ if len(tasks) == 0 {
|
|
| 21 |
+ return poll.Success() |
|
| 22 |
+ } |
|
| 23 |
+ if len(tasks) > 0 {
|
|
| 24 |
+ return poll.Continue("task count for service %s at %d waiting for 0", serviceID, len(tasks))
|
|
| 25 |
+ } |
|
| 26 |
+ return poll.Continue("waiting for tasks for service %s to be deleted", serviceID)
|
|
| 27 |
+ } |
|
| 28 |
+ // TODO we should not use an error as indication that the tasks are gone. There may be other reasons for an error to occur. |
|
| 29 |
+ return poll.Success() |
|
| 30 |
+ } |
|
| 31 |
+} |
|
| 32 |
+ |
|
| 33 |
+// NoTasks verifies that all tasks are gone |
|
| 34 |
+func NoTasks(ctx context.Context, client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
|
|
| 35 |
+ return func(log poll.LogT) poll.Result {
|
|
| 36 |
+ tasks, err := client.TaskList(ctx, types.TaskListOptions{})
|
|
| 37 |
+ switch {
|
|
| 38 |
+ case err != nil: |
|
| 39 |
+ return poll.Error(err) |
|
| 40 |
+ case len(tasks) == 0: |
|
| 41 |
+ return poll.Success() |
|
| 42 |
+ default: |
|
| 43 |
+ return poll.Continue("waiting for all tasks to be removed: task count at %d", len(tasks))
|
|
| 44 |
+ } |
|
| 45 |
+ } |
|
| 46 |
+} |
| ... | ... |
@@ -98,7 +98,7 @@ func TestInspectNetwork(t *testing.T) {
|
| 98 | 98 |
// TODO find out why removing networks is needed; other tests fail if the network is not removed, even though they run on a new daemon. |
| 99 | 99 |
err := c.ServiceRemove(ctx, serviceID) |
| 100 | 100 |
assert.NilError(t, err) |
| 101 |
- poll.WaitOn(t, serviceIsRemoved(c, serviceID), swarm.ServicePoll) |
|
| 101 |
+ poll.WaitOn(t, swarm.NoTasksForService(ctx, c, serviceID), swarm.ServicePoll) |
|
| 102 | 102 |
err = c.NetworkRemove(ctx, overlayID) |
| 103 | 103 |
assert.NilError(t, err) |
| 104 | 104 |
poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll) |
| ... | ... |
@@ -130,17 +130,3 @@ func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, |
| 130 | 130 |
} |
| 131 | 131 |
} |
| 132 | 132 |
} |
| 133 |
- |
|
| 134 |
-func serviceIsRemoved(client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result {
|
|
| 135 |
- return func(log poll.LogT) poll.Result {
|
|
| 136 |
- filter := filters.NewArgs() |
|
| 137 |
- filter.Add("service", serviceID)
|
|
| 138 |
- _, err := client.TaskList(context.Background(), types.TaskListOptions{
|
|
| 139 |
- Filters: filter, |
|
| 140 |
- }) |
|
| 141 |
- if err == nil {
|
|
| 142 |
- return poll.Continue("waiting for service %s to be deleted", serviceID)
|
|
| 143 |
- } |
|
| 144 |
- return poll.Success() |
|
| 145 |
- } |
|
| 146 |
-} |
| ... | ... |
@@ -254,18 +254,19 @@ func TestServiceRemoveKeepsIngressNetwork(t *testing.T) {
|
| 254 | 254 |
|
| 255 | 255 |
poll.WaitOn(t, serviceRunningCount(c, serviceID, instances), swarm.ServicePoll) |
| 256 | 256 |
|
| 257 |
- _, _, err := c.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
|
| 257 |
+ ctx := context.Background() |
|
| 258 |
+ _, _, err := c.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
|
|
| 258 | 259 |
assert.NilError(t, err) |
| 259 | 260 |
|
| 260 |
- err = c.ServiceRemove(context.Background(), serviceID) |
|
| 261 |
+ err = c.ServiceRemove(ctx, serviceID) |
|
| 261 | 262 |
assert.NilError(t, err) |
| 262 | 263 |
|
| 263 |
- poll.WaitOn(t, serviceIsRemoved(c, serviceID), swarm.ServicePoll) |
|
| 264 |
- poll.WaitOn(t, noServices(c), swarm.ServicePoll) |
|
| 264 |
+ poll.WaitOn(t, noServices(ctx, c), swarm.ServicePoll) |
|
| 265 |
+ poll.WaitOn(t, swarm.NoTasks(ctx, c), swarm.ServicePoll) |
|
| 265 | 266 |
|
| 266 | 267 |
// Ensure that "ingress" is not removed or corrupted |
| 267 | 268 |
time.Sleep(10 * time.Second) |
| 268 |
- netInfo, err := c.NetworkInspect(context.Background(), ingressNet, types.NetworkInspectOptions{
|
|
| 269 |
+ netInfo, err := c.NetworkInspect(ctx, ingressNet, types.NetworkInspectOptions{
|
|
| 269 | 270 |
Verbose: true, |
| 270 | 271 |
Scope: "swarm", |
| 271 | 272 |
}) |
| ... | ... |
@@ -312,16 +313,16 @@ func swarmIngressReady(client client.NetworkAPIClient) func(log poll.LogT) poll. |
| 312 | 312 |
} |
| 313 | 313 |
} |
| 314 | 314 |
|
| 315 |
-func noServices(client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
|
|
| 315 |
+func noServices(ctx context.Context, client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
|
|
| 316 | 316 |
return func(log poll.LogT) poll.Result {
|
| 317 |
- services, err := client.ServiceList(context.Background(), types.ServiceListOptions{})
|
|
| 317 |
+ services, err := client.ServiceList(ctx, types.ServiceListOptions{})
|
|
| 318 | 318 |
switch {
|
| 319 | 319 |
case err != nil: |
| 320 | 320 |
return poll.Error(err) |
| 321 | 321 |
case len(services) == 0: |
| 322 | 322 |
return poll.Success() |
| 323 | 323 |
default: |
| 324 |
- return poll.Continue("Service count at %d waiting for 0", len(services))
|
|
| 324 |
+ return poll.Continue("waiting for all services to be removed: service count at %d", len(services))
|
|
| 325 | 325 |
} |
| 326 | 326 |
} |
| 327 | 327 |
} |
| ... | ... |
@@ -82,9 +82,10 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
|
| 82 | 82 |
defer d.Stop(t) |
| 83 | 83 |
client := d.NewClientT(t) |
| 84 | 84 |
defer client.Close() |
| 85 |
+ ctx := context.Background() |
|
| 85 | 86 |
|
| 86 | 87 |
overlayName := "overlay1_" + t.Name() |
| 87 |
- overlayID := network.CreateNoError(t, context.Background(), client, overlayName, |
|
| 88 |
+ overlayID := network.CreateNoError(t, ctx, client, overlayName, |
|
| 88 | 89 |
network.WithCheckDuplicate(), |
| 89 | 90 |
network.WithDriver("overlay"),
|
| 90 | 91 |
) |
| ... | ... |
@@ -107,8 +108,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
|
| 107 | 107 |
err = client.ServiceRemove(context.Background(), serviceID) |
| 108 | 108 |
assert.NilError(t, err) |
| 109 | 109 |
|
| 110 |
- poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll) |
|
| 111 |
- poll.WaitOn(t, noTasks(client), swarm.ServicePoll) |
|
| 110 |
+ poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID), swarm.ServicePoll) |
|
| 112 | 111 |
|
| 113 | 112 |
serviceID2 := swarm.CreateService(t, d, serviceSpec...) |
| 114 | 113 |
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll) |
| ... | ... |
@@ -116,8 +116,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
|
| 116 | 116 |
err = client.ServiceRemove(context.Background(), serviceID2) |
| 117 | 117 |
assert.NilError(t, err) |
| 118 | 118 |
|
| 119 |
- poll.WaitOn(t, serviceIsRemoved(client, serviceID2), swarm.ServicePoll) |
|
| 120 |
- poll.WaitOn(t, noTasks(client), swarm.ServicePoll) |
|
| 119 |
+ poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID2), swarm.ServicePoll) |
|
| 121 | 120 |
|
| 122 | 121 |
err = client.NetworkRemove(context.Background(), overlayID) |
| 123 | 122 |
assert.NilError(t, err) |
| ... | ... |
@@ -180,19 +179,14 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
|
| 180 | 180 |
defer d.Stop(t) |
| 181 | 181 |
client := d.NewClientT(t) |
| 182 | 182 |
defer client.Close() |
| 183 |
+ ctx := context.Background() |
|
| 183 | 184 |
|
| 184 | 185 |
name := "foo_" + t.Name() |
| 185 |
- n1 := network.CreateNoError(t, context.Background(), client, name, |
|
| 186 |
- network.WithDriver("bridge"),
|
|
| 187 |
- ) |
|
| 188 |
- n2 := network.CreateNoError(t, context.Background(), client, name, |
|
| 189 |
- network.WithDriver("bridge"),
|
|
| 190 |
- ) |
|
| 186 |
+ n1 := network.CreateNoError(t, ctx, client, name, network.WithDriver("bridge"))
|
|
| 187 |
+ n2 := network.CreateNoError(t, ctx, client, name, network.WithDriver("bridge"))
|
|
| 191 | 188 |
|
| 192 | 189 |
// Duplicates with name but with different driver |
| 193 |
- n3 := network.CreateNoError(t, context.Background(), client, name, |
|
| 194 |
- network.WithDriver("overlay"),
|
|
| 195 |
- ) |
|
| 190 |
+ n3 := network.CreateNoError(t, ctx, client, name, network.WithDriver("overlay"))
|
|
| 196 | 191 |
|
| 197 | 192 |
// Create Service with the same name |
| 198 | 193 |
var instances uint64 = 1 |
| ... | ... |
@@ -206,16 +200,14 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
|
| 206 | 206 |
|
| 207 | 207 |
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll) |
| 208 | 208 |
|
| 209 |
- resp, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
|
| 209 |
+ resp, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
|
|
| 210 | 210 |
assert.NilError(t, err) |
| 211 | 211 |
assert.Check(t, is.Equal(n3, resp.Spec.TaskTemplate.Networks[0].Target)) |
| 212 | 212 |
|
| 213 |
- // Remove Service |
|
| 214 |
- err = client.ServiceRemove(context.Background(), serviceID) |
|
| 213 |
+ // Remove Service, and wait for its tasks to be removed |
|
| 214 |
+ err = client.ServiceRemove(ctx, serviceID) |
|
| 215 | 215 |
assert.NilError(t, err) |
| 216 |
- |
|
| 217 |
- // Make sure task has been destroyed. |
|
| 218 |
- poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll) |
|
| 216 |
+ poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID), swarm.ServicePoll) |
|
| 219 | 217 |
|
| 220 | 218 |
// Remove networks |
| 221 | 219 |
err = client.NetworkRemove(context.Background(), n3) |
| ... | ... |
@@ -291,9 +283,7 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
|
| 291 | 291 |
|
| 292 | 292 |
err = client.ServiceRemove(ctx, serviceID) |
| 293 | 293 |
assert.NilError(t, err) |
| 294 |
- |
|
| 295 |
- poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll) |
|
| 296 |
- poll.WaitOn(t, noTasks(client), swarm.ServicePoll) |
|
| 294 |
+ poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID), swarm.ServicePoll) |
|
| 297 | 295 |
|
| 298 | 296 |
err = client.SecretRemove(ctx, secretName) |
| 299 | 297 |
assert.NilError(t, err) |
| ... | ... |
@@ -357,9 +347,7 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
|
| 357 | 357 |
|
| 358 | 358 |
err = client.ServiceRemove(ctx, serviceID) |
| 359 | 359 |
assert.NilError(t, err) |
| 360 |
- |
|
| 361 |
- poll.WaitOn(t, serviceIsRemoved(client, serviceID)) |
|
| 362 |
- poll.WaitOn(t, noTasks(client)) |
|
| 360 |
+ poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID)) |
|
| 363 | 361 |
|
| 364 | 362 |
err = client.ConfigRemove(ctx, configName) |
| 365 | 363 |
assert.NilError(t, err) |
| ... | ... |
@@ -482,34 +470,3 @@ func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, |
| 482 | 482 |
} |
| 483 | 483 |
} |
| 484 | 484 |
} |
| 485 |
- |
|
| 486 |
-func noTasks(client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
|
|
| 487 |
- return func(log poll.LogT) poll.Result {
|
|
| 488 |
- filter := filters.NewArgs() |
|
| 489 |
- tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
|
|
| 490 |
- Filters: filter, |
|
| 491 |
- }) |
|
| 492 |
- switch {
|
|
| 493 |
- case err != nil: |
|
| 494 |
- return poll.Error(err) |
|
| 495 |
- case len(tasks) == 0: |
|
| 496 |
- return poll.Success() |
|
| 497 |
- default: |
|
| 498 |
- return poll.Continue("task count at %d waiting for 0", len(tasks))
|
|
| 499 |
- } |
|
| 500 |
- } |
|
| 501 |
-} |
|
| 502 |
- |
|
| 503 |
-func serviceIsRemoved(client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result {
|
|
| 504 |
- return func(log poll.LogT) poll.Result {
|
|
| 505 |
- filter := filters.NewArgs() |
|
| 506 |
- filter.Add("service", serviceID)
|
|
| 507 |
- _, err := client.TaskList(context.Background(), types.TaskListOptions{
|
|
| 508 |
- Filters: filter, |
|
| 509 |
- }) |
|
| 510 |
- if err == nil {
|
|
| 511 |
- return poll.Continue("waiting for service %s to be deleted", serviceID)
|
|
| 512 |
- } |
|
| 513 |
- return poll.Success() |
|
| 514 |
- } |
|
| 515 |
-} |