Merge pull request #110390 from major1201/fix_kubelet_test

fix defer in loop and optimize test cases with explicit field name
This commit is contained in:
Kubernetes Prow Robot 2022-08-23 16:04:37 -07:00 committed by GitHub
commit 052bfc35b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -67,9 +67,9 @@ func TestNodeHostsFileContent(t *testing.T) {
expectedHostsFileContent string expectedHostsFileContent string
}{ }{
{ {
"hosts_test_file1", hostsFileName: "hosts_test_file1",
[]v1.HostAlias{}, hostAliases: []v1.HostAlias{},
`# hosts file for testing. rawHostsFileContent: `# hosts file for testing.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet fe00::0 ip6-localnet
@ -78,7 +78,7 @@ fe00::1 ip6-allnodes
fe00::2 ip6-allrouters fe00::2 ip6-allrouters
123.45.67.89 some.domain 123.45.67.89 some.domain
`, `,
`# Kubernetes-managed hosts file (host network). expectedHostsFileContent: `# Kubernetes-managed hosts file (host network).
# hosts file for testing. # hosts file for testing.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
@ -90,9 +90,9 @@ fe00::2 ip6-allrouters
`, `,
}, },
{ {
"hosts_test_file2", hostsFileName: "hosts_test_file2",
[]v1.HostAlias{}, hostAliases: []v1.HostAlias{},
`# another hosts file for testing. rawHostsFileContent: `# another hosts file for testing.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet fe00::0 ip6-localnet
@ -101,7 +101,7 @@ fe00::1 ip6-allnodes
fe00::2 ip6-allrouters fe00::2 ip6-allrouters
12.34.56.78 another.domain 12.34.56.78 another.domain
`, `,
`# Kubernetes-managed hosts file (host network). expectedHostsFileContent: `# Kubernetes-managed hosts file (host network).
# another hosts file for testing. # another hosts file for testing.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
@ -113,11 +113,11 @@ fe00::2 ip6-allrouters
`, `,
}, },
{ {
"hosts_test_file1_with_host_aliases", hostsFileName: "hosts_test_file1_with_host_aliases",
[]v1.HostAlias{ hostAliases: []v1.HostAlias{
{IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}}, {IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
}, },
`# hosts file for testing. rawHostsFileContent: `# hosts file for testing.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet fe00::0 ip6-localnet
@ -126,7 +126,7 @@ fe00::1 ip6-allnodes
fe00::2 ip6-allrouters fe00::2 ip6-allrouters
123.45.67.89 some.domain 123.45.67.89 some.domain
`, `,
`# Kubernetes-managed hosts file (host network). expectedHostsFileContent: `# Kubernetes-managed hosts file (host network).
# hosts file for testing. # hosts file for testing.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
@ -141,12 +141,12 @@ fe00::2 ip6-allrouters
`, `,
}, },
{ {
"hosts_test_file2_with_host_aliases", hostsFileName: "hosts_test_file2_with_host_aliases",
[]v1.HostAlias{ hostAliases: []v1.HostAlias{
{IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}}, {IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
{IP: "456.78.90.123", Hostnames: []string{"park", "doo", "boo"}}, {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 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet fe00::0 ip6-localnet
@ -155,7 +155,7 @@ fe00::1 ip6-allnodes
fe00::2 ip6-allrouters fe00::2 ip6-allrouters
12.34.56.78 another.domain 12.34.56.78 another.domain
`, `,
`# Kubernetes-managed hosts file (host network). expectedHostsFileContent: `# Kubernetes-managed hosts file (host network).
# another hosts file for testing. # another hosts file for testing.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
@ -173,13 +173,15 @@ fe00::2 ip6-allrouters
} }
for _, testCase := range testCases { for _, testCase := range testCases {
tmpdir, err := writeHostsFile(testCase.hostsFileName, testCase.rawHostsFileContent) t.Run(testCase.hostsFileName, func(t *testing.T) {
require.NoError(t, err, "could not create a temp hosts file") tmpdir, err := writeHostsFile(testCase.hostsFileName, testCase.rawHostsFileContent)
defer os.RemoveAll(tmpdir) 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) actualContent, fileReadErr := nodeHostsFileContent(filepath.Join(tmpdir, testCase.hostsFileName), testCase.hostAliases)
require.NoError(t, fileReadErr, "could not create read hosts file") require.NoError(t, fileReadErr, "could not create read hosts file")
assert.Equal(t, testCase.expectedHostsFileContent, string(actualContent), "hosts file content not expected") assert.Equal(t, testCase.expectedHostsFileContent, string(actualContent), "hosts file content not expected")
})
} }
} }
@ -202,11 +204,10 @@ func TestManagedHostsFileContent(t *testing.T) {
expectedContent string expectedContent string
}{ }{
{ {
[]string{"123.45.67.89"}, hostIPs: []string{"123.45.67.89"},
"podFoo", hostName: "podFoo",
"", hostAliases: []v1.HostAlias{},
[]v1.HostAlias{}, expectedContent: `# Kubernetes-managed hosts file.
`# Kubernetes-managed hosts file.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet fe00::0 ip6-localnet
@ -217,11 +218,11 @@ fe00::2 ip6-allrouters
`, `,
}, },
{ {
[]string{"203.0.113.1"}, hostIPs: []string{"203.0.113.1"},
"podFoo", hostName: "podFoo",
"domainFoo", hostDomainName: "domainFoo",
[]v1.HostAlias{}, hostAliases: []v1.HostAlias{},
`# Kubernetes-managed hosts file. expectedContent: `# Kubernetes-managed hosts file.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet fe00::0 ip6-localnet
@ -232,13 +233,13 @@ fe00::2 ip6-allrouters
`, `,
}, },
{ {
[]string{"203.0.113.1"}, hostIPs: []string{"203.0.113.1"},
"podFoo", hostName: "podFoo",
"domainFoo", hostDomainName: "domainFoo",
[]v1.HostAlias{ hostAliases: []v1.HostAlias{
{IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}}, {IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
}, },
`# Kubernetes-managed hosts file. expectedContent: `# Kubernetes-managed hosts file.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet fe00::0 ip6-localnet
@ -252,14 +253,14 @@ fe00::2 ip6-allrouters
`, `,
}, },
{ {
[]string{"203.0.113.1"}, hostIPs: []string{"203.0.113.1"},
"podFoo", hostName: "podFoo",
"domainFoo", hostDomainName: "domainFoo",
[]v1.HostAlias{ hostAliases: []v1.HostAlias{
{IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}}, {IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
{IP: "456.78.90.123", Hostnames: []string{"park", "doo", "boo"}}, {IP: "456.78.90.123", Hostnames: []string{"park", "doo", "boo"}},
}, },
`# Kubernetes-managed hosts file. expectedContent: `# Kubernetes-managed hosts file.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet fe00::0 ip6-localnet
@ -274,11 +275,11 @@ fe00::2 ip6-allrouters
`, `,
}, },
{ {
[]string{"203.0.113.1", "fd00::6"}, hostIPs: []string{"203.0.113.1", "fd00::6"},
"podFoo", hostName: "podFoo",
"domainFoo", hostDomainName: "domainFoo",
[]v1.HostAlias{}, hostAliases: []v1.HostAlias{},
`# Kubernetes-managed hosts file. expectedContent: `# Kubernetes-managed hosts file.
127.0.0.1 localhost 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback ::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet fe00::0 ip6-localnet
@ -2961,34 +2962,36 @@ func TestGetExec(t *testing.T) {
}} }}
for _, tc := range testcases { for _, tc := range testcases {
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) t.Run(tc.description, func(t *testing.T) {
defer testKubelet.Cleanup() testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
kubelet := testKubelet.kubelet defer testKubelet.Cleanup()
testKubelet.fakeRuntime.PodList = []*containertest.FakePod{ kubelet := testKubelet.kubelet
{Pod: &kubecontainer.Pod{ testKubelet.fakeRuntime.PodList = []*containertest.FakePod{
ID: podUID, {Pod: &kubecontainer.Pod{
Name: podName, ID: podUID,
Namespace: podNamespace, Name: podName,
Containers: []*kubecontainer.Container{ Namespace: podNamespace,
{Name: containerID, Containers: []*kubecontainer.Container{
ID: kubecontainer.ContainerID{Type: "test", ID: containerID}, {Name: containerID,
ID: kubecontainer.ContainerID{Type: "test", ID: containerID},
},
}, },
}, }},
}}, }
}
description := "streaming - " + tc.description description := "streaming - " + tc.description
fakeRuntime := &containertest.FakeStreamingRuntime{FakeRuntime: testKubelet.fakeRuntime} fakeRuntime := &containertest.FakeStreamingRuntime{FakeRuntime: testKubelet.fakeRuntime}
kubelet.containerRuntime = fakeRuntime kubelet.containerRuntime = fakeRuntime
kubelet.streamingRuntime = fakeRuntime kubelet.streamingRuntime = fakeRuntime
redirect, err := kubelet.GetExec(tc.podFullName, podUID, tc.container, tc.command, remotecommand.Options{}) redirect, err := kubelet.GetExec(tc.podFullName, podUID, tc.container, tc.command, remotecommand.Options{})
if tc.expectError { if tc.expectError {
assert.Error(t, err, description) assert.Error(t, err, description)
} else { } else {
assert.NoError(t, err, description) assert.NoError(t, err, description)
assert.Equal(t, containertest.FakeHost, redirect.Host, description+": redirect") assert.Equal(t, containertest.FakeHost, redirect.Host, description+": redirect")
} }
})
} }
} }