Skip to content

Commit 2680095

Browse files
committed
kubelet: Remove unnecessary sorting in NodeAddress tests
Several of the tests in TestNodeAddress() were no-ops because the test code was only testing that NodeAddresses() returned all of the expected addresses, but not testing that it was returning them in the correct order. The order that NodeAddresses() returns addresses in is very important, so fix the tests to actually test it. One existing test ("NodeIP is external") had its expectedAddresses in the wrong order, but it seems clear from the name of the test that this isn't actually what it expected. Also, previously testKubeletHostname was "127.0.0.1" which ended up interacting weirdly with the IPv4-vs-IPv6 sorting code in a way that made some of the test results confusing if you didn't realize that testKubeletHostname was an IPv4 address. Fix that by making it an actual hostname instead, which then preserves the expected sorting.
1 parent 128b697 commit 2680095

File tree

1 file changed

+2
-19
lines changed

1 file changed

+2
-19
lines changed

pkg/kubelet/nodestatus/setters_test.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import (
5050
)
5151

5252
const (
53-
testKubeletHostname = "127.0.0.1"
53+
testKubeletHostname = "hostname"
5454
)
5555

5656
// TODO(mtaufen): below is ported from the old kubelet_node_status_test.go code, potentially add more test coverage for NodeAddress setter in future
@@ -86,8 +86,8 @@ func TestNodeAddress(t *testing.T) {
8686
{Type: v1.NodeHostName, Address: testKubeletHostname},
8787
},
8888
expectedAddresses: []v1.NodeAddress{
89-
{Type: v1.NodeInternalIP, Address: "10.1.1.1"},
9089
{Type: v1.NodeExternalIP, Address: "55.55.55.55"},
90+
{Type: v1.NodeInternalIP, Address: "10.1.1.1"},
9191
{Type: v1.NodeHostName, Address: testKubeletHostname},
9292
},
9393
shouldError: false,
@@ -433,10 +433,6 @@ func TestNodeAddress(t *testing.T) {
433433
return
434434
}
435435

436-
// Sort both sets for consistent equality
437-
sortNodeAddresses(testCase.expectedAddresses)
438-
sortNodeAddresses(existingNode.Status.Addresses)
439-
440436
assert.True(t, apiequality.Semantic.DeepEqual(testCase.expectedAddresses, existingNode.Status.Addresses),
441437
"Diff: %s", diff.ObjectDiff(testCase.expectedAddresses, existingNode.Status.Addresses))
442438
})
@@ -1678,19 +1674,6 @@ func TestVolumeLimits(t *testing.T) {
16781674

16791675
// Test Helpers:
16801676

1681-
// sortableNodeAddress is a type for sorting []v1.NodeAddress
1682-
type sortableNodeAddress []v1.NodeAddress
1683-
1684-
func (s sortableNodeAddress) Len() int { return len(s) }
1685-
func (s sortableNodeAddress) Less(i, j int) bool {
1686-
return (string(s[i].Type) + s[i].Address) < (string(s[j].Type) + s[j].Address)
1687-
}
1688-
func (s sortableNodeAddress) Swap(i, j int) { s[j], s[i] = s[i], s[j] }
1689-
1690-
func sortNodeAddresses(addrs sortableNodeAddress) {
1691-
sort.Sort(addrs)
1692-
}
1693-
16941677
// testEvent is used to record events for tests
16951678
type testEvent struct {
16961679
eventType string

0 commit comments

Comments
 (0)