add unit test
Signed-off-by: Crazykev <crazykev@zju.edu.cn>
This commit is contained in:
parent
9bf7ffd51a
commit
62d1e5dc10
@ -173,24 +173,22 @@ func getStartContainerTestData() (*runtime.ContainerConfig, *runtime.PodSandboxC
|
|||||||
|
|
||||||
func TestGeneralContainerSpec(t *testing.T) {
|
func TestGeneralContainerSpec(t *testing.T) {
|
||||||
testID := "test-id"
|
testID := "test-id"
|
||||||
testPodID := "test-pod-id"
|
|
||||||
testPid := uint32(1234)
|
testPid := uint32(1234)
|
||||||
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
|
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
|
||||||
c := newTestCRIContainerdService()
|
c := newTestCRIContainerdService()
|
||||||
spec, err := c.generateContainerSpec(testID, testPodID, testPid, config, sandboxConfig, imageConfig, nil)
|
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
specCheck(t, testID, testPid, spec)
|
specCheck(t, testID, testPid, spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainerSpecTty(t *testing.T) {
|
func TestContainerSpecTty(t *testing.T) {
|
||||||
testID := "test-id"
|
testID := "test-id"
|
||||||
testPodID := "test-pod-id"
|
|
||||||
testPid := uint32(1234)
|
testPid := uint32(1234)
|
||||||
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
|
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
|
||||||
c := newTestCRIContainerdService()
|
c := newTestCRIContainerdService()
|
||||||
for _, tty := range []bool{true, false} {
|
for _, tty := range []bool{true, false} {
|
||||||
config.Tty = tty
|
config.Tty = tty
|
||||||
spec, err := c.generateContainerSpec(testID, testPodID, testPid, config, sandboxConfig, imageConfig, nil)
|
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
specCheck(t, testID, testPid, spec)
|
specCheck(t, testID, testPid, spec)
|
||||||
assert.Equal(t, tty, spec.Process.Terminal)
|
assert.Equal(t, tty, spec.Process.Terminal)
|
||||||
@ -199,13 +197,12 @@ func TestContainerSpecTty(t *testing.T) {
|
|||||||
|
|
||||||
func TestContainerSpecReadonlyRootfs(t *testing.T) {
|
func TestContainerSpecReadonlyRootfs(t *testing.T) {
|
||||||
testID := "test-id"
|
testID := "test-id"
|
||||||
testPodID := "test-pod-id"
|
|
||||||
testPid := uint32(1234)
|
testPid := uint32(1234)
|
||||||
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
|
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
|
||||||
c := newTestCRIContainerdService()
|
c := newTestCRIContainerdService()
|
||||||
for _, readonly := range []bool{true, false} {
|
for _, readonly := range []bool{true, false} {
|
||||||
config.Linux.SecurityContext.ReadonlyRootfs = readonly
|
config.Linux.SecurityContext.ReadonlyRootfs = readonly
|
||||||
spec, err := c.generateContainerSpec(testID, testPodID, testPid, config, sandboxConfig, imageConfig, nil)
|
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
specCheck(t, testID, testPid, spec)
|
specCheck(t, testID, testPid, spec)
|
||||||
assert.Equal(t, readonly, spec.Root.Readonly)
|
assert.Equal(t, readonly, spec.Root.Readonly)
|
||||||
@ -214,7 +211,6 @@ func TestContainerSpecReadonlyRootfs(t *testing.T) {
|
|||||||
|
|
||||||
func TestContainerSpecWithExtraMounts(t *testing.T) {
|
func TestContainerSpecWithExtraMounts(t *testing.T) {
|
||||||
testID := "test-id"
|
testID := "test-id"
|
||||||
testPodID := "test-pod-id"
|
|
||||||
testPid := uint32(1234)
|
testPid := uint32(1234)
|
||||||
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
|
config, sandboxConfig, imageConfig, specCheck := getStartContainerTestData()
|
||||||
c := newTestCRIContainerdService()
|
c := newTestCRIContainerdService()
|
||||||
@ -229,7 +225,7 @@ func TestContainerSpecWithExtraMounts(t *testing.T) {
|
|||||||
HostPath: "test-host-path-extra",
|
HostPath: "test-host-path-extra",
|
||||||
Readonly: true,
|
Readonly: true,
|
||||||
}
|
}
|
||||||
spec, err := c.generateContainerSpec(testID, testPodID, testPid, config, sandboxConfig, imageConfig, []*runtime.Mount{extraMount})
|
spec, err := c.generateContainerSpec(testID, testPid, config, sandboxConfig, imageConfig, []*runtime.Mount{extraMount})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
specCheck(t, testID, testPid, spec)
|
specCheck(t, testID, testPid, spec)
|
||||||
var mounts []runtimespec.Mount
|
var mounts []runtimespec.Mount
|
||||||
@ -313,23 +309,37 @@ func TestGenerateContainerMounts(t *testing.T) {
|
|||||||
securityContext *runtime.LinuxContainerSecurityContext
|
securityContext *runtime.LinuxContainerSecurityContext
|
||||||
expectedMounts []*runtime.Mount
|
expectedMounts []*runtime.Mount
|
||||||
}{
|
}{
|
||||||
"should setup ro /etc/hosts mount when rootfs is read-only": {
|
"should setup ro mount when rootfs is read-only": {
|
||||||
securityContext: &runtime.LinuxContainerSecurityContext{
|
securityContext: &runtime.LinuxContainerSecurityContext{
|
||||||
ReadonlyRootfs: true,
|
ReadonlyRootfs: true,
|
||||||
},
|
},
|
||||||
expectedMounts: []*runtime.Mount{{
|
expectedMounts: []*runtime.Mount{
|
||||||
|
{
|
||||||
ContainerPath: "/etc/hosts",
|
ContainerPath: "/etc/hosts",
|
||||||
HostPath: testSandboxRootDir + "/hosts",
|
HostPath: testSandboxRootDir + "/hosts",
|
||||||
Readonly: true,
|
Readonly: true,
|
||||||
}},
|
|
||||||
},
|
},
|
||||||
"should setup rw /etc/hosts mount when rootfs is read-write": {
|
{
|
||||||
|
ContainerPath: resolvConfPath,
|
||||||
|
HostPath: testSandboxRootDir + "/resolv.conf",
|
||||||
|
Readonly: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"should setup rw mount when rootfs is read-write": {
|
||||||
securityContext: &runtime.LinuxContainerSecurityContext{},
|
securityContext: &runtime.LinuxContainerSecurityContext{},
|
||||||
expectedMounts: []*runtime.Mount{{
|
expectedMounts: []*runtime.Mount{
|
||||||
|
{
|
||||||
ContainerPath: "/etc/hosts",
|
ContainerPath: "/etc/hosts",
|
||||||
HostPath: testSandboxRootDir + "/hosts",
|
HostPath: testSandboxRootDir + "/hosts",
|
||||||
Readonly: false,
|
Readonly: false,
|
||||||
}},
|
},
|
||||||
|
{
|
||||||
|
ContainerPath: resolvConfPath,
|
||||||
|
HostPath: getResolvPath(testSandboxRootDir),
|
||||||
|
Readonly: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
config := &runtime.ContainerConfig{
|
config := &runtime.ContainerConfig{
|
||||||
|
@ -270,5 +270,48 @@ func TestRunPodSandbox(t *testing.T) {
|
|||||||
assert.Equal(t, expectedPluginArgument, pluginArgument, "SetUpPod should be called with correct arguments")
|
assert.Equal(t, expectedPluginArgument, pluginArgument, "SetUpPod should be called with correct arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseDNSOption(t *testing.T) {
|
||||||
|
for desc, test := range map[string]struct {
|
||||||
|
servers []string
|
||||||
|
searches []string
|
||||||
|
options []string
|
||||||
|
expectedContent string
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
"empty dns options should return empty content": {},
|
||||||
|
"non-empty dns options should return correct content": {
|
||||||
|
servers: []string{"8.8.8.8", "server.google.com"},
|
||||||
|
searches: []string{"114.114.114.114"},
|
||||||
|
options: []string{"timeout:1"},
|
||||||
|
expectedContent: `search 114.114.114.114
|
||||||
|
nameserver 8.8.8.8
|
||||||
|
nameserver server.google.com
|
||||||
|
options timeout:1
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
"should return error if dns search exceeds limit(6)": {
|
||||||
|
searches: []string{
|
||||||
|
"server0.google.com",
|
||||||
|
"server1.google.com",
|
||||||
|
"server2.google.com",
|
||||||
|
"server3.google.com",
|
||||||
|
"server4.google.com",
|
||||||
|
"server5.google.com",
|
||||||
|
"server6.google.com",
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Logf("TestCase %q", desc)
|
||||||
|
resolvContent, err := parseDNSOptions(test.servers, test.searches, test.options)
|
||||||
|
if test.expectErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, resolvContent, test.expectedContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO(random-liu): [P1] Add unit test for different error cases to make sure
|
// TODO(random-liu): [P1] Add unit test for different error cases to make sure
|
||||||
// the function cleans up on error properly.
|
// the function cleans up on error properly.
|
||||||
|
Loading…
Reference in New Issue
Block a user