Integration tests: remove some duplicated code, and preserve context

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>
This commit is contained in:
Sebastiaan van Stijn 2019-01-14 02:20:09 +01:00
parent 60d93aab2e
commit 56a68c15f8
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 71 additions and 80 deletions

View file

@ -0,0 +1,47 @@
package swarm
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"gotest.tools/poll"
)
// NoTasksForService verifies that there are no more tasks for the given service
func NoTasksForService(ctx context.Context, client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result {
return func(log poll.LogT) poll.Result {
tasks, err := client.TaskList(ctx, types.TaskListOptions{
Filters: filters.NewArgs(
filters.Arg("service", serviceID),
),
})
if err == nil {
if len(tasks) == 0 {
return poll.Success()
}
if len(tasks) > 0 {
return poll.Continue("task count for service %s at %d waiting for 0", serviceID, len(tasks))
}
return poll.Continue("waiting for tasks for service %s to be deleted", serviceID)
}
// TODO we should not use an error as indication that the tasks are gone. There may be other reasons for an error to occur.
return poll.Success()
}
}
// NoTasks verifies that all tasks are gone
func NoTasks(ctx context.Context, client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
return func(log poll.LogT) poll.Result {
tasks, err := client.TaskList(ctx, types.TaskListOptions{})
switch {
case err != nil:
return poll.Error(err)
case len(tasks) == 0:
return poll.Success()
default:
return poll.Continue("waiting for all tasks to be removed: task count at %d", len(tasks))
}
}
}

View file

@ -98,7 +98,7 @@ func TestInspectNetwork(t *testing.T) {
// 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. // 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.
err := c.ServiceRemove(ctx, serviceID) err := c.ServiceRemove(ctx, serviceID)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, serviceIsRemoved(c, serviceID), swarm.ServicePoll) poll.WaitOn(t, swarm.NoTasksForService(ctx, c, serviceID), swarm.ServicePoll)
err = c.NetworkRemove(ctx, overlayID) err = c.NetworkRemove(ctx, overlayID)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll) poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll)
@ -130,17 +130,3 @@ func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string,
} }
} }
} }
func serviceIsRemoved(client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result {
return func(log poll.LogT) poll.Result {
filter := filters.NewArgs()
filter.Add("service", serviceID)
_, err := client.TaskList(context.Background(), types.TaskListOptions{
Filters: filter,
})
if err == nil {
return poll.Continue("waiting for service %s to be deleted", serviceID)
}
return poll.Success()
}
}

View file

@ -254,18 +254,19 @@ func TestServiceRemoveKeepsIngressNetwork(t *testing.T) {
poll.WaitOn(t, serviceRunningCount(c, serviceID, instances), swarm.ServicePoll) poll.WaitOn(t, serviceRunningCount(c, serviceID, instances), swarm.ServicePoll)
_, _, err := c.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{}) ctx := context.Background()
_, _, err := c.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
assert.NilError(t, err) assert.NilError(t, err)
err = c.ServiceRemove(context.Background(), serviceID) err = c.ServiceRemove(ctx, serviceID)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, serviceIsRemoved(c, serviceID), swarm.ServicePoll) poll.WaitOn(t, noServices(ctx, c), swarm.ServicePoll)
poll.WaitOn(t, noServices(c), swarm.ServicePoll) poll.WaitOn(t, swarm.NoTasks(ctx, c), swarm.ServicePoll)
// Ensure that "ingress" is not removed or corrupted // Ensure that "ingress" is not removed or corrupted
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
netInfo, err := c.NetworkInspect(context.Background(), ingressNet, types.NetworkInspectOptions{ netInfo, err := c.NetworkInspect(ctx, ingressNet, types.NetworkInspectOptions{
Verbose: true, Verbose: true,
Scope: "swarm", Scope: "swarm",
}) })
@ -312,16 +313,16 @@ func swarmIngressReady(client client.NetworkAPIClient) func(log poll.LogT) poll.
} }
} }
func noServices(client client.ServiceAPIClient) func(log poll.LogT) poll.Result { func noServices(ctx context.Context, client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
return func(log poll.LogT) poll.Result { return func(log poll.LogT) poll.Result {
services, err := client.ServiceList(context.Background(), types.ServiceListOptions{}) services, err := client.ServiceList(ctx, types.ServiceListOptions{})
switch { switch {
case err != nil: case err != nil:
return poll.Error(err) return poll.Error(err)
case len(services) == 0: case len(services) == 0:
return poll.Success() return poll.Success()
default: default:
return poll.Continue("Service count at %d waiting for 0", len(services)) return poll.Continue("waiting for all services to be removed: service count at %d", len(services))
} }
} }
} }

View file

@ -82,9 +82,10 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
defer d.Stop(t) defer d.Stop(t)
client := d.NewClientT(t) client := d.NewClientT(t)
defer client.Close() defer client.Close()
ctx := context.Background()
overlayName := "overlay1_" + t.Name() overlayName := "overlay1_" + t.Name()
overlayID := network.CreateNoError(t, context.Background(), client, overlayName, overlayID := network.CreateNoError(t, ctx, client, overlayName,
network.WithCheckDuplicate(), network.WithCheckDuplicate(),
network.WithDriver("overlay"), network.WithDriver("overlay"),
) )
@ -107,8 +108,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
err = client.ServiceRemove(context.Background(), serviceID) err = client.ServiceRemove(context.Background(), serviceID)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll) poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID), swarm.ServicePoll)
poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
serviceID2 := swarm.CreateService(t, d, serviceSpec...) serviceID2 := swarm.CreateService(t, d, serviceSpec...)
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll) poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll)
@ -116,8 +116,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
err = client.ServiceRemove(context.Background(), serviceID2) err = client.ServiceRemove(context.Background(), serviceID2)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, serviceIsRemoved(client, serviceID2), swarm.ServicePoll) poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID2), swarm.ServicePoll)
poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
err = client.NetworkRemove(context.Background(), overlayID) err = client.NetworkRemove(context.Background(), overlayID)
assert.NilError(t, err) assert.NilError(t, err)
@ -180,19 +179,14 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
defer d.Stop(t) defer d.Stop(t)
client := d.NewClientT(t) client := d.NewClientT(t)
defer client.Close() defer client.Close()
ctx := context.Background()
name := "foo_" + t.Name() name := "foo_" + t.Name()
n1 := network.CreateNoError(t, context.Background(), client, name, n1 := network.CreateNoError(t, ctx, client, name, network.WithDriver("bridge"))
network.WithDriver("bridge"), n2 := network.CreateNoError(t, ctx, client, name, network.WithDriver("bridge"))
)
n2 := network.CreateNoError(t, context.Background(), client, name,
network.WithDriver("bridge"),
)
// Duplicates with name but with different driver // Duplicates with name but with different driver
n3 := network.CreateNoError(t, context.Background(), client, name, n3 := network.CreateNoError(t, ctx, client, name, network.WithDriver("overlay"))
network.WithDriver("overlay"),
)
// Create Service with the same name // Create Service with the same name
var instances uint64 = 1 var instances uint64 = 1
@ -206,16 +200,14 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll) poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
resp, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{}) resp, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Equal(n3, resp.Spec.TaskTemplate.Networks[0].Target)) assert.Check(t, is.Equal(n3, resp.Spec.TaskTemplate.Networks[0].Target))
// Remove Service // Remove Service, and wait for its tasks to be removed
err = client.ServiceRemove(context.Background(), serviceID) err = client.ServiceRemove(ctx, serviceID)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID), swarm.ServicePoll)
// Make sure task has been destroyed.
poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll)
// Remove networks // Remove networks
err = client.NetworkRemove(context.Background(), n3) err = client.NetworkRemove(context.Background(), n3)
@ -291,9 +283,7 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
err = client.ServiceRemove(ctx, serviceID) err = client.ServiceRemove(ctx, serviceID)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID), swarm.ServicePoll)
poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll)
poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
err = client.SecretRemove(ctx, secretName) err = client.SecretRemove(ctx, secretName)
assert.NilError(t, err) assert.NilError(t, err)
@ -357,9 +347,7 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
err = client.ServiceRemove(ctx, serviceID) err = client.ServiceRemove(ctx, serviceID)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID))
poll.WaitOn(t, serviceIsRemoved(client, serviceID))
poll.WaitOn(t, noTasks(client))
err = client.ConfigRemove(ctx, configName) err = client.ConfigRemove(ctx, configName)
assert.NilError(t, err) assert.NilError(t, err)
@ -482,34 +470,3 @@ func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string,
} }
} }
} }
func noTasks(client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
return func(log poll.LogT) poll.Result {
filter := filters.NewArgs()
tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
Filters: filter,
})
switch {
case err != nil:
return poll.Error(err)
case len(tasks) == 0:
return poll.Success()
default:
return poll.Continue("task count at %d waiting for 0", len(tasks))
}
}
}
func serviceIsRemoved(client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result {
return func(log poll.LogT) poll.Result {
filter := filters.NewArgs()
filter.Add("service", serviceID)
_, err := client.TaskList(context.Background(), types.TaskListOptions{
Filters: filter,
})
if err == nil {
return poll.Continue("waiting for service %s to be deleted", serviceID)
}
return poll.Success()
}
}