pkg/cri/server: sub-test uses array and capture range var
Using array to build sub-tests is to avoid random pick. The shuffle thing should be handled by go-test framework. And we should capture range var before runing sub-test. Signed-off-by: Wei Fu <fuweid89@gmail.com>
This commit is contained in:
@@ -117,12 +117,14 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
Size: 10,
|
||||
}
|
||||
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
configChange func(*runtime.PodSandboxConfig)
|
||||
specCheck func(*testing.T, *runtimespec.Spec)
|
||||
expectErr bool
|
||||
}{
|
||||
"spec should reflect original config": {
|
||||
{
|
||||
desc: "spec should reflect original config",
|
||||
specCheck: func(t *testing.T, spec *runtimespec.Spec) {
|
||||
// runtime spec should have expected namespaces enabled by default.
|
||||
require.NotNil(t, spec.Linux)
|
||||
@@ -146,7 +148,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
})
|
||||
},
|
||||
},
|
||||
"host namespace": {
|
||||
{
|
||||
desc: "host namespace",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||
NamespaceOptions: &runtime.NamespaceOption{
|
||||
@@ -178,7 +181,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
assert.NotContains(t, spec.Linux.Sysctl["net.ipv4.ping_group_range"], "0 2147483647")
|
||||
},
|
||||
},
|
||||
"user namespace": {
|
||||
{
|
||||
desc: "user namespace",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||
NamespaceOptions: &runtime.NamespaceOption{
|
||||
@@ -200,7 +204,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
|
||||
},
|
||||
},
|
||||
"user namespace mode node and mappings": {
|
||||
{
|
||||
desc: "user namespace mode node and mappings",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||
NamespaceOptions: &runtime.NamespaceOption{
|
||||
@@ -214,7 +219,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
"user namespace with several mappings": {
|
||||
{
|
||||
desc: "user namespace with several mappings",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||
NamespaceOptions: &runtime.NamespaceOption{
|
||||
@@ -228,7 +234,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
"user namespace with uneven mappings": {
|
||||
{
|
||||
desc: "user namespace with uneven mappings",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||
NamespaceOptions: &runtime.NamespaceOption{
|
||||
@@ -242,7 +249,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
"user namespace mode container": {
|
||||
{
|
||||
desc: "user namespace mode container",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||
NamespaceOptions: &runtime.NamespaceOption{
|
||||
@@ -254,7 +262,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
"user namespace mode target": {
|
||||
{
|
||||
desc: "user namespace mode target",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||
NamespaceOptions: &runtime.NamespaceOption{
|
||||
@@ -266,7 +275,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
"user namespace unknown mode": {
|
||||
{
|
||||
desc: "user namespace unknown mode",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||
NamespaceOptions: &runtime.NamespaceOption{
|
||||
@@ -278,7 +288,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
"should set supplemental groups correctly": {
|
||||
{
|
||||
desc: "should set supplemental groups correctly",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||
SupplementalGroups: []int64{1111, 2222},
|
||||
@@ -290,7 +301,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
assert.Contains(t, spec.Process.User.AdditionalGids, uint32(2222))
|
||||
},
|
||||
},
|
||||
"should overwrite default sysctls": {
|
||||
{
|
||||
desc: "should overwrite default sysctls",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.Sysctls = map[string]string{
|
||||
"net.ipv4.ip_unprivileged_port_start": "500",
|
||||
@@ -303,7 +315,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
assert.Contains(t, spec.Linux.Sysctl["net.ipv4.ping_group_range"], "1 1000")
|
||||
},
|
||||
},
|
||||
"sandbox sizing annotations should be set if LinuxContainerResources were provided": {
|
||||
{
|
||||
desc: "sandbox sizing annotations should be set if LinuxContainerResources were provided",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.Resources = &v1.LinuxContainerResources{
|
||||
CpuPeriod: 100,
|
||||
@@ -331,7 +344,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
assert.EqualValues(t, "1024", value)
|
||||
},
|
||||
},
|
||||
"sandbox sizing annotations should not be set if LinuxContainerResources were not provided": {
|
||||
{
|
||||
desc: "sandbox sizing annotations should not be set if LinuxContainerResources were not provided",
|
||||
specCheck: func(t *testing.T, spec *runtimespec.Spec) {
|
||||
_, ok := spec.Annotations[annotations.SandboxCPUPeriod]
|
||||
assert.False(t, ok)
|
||||
@@ -343,7 +357,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
assert.False(t, ok)
|
||||
},
|
||||
},
|
||||
"sandbox sizing annotations are zero if the resources are set to 0": {
|
||||
{
|
||||
desc: "sandbox sizing annotations are zero if the resources are set to 0",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Linux.Resources = &v1.LinuxContainerResources{}
|
||||
},
|
||||
@@ -363,7 +378,8 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
c := newTestCRIService()
|
||||
c.config.EnableUnprivilegedICMP = true
|
||||
c.config.EnableUnprivilegedPorts = true
|
||||
@@ -392,13 +408,15 @@ func TestSetupSandboxFiles(t *testing.T) {
|
||||
testID = "test-id"
|
||||
realhostname = "test-real-hostname"
|
||||
)
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
dnsConfig *runtime.DNSConfig
|
||||
hostname string
|
||||
ipcMode runtime.NamespaceMode
|
||||
expectedCalls []ostesting.CalledDetail
|
||||
}{
|
||||
"should check host /dev/shm existence when ipc mode is NODE": {
|
||||
{
|
||||
desc: "should check host /dev/shm existence when ipc mode is NODE",
|
||||
ipcMode: runtime.NamespaceMode_NODE,
|
||||
expectedCalls: []ostesting.CalledDetail{
|
||||
{
|
||||
@@ -434,7 +452,8 @@ func TestSetupSandboxFiles(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
"should create new /etc/resolv.conf if DNSOptions is set": {
|
||||
{
|
||||
desc: "should create new /etc/resolv.conf if DNSOptions is set",
|
||||
dnsConfig: &runtime.DNSConfig{
|
||||
Servers: []string{"8.8.8.8"},
|
||||
Searches: []string{"114.114.114.114"},
|
||||
@@ -477,7 +496,8 @@ options timeout:1
|
||||
},
|
||||
},
|
||||
},
|
||||
"should create sandbox shm when ipc namespace mode is not NODE": {
|
||||
{
|
||||
desc: "should create sandbox shm when ipc namespace mode is not NODE",
|
||||
ipcMode: runtime.NamespaceMode_POD,
|
||||
expectedCalls: []ostesting.CalledDetail{
|
||||
{
|
||||
@@ -520,7 +540,8 @@ options timeout:1
|
||||
},
|
||||
},
|
||||
},
|
||||
"should create /etc/hostname when hostname is set": {
|
||||
{
|
||||
desc: "should create /etc/hostname when hostname is set",
|
||||
hostname: "test-hostname",
|
||||
ipcMode: runtime.NamespaceMode_NODE,
|
||||
expectedCalls: []ostesting.CalledDetail{
|
||||
@@ -555,7 +576,8 @@ options timeout:1
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
c := newTestCRIService()
|
||||
c.os.(*ostesting.FakeOS).HostnameFn = func() (string, error) {
|
||||
return realhostname, nil
|
||||
@@ -586,15 +608,19 @@ options timeout:1
|
||||
}
|
||||
|
||||
func TestParseDNSOption(t *testing.T) {
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
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": {
|
||||
{
|
||||
desc: "empty dns options should return empty content",
|
||||
},
|
||||
{
|
||||
desc: "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"},
|
||||
@@ -604,7 +630,8 @@ nameserver server.google.com
|
||||
options timeout:1
|
||||
`,
|
||||
},
|
||||
"expanded dns config should return correct content on modern libc (e.g. glibc 2.26 and above)": {
|
||||
{
|
||||
desc: "expanded dns config should return correct content on modern libc (e.g. glibc 2.26 and above)",
|
||||
servers: []string{"8.8.8.8", "server.google.com"},
|
||||
searches: []string{
|
||||
"server0.google.com",
|
||||
@@ -623,7 +650,8 @@ options timeout:1
|
||||
`,
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
resolvContent, err := parseDNSOptions(test.servers, test.searches, test.options)
|
||||
if test.expectErr {
|
||||
assert.Error(t, err)
|
||||
|
||||
Reference in New Issue
Block a user