move api test client setup to a package.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-07-12 18:26:09 -04:00
parent 5fa134b906
commit c66c0447ef
4 changed files with 35 additions and 25 deletions

View File

@ -10,7 +10,9 @@ source "$SCRIPTDIR/make/.go-autogen"
: ${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() {
local flags="-test.v -test.timeout=${TIMEOUT} $TESTFLAGS"

View File

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

View File

@ -5,9 +5,7 @@ import (
"os"
"testing"
"github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/environment"
"github.com/stretchr/testify/require"
)
var (
@ -33,12 +31,6 @@ func TestMain(m *testing.M) {
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() {
environment.ProtectImages(t, testEnv)
return func() { testEnv.Clean(t, testEnv.DockerBinary()) }

View File

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