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

Commit 0ab02d0

Browse files
committed
add unit test
Signed-off-by: Crazykev <[email protected]>
1 parent 2bc5c9e commit 0ab02d0

File tree

2 files changed

+73
-20
lines changed

2 files changed

+73
-20
lines changed

pkg/server/container_start_test.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,22 @@ func getStartContainerTestData() (*runtime.ContainerConfig, *runtime.PodSandboxC
173173

174174
func TestGeneralContainerSpec(t *testing.T) {
175175
testID := "test-id"
176-
testPodID := "test-pod-id"
177176
testPid := uint32(1234)
178177
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
179178
c := newTestCRIContainerdService()
180-
spec, err := c.generateContainerSpec(testID, testPodID, testPid, config, sandboxConfig, imageConfig, nil)
179+
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, nil)
181180
assert.NoError(t, err)
182181
specCheck(t, testID, testPid, spec)
183182
}
184183

185184
func TestContainerSpecTty(t *testing.T) {
186185
testID := "test-id"
187-
testPodID := "test-pod-id"
188186
testPid := uint32(1234)
189187
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
190188
c := newTestCRIContainerdService()
191189
for _, tty := range []bool{true, false} {
192190
config.Tty = tty
193-
spec, err := c.generateContainerSpec(testID, testPodID, testPid, config, sandboxConfig, imageConfig, nil)
191+
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, nil)
194192
assert.NoError(t, err)
195193
specCheck(t, testID, testPid, spec)
196194
assert.Equal(t, tty, spec.Process.Terminal)
@@ -199,13 +197,12 @@ func TestContainerSpecTty(t *testing.T) {
199197

200198
func TestContainerSpecReadonlyRootfs(t *testing.T) {
201199
testID := "test-id"
202-
testPodID := "test-pod-id"
203200
testPid := uint32(1234)
204201
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
205202
c := newTestCRIContainerdService()
206203
for _, readonly := range []bool{true, false} {
207204
config.Linux.SecurityContext.ReadonlyRootfs = readonly
208-
spec, err := c.generateContainerSpec(testID, testPodID, testPid, config, sandboxConfig, imageConfig, nil)
205+
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, nil)
209206
assert.NoError(t, err)
210207
specCheck(t, testID, testPid, spec)
211208
assert.Equal(t, readonly, spec.Root.Readonly)
@@ -214,7 +211,6 @@ func TestContainerSpecReadonlyRootfs(t *testing.T) {
214211

215212
func TestContainerSpecWithExtraMounts(t *testing.T) {
216213
testID := "test-id"
217-
testPodID := "test-pod-id"
218214
testPid := uint32(1234)
219215
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
220216
c := newTestCRIContainerdService()
@@ -229,7 +225,7 @@ func TestContainerSpecWithExtraMounts(t *testing.T) {
229225
HostPath: "test-host-path-extra",
230226
Readonly: true,
231227
}
232-
spec, err := c.generateContainerSpec(testID, testPodID, testPid, config, sandboxConfig, imageConfig, []*runtime.Mount{extraMount})
228+
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, []*runtime.Mount{extraMount})
233229
assert.NoError(t, err)
234230
specCheck(t, testID, testPid, spec)
235231
var mounts []runtimespec.Mount
@@ -313,23 +309,37 @@ func TestGenerateContainerMounts(t *testing.T) {
313309
securityContext *runtime.LinuxContainerSecurityContext
314310
expectedMounts []*runtime.Mount
315311
}{
316-
"should setup ro /etc/hosts mount when rootfs is read-only": {
312+
"should setup ro mount when rootfs is read-only": {
317313
securityContext: &runtime.LinuxContainerSecurityContext{
318314
ReadonlyRootfs: true,
319315
},
320-
expectedMounts: []*runtime.Mount{{
321-
ContainerPath: "/etc/hosts",
322-
HostPath: testSandboxRootDir + "/hosts",
323-
Readonly: true,
324-
}},
316+
expectedMounts: []*runtime.Mount{
317+
{
318+
ContainerPath: "/etc/hosts",
319+
HostPath: testSandboxRootDir + "/hosts",
320+
Readonly: true,
321+
},
322+
{
323+
ContainerPath: resolvConfPath,
324+
HostPath: getResolvPath(testSandboxRootDir),
325+
Readonly: true,
326+
},
327+
},
325328
},
326-
"should setup rw /etc/hosts mount when rootfs is read-write": {
329+
"should setup rw mount when rootfs is read-write": {
327330
securityContext: &runtime.LinuxContainerSecurityContext{},
328-
expectedMounts: []*runtime.Mount{{
329-
ContainerPath: "/etc/hosts",
330-
HostPath: testSandboxRootDir + "/hosts",
331-
Readonly: false,
332-
}},
331+
expectedMounts: []*runtime.Mount{
332+
{
333+
ContainerPath: "/etc/hosts",
334+
HostPath: testSandboxRootDir + "/hosts",
335+
Readonly: false,
336+
},
337+
{
338+
ContainerPath: resolvConfPath,
339+
HostPath: getResolvPath(testSandboxRootDir),
340+
Readonly: false,
341+
},
342+
},
333343
},
334344
} {
335345
config := &runtime.ContainerConfig{

pkg/server/sandbox_run_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,5 +269,48 @@ func TestRunPodSandbox(t *testing.T) {
269269
assert.Equal(t, expectedPluginArgument, pluginArgument, "SetUpPod should be called with correct arguments")
270270
}
271271

272+
func TestParseDNSOption(t *testing.T) {
273+
for desc, test := range map[string]struct {
274+
servers []string
275+
searches []string
276+
options []string
277+
expectedContent string
278+
expectErr bool
279+
}{
280+
"empty dns options should return empty content": {},
281+
"non-empty dns options should return correct content": {
282+
servers: []string{"8.8.8.8", "server.google.com"},
283+
searches: []string{"114.114.114.114"},
284+
options: []string{"timeout:1"},
285+
expectedContent: `search 114.114.114.114
286+
nameserver 8.8.8.8
287+
nameserver server.google.com
288+
options timeout:1
289+
`,
290+
},
291+
"should return error if dns search exceeds limit(6)": {
292+
searches: []string{
293+
"server0.google.com",
294+
"server1.google.com",
295+
"server2.google.com",
296+
"server3.google.com",
297+
"server4.google.com",
298+
"server5.google.com",
299+
"server6.google.com",
300+
},
301+
expectErr: true,
302+
},
303+
} {
304+
t.Logf("TestCase %q", desc)
305+
resolvContent, err := parseDNSOptions(test.servers, test.searches, test.options)
306+
if test.expectErr {
307+
assert.Error(t, err)
308+
continue
309+
}
310+
assert.NoError(t, err)
311+
assert.Equal(t, resolvContent, test.expectedContent)
312+
}
313+
}
314+
272315
// TODO(random-liu): [P1] Add unit test for different error cases to make sure
273316
// the function cleans up on error properly.

0 commit comments

Comments
 (0)