Browse code

daemon: use t.Context() in tests

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

Sebastiaan van Stijn authored on 2025/09/26 02:28:14
Showing 3 changed files
... ...
@@ -93,7 +93,7 @@ func TestTmpfsDevShmNoDupMount(t *testing.T) {
93 93
 	}
94 94
 	d := setupFakeDaemon(t, c)
95 95
 
96
-	_, err := d.createSpec(context.TODO(), &configStore{}, c, nil)
96
+	_, err := d.createSpec(t.Context(), &configStore{}, c, nil)
97 97
 	assert.Check(t, err)
98 98
 }
99 99
 
... ...
@@ -111,7 +111,7 @@ func TestIpcPrivateVsReadonly(t *testing.T) {
111 111
 	}
112 112
 	d := setupFakeDaemon(t, c)
113 113
 
114
-	s, err := d.createSpec(context.TODO(), &configStore{}, c, nil)
114
+	s, err := d.createSpec(t.Context(), &configStore{}, c, nil)
115 115
 	assert.Check(t, err)
116 116
 
117 117
 	// Find the /dev/shm mount in ms, check it does not have ro
... ...
@@ -127,6 +127,7 @@ func TestIpcPrivateVsReadonly(t *testing.T) {
127 127
 // Config.Domainname) are overridden by an explicit sysctl in the HostConfig.
128 128
 func TestSysctlOverride(t *testing.T) {
129 129
 	skip.If(t, os.Getuid() != 0, "skipping test that requires root")
130
+	ctx := t.Context()
130 131
 	c := &container.Container{
131 132
 		Config: &containertypes.Config{
132 133
 			Hostname:   "foobar",
... ...
@@ -140,7 +141,7 @@ func TestSysctlOverride(t *testing.T) {
140 140
 	d := setupFakeDaemon(t, c)
141 141
 
142 142
 	// Ensure that the implicit sysctl is set correctly.
143
-	s, err := d.createSpec(context.TODO(), &configStore{}, c, nil)
143
+	s, err := d.createSpec(ctx, &configStore{}, c, nil)
144 144
 	assert.NilError(t, err)
145 145
 	assert.Equal(t, s.Hostname, "foobar")
146 146
 	assert.Equal(t, s.Linux.Sysctl["kernel.domainname"], c.Config.Domainname)
... ...
@@ -156,14 +157,14 @@ func TestSysctlOverride(t *testing.T) {
156 156
 	assert.Assert(t, c.HostConfig.Sysctls["kernel.domainname"] != c.Config.Domainname)
157 157
 	c.HostConfig.Sysctls["net.ipv4.ip_unprivileged_port_start"] = "1024"
158 158
 
159
-	s, err = d.createSpec(context.TODO(), &configStore{}, c, nil)
159
+	s, err = d.createSpec(ctx, &configStore{}, c, nil)
160 160
 	assert.NilError(t, err)
161 161
 	assert.Equal(t, s.Hostname, "foobar")
162 162
 	assert.Equal(t, s.Linux.Sysctl["kernel.domainname"], c.HostConfig.Sysctls["kernel.domainname"])
163 163
 	assert.Equal(t, s.Linux.Sysctl["net.ipv4.ip_unprivileged_port_start"], c.HostConfig.Sysctls["net.ipv4.ip_unprivileged_port_start"])
164 164
 
165 165
 	// Ensure the ping_group_range is not set on a daemon with user-namespaces enabled
166
-	s, err = d.createSpec(context.TODO(), &configStore{Config: config.Config{RemappedRoot: "dummy:dummy"}}, c, nil)
166
+	s, err = d.createSpec(ctx, &configStore{Config: config.Config{RemappedRoot: "dummy:dummy"}}, c, nil)
167 167
 	assert.NilError(t, err)
168 168
 	_, ok := s.Linux.Sysctl["net.ipv4.ping_group_range"]
169 169
 	assert.Assert(t, !ok)
... ...
@@ -171,7 +172,7 @@ func TestSysctlOverride(t *testing.T) {
171 171
 	// Ensure the ping_group_range is set on a container in "host" userns mode
172 172
 	// on a daemon with user-namespaces enabled
173 173
 	c.HostConfig.UsernsMode = "host"
174
-	s, err = d.createSpec(context.TODO(), &configStore{Config: config.Config{RemappedRoot: "dummy:dummy"}}, c, nil)
174
+	s, err = d.createSpec(ctx, &configStore{Config: config.Config{RemappedRoot: "dummy:dummy"}}, c, nil)
175 175
 	assert.NilError(t, err)
176 176
 	assert.Equal(t, s.Linux.Sysctl["net.ipv4.ping_group_range"], "0 2147483647")
177 177
 }
... ...
@@ -180,6 +181,7 @@ func TestSysctlOverride(t *testing.T) {
180 180
 // with host networking
181 181
 func TestSysctlOverrideHost(t *testing.T) {
182 182
 	skip.If(t, os.Getuid() != 0, "skipping test that requires root")
183
+	ctx := t.Context()
183 184
 	c := &container.Container{
184 185
 		Config: &containertypes.Config{},
185 186
 		HostConfig: &containertypes.HostConfig{
... ...
@@ -190,7 +192,7 @@ func TestSysctlOverrideHost(t *testing.T) {
190 190
 	d := setupFakeDaemon(t, c)
191 191
 
192 192
 	// Ensure that the implicit sysctl is not set
193
-	s, err := d.createSpec(context.TODO(), &configStore{}, c, nil)
193
+	s, err := d.createSpec(ctx, &configStore{}, c, nil)
194 194
 	assert.NilError(t, err)
195 195
 	assert.Equal(t, s.Linux.Sysctl["net.ipv4.ip_unprivileged_port_start"], "")
196 196
 	assert.Equal(t, s.Linux.Sysctl["net.ipv4.ping_group_range"], "")
... ...
@@ -198,7 +200,7 @@ func TestSysctlOverrideHost(t *testing.T) {
198 198
 	// Set an explicit sysctl.
199 199
 	c.HostConfig.Sysctls["net.ipv4.ip_unprivileged_port_start"] = "1024"
200 200
 
201
-	s, err = d.createSpec(context.TODO(), &configStore{}, c, nil)
201
+	s, err = d.createSpec(ctx, &configStore{}, c, nil)
202 202
 	assert.NilError(t, err)
203 203
 	assert.Equal(t, s.Linux.Sysctl["net.ipv4.ip_unprivileged_port_start"], c.HostConfig.Sysctls["net.ipv4.ip_unprivileged_port_start"])
204 204
 }
... ...
@@ -25,7 +25,7 @@ func muteLogs(t *testing.T) {
25 25
 func newDaemonForReloadT(t *testing.T, cfg *config.Config) *Daemon {
26 26
 	t.Helper()
27 27
 	daemon := &Daemon{
28
-		imageService: images.NewImageService(context.TODO(), images.ImageServiceConfig{}),
28
+		imageService: images.NewImageService(t.Context(), images.ImageServiceConfig{}),
29 29
 	}
30 30
 	var err error
31 31
 	daemon.registryService, err = registry.NewService(registry.ServiceOptions{})
... ...
@@ -63,7 +63,7 @@ func TestDaemonReloadLabels(t *testing.T) {
63 63
 
64 64
 func TestDaemonReloadMirrors(t *testing.T) {
65 65
 	daemon := &Daemon{
66
-		imageService: images.NewImageService(context.TODO(), images.ImageServiceConfig{}),
66
+		imageService: images.NewImageService(t.Context(), images.ImageServiceConfig{}),
67 67
 	}
68 68
 	muteLogs(t)
69 69
 
... ...
@@ -162,7 +162,7 @@ func TestDaemonReloadMirrors(t *testing.T) {
162 162
 
163 163
 func TestDaemonReloadInsecureRegistries(t *testing.T) {
164 164
 	daemon := &Daemon{
165
-		imageService: images.NewImageService(context.TODO(), images.ImageServiceConfig{}),
165
+		imageService: images.NewImageService(t.Context(), images.ImageServiceConfig{}),
166 166
 	}
167 167
 	muteLogs(t)
168 168
 
... ...
@@ -29,7 +29,7 @@ func TestExecResizeNoSuchExec(t *testing.T) {
29 29
 		Container: c,
30 30
 	}
31 31
 	d.registerExecCommand(c, ec)
32
-	err := d.ContainerExecResize(context.TODO(), "no-such-exec", height, width)
32
+	err := d.ContainerExecResize(t.Context(), "no-such-exec", height, width)
33 33
 	assert.ErrorContains(t, err, "No such exec instance")
34 34
 }
35 35
 
... ...
@@ -70,7 +70,7 @@ func TestExecResize(t *testing.T) {
70 70
 	close(ec.Started)
71 71
 	d.containers.Add(n, c)
72 72
 	d.registerExecCommand(c, ec)
73
-	err := d.ContainerExecResize(context.TODO(), n, height, width)
73
+	err := d.ContainerExecResize(t.Context(), n, height, width)
74 74
 	assert.NilError(t, err)
75 75
 	assert.Equal(t, mp.Width, width)
76 76
 	assert.Equal(t, mp.Height, height)
... ...
@@ -103,6 +103,6 @@ func TestExecResizeTimeout(t *testing.T) {
103 103
 	}
104 104
 	d.containers.Add(n, c)
105 105
 	d.registerExecCommand(c, ec)
106
-	err := d.ContainerExecResize(context.TODO(), n, height, width)
106
+	err := d.ContainerExecResize(t.Context(), n, height, width)
107 107
 	assert.ErrorContains(t, err, "timeout waiting for exec session ready")
108 108
 }