Browse code

Merge pull request #50525 from thaJeztah/migrate_defaultshmsize_test

integration-cli: migrate TestPostContainersCreateShmSizeXXX to integration

Sebastiaan van Stijn authored on 2025/07/28 19:17:17
Showing 3 changed files
... ...
@@ -11,14 +11,12 @@ import (
11 11
 	"net/http"
12 12
 	"os"
13 13
 	"path/filepath"
14
-	"regexp"
15 14
 	"runtime"
16 15
 	"strings"
17 16
 	"testing"
18 17
 	"time"
19 18
 
20 19
 	cerrdefs "github.com/containerd/errdefs"
21
-	dconfig "github.com/docker/docker/daemon/config"
22 20
 	"github.com/docker/docker/daemon/volume"
23 21
 	"github.com/docker/docker/integration-cli/cli"
24 22
 	"github.com/docker/docker/integration-cli/cli/build"
... ...
@@ -1142,110 +1140,6 @@ func (s *DockerAPISuite) TestPostContainersCreateWithWrongCpusetValues(c *testin
1142 1142
 	assert.ErrorContains(c, err, expected)
1143 1143
 }
1144 1144
 
1145
-func (s *DockerAPISuite) TestPostContainersCreateShmSizeNegative(c *testing.T) {
1146
-	// ShmSize is not supported on Windows
1147
-	testRequires(c, DaemonIsLinux)
1148
-	config := container.Config{
1149
-		Image: "busybox",
1150
-	}
1151
-	hostConfig := container.HostConfig{
1152
-		ShmSize: -1,
1153
-	}
1154
-
1155
-	apiClient, err := client.NewClientWithOpts(client.FromEnv)
1156
-	assert.NilError(c, err)
1157
-	defer apiClient.Close()
1158
-
1159
-	_, err = apiClient.ContainerCreate(testutil.GetContext(c), &config, &hostConfig, &network.NetworkingConfig{}, nil, "")
1160
-	assert.ErrorContains(c, err, "SHM size can not be less than 0")
1161
-}
1162
-
1163
-func (s *DockerAPISuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *testing.T) {
1164
-	// ShmSize is not supported on Windows
1165
-	testRequires(c, DaemonIsLinux)
1166
-
1167
-	config := container.Config{
1168
-		Image: "busybox",
1169
-		Cmd:   []string{"mount"},
1170
-	}
1171
-
1172
-	apiClient, err := client.NewClientWithOpts(client.FromEnv)
1173
-	assert.NilError(c, err)
1174
-	defer apiClient.Close()
1175
-
1176
-	ctr, err := apiClient.ContainerCreate(testutil.GetContext(c), &config, &container.HostConfig{}, &network.NetworkingConfig{}, nil, "")
1177
-	assert.NilError(c, err)
1178
-
1179
-	containerJSON, err := apiClient.ContainerInspect(testutil.GetContext(c), ctr.ID)
1180
-	assert.NilError(c, err)
1181
-
1182
-	assert.Equal(c, containerJSON.HostConfig.ShmSize, dconfig.DefaultShmSize)
1183
-
1184
-	out := cli.DockerCmd(c, "start", "-i", containerJSON.ID).Combined()
1185
-	shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
1186
-	if !shmRegexp.MatchString(out) {
1187
-		c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
1188
-	}
1189
-}
1190
-
1191
-func (s *DockerAPISuite) TestPostContainersCreateShmSizeOmitted(c *testing.T) {
1192
-	// ShmSize is not supported on Windows
1193
-	testRequires(c, DaemonIsLinux)
1194
-	config := container.Config{
1195
-		Image: "busybox",
1196
-		Cmd:   []string{"mount"},
1197
-	}
1198
-
1199
-	apiClient, err := client.NewClientWithOpts(client.FromEnv)
1200
-	assert.NilError(c, err)
1201
-	defer apiClient.Close()
1202
-
1203
-	ctr, err := apiClient.ContainerCreate(testutil.GetContext(c), &config, &container.HostConfig{}, &network.NetworkingConfig{}, nil, "")
1204
-	assert.NilError(c, err)
1205
-
1206
-	containerJSON, err := apiClient.ContainerInspect(testutil.GetContext(c), ctr.ID)
1207
-	assert.NilError(c, err)
1208
-
1209
-	assert.Equal(c, containerJSON.HostConfig.ShmSize, int64(67108864))
1210
-
1211
-	out := cli.DockerCmd(c, "start", "-i", containerJSON.ID).Combined()
1212
-	shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
1213
-	if !shmRegexp.MatchString(out) {
1214
-		c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
1215
-	}
1216
-}
1217
-
1218
-func (s *DockerAPISuite) TestPostContainersCreateWithShmSize(c *testing.T) {
1219
-	// ShmSize is not supported on Windows
1220
-	testRequires(c, DaemonIsLinux)
1221
-	config := container.Config{
1222
-		Image: "busybox",
1223
-		Cmd:   []string{"mount"},
1224
-	}
1225
-
1226
-	hostConfig := container.HostConfig{
1227
-		ShmSize: 1073741824,
1228
-	}
1229
-
1230
-	apiClient, err := client.NewClientWithOpts(client.FromEnv)
1231
-	assert.NilError(c, err)
1232
-	defer apiClient.Close()
1233
-
1234
-	ctr, err := apiClient.ContainerCreate(testutil.GetContext(c), &config, &hostConfig, &network.NetworkingConfig{}, nil, "")
1235
-	assert.NilError(c, err)
1236
-
1237
-	containerJSON, err := apiClient.ContainerInspect(testutil.GetContext(c), ctr.ID)
1238
-	assert.NilError(c, err)
1239
-
1240
-	assert.Equal(c, containerJSON.HostConfig.ShmSize, int64(1073741824))
1241
-
1242
-	out := cli.DockerCmd(c, "start", "-i", containerJSON.ID).Combined()
1243
-	shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=1048576k`)
1244
-	if !shmRegex.MatchString(out) {
1245
-		c.Fatalf("Expected shm of 1GB in mount command, got %v", out)
1246
-	}
1247
-}
1248
-
1249 1145
 func (s *DockerAPISuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(c *testing.T) {
1250 1146
 	// Swappiness is not supported on Windows
1251 1147
 	testRequires(c, DaemonIsLinux)
... ...
@@ -9,10 +9,12 @@ import (
9 9
 	"strings"
10 10
 	"testing"
11 11
 
12
+	cerrdefs "github.com/containerd/errdefs"
12 13
 	"github.com/docker/docker/integration/internal/container"
13 14
 	net "github.com/docker/docker/integration/internal/network"
14 15
 	"github.com/docker/docker/testutil"
15 16
 	"github.com/docker/docker/testutil/daemon"
17
+	"github.com/docker/go-units"
16 18
 	"github.com/moby/moby/api/stdcopy"
17 19
 	containertypes "github.com/moby/moby/api/types/container"
18 20
 	"github.com/moby/moby/api/types/versions"
... ...
@@ -457,8 +459,8 @@ func TestCgroupRW(t *testing.T) {
457 457
 	}
458 458
 	for _, tc := range testCases {
459 459
 		t.Run(tc.name, func(t *testing.T) {
460
-			config := container.NewTestConfig(tc.ops...)
461
-			resp, err := container.CreateFromConfig(ctx, apiClient, config)
460
+			cfg := container.NewTestConfig(tc.ops...)
461
+			resp, err := container.CreateFromConfig(ctx, apiClient, cfg)
462 462
 			if err != nil {
463 463
 				assert.Equal(t, tc.expectedErrMsg, err.Error())
464 464
 				return
... ...
@@ -488,3 +490,79 @@ func TestCgroupRW(t *testing.T) {
488 488
 		})
489 489
 	}
490 490
 }
491
+
492
+func TestContainerShmSize(t *testing.T) {
493
+	ctx := setupTest(t)
494
+
495
+	const defaultSize = "1000k"
496
+	defaultSizeBytes, err := units.RAMInBytes(defaultSize)
497
+	assert.NilError(t, err)
498
+
499
+	d := daemon.New(t)
500
+	d.StartWithBusybox(ctx, t, "--default-shm-size="+defaultSize)
501
+	defer d.Stop(t)
502
+
503
+	apiClient := d.NewClientT(t)
504
+
505
+	tests := []struct {
506
+		doc     string
507
+		opt     container.ConfigOpt
508
+		expSize string
509
+		expErr  string
510
+	}{
511
+		{
512
+			doc:     "nil hostConfig",
513
+			opt:     container.WithHostConfig(nil),
514
+			expSize: defaultSize,
515
+		},
516
+		{
517
+			doc:     "empty hostConfig",
518
+			opt:     container.WithHostConfig(&containertypes.HostConfig{}),
519
+			expSize: defaultSize,
520
+		},
521
+		{
522
+			doc:     "custom shmSize",
523
+			opt:     container.WithHostConfig(&containertypes.HostConfig{ShmSize: defaultSizeBytes * 2}),
524
+			expSize: "2000k",
525
+		},
526
+		{
527
+			doc:    "negative shmSize",
528
+			opt:    container.WithHostConfig(&containertypes.HostConfig{ShmSize: -1}),
529
+			expErr: "Error response from daemon: SHM size can not be less than 0",
530
+		},
531
+	}
532
+
533
+	for _, tc := range tests {
534
+		t.Run(tc.doc, func(t *testing.T) {
535
+			if tc.expErr != "" {
536
+				cfg := container.NewTestConfig(container.WithCmd("sh", "-c", "grep /dev/shm /proc/self/mountinfo"), tc.opt)
537
+				_, err := container.CreateFromConfig(ctx, apiClient, cfg)
538
+				assert.Check(t, is.ErrorContains(err, tc.expErr))
539
+				assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
540
+				return
541
+			}
542
+
543
+			cID := container.Run(ctx, t, apiClient,
544
+				container.WithCmd("sh", "-c", "grep /dev/shm /proc/self/mountinfo"),
545
+				tc.opt,
546
+			)
547
+
548
+			t.Cleanup(func() {
549
+				container.Remove(ctx, t, apiClient, cID, containertypes.RemoveOptions{})
550
+			})
551
+
552
+			expectedSize, err := units.RAMInBytes(tc.expSize)
553
+			assert.NilError(t, err)
554
+
555
+			ctr := container.Inspect(ctx, t, apiClient, cID)
556
+			assert.Check(t, is.Equal(ctr.HostConfig.ShmSize, expectedSize))
557
+
558
+			out, err := container.Output(ctx, apiClient, cID)
559
+			assert.NilError(t, err)
560
+
561
+			// e.g., "218 213 0:87 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=1000k"
562
+			assert.Assert(t, is.Contains(out.Stdout, "/dev/shm "), "shm mount not found in output: \n%v", out.Stdout)
563
+			assert.Check(t, is.Contains(out.Stdout, "size="+tc.expSize))
564
+		})
565
+	}
566
+}
... ...
@@ -12,6 +12,9 @@ import (
12 12
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
13 13
 )
14 14
 
15
+// ConfigOpt is an option to apply to a container.
16
+type ConfigOpt func(*TestContainerConfig)
17
+
15 18
 // WithName sets the name of the container
16 19
 func WithName(name string) func(*TestContainerConfig) {
17 20
 	return func(c *TestContainerConfig) {
... ...
@@ -361,3 +364,10 @@ func WithContainerWideMacAddress(address string) func(c *TestContainerConfig) {
361 361
 		c.Config.MacAddress = address //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
362 362
 	}
363 363
 }
364
+
365
+// WithHostConfig sets a custom [container.HostConfig] for the container.
366
+func WithHostConfig(hc *container.HostConfig) func(c *TestContainerConfig) {
367
+	return func(c *TestContainerConfig) {
368
+		c.HostConfig = hc
369
+	}
370
+}