|
| 1 | +package network // import "github.com/docker/docker/integration/network" |
| 2 | + |
| 3 | +import ( |
| 4 | + "runtime" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/docker/docker/api/types" |
| 9 | + "github.com/docker/docker/api/types/filters" |
| 10 | + "github.com/docker/docker/api/types/swarm" |
| 11 | + "github.com/docker/docker/client" |
| 12 | + "github.com/gotestyourself/gotestyourself/poll" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "golang.org/x/net/context" |
| 15 | +) |
| 16 | + |
| 17 | +func TestServiceWithPredefinedNetwork(t *testing.T) { |
| 18 | + defer setupTest(t)() |
| 19 | + d := newSwarm(t) |
| 20 | + defer d.Stop(t) |
| 21 | + client, err := client.NewClientWithOpts(client.WithHost((d.Sock()))) |
| 22 | + require.NoError(t, err) |
| 23 | + |
| 24 | + hostName := "host" |
| 25 | + var instances uint64 = 1 |
| 26 | + serviceName := "TestService" |
| 27 | + serviceSpec := swarmServiceSpec(serviceName, instances) |
| 28 | + serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarm.NetworkAttachmentConfig{Target: hostName}) |
| 29 | + |
| 30 | + serviceResp, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{ |
| 31 | + QueryRegistry: false, |
| 32 | + }) |
| 33 | + require.NoError(t, err) |
| 34 | + |
| 35 | + pollSettings := func(config *poll.Settings) { |
| 36 | + if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" { |
| 37 | + config.Timeout = 50 * time.Second |
| 38 | + config.Delay = 100 * time.Millisecond |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + serviceID := serviceResp.ID |
| 43 | + poll.WaitOn(t, serviceRunningCount(client, serviceID, instances), pollSettings) |
| 44 | + |
| 45 | + _, _, err = client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{}) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + err = client.ServiceRemove(context.Background(), serviceID) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + poll.WaitOn(t, serviceIsRemoved(client, serviceID), pollSettings) |
| 52 | + poll.WaitOn(t, noTasks(client), pollSettings) |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +func serviceRunningCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result { |
| 57 | + return func(log poll.LogT) poll.Result { |
| 58 | + filter := filters.NewArgs() |
| 59 | + filter.Add("service", serviceID) |
| 60 | + services, err := client.ServiceList(context.Background(), types.ServiceListOptions{}) |
| 61 | + if err != nil { |
| 62 | + return poll.Error(err) |
| 63 | + } |
| 64 | + |
| 65 | + if len(services) != int(instances) { |
| 66 | + return poll.Continue("Service count at %d waiting for %d", len(services), instances) |
| 67 | + } |
| 68 | + return poll.Success() |
| 69 | + } |
| 70 | +} |
0 commit comments