Browse code

integration/internal/container: use consistent name for api-client

The `client` variable was colliding with the `client` import. In some cases
the confusing `cli` name (it's not the "cli") was used. Given that such names
can easily start spreading (through copy/paste, or "code by example"), let's
make a one-time pass through all of them in this package to use the same name.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2023/08/11 20:56:17
Showing 4 changed files
... ...
@@ -28,7 +28,7 @@ type TestContainerConfig struct {
28 28
 }
29 29
 
30 30
 // create creates a container with the specified options
31
-func create(ctx context.Context, t *testing.T, client client.APIClient, ops ...func(*TestContainerConfig)) (container.CreateResponse, error) {
31
+func create(ctx context.Context, t *testing.T, apiClient client.APIClient, ops ...func(*TestContainerConfig)) (container.CreateResponse, error) {
32 32
 	t.Helper()
33 33
 	cmd := []string{"top"}
34 34
 	if runtime.GOOS == "windows" {
... ...
@@ -47,30 +47,30 @@ func create(ctx context.Context, t *testing.T, client client.APIClient, ops ...f
47 47
 		op(config)
48 48
 	}
49 49
 
50
-	return client.ContainerCreate(ctx, config.Config, config.HostConfig, config.NetworkingConfig, config.Platform, config.Name)
50
+	return apiClient.ContainerCreate(ctx, config.Config, config.HostConfig, config.NetworkingConfig, config.Platform, config.Name)
51 51
 }
52 52
 
53 53
 // Create creates a container with the specified options, asserting that there was no error
54
-func Create(ctx context.Context, t *testing.T, client client.APIClient, ops ...func(*TestContainerConfig)) string {
54
+func Create(ctx context.Context, t *testing.T, apiClient client.APIClient, ops ...func(*TestContainerConfig)) string {
55 55
 	t.Helper()
56
-	c, err := create(ctx, t, client, ops...)
56
+	c, err := create(ctx, t, apiClient, ops...)
57 57
 	assert.NilError(t, err)
58 58
 
59 59
 	return c.ID
60 60
 }
61 61
 
62 62
 // CreateExpectingErr creates a container, expecting an error with the specified message
63
-func CreateExpectingErr(ctx context.Context, t *testing.T, client client.APIClient, errMsg string, ops ...func(*TestContainerConfig)) {
64
-	_, err := create(ctx, t, client, ops...)
63
+func CreateExpectingErr(ctx context.Context, t *testing.T, apiClient client.APIClient, errMsg string, ops ...func(*TestContainerConfig)) {
64
+	_, err := create(ctx, t, apiClient, ops...)
65 65
 	assert.ErrorContains(t, err, errMsg)
66 66
 }
67 67
 
68 68
 // Run creates and start a container with the specified options
69
-func Run(ctx context.Context, t *testing.T, client client.APIClient, ops ...func(*TestContainerConfig)) string {
69
+func Run(ctx context.Context, t *testing.T, apiClient client.APIClient, ops ...func(*TestContainerConfig)) string {
70 70
 	t.Helper()
71
-	id := Create(ctx, t, client, ops...)
71
+	id := Create(ctx, t, apiClient, ops...)
72 72
 
73
-	err := client.ContainerStart(ctx, id, types.ContainerStartOptions{})
73
+	err := apiClient.ContainerStart(ctx, id, types.ContainerStartOptions{})
74 74
 	assert.NilError(t, err)
75 75
 
76 76
 	return id
... ...
@@ -83,23 +83,23 @@ type RunResult struct {
83 83
 	Stderr      *bytes.Buffer
84 84
 }
85 85
 
86
-func RunAttach(ctx context.Context, t *testing.T, client client.APIClient, ops ...func(config *TestContainerConfig)) RunResult {
86
+func RunAttach(ctx context.Context, t *testing.T, apiClient client.APIClient, ops ...func(config *TestContainerConfig)) RunResult {
87 87
 	t.Helper()
88 88
 
89 89
 	ops = append(ops, func(c *TestContainerConfig) {
90 90
 		c.Config.AttachStdout = true
91 91
 		c.Config.AttachStderr = true
92 92
 	})
93
-	id := Create(ctx, t, client, ops...)
93
+	id := Create(ctx, t, apiClient, ops...)
94 94
 
95
-	aresp, err := client.ContainerAttach(ctx, id, types.ContainerAttachOptions{
95
+	aresp, err := apiClient.ContainerAttach(ctx, id, types.ContainerAttachOptions{
96 96
 		Stream: true,
97 97
 		Stdout: true,
98 98
 		Stderr: true,
99 99
 	})
100 100
 	assert.NilError(t, err)
101 101
 
102
-	err = client.ContainerStart(ctx, id, types.ContainerStartOptions{})
102
+	err = apiClient.ContainerStart(ctx, id, types.ContainerStartOptions{})
103 103
 	assert.NilError(t, err)
104 104
 
105 105
 	s, err := demultiplexStreams(ctx, aresp)
... ...
@@ -109,7 +109,7 @@ func RunAttach(ctx context.Context, t *testing.T, client client.APIClient, ops .
109 109
 
110 110
 	// Inspect to get the exit code. A new context is used here to make sure that if the context passed as argument as
111 111
 	// reached timeout during the demultiplexStream call, we still return a RunResult.
112
-	resp, err := client.ContainerInspect(context.Background(), id)
112
+	resp, err := apiClient.ContainerInspect(context.Background(), id)
113 113
 	assert.NilError(t, err)
114 114
 
115 115
 	return RunResult{ContainerID: id, ExitCode: resp.State.ExitCode, Stdout: &s.stdout, Stderr: &s.stderr}
... ...
@@ -34,7 +34,7 @@ func (res *ExecResult) Combined() string {
34 34
 // containing stdout, stderr, and exit code. Note:
35 35
 //   - this is a synchronous operation;
36 36
 //   - cmd stdin is closed.
37
-func Exec(ctx context.Context, cli client.APIClient, id string, cmd []string, ops ...func(*types.ExecConfig)) (ExecResult, error) {
37
+func Exec(ctx context.Context, apiClient client.APIClient, id string, cmd []string, ops ...func(*types.ExecConfig)) (ExecResult, error) {
38 38
 	// prepare exec
39 39
 	execConfig := types.ExecConfig{
40 40
 		AttachStdout: true,
... ...
@@ -46,14 +46,14 @@ func Exec(ctx context.Context, cli client.APIClient, id string, cmd []string, op
46 46
 		op(&execConfig)
47 47
 	}
48 48
 
49
-	cresp, err := cli.ContainerExecCreate(ctx, id, execConfig)
49
+	cresp, err := apiClient.ContainerExecCreate(ctx, id, execConfig)
50 50
 	if err != nil {
51 51
 		return ExecResult{}, err
52 52
 	}
53 53
 	execID := cresp.ID
54 54
 
55 55
 	// run it, with stdout/stderr attached
56
-	aresp, err := cli.ContainerExecAttach(ctx, execID, types.ExecStartCheck{})
56
+	aresp, err := apiClient.ContainerExecAttach(ctx, execID, types.ExecStartCheck{})
57 57
 	if err != nil {
58 58
 		return ExecResult{}, err
59 59
 	}
... ...
@@ -65,7 +65,7 @@ func Exec(ctx context.Context, cli client.APIClient, id string, cmd []string, op
65 65
 	}
66 66
 
67 67
 	// get the exit code
68
-	iresp, err := cli.ContainerExecInspect(ctx, execID)
68
+	iresp, err := apiClient.ContainerExecInspect(ctx, execID)
69 69
 	if err != nil {
70 70
 		return ExecResult{}, err
71 71
 	}
... ...
@@ -11,9 +11,9 @@ import (
11 11
 )
12 12
 
13 13
 // GetContainerNS gets the value of the specified namespace of a container
14
-func GetContainerNS(ctx context.Context, t *testing.T, client client.APIClient, cID, nsName string) string {
14
+func GetContainerNS(ctx context.Context, t *testing.T, apiClient client.APIClient, cID, nsName string) string {
15 15
 	t.Helper()
16
-	res, err := Exec(ctx, client, cID, []string{"readlink", "/proc/self/ns/" + nsName})
16
+	res, err := Exec(ctx, apiClient, cID, []string{"readlink", "/proc/self/ns/" + nsName})
17 17
 	assert.NilError(t, err)
18 18
 	assert.Assert(t, is.Len(res.Stderr(), 0))
19 19
 	assert.Equal(t, 0, res.ExitCode)
... ...
@@ -11,9 +11,9 @@ import (
11 11
 )
12 12
 
13 13
 // IsStopped verifies the container is in stopped state.
14
-func IsStopped(ctx context.Context, client client.APIClient, containerID string) func(log poll.LogT) poll.Result {
14
+func IsStopped(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result {
15 15
 	return func(log poll.LogT) poll.Result {
16
-		inspect, err := client.ContainerInspect(ctx, containerID)
16
+		inspect, err := apiClient.ContainerInspect(ctx, containerID)
17 17
 
18 18
 		switch {
19 19
 		case err != nil:
... ...
@@ -27,9 +27,9 @@ func IsStopped(ctx context.Context, client client.APIClient, containerID string)
27 27
 }
28 28
 
29 29
 // IsInState verifies the container is in one of the specified state, e.g., "running", "exited", etc.
30
-func IsInState(ctx context.Context, client client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result {
30
+func IsInState(ctx context.Context, apiClient client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result {
31 31
 	return func(log poll.LogT) poll.Result {
32
-		inspect, err := client.ContainerInspect(ctx, containerID)
32
+		inspect, err := apiClient.ContainerInspect(ctx, containerID)
33 33
 		if err != nil {
34 34
 			return poll.Error(err)
35 35
 		}
... ...
@@ -43,9 +43,9 @@ func IsInState(ctx context.Context, client client.APIClient, containerID string,
43 43
 }
44 44
 
45 45
 // IsSuccessful verifies state.Status == "exited" && state.ExitCode == 0
46
-func IsSuccessful(ctx context.Context, client client.APIClient, containerID string) func(log poll.LogT) poll.Result {
46
+func IsSuccessful(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result {
47 47
 	return func(log poll.LogT) poll.Result {
48
-		inspect, err := client.ContainerInspect(ctx, containerID)
48
+		inspect, err := apiClient.ContainerInspect(ctx, containerID)
49 49
 		if err != nil {
50 50
 			return poll.Error(err)
51 51
 		}
... ...
@@ -60,9 +60,9 @@ func IsSuccessful(ctx context.Context, client client.APIClient, containerID stri
60 60
 }
61 61
 
62 62
 // IsRemoved verifies the container has been removed
63
-func IsRemoved(ctx context.Context, cli client.APIClient, containerID string) func(log poll.LogT) poll.Result {
63
+func IsRemoved(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result {
64 64
 	return func(log poll.LogT) poll.Result {
65
-		inspect, err := cli.ContainerInspect(ctx, containerID)
65
+		inspect, err := apiClient.ContainerInspect(ctx, containerID)
66 66
 		if err != nil {
67 67
 			if errdefs.IsNotFound(err) {
68 68
 				return poll.Success()