Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit f216d44

Browse files
committed
Add unit test.
Signed-off-by: Lantao Liu <[email protected]>
1 parent 4787598 commit f216d44

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

pkg/server/container_start_test.go

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestGeneralContainerSpec(t *testing.T) {
176176
testPid := uint32(1234)
177177
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
178178
c := newTestCRIContainerdService()
179-
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig)
179+
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, nil)
180180
assert.NoError(t, err)
181181
specCheck(t, testID, testPid, spec)
182182
}
@@ -188,7 +188,7 @@ func TestContainerSpecTty(t *testing.T) {
188188
c := newTestCRIContainerdService()
189189
for _, tty := range []bool{true, false} {
190190
config.Tty = tty
191-
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig)
191+
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, nil)
192192
assert.NoError(t, err)
193193
specCheck(t, testID, testPid, spec)
194194
assert.Equal(t, tty, spec.Process.Terminal)
@@ -202,13 +202,46 @@ func TestContainerSpecReadonlyRootfs(t *testing.T) {
202202
c := newTestCRIContainerdService()
203203
for _, readonly := range []bool{true, false} {
204204
config.Linux.SecurityContext.ReadonlyRootfs = readonly
205-
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig)
205+
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, nil)
206206
assert.NoError(t, err)
207207
specCheck(t, testID, testPid, spec)
208208
assert.Equal(t, readonly, spec.Root.Readonly)
209209
}
210210
}
211211

212+
func TestContainerSpecWithExtraMounts(t *testing.T) {
213+
testID := "test-id"
214+
testPid := uint32(1234)
215+
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
216+
c := newTestCRIContainerdService()
217+
mountInConfig := &runtime.Mount{
218+
ContainerPath: "test-container-path",
219+
HostPath: "test-host-path",
220+
Readonly: false,
221+
}
222+
config.Mounts = append(config.Mounts, mountInConfig)
223+
extraMount := &runtime.Mount{
224+
ContainerPath: "test-container-path",
225+
HostPath: "test-host-path-extra",
226+
Readonly: true,
227+
}
228+
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, []*runtime.Mount{extraMount})
229+
assert.NoError(t, err)
230+
specCheck(t, testID, testPid, spec)
231+
var mounts []runtimespec.Mount
232+
for _, m := range spec.Mounts {
233+
if m.Destination == "test-container-path" {
234+
mounts = append(mounts, m)
235+
}
236+
}
237+
t.Logf("Extra mounts should come first")
238+
require.Len(t, mounts, 2)
239+
assert.Equal(t, "test-host-path-extra", mounts[0].Source)
240+
assert.Contains(t, mounts[0].Options, "ro")
241+
assert.Equal(t, "test-host-path", mounts[1].Source)
242+
assert.Contains(t, mounts[1].Options, "rw")
243+
}
244+
212245
func TestContainerSpecCommand(t *testing.T) {
213246
for desc, test := range map[string]struct {
214247
criEntrypoint []string
@@ -270,6 +303,46 @@ func TestContainerSpecCommand(t *testing.T) {
270303
}
271304
}
272305

306+
func TestGenerateContainerMounts(t *testing.T) {
307+
testSandboxRootDir := "test-sandbox-root"
308+
for desc, test := range map[string]struct {
309+
securityContext *runtime.LinuxContainerSecurityContext
310+
expectedMounts []*runtime.Mount
311+
}{
312+
"should setup ro /etc/hosts mount when rootfs is read-only": {
313+
securityContext: &runtime.LinuxContainerSecurityContext{
314+
ReadonlyRootfs: true,
315+
},
316+
expectedMounts: []*runtime.Mount{{
317+
ContainerPath: "/etc/hosts",
318+
HostPath: testSandboxRootDir + "/hosts",
319+
Readonly: true,
320+
}},
321+
},
322+
"should setup rw /etc/hosts mount when rootfs is read-write": {
323+
securityContext: &runtime.LinuxContainerSecurityContext{},
324+
expectedMounts: []*runtime.Mount{{
325+
ContainerPath: "/etc/hosts",
326+
HostPath: testSandboxRootDir + "/hosts",
327+
Readonly: false,
328+
}},
329+
},
330+
} {
331+
config := &runtime.ContainerConfig{
332+
Metadata: &runtime.ContainerMetadata{
333+
Name: "test-name",
334+
Attempt: 1,
335+
},
336+
Linux: &runtime.LinuxContainerConfig{
337+
SecurityContext: test.securityContext,
338+
},
339+
}
340+
c := newTestCRIContainerdService()
341+
mounts := c.generateContainerMounts(testSandboxRootDir, config)
342+
assert.Equal(t, test.expectedMounts, mounts, desc)
343+
}
344+
}
345+
273346
func TestStartContainer(t *testing.T) {
274347
testID := "test-id"
275348
testSandboxID := "test-sandbox-id"

pkg/server/sandbox_run_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ func TestGenerateSandboxContainerSpec(t *testing.T) {
157157
}
158158
}
159159

160+
func TestSetupSandboxFiles(t *testing.T) {
161+
testRootDir := "test-sandbox-root"
162+
expectedCopys := [][]interface{}{
163+
{"/etc/hosts", testRootDir + "/hosts", os.FileMode(0666)},
164+
}
165+
c := newTestCRIContainerdService()
166+
var copys [][]interface{}
167+
c.os.(*ostesting.FakeOS).CopyFileFn = func(src string, dest string, perm os.FileMode) error {
168+
copys = append(copys, []interface{}{src, dest, perm})
169+
return nil
170+
}
171+
c.setupSandboxFiles(testRootDir, nil)
172+
assert.Equal(t, expectedCopys, copys, "should copy /etc/hosts for sandbox")
173+
}
174+
160175
func TestRunPodSandbox(t *testing.T) {
161176
config, imageConfig, specCheck := getRunPodSandboxTestData()
162177
c := newTestCRIContainerdService()

0 commit comments

Comments
 (0)