浏览代码

move api test client setup to a package.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
Daniel Nephin 8 年之前
父节点
当前提交
c66c0447ef

+ 3 - 1
hack/make/.integration-test-helpers

@@ -10,7 +10,9 @@ source "$SCRIPTDIR/make/.go-autogen"
 
 
 : ${TEST_REPEAT:=1}
 : ${TEST_REPEAT:=1}
 
 
-integration_api_dirs=("$(find ./integration -type d | grep -vE '^./integration$')")
+integration_api_dirs=("$(
+	find ./integration -type d |
+	grep -vE '^(./integration$|./integration/util)')")
 
 
 run_test_integration() {
 run_test_integration() {
 	local flags="-test.v -test.timeout=${TIMEOUT} $TESTFLAGS"
 	local flags="-test.v -test.timeout=${TIMEOUT} $TESTFLAGS"

+ 17 - 16
integration/container/create_test.go

@@ -7,52 +7,54 @@ 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/stretchr/testify/require"
+	"github.com/docker/docker/integration/util/request"
+	"github.com/docker/docker/pkg/testutil"
 )
 )
 
 
-func TestAPICreateWithNotExistImage(t *testing.T) {
+func TestCreateFailsWhenIdentifierDoesNotExist(t *testing.T) {
 	defer setupTest(t)()
 	defer setupTest(t)()
-	clt := createClient(t)
+	client := request.NewAPIClient(t)
 
 
 	testCases := []struct {
 	testCases := []struct {
+		doc           string
 		image         string
 		image         string
 		expectedError string
 		expectedError string
 	}{
 	}{
 		{
 		{
+			doc:           "image and tag",
 			image:         "test456:v1",
 			image:         "test456:v1",
 			expectedError: "No such image: test456:v1",
 			expectedError: "No such image: test456:v1",
 		},
 		},
 		{
 		{
+			doc:           "image no tag",
 			image:         "test456",
 			image:         "test456",
 			expectedError: "No such image: test456",
 			expectedError: "No such image: test456",
 		},
 		},
 		{
 		{
+			doc:           "digest",
 			image:         "sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
 			image:         "sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
 			expectedError: "No such image: sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
 			expectedError: "No such image: sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
 		},
 		},
 	}
 	}
 
 
-	for index, tc := range testCases {
+	for _, tc := range testCases {
 		tc := tc
 		tc := tc
-		t.Run(strconv.Itoa(index), func(t *testing.T) {
+		t.Run(tc.doc, func(t *testing.T) {
 			t.Parallel()
 			t.Parallel()
-			_, err := clt.ContainerCreate(context.Background(),
-				&container.Config{
-					Image: tc.image,
-				},
+			_, err := client.ContainerCreate(context.Background(),
+				&container.Config{Image: tc.image},
 				&container.HostConfig{},
 				&container.HostConfig{},
 				&network.NetworkingConfig{},
 				&network.NetworkingConfig{},
 				"foo",
 				"foo",
 			)
 			)
-			require.Error(t, err)
-			require.Contains(t, err.Error(), tc.expectedError)
+			testutil.ErrorContains(t, err, tc.expectedError)
 		})
 		})
 	}
 	}
 }
 }
 
 
-func TestAPICreateEmptyEnv(t *testing.T) {
+func TestCreateWithInvalidEnv(t *testing.T) {
 	defer setupTest(t)()
 	defer setupTest(t)()
-	clt := createClient(t)
+	client := request.NewAPIClient(t)
 
 
 	testCases := []struct {
 	testCases := []struct {
 		env           string
 		env           string
@@ -76,7 +78,7 @@ func TestAPICreateEmptyEnv(t *testing.T) {
 		tc := tc
 		tc := tc
 		t.Run(strconv.Itoa(index), func(t *testing.T) {
 		t.Run(strconv.Itoa(index), func(t *testing.T) {
 			t.Parallel()
 			t.Parallel()
-			_, err := clt.ContainerCreate(context.Background(),
+			_, err := client.ContainerCreate(context.Background(),
 				&container.Config{
 				&container.Config{
 					Image: "busybox",
 					Image: "busybox",
 					Env:   []string{tc.env},
 					Env:   []string{tc.env},
@@ -85,8 +87,7 @@ func TestAPICreateEmptyEnv(t *testing.T) {
 				&network.NetworkingConfig{},
 				&network.NetworkingConfig{},
 				"foo",
 				"foo",
 			)
 			)
-			require.Error(t, err)
-			require.Contains(t, err.Error(), tc.expectedError)
+			testutil.ErrorContains(t, err, tc.expectedError)
 		})
 		})
 	}
 	}
 }
 }

+ 0 - 8
integration/container/main_test.go

@@ -5,9 +5,7 @@ import (
 	"os"
 	"os"
 	"testing"
 	"testing"
 
 
-	"github.com/docker/docker/client"
 	"github.com/docker/docker/integration-cli/environment"
 	"github.com/docker/docker/integration-cli/environment"
-	"github.com/stretchr/testify/require"
 )
 )
 
 
 var (
 var (
@@ -33,12 +31,6 @@ func TestMain(m *testing.M) {
 	os.Exit(res)
 	os.Exit(res)
 }
 }
 
 
-func createClient(t *testing.T) client.APIClient {
-	clt, err := client.NewEnvClient()
-	require.NoError(t, err)
-	return clt
-}
-
 func setupTest(t *testing.T) func() {
 func setupTest(t *testing.T) func() {
 	environment.ProtectImages(t, testEnv)
 	environment.ProtectImages(t, testEnv)
 	return func() { testEnv.Clean(t, testEnv.DockerBinary()) }
 	return func() { testEnv.Clean(t, testEnv.DockerBinary()) }

+ 15 - 0
integration/util/request/client.go

@@ -0,0 +1,15 @@
+package request
+
+import (
+	"testing"
+
+	"github.com/docker/docker/client"
+	"github.com/stretchr/testify/require"
+)
+
+// NewAPIClient returns a docker API client configured from environment variables
+func NewAPIClient(t *testing.T) client.APIClient {
+	clt, err := client.NewEnvClient()
+	require.NoError(t, err)
+	return clt
+}