daemon: use t.Context() in tests

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-09-25 19:28:14 +02:00
parent 8905c3052b
commit aca0adfb97
3 changed files with 16 additions and 14 deletions

View File

@@ -93,7 +93,7 @@ func TestTmpfsDevShmNoDupMount(t *testing.T) {
}
d := setupFakeDaemon(t, c)
_, err := d.createSpec(context.TODO(), &configStore{}, c, nil)
_, err := d.createSpec(t.Context(), &configStore{}, c, nil)
assert.Check(t, err)
}
@@ -111,7 +111,7 @@ func TestIpcPrivateVsReadonly(t *testing.T) {
}
d := setupFakeDaemon(t, c)
s, err := d.createSpec(context.TODO(), &configStore{}, c, nil)
s, err := d.createSpec(t.Context(), &configStore{}, c, nil)
assert.Check(t, err)
// Find the /dev/shm mount in ms, check it does not have ro
@@ -127,6 +127,7 @@ func TestIpcPrivateVsReadonly(t *testing.T) {
// Config.Domainname) are overridden by an explicit sysctl in the HostConfig.
func TestSysctlOverride(t *testing.T) {
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
ctx := t.Context()
c := &container.Container{
Config: &containertypes.Config{
Hostname: "foobar",
@@ -140,7 +141,7 @@ func TestSysctlOverride(t *testing.T) {
d := setupFakeDaemon(t, c)
// Ensure that the implicit sysctl is set correctly.
s, err := d.createSpec(context.TODO(), &configStore{}, c, nil)
s, err := d.createSpec(ctx, &configStore{}, c, nil)
assert.NilError(t, err)
assert.Equal(t, s.Hostname, "foobar")
assert.Equal(t, s.Linux.Sysctl["kernel.domainname"], c.Config.Domainname)
@@ -156,14 +157,14 @@ func TestSysctlOverride(t *testing.T) {
assert.Assert(t, c.HostConfig.Sysctls["kernel.domainname"] != c.Config.Domainname)
c.HostConfig.Sysctls["net.ipv4.ip_unprivileged_port_start"] = "1024"
s, err = d.createSpec(context.TODO(), &configStore{}, c, nil)
s, err = d.createSpec(ctx, &configStore{}, c, nil)
assert.NilError(t, err)
assert.Equal(t, s.Hostname, "foobar")
assert.Equal(t, s.Linux.Sysctl["kernel.domainname"], c.HostConfig.Sysctls["kernel.domainname"])
assert.Equal(t, s.Linux.Sysctl["net.ipv4.ip_unprivileged_port_start"], c.HostConfig.Sysctls["net.ipv4.ip_unprivileged_port_start"])
// Ensure the ping_group_range is not set on a daemon with user-namespaces enabled
s, err = d.createSpec(context.TODO(), &configStore{Config: config.Config{RemappedRoot: "dummy:dummy"}}, c, nil)
s, err = d.createSpec(ctx, &configStore{Config: config.Config{RemappedRoot: "dummy:dummy"}}, c, nil)
assert.NilError(t, err)
_, ok := s.Linux.Sysctl["net.ipv4.ping_group_range"]
assert.Assert(t, !ok)
@@ -171,7 +172,7 @@ func TestSysctlOverride(t *testing.T) {
// Ensure the ping_group_range is set on a container in "host" userns mode
// on a daemon with user-namespaces enabled
c.HostConfig.UsernsMode = "host"
s, err = d.createSpec(context.TODO(), &configStore{Config: config.Config{RemappedRoot: "dummy:dummy"}}, c, nil)
s, err = d.createSpec(ctx, &configStore{Config: config.Config{RemappedRoot: "dummy:dummy"}}, c, nil)
assert.NilError(t, err)
assert.Equal(t, s.Linux.Sysctl["net.ipv4.ping_group_range"], "0 2147483647")
}
@@ -180,6 +181,7 @@ func TestSysctlOverride(t *testing.T) {
// with host networking
func TestSysctlOverrideHost(t *testing.T) {
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
ctx := t.Context()
c := &container.Container{
Config: &containertypes.Config{},
HostConfig: &containertypes.HostConfig{
@@ -190,7 +192,7 @@ func TestSysctlOverrideHost(t *testing.T) {
d := setupFakeDaemon(t, c)
// Ensure that the implicit sysctl is not set
s, err := d.createSpec(context.TODO(), &configStore{}, c, nil)
s, err := d.createSpec(ctx, &configStore{}, c, nil)
assert.NilError(t, err)
assert.Equal(t, s.Linux.Sysctl["net.ipv4.ip_unprivileged_port_start"], "")
assert.Equal(t, s.Linux.Sysctl["net.ipv4.ping_group_range"], "")
@@ -198,7 +200,7 @@ func TestSysctlOverrideHost(t *testing.T) {
// Set an explicit sysctl.
c.HostConfig.Sysctls["net.ipv4.ip_unprivileged_port_start"] = "1024"
s, err = d.createSpec(context.TODO(), &configStore{}, c, nil)
s, err = d.createSpec(ctx, &configStore{}, c, nil)
assert.NilError(t, err)
assert.Equal(t, s.Linux.Sysctl["net.ipv4.ip_unprivileged_port_start"], c.HostConfig.Sysctls["net.ipv4.ip_unprivileged_port_start"])
}

View File

@@ -25,7 +25,7 @@ func muteLogs(t *testing.T) {
func newDaemonForReloadT(t *testing.T, cfg *config.Config) *Daemon {
t.Helper()
daemon := &Daemon{
imageService: images.NewImageService(context.TODO(), images.ImageServiceConfig{}),
imageService: images.NewImageService(t.Context(), images.ImageServiceConfig{}),
}
var err error
daemon.registryService, err = registry.NewService(registry.ServiceOptions{})
@@ -63,7 +63,7 @@ func TestDaemonReloadLabels(t *testing.T) {
func TestDaemonReloadMirrors(t *testing.T) {
daemon := &Daemon{
imageService: images.NewImageService(context.TODO(), images.ImageServiceConfig{}),
imageService: images.NewImageService(t.Context(), images.ImageServiceConfig{}),
}
muteLogs(t)
@@ -162,7 +162,7 @@ func TestDaemonReloadMirrors(t *testing.T) {
func TestDaemonReloadInsecureRegistries(t *testing.T) {
daemon := &Daemon{
imageService: images.NewImageService(context.TODO(), images.ImageServiceConfig{}),
imageService: images.NewImageService(t.Context(), images.ImageServiceConfig{}),
}
muteLogs(t)

View File

@@ -29,7 +29,7 @@ func TestExecResizeNoSuchExec(t *testing.T) {
Container: c,
}
d.registerExecCommand(c, ec)
err := d.ContainerExecResize(context.TODO(), "no-such-exec", height, width)
err := d.ContainerExecResize(t.Context(), "no-such-exec", height, width)
assert.ErrorContains(t, err, "No such exec instance")
}
@@ -70,7 +70,7 @@ func TestExecResize(t *testing.T) {
close(ec.Started)
d.containers.Add(n, c)
d.registerExecCommand(c, ec)
err := d.ContainerExecResize(context.TODO(), n, height, width)
err := d.ContainerExecResize(t.Context(), n, height, width)
assert.NilError(t, err)
assert.Equal(t, mp.Width, width)
assert.Equal(t, mp.Height, height)
@@ -103,6 +103,6 @@ func TestExecResizeTimeout(t *testing.T) {
}
d.containers.Add(n, c)
d.registerExecCommand(c, ec)
err := d.ContainerExecResize(context.TODO(), n, height, width)
err := d.ContainerExecResize(t.Context(), n, height, width)
assert.ErrorContains(t, err, "timeout waiting for exec session ready")
}