1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Migrate usage of newSwarm in integration/service to use integration/utils/swarm.NewSwarm

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2018-02-02 22:36:59 +00:00
parent 71c794d912
commit 1d40e92899
4 changed files with 59 additions and 78 deletions

View file

@ -8,9 +8,10 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm" swarmtypes "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/request" "github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/integration/util/swarm"
"github.com/gotestyourself/gotestyourself/poll" "github.com/gotestyourself/gotestyourself/poll"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -19,7 +20,7 @@ import (
func TestCreateServiceMultipleTimes(t *testing.T) { func TestCreateServiceMultipleTimes(t *testing.T) {
defer setupTest(t)() defer setupTest(t)()
d := newSwarm(t) d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t) defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock()) client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err) require.NoError(t, err)
@ -36,7 +37,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
var instances uint64 = 4 var instances uint64 = 4
serviceSpec := swarmServiceSpec("TestService", instances) serviceSpec := swarmServiceSpec("TestService", instances)
serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarm.NetworkAttachmentConfig{Target: overlayName}) serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarmtypes.NetworkAttachmentConfig{Target: overlayName})
serviceResp, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{ serviceResp, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{
QueryRegistry: false, QueryRegistry: false,
@ -85,7 +86,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
func TestCreateWithDuplicateNetworkNames(t *testing.T) { func TestCreateWithDuplicateNetworkNames(t *testing.T) {
defer setupTest(t)() defer setupTest(t)()
d := newSwarm(t) d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t) defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock()) client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err) require.NoError(t, err)
@ -111,7 +112,7 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
var instances uint64 = 1 var instances uint64 = 1
serviceSpec := swarmServiceSpec("top", instances) serviceSpec := swarmServiceSpec("top", instances)
serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarm.NetworkAttachmentConfig{Target: name}) serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarmtypes.NetworkAttachmentConfig{Target: name})
service, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{}) service, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{})
require.NoError(t, err) require.NoError(t, err)
@ -147,14 +148,14 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
func TestCreateServiceSecretFileMode(t *testing.T) { func TestCreateServiceSecretFileMode(t *testing.T) {
defer setupTest(t)() defer setupTest(t)()
d := newSwarm(t) d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t) defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock()) client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err) require.NoError(t, err)
ctx := context.Background() ctx := context.Background()
secretResp, err := client.SecretCreate(ctx, swarm.SecretSpec{ secretResp, err := client.SecretCreate(ctx, swarmtypes.SecretSpec{
Annotations: swarm.Annotations{ Annotations: swarmtypes.Annotations{
Name: "TestSecret", Name: "TestSecret",
}, },
Data: []byte("TESTSECRET"), Data: []byte("TESTSECRET"),
@ -162,17 +163,17 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
var instances uint64 = 1 var instances uint64 = 1
serviceSpec := swarm.ServiceSpec{ serviceSpec := swarmtypes.ServiceSpec{
Annotations: swarm.Annotations{ Annotations: swarmtypes.Annotations{
Name: "TestService", Name: "TestService",
}, },
TaskTemplate: swarm.TaskSpec{ TaskTemplate: swarmtypes.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{ ContainerSpec: &swarmtypes.ContainerSpec{
Image: "busybox:latest", Image: "busybox:latest",
Command: []string{"/bin/sh", "-c", "ls -l /etc/secret || /bin/top"}, Command: []string{"/bin/sh", "-c", "ls -l /etc/secret || /bin/top"},
Secrets: []*swarm.SecretReference{ Secrets: []*swarmtypes.SecretReference{
{ {
File: &swarm.SecretReferenceFileTarget{ File: &swarmtypes.SecretReferenceFileTarget{
Name: "/etc/secret", Name: "/etc/secret",
UID: "0", UID: "0",
GID: "0", GID: "0",
@ -184,8 +185,8 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
}, },
}, },
}, },
Mode: swarm.ServiceMode{ Mode: swarmtypes.ServiceMode{
Replicated: &swarm.ReplicatedService{ Replicated: &swarmtypes.ReplicatedService{
Replicas: &instances, Replicas: &instances,
}, },
}, },
@ -228,14 +229,14 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
func TestCreateServiceConfigFileMode(t *testing.T) { func TestCreateServiceConfigFileMode(t *testing.T) {
defer setupTest(t)() defer setupTest(t)()
d := newSwarm(t) d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t) defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock()) client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err) require.NoError(t, err)
ctx := context.Background() ctx := context.Background()
configResp, err := client.ConfigCreate(ctx, swarm.ConfigSpec{ configResp, err := client.ConfigCreate(ctx, swarmtypes.ConfigSpec{
Annotations: swarm.Annotations{ Annotations: swarmtypes.Annotations{
Name: "TestConfig", Name: "TestConfig",
}, },
Data: []byte("TESTCONFIG"), Data: []byte("TESTCONFIG"),
@ -243,17 +244,17 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
var instances uint64 = 1 var instances uint64 = 1
serviceSpec := swarm.ServiceSpec{ serviceSpec := swarmtypes.ServiceSpec{
Annotations: swarm.Annotations{ Annotations: swarmtypes.Annotations{
Name: "TestService", Name: "TestService",
}, },
TaskTemplate: swarm.TaskSpec{ TaskTemplate: swarmtypes.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{ ContainerSpec: &swarmtypes.ContainerSpec{
Image: "busybox:latest", Image: "busybox:latest",
Command: []string{"/bin/sh", "-c", "ls -l /etc/config || /bin/top"}, Command: []string{"/bin/sh", "-c", "ls -l /etc/config || /bin/top"},
Configs: []*swarm.ConfigReference{ Configs: []*swarmtypes.ConfigReference{
{ {
File: &swarm.ConfigReferenceFileTarget{ File: &swarmtypes.ConfigReferenceFileTarget{
Name: "/etc/config", Name: "/etc/config",
UID: "0", UID: "0",
GID: "0", GID: "0",
@ -265,8 +266,8 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
}, },
}, },
}, },
Mode: swarm.ServiceMode{ Mode: swarmtypes.ServiceMode{
Replicated: &swarm.ReplicatedService{ Replicated: &swarmtypes.ReplicatedService{
Replicas: &instances, Replicas: &instances,
}, },
}, },
@ -307,19 +308,19 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
func swarmServiceSpec(name string, replicas uint64) swarm.ServiceSpec { func swarmServiceSpec(name string, replicas uint64) swarmtypes.ServiceSpec {
return swarm.ServiceSpec{ return swarmtypes.ServiceSpec{
Annotations: swarm.Annotations{ Annotations: swarmtypes.Annotations{
Name: name, Name: name,
}, },
TaskTemplate: swarm.TaskSpec{ TaskTemplate: swarmtypes.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{ ContainerSpec: &swarmtypes.ContainerSpec{
Image: "busybox:latest", Image: "busybox:latest",
Command: []string{"/bin/top"}, Command: []string{"/bin/top"},
}, },
}, },
Mode: swarm.ServiceMode{ Mode: swarmtypes.ServiceMode{
Replicated: &swarm.ReplicatedService{ Replicated: &swarmtypes.ReplicatedService{
Replicas: &replicas, Replicas: &replicas,
}, },
}, },
@ -338,7 +339,7 @@ func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string,
return poll.Error(err) return poll.Error(err)
case len(tasks) == int(instances): case len(tasks) == int(instances):
for _, task := range tasks { for _, task := range tasks {
if task.Status.State != swarm.TaskStateRunning { if task.Status.State != swarmtypes.TaskStateRunning {
return poll.Continue("waiting for tasks to enter run state") return poll.Continue("waiting for tasks to enter run state")
} }
} }

View file

@ -1,17 +1,16 @@
package service package service
import ( import (
"fmt"
"testing" "testing"
"time" "time"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm" swarmtypes "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/daemon"
"github.com/docker/docker/integration-cli/request" "github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/integration/util/swarm"
"github.com/gotestyourself/gotestyourself/poll" "github.com/gotestyourself/gotestyourself/poll"
"github.com/gotestyourself/gotestyourself/skip" "github.com/gotestyourself/gotestyourself/skip"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -22,7 +21,7 @@ import (
func TestInspect(t *testing.T) { func TestInspect(t *testing.T) {
skip.IfCondition(t, !testEnv.IsLocalDaemon()) skip.IfCondition(t, !testEnv.IsLocalDaemon())
defer setupTest(t)() defer setupTest(t)()
d := newSwarm(t) d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t) defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock()) client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err) require.NoError(t, err)
@ -49,19 +48,19 @@ func TestInspect(t *testing.T) {
assert.WithinDuration(t, before, service.UpdatedAt, 30*time.Second) assert.WithinDuration(t, before, service.UpdatedAt, 30*time.Second)
} }
func fullSwarmServiceSpec(name string, replicas uint64) swarm.ServiceSpec { func fullSwarmServiceSpec(name string, replicas uint64) swarmtypes.ServiceSpec {
restartDelay := 100 * time.Millisecond restartDelay := 100 * time.Millisecond
maxAttempts := uint64(4) maxAttempts := uint64(4)
return swarm.ServiceSpec{ return swarmtypes.ServiceSpec{
Annotations: swarm.Annotations{ Annotations: swarmtypes.Annotations{
Name: name, Name: name,
Labels: map[string]string{ Labels: map[string]string{
"service-label": "service-label-value", "service-label": "service-label-value",
}, },
}, },
TaskTemplate: swarm.TaskSpec{ TaskTemplate: swarmtypes.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{ ContainerSpec: &swarmtypes.ContainerSpec{
Image: "busybox:latest", Image: "busybox:latest",
Labels: map[string]string{"container-label": "container-value"}, Labels: map[string]string{"container-label": "container-value"},
Command: []string{"/bin/top"}, Command: []string{"/bin/top"},
@ -73,64 +72,43 @@ func fullSwarmServiceSpec(name string, replicas uint64) swarm.ServiceSpec {
StopSignal: "SIGINT", StopSignal: "SIGINT",
StopGracePeriod: &restartDelay, StopGracePeriod: &restartDelay,
Hosts: []string{"8.8.8.8 google"}, Hosts: []string{"8.8.8.8 google"},
DNSConfig: &swarm.DNSConfig{ DNSConfig: &swarmtypes.DNSConfig{
Nameservers: []string{"8.8.8.8"}, Nameservers: []string{"8.8.8.8"},
Search: []string{"somedomain"}, Search: []string{"somedomain"},
}, },
Isolation: container.IsolationDefault, Isolation: container.IsolationDefault,
}, },
RestartPolicy: &swarm.RestartPolicy{ RestartPolicy: &swarmtypes.RestartPolicy{
Delay: &restartDelay, Delay: &restartDelay,
Condition: swarm.RestartPolicyConditionOnFailure, Condition: swarmtypes.RestartPolicyConditionOnFailure,
MaxAttempts: &maxAttempts, MaxAttempts: &maxAttempts,
}, },
Runtime: swarm.RuntimeContainer, Runtime: swarmtypes.RuntimeContainer,
}, },
Mode: swarm.ServiceMode{ Mode: swarmtypes.ServiceMode{
Replicated: &swarm.ReplicatedService{ Replicated: &swarmtypes.ReplicatedService{
Replicas: &replicas, Replicas: &replicas,
}, },
}, },
UpdateConfig: &swarm.UpdateConfig{ UpdateConfig: &swarmtypes.UpdateConfig{
Parallelism: 2, Parallelism: 2,
Delay: 200 * time.Second, Delay: 200 * time.Second,
FailureAction: swarm.UpdateFailureActionContinue, FailureAction: swarmtypes.UpdateFailureActionContinue,
Monitor: 2 * time.Second, Monitor: 2 * time.Second,
MaxFailureRatio: 0.2, MaxFailureRatio: 0.2,
Order: swarm.UpdateOrderStopFirst, Order: swarmtypes.UpdateOrderStopFirst,
}, },
RollbackConfig: &swarm.UpdateConfig{ RollbackConfig: &swarmtypes.UpdateConfig{
Parallelism: 3, Parallelism: 3,
Delay: 300 * time.Second, Delay: 300 * time.Second,
FailureAction: swarm.UpdateFailureActionPause, FailureAction: swarmtypes.UpdateFailureActionPause,
Monitor: 3 * time.Second, Monitor: 3 * time.Second,
MaxFailureRatio: 0.3, MaxFailureRatio: 0.3,
Order: swarm.UpdateOrderStartFirst, Order: swarmtypes.UpdateOrderStartFirst,
}, },
} }
} }
const defaultSwarmPort = 2477
func newSwarm(t *testing.T) *daemon.Swarm {
d := &daemon.Swarm{
Daemon: daemon.New(t, "", dockerdBinary, daemon.Config{
Experimental: testEnv.DaemonInfo.ExperimentalBuild,
}),
// TODO: better method of finding an unused port
Port: defaultSwarmPort,
}
// TODO: move to a NewSwarm constructor
d.ListenAddr = fmt.Sprintf("0.0.0.0:%d", d.Port)
// avoid networking conflicts
args := []string{"--iptables=false", "--swarm-default-advertise-addr=lo"}
d.StartWithBusybox(t, args...)
require.NoError(t, d.Init(swarm.InitRequest{}))
return d
}
func serviceContainerCount(client client.ServiceAPIClient, id string, count uint64) func(log poll.LogT) poll.Result { func serviceContainerCount(client client.ServiceAPIClient, id string, count uint64) func(log poll.LogT) poll.Result {
return func(log poll.LogT) poll.Result { return func(log poll.LogT) poll.Result {
filter := filters.NewArgs() filter := filters.NewArgs()

View file

@ -8,13 +8,14 @@ import (
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/integration-cli/request" "github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/integration/util/swarm"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestDockerNetworkConnectAlias(t *testing.T) { func TestDockerNetworkConnectAlias(t *testing.T) {
defer setupTest(t)() defer setupTest(t)()
d := newSwarm(t) d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t) defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock()) client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err) require.NoError(t, err)

View file

@ -15,6 +15,7 @@ const (
defaultSwarmPort = 2477 defaultSwarmPort = 2477
) )
// NewSwarm creates a swarm daemon for testing
func NewSwarm(t *testing.T, testEnv *environment.Execution) *daemon.Swarm { func NewSwarm(t *testing.T, testEnv *environment.Execution) *daemon.Swarm {
d := &daemon.Swarm{ d := &daemon.Swarm{
Daemon: daemon.New(t, "", dockerdBinary, daemon.Config{ Daemon: daemon.New(t, "", dockerdBinary, daemon.Config{