Merge pull request #38598 from yongtang/serviceRunningTasksCount
Move serviceRunningTasksCount to integration/internal/swarm
This commit is contained in:
commit
de86ba27fb
3 changed files with 36 additions and 64 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue