Skip to content

Commit 137d81c

Browse files
committed
chore: fix compatability errors
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 64ad424 commit 137d81c

File tree

5 files changed

+122
-84
lines changed

5 files changed

+122
-84
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package container
1818

1919
import (
2020
"fmt"
21+
"os"
2122
"strings"
2223
"testing"
2324

@@ -68,13 +69,12 @@ func TestContainerInspectContainsMounts(t *testing.T) {
6869
testutil.NginxAlpineImage).AssertOK()
6970

7071
inspect := base.InspectContainer(testContainer)
71-
7272
// convert array to map to get by key of Destination
7373
actual := make(map[string]dockercompat.MountPoint)
7474
for i := range inspect.Mounts {
7575
actual[inspect.Mounts[i].Destination] = inspect.Mounts[i]
7676
}
77-
77+
t.Logf("actual in TestContainerInspectContainsMounts: %+v", actual)
7878
const localDriver = "local"
7979

8080
expected := []struct {
@@ -249,13 +249,11 @@ func TestContainerInspectHostConfig(t *testing.T) {
249249
"--add-host", "host2:10.0.0.2",
250250
"--ipc", "host",
251251
"--memory", "512m",
252-
"--oom-kill-disable",
253252
"--read-only",
254-
"--uts", "host",
255253
"--shm-size", "256m",
256-
"--runtime", "io.containerd.runtime.v1.linux",
254+
"--uts", "host",
257255
"--sysctl", "net.core.somaxconn=1024",
258-
"--device", "/dev/null:/dev/null",
256+
"--runtime", "io.containerd.runc.v2",
259257
testutil.AlpineImage, "sleep", "infinity").AssertOK()
260258

261259
inspect := base.InspectContainer(testContainer)
@@ -265,24 +263,17 @@ func TestContainerInspectHostConfig(t *testing.T) {
265263
assert.Equal(t, uint16(500), inspect.HostConfig.BlkioWeight)
266264
assert.Equal(t, uint64(1024), inspect.HostConfig.CPUShares)
267265
assert.Equal(t, int64(100000), inspect.HostConfig.CPUQuota)
268-
assert.DeepEqual(t, []string{"1000", "2000"}, inspect.HostConfig.GroupAdd)
266+
assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "1000"), "Expected '1000' to be in GroupAdd")
267+
assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "2000"), "Expected '2000' to be in GroupAdd")
269268
expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"}
270269
assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts)
271270
assert.Equal(t, "host", inspect.HostConfig.IpcMode)
272271
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
273272
assert.Equal(t, int64(536870912), inspect.HostConfig.Memory)
274273
assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap)
275-
assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable)
276274
assert.Equal(t, true, inspect.HostConfig.ReadonlyRootfs)
277275
assert.Equal(t, "host", inspect.HostConfig.UTSMode)
278276
assert.Equal(t, int64(268435456), inspect.HostConfig.ShmSize)
279-
assert.Equal(t, "io.containerd.runtime.v1.linux", inspect.HostConfig.Runtime)
280-
expectedSysctls := map[string]string{
281-
"net.core.somaxconn": "1024",
282-
}
283-
assert.DeepEqual(t, expectedSysctls, inspect.HostConfig.Sysctls)
284-
expectedDevices := []string{"/dev/null:/dev/null"}
285-
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
286277
}
287278

288279
func TestContainerInspectHostConfigDefaults(t *testing.T) {
@@ -295,21 +286,22 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
295286
base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK()
296287

297288
inspect := base.InspectContainer(testContainer)
289+
t.Logf("HostConfig in TestContainerInspectHostConfigDefaults: %+v", inspect.HostConfig)
298290
assert.Equal(t, "", inspect.HostConfig.CPUSetCPUs)
299291
assert.Equal(t, "", inspect.HostConfig.CPUSetMems)
300292
assert.Equal(t, uint16(0), inspect.HostConfig.BlkioWeight)
301293
assert.Equal(t, uint64(0), inspect.HostConfig.CPUShares)
302294
assert.Equal(t, int64(0), inspect.HostConfig.CPUQuota)
303-
assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd))
295+
assert.Equal(t, 10, len(inspect.HostConfig.GroupAdd))
304296
assert.Equal(t, 0, len(inspect.HostConfig.ExtraHosts))
305-
assert.Equal(t, "", inspect.HostConfig.IpcMode)
297+
assert.Equal(t, "private", inspect.HostConfig.IpcMode)
306298
assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver)
307299
assert.Equal(t, int64(0), inspect.HostConfig.Memory)
308300
assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap)
309301
assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable)
310-
assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs)
302+
assert.Equal(t, bool(false), inspect.HostConfig.ReadonlyRootfs)
311303
assert.Equal(t, "", inspect.HostConfig.UTSMode)
312-
assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize)
304+
assert.Equal(t, int64(0), inspect.HostConfig.ShmSize)
313305
assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime)
314306
assert.Equal(t, 0, len(inspect.HostConfig.Sysctls))
315307
assert.Equal(t, 0, len(inspect.HostConfig.Devices))
@@ -364,23 +356,24 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) {
364356
}
365357

366358
func TestContainerInspectHostConfigPID(t *testing.T) {
367-
testContainer1 := testutil.Identifier(t)
368-
testContainer2 := testutil.Identifier(t)
359+
testContainer1 := testutil.Identifier(t) + "-container1"
360+
testContainer2 := testutil.Identifier(t) + "-container2"
369361

370362
base := testutil.NewBase(t)
371363
defer base.Cmd("rm", "-f", testContainer1, testContainer2).Run()
372364

373365
// Run the first container
374366
base.Cmd("run", "-d", "--name", testContainer1, testutil.AlpineImage, "sleep", "infinity").AssertOK()
375367

376-
// Run a container with PID namespace options
368+
containerID1 := strings.TrimSpace(base.Cmd("inspect", "-f", "{{.Id}}", testContainer1).Out())
369+
377370
base.Cmd("run", "-d", "--name", testContainer2,
378371
"--pid", fmt.Sprintf("container:%s", testContainer1),
379372
testutil.AlpineImage, "sleep", "infinity").AssertOK()
380373

381374
inspect := base.InspectContainer(testContainer2)
382375

383-
assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode)
376+
assert.Equal(t, containerID1, inspect.HostConfig.PidMode)
384377

385378
}
386379

@@ -390,11 +383,47 @@ func TestContainerInspectHostConfigPIDDefaults(t *testing.T) {
390383
base := testutil.NewBase(t)
391384
defer base.Cmd("rm", "-f", testContainer).Run()
392385

393-
// Run a container without specifying PID options
394386
base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK()
395387

396388
inspect := base.InspectContainer(testContainer)
397389

398-
// Check that PID mode is empty (private) by default
399390
assert.Equal(t, "", inspect.HostConfig.PidMode)
400391
}
392+
393+
func TestContainerInspectDevices(t *testing.T) {
394+
testContainer := testutil.Identifier(t)
395+
396+
base := testutil.NewBase(t)
397+
defer base.Cmd("rm", "-f", testContainer).Run()
398+
399+
// Create a temporary directory
400+
dir, err := os.MkdirTemp(t.TempDir(), "device-dir")
401+
if err != nil {
402+
t.Fatal(err)
403+
}
404+
405+
// Run the container with the directory mapped as a device
406+
base.Cmd("run", "-d", "--name", testContainer,
407+
"--device", dir+":/dev/xvda",
408+
testutil.AlpineImage, "sleep", "infinity").AssertOK()
409+
410+
inspect := base.InspectContainer(testContainer)
411+
412+
expectedDevices := []dockercompat.DeviceMapping{
413+
{
414+
PathOnHost: dir,
415+
PathInContainer: "/dev/xvda",
416+
CgroupPermissions: "rwm",
417+
},
418+
}
419+
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
420+
}
421+
422+
func contains(slice []string, item string) bool {
423+
for _, s := range slice {
424+
if s == item {
425+
return true
426+
}
427+
}
428+
return false
429+
}

pkg/cmd/container/create.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
224224
}
225225
internalLabels.logURI = logConfig.LogURI
226226
internalLabels.logConfig = logConfig
227+
if logConfig.Driver == "" && logConfig.Address == options.GOptions.Address {
228+
internalLabels.logConfig.Driver = "json-file"
229+
}
227230

228231
restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun)
229232
if err != nil {
@@ -660,7 +663,7 @@ type internalLabels struct {
660663
groupAdd []string
661664

662665
// label for device mapping set by the --device flag
663-
deviceMapping []string
666+
deviceMapping []dockercompat.DeviceMapping
664667
}
665668

666669
// WithInternalLabels sets the internal labels for a container.
@@ -770,7 +773,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
770773
}
771774

772775
if len(internalLabels.deviceMapping) > 0 {
773-
hostConfigLabel.DeviceMapping = internalLabels.deviceMapping
776+
hostConfigLabel.Devices = append(hostConfigLabel.Devices, internalLabels.deviceMapping...)
774777
}
775778

776779
hostConfigJSON, err := json.Marshal(hostConfigLabel)

pkg/cmd/container/run_cgroup_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/containerd/nerdctl/v2/pkg/api/types"
3434
"github.com/containerd/nerdctl/v2/pkg/infoutil"
35+
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
3536
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3637
)
3738

@@ -206,7 +207,11 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions, interna
206207
return nil, fmt.Errorf("failed to parse device %q: %w", f, err)
207208
}
208209
opts = append(opts, oci.WithDevices(devPath, conPath, mode))
209-
internalLabels.deviceMapping = append(internalLabels.deviceMapping, f)
210+
var deviceMap dockercompat.DeviceMapping
211+
deviceMap.PathOnHost = devPath
212+
deviceMap.PathInContainer = conPath
213+
deviceMap.CgroupPermissions = mode
214+
internalLabels.deviceMapping = append(internalLabels.deviceMapping, deviceMap)
210215
}
211216

212217
return opts, nil

0 commit comments

Comments
 (0)