Browse code

migrate TestContainerAPIPostContainerStop to integration

Migrates:

- TestContainerAPIPostContainerStop

Signed-off-by: Yudai Nakakubo <nyggl4dev@gmail.com>

Yudai Nakakubo authored on 2026/01/17 11:59:52
Showing 2 changed files
... ...
@@ -953,19 +953,6 @@ func (s *DockerAPISuite) TestContainerAPIChunkedEncoding(c *testing.T) {
953 953
 	assert.Equal(c, resp.StatusCode, http.StatusCreated)
954 954
 }
955 955
 
956
-func (s *DockerAPISuite) TestContainerAPIPostContainerStop(c *testing.T) {
957
-	containerID := runSleepingContainer(c)
958
-	cli.WaitRun(c, containerID)
959
-
960
-	apiClient, err := client.New(client.FromEnv)
961
-	assert.NilError(c, err)
962
-	defer apiClient.Close()
963
-
964
-	_, err = apiClient.ContainerStop(testutil.GetContext(c), containerID, client.ContainerStopOptions{})
965
-	assert.NilError(c, err)
966
-	assert.NilError(c, waitInspect(containerID, "{{ .State.Running  }}", "false", 60*time.Second))
967
-}
968
-
969 956
 // Ensure an error occurs when you have a container read-only rootfs but you
970 957
 // extract an archive to a symlink in a writable volume which points to a
971 958
 // directory outside of the volume.
... ...
@@ -1,12 +1,15 @@
1 1
 package container
2 2
 
3 3
 import (
4
+	"net/http"
4 5
 	"testing"
5 6
 	"time"
6 7
 
8
+	"github.com/moby/moby/api/types/common"
7 9
 	containertypes "github.com/moby/moby/api/types/container"
8 10
 	"github.com/moby/moby/client"
9 11
 	"github.com/moby/moby/v2/integration/internal/container"
12
+	"github.com/moby/moby/v2/internal/testutil/request"
10 13
 	"gotest.tools/v3/assert"
11 14
 	is "gotest.tools/v3/assert/cmp"
12 15
 	"gotest.tools/v3/poll"
... ...
@@ -111,3 +114,60 @@ func TestStopContainerWithTimeout(t *testing.T) {
111 111
 		})
112 112
 	}
113 113
 }
114
+
115
+func TestContainerAPIPostContainerStop(t *testing.T) {
116
+	apiClient := testEnv.APIClient()
117
+	ctx := setupTest(t)
118
+
119
+	tests := []struct {
120
+		testName            string
121
+		id                  string
122
+		expStatusCode       int
123
+		expError            string
124
+		checkContainerState bool
125
+		expStateRunning     bool
126
+	}{
127
+		{
128
+			testName:            "no error",
129
+			id:                  container.Run(ctx, t, apiClient),
130
+			expStatusCode:       http.StatusNoContent,
131
+			checkContainerState: true,
132
+			expStateRunning:     false,
133
+		},
134
+		{
135
+			testName:            "container already stopped",
136
+			id:                  container.Create(ctx, t, apiClient),
137
+			expStatusCode:       http.StatusNotModified,
138
+			checkContainerState: true,
139
+			expStateRunning:     false,
140
+		},
141
+		{
142
+			testName:            "no such container",
143
+			id:                  "test1234",
144
+			expStatusCode:       http.StatusNotFound,
145
+			expError:            `No such container: test1234`,
146
+			checkContainerState: false,
147
+		},
148
+	}
149
+
150
+	for _, tc := range tests {
151
+		t.Run(tc.testName, func(t *testing.T) {
152
+
153
+			res, _, err := request.Post(ctx, "/containers/"+tc.id+"/stop")
154
+
155
+			assert.Equal(t, res.StatusCode, tc.expStatusCode)
156
+			assert.NilError(t, err)
157
+
158
+			if tc.expError != "" {
159
+				var respErr common.ErrorResponse
160
+				assert.NilError(t, request.ReadJSONResponse(res, &respErr))
161
+				assert.ErrorContains(t, respErr, tc.expError)
162
+			}
163
+
164
+			if tc.checkContainerState {
165
+				inspectRes := container.Inspect(ctx, t, apiClient, tc.id)
166
+				assert.Equal(t, inspectRes.State.Running, tc.expStateRunning)
167
+			}
168
+		})
169
+	}
170
+}