fix defer in loop and optimize test case with explicit field name
This commit is contained in:
parent
ca630bc118
commit
c87a559ed7
@ -63,9 +63,9 @@ func TestNodeHostsFileContent(t *testing.T) {
|
||||
expectedHostsFileContent string
|
||||
}{
|
||||
{
|
||||
"hosts_test_file1",
|
||||
[]v1.HostAlias{},
|
||||
`# hosts file for testing.
|
||||
hostsFileName: "hosts_test_file1",
|
||||
hostAliases: []v1.HostAlias{},
|
||||
rawHostsFileContent: `# hosts file for testing.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
@ -74,7 +74,7 @@ fe00::1 ip6-allnodes
|
||||
fe00::2 ip6-allrouters
|
||||
123.45.67.89 some.domain
|
||||
`,
|
||||
`# Kubernetes-managed hosts file (host network).
|
||||
expectedHostsFileContent: `# Kubernetes-managed hosts file (host network).
|
||||
# hosts file for testing.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
@ -86,9 +86,9 @@ fe00::2 ip6-allrouters
|
||||
`,
|
||||
},
|
||||
{
|
||||
"hosts_test_file2",
|
||||
[]v1.HostAlias{},
|
||||
`# another hosts file for testing.
|
||||
hostsFileName: "hosts_test_file2",
|
||||
hostAliases: []v1.HostAlias{},
|
||||
rawHostsFileContent: `# another hosts file for testing.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
@ -97,7 +97,7 @@ fe00::1 ip6-allnodes
|
||||
fe00::2 ip6-allrouters
|
||||
12.34.56.78 another.domain
|
||||
`,
|
||||
`# Kubernetes-managed hosts file (host network).
|
||||
expectedHostsFileContent: `# Kubernetes-managed hosts file (host network).
|
||||
# another hosts file for testing.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
@ -109,11 +109,11 @@ fe00::2 ip6-allrouters
|
||||
`,
|
||||
},
|
||||
{
|
||||
"hosts_test_file1_with_host_aliases",
|
||||
[]v1.HostAlias{
|
||||
hostsFileName: "hosts_test_file1_with_host_aliases",
|
||||
hostAliases: []v1.HostAlias{
|
||||
{IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
|
||||
},
|
||||
`# hosts file for testing.
|
||||
rawHostsFileContent: `# hosts file for testing.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
@ -122,7 +122,7 @@ fe00::1 ip6-allnodes
|
||||
fe00::2 ip6-allrouters
|
||||
123.45.67.89 some.domain
|
||||
`,
|
||||
`# Kubernetes-managed hosts file (host network).
|
||||
expectedHostsFileContent: `# Kubernetes-managed hosts file (host network).
|
||||
# hosts file for testing.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
@ -137,12 +137,12 @@ fe00::2 ip6-allrouters
|
||||
`,
|
||||
},
|
||||
{
|
||||
"hosts_test_file2_with_host_aliases",
|
||||
[]v1.HostAlias{
|
||||
hostsFileName: "hosts_test_file2_with_host_aliases",
|
||||
hostAliases: []v1.HostAlias{
|
||||
{IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
|
||||
{IP: "456.78.90.123", Hostnames: []string{"park", "doo", "boo"}},
|
||||
},
|
||||
`# another hosts file for testing.
|
||||
rawHostsFileContent: `# another hosts file for testing.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
@ -151,7 +151,7 @@ fe00::1 ip6-allnodes
|
||||
fe00::2 ip6-allrouters
|
||||
12.34.56.78 another.domain
|
||||
`,
|
||||
`# Kubernetes-managed hosts file (host network).
|
||||
expectedHostsFileContent: `# Kubernetes-managed hosts file (host network).
|
||||
# another hosts file for testing.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
@ -169,13 +169,15 @@ fe00::2 ip6-allrouters
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
tmpdir, err := writeHostsFile(testCase.hostsFileName, testCase.rawHostsFileContent)
|
||||
require.NoError(t, err, "could not create a temp hosts file")
|
||||
defer os.RemoveAll(tmpdir)
|
||||
t.Run(testCase.hostsFileName, func(t *testing.T) {
|
||||
tmpdir, err := writeHostsFile(testCase.hostsFileName, testCase.rawHostsFileContent)
|
||||
require.NoError(t, err, "could not create a temp hosts file")
|
||||
defer os.RemoveAll(tmpdir)
|
||||
|
||||
actualContent, fileReadErr := nodeHostsFileContent(filepath.Join(tmpdir, testCase.hostsFileName), testCase.hostAliases)
|
||||
require.NoError(t, fileReadErr, "could not create read hosts file")
|
||||
assert.Equal(t, testCase.expectedHostsFileContent, string(actualContent), "hosts file content not expected")
|
||||
actualContent, fileReadErr := nodeHostsFileContent(filepath.Join(tmpdir, testCase.hostsFileName), testCase.hostAliases)
|
||||
require.NoError(t, fileReadErr, "could not create read hosts file")
|
||||
assert.Equal(t, testCase.expectedHostsFileContent, string(actualContent), "hosts file content not expected")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -198,11 +200,10 @@ func TestManagedHostsFileContent(t *testing.T) {
|
||||
expectedContent string
|
||||
}{
|
||||
{
|
||||
[]string{"123.45.67.89"},
|
||||
"podFoo",
|
||||
"",
|
||||
[]v1.HostAlias{},
|
||||
`# Kubernetes-managed hosts file.
|
||||
hostIPs: []string{"123.45.67.89"},
|
||||
hostName: "podFoo",
|
||||
hostAliases: []v1.HostAlias{},
|
||||
expectedContent: `# Kubernetes-managed hosts file.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
@ -213,11 +214,11 @@ fe00::2 ip6-allrouters
|
||||
`,
|
||||
},
|
||||
{
|
||||
[]string{"203.0.113.1"},
|
||||
"podFoo",
|
||||
"domainFoo",
|
||||
[]v1.HostAlias{},
|
||||
`# Kubernetes-managed hosts file.
|
||||
hostIPs: []string{"203.0.113.1"},
|
||||
hostName: "podFoo",
|
||||
hostDomainName: "domainFoo",
|
||||
hostAliases: []v1.HostAlias{},
|
||||
expectedContent: `# Kubernetes-managed hosts file.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
@ -228,13 +229,13 @@ fe00::2 ip6-allrouters
|
||||
`,
|
||||
},
|
||||
{
|
||||
[]string{"203.0.113.1"},
|
||||
"podFoo",
|
||||
"domainFoo",
|
||||
[]v1.HostAlias{
|
||||
hostIPs: []string{"203.0.113.1"},
|
||||
hostName: "podFoo",
|
||||
hostDomainName: "domainFoo",
|
||||
hostAliases: []v1.HostAlias{
|
||||
{IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
|
||||
},
|
||||
`# Kubernetes-managed hosts file.
|
||||
expectedContent: `# Kubernetes-managed hosts file.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
@ -248,14 +249,14 @@ fe00::2 ip6-allrouters
|
||||
`,
|
||||
},
|
||||
{
|
||||
[]string{"203.0.113.1"},
|
||||
"podFoo",
|
||||
"domainFoo",
|
||||
[]v1.HostAlias{
|
||||
hostIPs: []string{"203.0.113.1"},
|
||||
hostName: "podFoo",
|
||||
hostDomainName: "domainFoo",
|
||||
hostAliases: []v1.HostAlias{
|
||||
{IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
|
||||
{IP: "456.78.90.123", Hostnames: []string{"park", "doo", "boo"}},
|
||||
},
|
||||
`# Kubernetes-managed hosts file.
|
||||
expectedContent: `# Kubernetes-managed hosts file.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
@ -270,11 +271,11 @@ fe00::2 ip6-allrouters
|
||||
`,
|
||||
},
|
||||
{
|
||||
[]string{"203.0.113.1", "fd00::6"},
|
||||
"podFoo",
|
||||
"domainFoo",
|
||||
[]v1.HostAlias{},
|
||||
`# Kubernetes-managed hosts file.
|
||||
hostIPs: []string{"203.0.113.1", "fd00::6"},
|
||||
hostName: "podFoo",
|
||||
hostDomainName: "domainFoo",
|
||||
hostAliases: []v1.HostAlias{},
|
||||
expectedContent: `# Kubernetes-managed hosts file.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
@ -2905,34 +2906,36 @@ func TestGetExec(t *testing.T) {
|
||||
}}
|
||||
|
||||
for _, tc := range testcases {
|
||||
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||
defer testKubelet.Cleanup()
|
||||
kubelet := testKubelet.kubelet
|
||||
testKubelet.fakeRuntime.PodList = []*containertest.FakePod{
|
||||
{Pod: &kubecontainer.Pod{
|
||||
ID: podUID,
|
||||
Name: podName,
|
||||
Namespace: podNamespace,
|
||||
Containers: []*kubecontainer.Container{
|
||||
{Name: containerID,
|
||||
ID: kubecontainer.ContainerID{Type: "test", ID: containerID},
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||
defer testKubelet.Cleanup()
|
||||
kubelet := testKubelet.kubelet
|
||||
testKubelet.fakeRuntime.PodList = []*containertest.FakePod{
|
||||
{Pod: &kubecontainer.Pod{
|
||||
ID: podUID,
|
||||
Name: podName,
|
||||
Namespace: podNamespace,
|
||||
Containers: []*kubecontainer.Container{
|
||||
{Name: containerID,
|
||||
ID: kubecontainer.ContainerID{Type: "test", ID: containerID},
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
}
|
||||
}},
|
||||
}
|
||||
|
||||
description := "streaming - " + tc.description
|
||||
fakeRuntime := &containertest.FakeStreamingRuntime{FakeRuntime: testKubelet.fakeRuntime}
|
||||
kubelet.containerRuntime = fakeRuntime
|
||||
kubelet.streamingRuntime = fakeRuntime
|
||||
description := "streaming - " + tc.description
|
||||
fakeRuntime := &containertest.FakeStreamingRuntime{FakeRuntime: testKubelet.fakeRuntime}
|
||||
kubelet.containerRuntime = fakeRuntime
|
||||
kubelet.streamingRuntime = fakeRuntime
|
||||
|
||||
redirect, err := kubelet.GetExec(tc.podFullName, podUID, tc.container, tc.command, remotecommand.Options{})
|
||||
if tc.expectError {
|
||||
assert.Error(t, err, description)
|
||||
} else {
|
||||
assert.NoError(t, err, description)
|
||||
assert.Equal(t, containertest.FakeHost, redirect.Host, description+": redirect")
|
||||
}
|
||||
redirect, err := kubelet.GetExec(tc.podFullName, podUID, tc.container, tc.command, remotecommand.Options{})
|
||||
if tc.expectError {
|
||||
assert.Error(t, err, description)
|
||||
} else {
|
||||
assert.NoError(t, err, description)
|
||||
assert.Equal(t, containertest.FakeHost, redirect.Host, description+": redirect")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user