moby/libnetwork/endpoint_test.go
Sebastiaan van Stijn 0a9bc3b507
libnetwork: Sandbox.ResolveName: refactor ordering of endpoints
When resolving names in swarm mode, services with exposed ports are
connected to user overlay network, ingress network, and local (docker_gwbridge)
networks. Name resolution should prioritize returning the VIP/IPs on user
overlay network over ingress and local networks.

Sandbox.ResolveName implemented this by taking the list of endpoints,
splitting the list into 3 separate lists based on the type of network
that the endpoint was attached to (dynamic, ingress, local), and then
creating a new list, applying the networks in that order.

This patch refactors that logic to use a custom sorter (sort.Interface),
which makes the code more transparent, and prevents iterating over the
list of endpoints multiple times.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-01-20 12:41:33 +01:00

43 lines
832 B
Go

package libnetwork
import (
"sort"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestSortByNetworkType(t *testing.T) {
nws := []*Network{
{name: "local2"},
{name: "ovl2", dynamic: true},
{name: "local3"},
{name: "ingress", ingress: true},
{name: "ovl3", dynamic: true},
{name: "local1"},
{name: "ovl1", dynamic: true},
}
eps := make([]*Endpoint, 0, len(nws))
for _, nw := range nws {
eps = append(eps, &Endpoint{
name: "ep-" + nw.name,
network: nw,
})
}
sort.Sort(ByNetworkType(eps))
actual := make([]string, 0, len(eps))
for _, ep := range eps {
actual = append(actual, ep.name)
}
expected := []string{
"ep-ovl2",
"ep-ovl3",
"ep-ovl1",
"ep-ingress",
"ep-local2",
"ep-local3",
"ep-local1",
}
assert.Check(t, is.DeepEqual(actual, expected))
}