mirror of
https://github.com/genuinetools/reg.git
synced 2024-08-21 14:20:23 -04:00
165 lines
4.8 KiB
Go
165 lines
4.8 KiB
Go
package service
|
|
|
|
import (
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/client"
|
|
"github.com/docker/docker/integration-cli/request"
|
|
"github.com/gotestyourself/gotestyourself/poll"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
func TestCreateServiceMultipleTimes(t *testing.T) {
|
|
defer setupTest(t)()
|
|
d := newSwarm(t)
|
|
defer d.Stop(t)
|
|
client, err := request.NewClientForHost(d.Sock())
|
|
require.NoError(t, err)
|
|
|
|
overlayName := "overlay1"
|
|
networkCreate := types.NetworkCreate{
|
|
CheckDuplicate: true,
|
|
Driver: "overlay",
|
|
}
|
|
|
|
netResp, err := client.NetworkCreate(context.Background(), overlayName, networkCreate)
|
|
require.NoError(t, err)
|
|
overlayID := netResp.ID
|
|
|
|
var instances uint64 = 4
|
|
serviceSpec := swarmServiceSpec("TestService", instances)
|
|
serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarm.NetworkAttachmentConfig{Target: overlayName})
|
|
|
|
serviceResp, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{
|
|
QueryRegistry: false,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
pollSettings := func(config *poll.Settings) {
|
|
if runtime.GOARCH == "arm" {
|
|
config.Timeout = 30 * time.Second
|
|
config.Delay = 100 * time.Millisecond
|
|
}
|
|
}
|
|
|
|
serviceID := serviceResp.ID
|
|
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), pollSettings)
|
|
|
|
_, _, err = client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
|
require.NoError(t, err)
|
|
|
|
err = client.ServiceRemove(context.Background(), serviceID)
|
|
require.NoError(t, err)
|
|
|
|
poll.WaitOn(t, serviceIsRemoved(client, serviceID), pollSettings)
|
|
poll.WaitOn(t, noTasks(client), pollSettings)
|
|
|
|
serviceResp, err = client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{
|
|
QueryRegistry: false,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
serviceID2 := serviceResp.ID
|
|
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), pollSettings)
|
|
|
|
err = client.ServiceRemove(context.Background(), serviceID2)
|
|
require.NoError(t, err)
|
|
|
|
poll.WaitOn(t, serviceIsRemoved(client, serviceID2), pollSettings)
|
|
poll.WaitOn(t, noTasks(client), pollSettings)
|
|
|
|
err = client.NetworkRemove(context.Background(), overlayID)
|
|
require.NoError(t, err)
|
|
|
|
poll.WaitOn(t, networkIsRemoved(client, overlayID), poll.WithTimeout(1*time.Minute), poll.WithDelay(10*time.Second))
|
|
}
|
|
|
|
func swarmServiceSpec(name string, replicas uint64) swarm.ServiceSpec {
|
|
return swarm.ServiceSpec{
|
|
Annotations: swarm.Annotations{
|
|
Name: name,
|
|
},
|
|
TaskTemplate: swarm.TaskSpec{
|
|
ContainerSpec: &swarm.ContainerSpec{
|
|
Image: "busybox:latest",
|
|
Command: []string{"/bin/top"},
|
|
},
|
|
},
|
|
Mode: swarm.ServiceMode{
|
|
Replicated: &swarm.ReplicatedService{
|
|
Replicas: &replicas,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
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 != swarm.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)
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
func networkIsRemoved(client client.NetworkAPIClient, networkID string) func(log poll.LogT) poll.Result {
|
|
return func(log poll.LogT) poll.Result {
|
|
_, err := client.NetworkInspect(context.Background(), networkID, types.NetworkInspectOptions{})
|
|
if err == nil {
|
|
return poll.Continue("waiting for network %s to be removed", networkID)
|
|
}
|
|
return poll.Success()
|
|
}
|
|
}
|