From e485a60e2bcc59860f387c94f6afaa0130ea7040 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Sat, 19 Jan 2019 18:54:32 +0000 Subject: [PATCH] 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 --- integration/internal/swarm/states.go | 25 ++++++++++++++++ integration/network/inspect_test.go | 32 +-------------------- integration/service/create_test.go | 43 +++++++--------------------- 3 files changed, 36 insertions(+), 64 deletions(-) diff --git a/integration/internal/swarm/states.go b/integration/internal/swarm/states.go index 51d6200594..c51e1eed40 100644 --- a/integration/internal/swarm/states.go +++ b/integration/internal/swarm/states.go @@ -5,6 +5,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" + swarmtypes "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" "gotest.tools/poll" ) @@ -45,3 +46,27 @@ func NoTasks(ctx context.Context, client client.ServiceAPIClient) func(log poll. } } } + +// RunningTasksCount verifies there are `instances` tasks running for `serviceID` +func RunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result { + return func(log poll.LogT) poll.Result { + filter := filters.NewArgs() + filter.Add("service", serviceID) + tasks, err := client.TaskList(context.Background(), types.TaskListOptions{ + Filters: filter, + }) + switch { + case err != nil: + return poll.Error(err) + case len(tasks) == int(instances): + for _, task := range tasks { + if task.Status.State != swarmtypes.TaskStateRunning { + return poll.Continue("waiting for tasks to enter run state") + } + } + return poll.Success() + default: + return poll.Continue("task count at %d waiting for %d", len(tasks), instances) + } + } +} diff --git a/integration/network/inspect_test.go b/integration/network/inspect_test.go index d12ad6781d..02d2b754f5 100644 --- a/integration/network/inspect_test.go +++ b/integration/network/inspect_test.go @@ -5,9 +5,6 @@ import ( "testing" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/filters" - swarmtypes "github.com/docker/docker/api/types/swarm" - "github.com/docker/docker/client" "github.com/docker/docker/integration/internal/network" "github.com/docker/docker/integration/internal/swarm" "gotest.tools/assert" @@ -38,7 +35,7 @@ func TestInspectNetwork(t *testing.T) { swarm.ServiceWithNetwork(networkName), ) - poll.WaitOn(t, serviceRunningTasksCount(c, serviceID, instances), swarm.ServicePoll) + poll.WaitOn(t, swarm.RunningTasksCount(c, serviceID, instances), swarm.ServicePoll) tests := []struct { name string @@ -103,30 +100,3 @@ func TestInspectNetwork(t *testing.T) { assert.NilError(t, err) poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll) } - -func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result { - return func(log poll.LogT) poll.Result { - tasks, err := client.TaskList(context.Background(), types.TaskListOptions{ - Filters: filters.NewArgs( - filters.Arg("service", serviceID), - filters.Arg("desired-state", string(swarmtypes.TaskStateRunning)), - ), - }) - switch { - case err != nil: - return poll.Error(err) - case len(tasks) == int(instances): - for _, task := range tasks { - if task.Status.Err != "" { - log.Log("task error:", task.Status.Err) - } - if task.Status.State != swarmtypes.TaskStateRunning { - return poll.Continue("waiting for tasks to enter run state (current status: %s)", task.Status.State) - } - } - return poll.Success() - default: - return poll.Continue("task count for service %s at %d waiting for %d", serviceID, len(tasks), instances) - } - } -} diff --git a/integration/service/create_test.go b/integration/service/create_test.go index 41fe44ab16..553124a284 100644 --- a/integration/service/create_test.go +++ b/integration/service/create_test.go @@ -45,18 +45,18 @@ func testServiceCreateInit(daemonEnabled bool) func(t *testing.T) { booleanFalse := false serviceID := swarm.CreateService(t, d) - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, 1), swarm.ServicePoll) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, 1), swarm.ServicePoll) i := inspectServiceContainer(t, client, serviceID) // HostConfig.Init == nil means that it delegates to daemon configuration assert.Check(t, i.HostConfig.Init == nil) serviceID = swarm.CreateService(t, d, swarm.ServiceWithInit(&booleanTrue)) - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, 1), swarm.ServicePoll) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, 1), swarm.ServicePoll) i = inspectServiceContainer(t, client, serviceID) assert.Check(t, is.Equal(true, *i.HostConfig.Init)) serviceID = swarm.CreateService(t, d, swarm.ServiceWithInit(&booleanFalse)) - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, 1), swarm.ServicePoll) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, 1), swarm.ServicePoll) i = inspectServiceContainer(t, client, serviceID) assert.Check(t, is.Equal(false, *i.HostConfig.Init)) } @@ -100,7 +100,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) { } serviceID := swarm.CreateService(t, d, serviceSpec...) - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances), swarm.ServicePoll) _, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{}) assert.NilError(t, err) @@ -111,7 +111,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) { poll.WaitOn(t, swarm.NoTasksForService(ctx, client, serviceID), swarm.ServicePoll) serviceID2 := swarm.CreateService(t, d, serviceSpec...) - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID2, instances), swarm.ServicePoll) err = client.ServiceRemove(context.Background(), serviceID2) assert.NilError(t, err) @@ -166,7 +166,7 @@ func TestCreateServiceMaxReplicas(t *testing.T) { } serviceID := swarm.CreateService(t, d, serviceSpec...) - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, maxReplicas), swarm.ServicePoll) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, maxReplicas), swarm.ServicePoll) _, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{}) assert.NilError(t, err) @@ -198,7 +198,7 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) { swarm.ServiceWithNetwork(name), ) - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances), swarm.ServicePoll) resp, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{}) assert.NilError(t, err) @@ -261,7 +261,7 @@ func TestCreateServiceSecretFileMode(t *testing.T) { }), ) - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances), swarm.ServicePoll) filter := filters.NewArgs() filter.Add("service", serviceID) @@ -325,7 +325,7 @@ func TestCreateServiceConfigFileMode(t *testing.T) { }), ) - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances)) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances)) filter := filters.NewArgs() filter.Add("service", serviceID) @@ -404,7 +404,7 @@ func TestCreateServiceSysctls(t *testing.T) { ) // wait for the service to converge to 1 running task as expected - poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances)) + poll.WaitOn(t, swarm.RunningTasksCount(client, serviceID, instances)) // we're going to check 3 things: // @@ -447,26 +447,3 @@ func TestCreateServiceSysctls(t *testing.T) { ) } } - -func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result { - return func(log poll.LogT) poll.Result { - filter := filters.NewArgs() - filter.Add("service", serviceID) - tasks, err := client.TaskList(context.Background(), types.TaskListOptions{ - Filters: filter, - }) - switch { - case err != nil: - return poll.Error(err) - case len(tasks) == int(instances): - for _, task := range tasks { - if task.Status.State != swarmtypes.TaskStateRunning { - return poll.Continue("waiting for tasks to enter run state") - } - } - return poll.Success() - default: - return poll.Continue("task count at %d waiting for %d", len(tasks), instances) - } - } -}