mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
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:
commit
052bfc35b2
@ -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,6 +173,7 @@ fe00::2 ip6-allrouters
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
|
t.Run(testCase.hostsFileName, func(t *testing.T) {
|
||||||
tmpdir, err := writeHostsFile(testCase.hostsFileName, testCase.rawHostsFileContent)
|
tmpdir, err := writeHostsFile(testCase.hostsFileName, testCase.rawHostsFileContent)
|
||||||
require.NoError(t, err, "could not create a temp hosts file")
|
require.NoError(t, err, "could not create a temp hosts file")
|
||||||
defer os.RemoveAll(tmpdir)
|
defer os.RemoveAll(tmpdir)
|
||||||
@ -180,6 +181,7 @@ fe00::2 ip6-allrouters
|
|||||||
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,6 +2962,7 @@ func TestGetExec(t *testing.T) {
|
|||||||
}}
|
}}
|
||||||
|
|
||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
|
t.Run(tc.description, func(t *testing.T) {
|
||||||
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||||
defer testKubelet.Cleanup()
|
defer testKubelet.Cleanup()
|
||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
@ -2989,6 +2991,7 @@ func TestGetExec(t *testing.T) {
|
|||||||
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")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user