Skip to content

Commit c66c044

Browse files
committed
move api test client setup to a package.
Signed-off-by: Daniel Nephin <[email protected]>
1 parent 5fa134b commit c66c044

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

hack/make/.integration-test-helpers

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ source "$SCRIPTDIR/make/.go-autogen"
1010

1111
: ${TEST_REPEAT:=1}
1212

13-
integration_api_dirs=("$(find ./integration -type d | grep -vE '^./integration$')")
13+
integration_api_dirs=("$(
14+
find ./integration -type d |
15+
grep -vE '^(./integration$|./integration/util)')")
1416

1517
run_test_integration() {
1618
local flags="-test.v -test.timeout=${TIMEOUT} $TESTFLAGS"

integration/container/create_test.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,54 @@ import (
77

88
"github.com/docker/docker/api/types/container"
99
"github.com/docker/docker/api/types/network"
10-
"github.com/stretchr/testify/require"
10+
"github.com/docker/docker/integration/util/request"
11+
"github.com/docker/docker/pkg/testutil"
1112
)
1213

13-
func TestAPICreateWithNotExistImage(t *testing.T) {
14+
func TestCreateFailsWhenIdentifierDoesNotExist(t *testing.T) {
1415
defer setupTest(t)()
15-
clt := createClient(t)
16+
client := request.NewAPIClient(t)
1617

1718
testCases := []struct {
19+
doc string
1820
image string
1921
expectedError string
2022
}{
2123
{
24+
doc: "image and tag",
2225
image: "test456:v1",
2326
expectedError: "No such image: test456:v1",
2427
},
2528
{
29+
doc: "image no tag",
2630
image: "test456",
2731
expectedError: "No such image: test456",
2832
},
2933
{
34+
doc: "digest",
3035
image: "sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
3136
expectedError: "No such image: sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
3237
},
3338
}
3439

35-
for index, tc := range testCases {
40+
for _, tc := range testCases {
3641
tc := tc
37-
t.Run(strconv.Itoa(index), func(t *testing.T) {
42+
t.Run(tc.doc, func(t *testing.T) {
3843
t.Parallel()
39-
_, err := clt.ContainerCreate(context.Background(),
40-
&container.Config{
41-
Image: tc.image,
42-
},
44+
_, err := client.ContainerCreate(context.Background(),
45+
&container.Config{Image: tc.image},
4346
&container.HostConfig{},
4447
&network.NetworkingConfig{},
4548
"foo",
4649
)
47-
require.Error(t, err)
48-
require.Contains(t, err.Error(), tc.expectedError)
50+
testutil.ErrorContains(t, err, tc.expectedError)
4951
})
5052
}
5153
}
5254

53-
func TestAPICreateEmptyEnv(t *testing.T) {
55+
func TestCreateWithInvalidEnv(t *testing.T) {
5456
defer setupTest(t)()
55-
clt := createClient(t)
57+
client := request.NewAPIClient(t)
5658

5759
testCases := []struct {
5860
env string
@@ -76,7 +78,7 @@ func TestAPICreateEmptyEnv(t *testing.T) {
7678
tc := tc
7779
t.Run(strconv.Itoa(index), func(t *testing.T) {
7880
t.Parallel()
79-
_, err := clt.ContainerCreate(context.Background(),
81+
_, err := client.ContainerCreate(context.Background(),
8082
&container.Config{
8183
Image: "busybox",
8284
Env: []string{tc.env},
@@ -85,8 +87,7 @@ func TestAPICreateEmptyEnv(t *testing.T) {
8587
&network.NetworkingConfig{},
8688
"foo",
8789
)
88-
require.Error(t, err)
89-
require.Contains(t, err.Error(), tc.expectedError)
90+
testutil.ErrorContains(t, err, tc.expectedError)
9091
})
9192
}
9293
}

integration/container/main_test.go

-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import (
55
"os"
66
"testing"
77

8-
"github.com/docker/docker/client"
98
"github.com/docker/docker/integration-cli/environment"
10-
"github.com/stretchr/testify/require"
119
)
1210

1311
var (
@@ -33,12 +31,6 @@ func TestMain(m *testing.M) {
3331
os.Exit(res)
3432
}
3533

36-
func createClient(t *testing.T) client.APIClient {
37-
clt, err := client.NewEnvClient()
38-
require.NoError(t, err)
39-
return clt
40-
}
41-
4234
func setupTest(t *testing.T) func() {
4335
environment.ProtectImages(t, testEnv)
4436
return func() { testEnv.Clean(t, testEnv.DockerBinary()) }

integration/util/request/client.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package request
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/docker/client"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// NewAPIClient returns a docker API client configured from environment variables
11+
func NewAPIClient(t *testing.T) client.APIClient {
12+
clt, err := client.NewEnvClient()
13+
require.NoError(t, err)
14+
return clt
15+
}

0 commit comments

Comments
 (0)