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/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/integration-cli/request"
"github.com/docker/docker/integration/util/swarm"
"github.com/gotestyourself/gotestyourself/poll"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -19,7 +20,7 @@ import (
func TestCreateServiceMultipleTimes(t *testing.T) {
defer setupTest(t)()
d := newSwarm(t)
d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err)
@ -36,7 +37,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
var instances uint64 = 4
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{
QueryRegistry: false,
@ -85,7 +86,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
func TestCreateWithDuplicateNetworkNames(t *testing.T) {
defer setupTest(t)()
d := newSwarm(t)
d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err)
@ -111,7 +112,7 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
var instances uint64 = 1
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{})
require.NoError(t, err)
@ -147,14 +148,14 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
func TestCreateServiceSecretFileMode(t *testing.T) {
defer setupTest(t)()
d := newSwarm(t)
d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err)
ctx := context.Background()
secretResp, err := client.SecretCreate(ctx, swarm.SecretSpec{
Annotations: swarm.Annotations{
secretResp, err := client.SecretCreate(ctx, swarmtypes.SecretSpec{
Annotations: swarmtypes.Annotations{
Name: "TestSecret",
},
Data: []byte("TESTSECRET"),
@ -162,17 +163,17 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
require.NoError(t, err)
var instances uint64 = 1
serviceSpec := swarm.ServiceSpec{
Annotations: swarm.Annotations{
serviceSpec := swarmtypes.ServiceSpec{
Annotations: swarmtypes.Annotations{
Name: "TestService",
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
TaskTemplate: swarmtypes.TaskSpec{
ContainerSpec: &swarmtypes.ContainerSpec{
Image: "busybox:latest",
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",
UID: "0",
GID: "0",
@ -184,8 +185,8 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
},
},
},
Mode: swarm.ServiceMode{
Replicated: &swarm.ReplicatedService{
Mode: swarmtypes.ServiceMode{
Replicated: &swarmtypes.ReplicatedService{
Replicas: &instances,
},
},
@ -228,14 +229,14 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
func TestCreateServiceConfigFileMode(t *testing.T) {
defer setupTest(t)()
d := newSwarm(t)
d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err)
ctx := context.Background()
configResp, err := client.ConfigCreate(ctx, swarm.ConfigSpec{
Annotations: swarm.Annotations{
configResp, err := client.ConfigCreate(ctx, swarmtypes.ConfigSpec{
Annotations: swarmtypes.Annotations{
Name: "TestConfig",
},
Data: []byte("TESTCONFIG"),
@ -243,17 +244,17 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
require.NoError(t, err)
var instances uint64 = 1
serviceSpec := swarm.ServiceSpec{
Annotations: swarm.Annotations{
serviceSpec := swarmtypes.ServiceSpec{
Annotations: swarmtypes.Annotations{
Name: "TestService",
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
TaskTemplate: swarmtypes.TaskSpec{
ContainerSpec: &swarmtypes.ContainerSpec{
Image: "busybox:latest",
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",
UID: "0",
GID: "0",
@ -265,8 +266,8 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
},
},
},
Mode: swarm.ServiceMode{
Replicated: &swarm.ReplicatedService{
Mode: swarmtypes.ServiceMode{
Replicated: &swarmtypes.ReplicatedService{
Replicas: &instances,
},
},
@ -307,19 +308,19 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
require.NoError(t, err)
}
func swarmServiceSpec(name string, replicas uint64) swarm.ServiceSpec {
return swarm.ServiceSpec{
Annotations: swarm.Annotations{
func swarmServiceSpec(name string, replicas uint64) swarmtypes.ServiceSpec {
return swarmtypes.ServiceSpec{
Annotations: swarmtypes.Annotations{
Name: name,
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
TaskTemplate: swarmtypes.TaskSpec{
ContainerSpec: &swarmtypes.ContainerSpec{
Image: "busybox:latest",
Command: []string{"/bin/top"},
},
},
Mode: swarm.ServiceMode{
Replicated: &swarm.ReplicatedService{
Mode: swarmtypes.ServiceMode{
Replicated: &swarmtypes.ReplicatedService{
Replicas: &replicas,
},
},
@ -338,7 +339,7 @@ func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string,
return poll.Error(err)
case len(tasks) == int(instances):
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")
}
}

View file

@ -1,17 +1,16 @@
package service
import (
"fmt"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"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/integration-cli/daemon"
"github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/integration/util/swarm"
"github.com/gotestyourself/gotestyourself/poll"
"github.com/gotestyourself/gotestyourself/skip"
"github.com/stretchr/testify/assert"
@ -22,7 +21,7 @@ import (
func TestInspect(t *testing.T) {
skip.IfCondition(t, !testEnv.IsLocalDaemon())
defer setupTest(t)()
d := newSwarm(t)
d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t)
client, err := request.NewClientForHost(d.Sock())
require.NoError(t, err)
@ -49,19 +48,19 @@ func TestInspect(t *testing.T) {
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
maxAttempts := uint64(4)
return swarm.ServiceSpec{
Annotations: swarm.Annotations{
return swarmtypes.ServiceSpec{
Annotations: swarmtypes.Annotations{
Name: name,
Labels: map[string]string{
"service-label": "service-label-value",
},
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
TaskTemplate: swarmtypes.TaskSpec{
ContainerSpec: &swarmtypes.ContainerSpec{
Image: "busybox:latest",
Labels: map[string]string{"container-label": "container-value"},
Command: []string{"/bin/top"},
@ -73,64 +72,43 @@ func fullSwarmServiceSpec(name string, replicas uint64) swarm.ServiceSpec {
StopSignal: "SIGINT",
StopGracePeriod: &restartDelay,
Hosts: []string{"8.8.8.8 google"},
DNSConfig: &swarm.DNSConfig{
DNSConfig: &swarmtypes.DNSConfig{
Nameservers: []string{"8.8.8.8"},
Search: []string{"somedomain"},
},
Isolation: container.IsolationDefault,
},
RestartPolicy: &swarm.RestartPolicy{
RestartPolicy: &swarmtypes.RestartPolicy{
Delay: &restartDelay,
Condition: swarm.RestartPolicyConditionOnFailure,
Condition: swarmtypes.RestartPolicyConditionOnFailure,
MaxAttempts: &maxAttempts,
},
Runtime: swarm.RuntimeContainer,
Runtime: swarmtypes.RuntimeContainer,
},
Mode: swarm.ServiceMode{
Replicated: &swarm.ReplicatedService{
Mode: swarmtypes.ServiceMode{
Replicated: &swarmtypes.ReplicatedService{
Replicas: &replicas,
},
},
UpdateConfig: &swarm.UpdateConfig{
UpdateConfig: &swarmtypes.UpdateConfig{
Parallelism: 2,
Delay: 200 * time.Second,
FailureAction: swarm.UpdateFailureActionContinue,
FailureAction: swarmtypes.UpdateFailureActionContinue,
Monitor: 2 * time.Second,
MaxFailureRatio: 0.2,
Order: swarm.UpdateOrderStopFirst,
Order: swarmtypes.UpdateOrderStopFirst,
},
RollbackConfig: &swarm.UpdateConfig{
RollbackConfig: &swarmtypes.UpdateConfig{
Parallelism: 3,
Delay: 300 * time.Second,
FailureAction: swarm.UpdateFailureActionPause,
FailureAction: swarmtypes.UpdateFailureActionPause,
Monitor: 3 * time.Second,
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 {
return func(log poll.LogT) poll.Result {
filter := filters.NewArgs()

View file

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

View file

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