Procházet zdrojové kódy

Merge pull request #36188 from yongtang/02012018-TestSecretInspect

Migrate TestSecretInspect from integration-cli to api tests
Vincent Demeester před 7 roky
rodič
revize
30a8c6c109

+ 0 - 23
integration-cli/docker_cli_secret_inspect_test.go

@@ -10,29 +10,6 @@ import (
 	"github.com/go-check/check"
 )
 
-func (s *DockerSwarmSuite) TestSecretInspect(c *check.C) {
-	d := s.AddDaemon(c, true, true)
-
-	testName := "test_secret"
-	id := d.CreateSecret(c, swarm.SecretSpec{
-		Annotations: swarm.Annotations{
-			Name: testName,
-		},
-		Data: []byte("TESTINGDATA"),
-	})
-	c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id))
-
-	secret := d.GetSecret(c, id)
-	c.Assert(secret.Spec.Name, checker.Equals, testName)
-
-	out, err := d.Cmd("secret", "inspect", testName)
-	c.Assert(err, checker.IsNil, check.Commentf(out))
-
-	var secrets []swarm.Secret
-	c.Assert(json.Unmarshal([]byte(out), &secrets), checker.IsNil)
-	c.Assert(secrets, checker.HasLen, 1)
-}
-
 func (s *DockerSwarmSuite) TestSecretInspectMultiple(c *check.C) {
 	d := s.AddDaemon(c, true, true)
 

+ 35 - 0
integration/secret/main_test.go

@@ -0,0 +1,35 @@
+package secret
+
+import (
+	"fmt"
+	"os"
+	"testing"
+
+	"github.com/docker/docker/internal/test/environment"
+)
+
+var testEnv *environment.Execution
+
+const dockerdBinary = "dockerd"
+
+func TestMain(m *testing.M) {
+	var err error
+	testEnv, err = environment.New()
+	if err != nil {
+		fmt.Println(err)
+		os.Exit(1)
+	}
+	err = environment.EnsureFrozenImagesLinux(testEnv)
+	if err != nil {
+		fmt.Println(err)
+		os.Exit(1)
+	}
+
+	testEnv.Print()
+	os.Exit(m.Run())
+}
+
+func setupTest(t *testing.T) func() {
+	environment.ProtectAll(t, testEnv)
+	return func() { testEnv.Clean(t) }
+}

+ 43 - 0
integration/secret/secret_test.go

@@ -0,0 +1,43 @@
+package secret
+
+import (
+	"testing"
+
+	swarmtypes "github.com/docker/docker/api/types/swarm"
+	"github.com/docker/docker/integration-cli/request"
+	"github.com/docker/docker/integration/util/swarm"
+	"github.com/gotestyourself/gotestyourself/skip"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+	"golang.org/x/net/context"
+)
+
+func TestSecretInspect(t *testing.T) {
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
+
+	defer setupTest(t)()
+	d := swarm.NewSwarm(t, testEnv)
+	defer d.Stop(t)
+	client, err := request.NewClientForHost(d.Sock())
+	require.NoError(t, err)
+
+	ctx := context.Background()
+
+	testName := "test_secret"
+	secretResp, err := client.SecretCreate(ctx, swarmtypes.SecretSpec{
+		Annotations: swarmtypes.Annotations{
+			Name: testName,
+		},
+		Data: []byte("TESTINGDATA"),
+	})
+	require.NoError(t, err)
+	assert.NotEqual(t, secretResp.ID, "")
+
+	secret, _, err := client.SecretInspectWithRaw(context.Background(), secretResp.ID)
+	require.NoError(t, err)
+	assert.Equal(t, secret.Spec.Name, testName)
+
+	secret, _, err = client.SecretInspectWithRaw(context.Background(), testName)
+	require.NoError(t, err)
+	assert.Equal(t, secret.ID, secretResp.ID)
+}

+ 36 - 35
integration/service/create_test.go

@@ -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")
 				}
 			}

+ 20 - 42
integration/service/inspect_test.go

@@ -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()

+ 2 - 1
integration/service/network_test.go

@@ -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)

+ 36 - 0
integration/util/swarm/service.go

@@ -0,0 +1,36 @@
+package swarm
+
+import (
+	"fmt"
+	"testing"
+
+	swarmtypes "github.com/docker/docker/api/types/swarm"
+	"github.com/docker/docker/integration-cli/daemon"
+	"github.com/docker/docker/internal/test/environment"
+	"github.com/stretchr/testify/require"
+)
+
+const (
+	dockerdBinary    = "dockerd"
+	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{
+			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(swarmtypes.InitRequest{}))
+	return d
+}