2023-08-10 10:50:13 +00:00
|
|
|
//go:build !windows
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2023-07-16 18:13:43 +00:00
|
|
|
"strconv"
|
2015-07-02 05:00:48 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-08-12 12:38:43 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2023-07-26 19:06:35 +00:00
|
|
|
"github.com/docker/docker/internal/testutils/netnsutils"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/config"
|
|
|
|
"github.com/docker/docker/libnetwork/ipamapi"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
|
|
|
"github.com/docker/docker/libnetwork/osl"
|
2023-08-12 12:38:43 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-07-02 05:00:48 +00:00
|
|
|
)
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func getTestEnv(t *testing.T, opts ...[]NetworkOption) (*Controller, []*Network) {
|
2023-07-16 18:13:43 +00:00
|
|
|
const netType = "bridge"
|
2022-11-08 23:06:30 +00:00
|
|
|
c, err := New(
|
|
|
|
OptionBoltdbWithRandomDBFile(t),
|
2023-07-16 18:13:43 +00:00
|
|
|
config.OptionDriverConfig(netType, map[string]any{
|
|
|
|
netlabel.GenericData: options.Generic{"EnableIPForwarding": true},
|
|
|
|
}),
|
2022-11-08 23:06:30 +00:00
|
|
|
)
|
2015-09-18 21:00:36 +00:00
|
|
|
if err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-11-08 22:49:52 +00:00
|
|
|
t.Cleanup(c.Stop)
|
2015-07-02 05:00:48 +00:00
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
if len(opts) == 0 {
|
2016-10-13 18:14:39 +00:00
|
|
|
return c, nil
|
2016-05-12 21:33:41 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
nwList := make([]*Network, 0, len(opts))
|
2018-03-28 14:15:55 +00:00
|
|
|
for i, opt := range opts {
|
2023-07-16 18:13:43 +00:00
|
|
|
name := "test_nw_" + strconv.Itoa(i)
|
|
|
|
newOptions := []NetworkOption{
|
|
|
|
NetworkOptionGeneric(options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{"BridgeName": name},
|
|
|
|
}),
|
2016-10-13 18:14:39 +00:00
|
|
|
}
|
2018-03-28 14:15:55 +00:00
|
|
|
newOptions = append(newOptions, opt...)
|
|
|
|
n, err := c.NewNetwork(netType, name, "", newOptions...)
|
2016-10-13 18:14:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
|
2016-10-13 18:14:39 +00:00
|
|
|
nwList = append(nwList, n)
|
2015-07-02 05:00:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 18:14:39 +00:00
|
|
|
return c, nwList
|
2015-07-02 05:00:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 12:38:43 +00:00
|
|
|
func TestControllerGetSandbox(t *testing.T) {
|
|
|
|
ctrlr, _ := getTestEnv(t)
|
|
|
|
t.Run("invalid id", func(t *testing.T) {
|
|
|
|
const cID = ""
|
|
|
|
sb, err := ctrlr.GetSandbox(cID)
|
|
|
|
_, ok := err.(ErrInvalidID)
|
|
|
|
assert.Check(t, ok, "expected ErrInvalidID, got %[1]v (%[1]T)", err)
|
|
|
|
assert.Check(t, is.Nil(sb))
|
|
|
|
})
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
|
|
|
const cID = "container-id-with-no-sandbox"
|
|
|
|
sb, err := ctrlr.GetSandbox(cID)
|
|
|
|
assert.Check(t, errdefs.IsNotFound(err), "expected a ErrNotFound, got %[1]v (%[1]T)", err)
|
|
|
|
assert.Check(t, is.Nil(sb))
|
|
|
|
})
|
|
|
|
t.Run("existing sandbox", func(t *testing.T) {
|
|
|
|
const cID = "test-container-id"
|
|
|
|
expected, err := ctrlr.NewSandbox(cID)
|
|
|
|
assert.Check(t, err)
|
|
|
|
|
|
|
|
sb, err := ctrlr.GetSandbox(cID)
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.Equal(sb.ContainerID(), cID))
|
|
|
|
assert.Check(t, is.Equal(sb.ID(), expected.ID()))
|
|
|
|
assert.Check(t, is.Equal(sb.Key(), expected.Key()))
|
|
|
|
assert.Check(t, is.Equal(sb.ContainerID(), expected.ContainerID()))
|
|
|
|
|
|
|
|
err = sb.Delete()
|
|
|
|
assert.Check(t, err)
|
|
|
|
|
|
|
|
sb, err = ctrlr.GetSandbox(cID)
|
|
|
|
assert.Check(t, errdefs.IsNotFound(err), "expected a ErrNotFound, got %[1]v (%[1]T)", err)
|
|
|
|
assert.Check(t, is.Nil(sb))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
func TestSandboxAddEmpty(t *testing.T) {
|
2023-01-11 22:43:32 +00:00
|
|
|
ctrlr, _ := getTestEnv(t)
|
2015-07-02 05:00:48 +00:00
|
|
|
|
|
|
|
sbx, err := ctrlr.NewSandbox("sandbox0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sbx.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
|
|
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
|
|
|
|
}
|
|
|
|
|
|
|
|
osl.GC()
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
// // If different priorities are specified, internal option and ipv6 addresses mustn't influence endpoint order
|
2015-07-02 05:00:48 +00:00
|
|
|
func TestSandboxAddMultiPrio(t *testing.T) {
|
2023-07-26 19:06:35 +00:00
|
|
|
defer netnsutils.SetupTestOSContext(t)()
|
2015-07-02 05:00:48 +00:00
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
opts := [][]NetworkOption{
|
|
|
|
{NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)},
|
|
|
|
{NetworkOptionInternalNetwork()},
|
|
|
|
{},
|
|
|
|
}
|
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
ctrlr, nws := getTestEnv(t, opts...)
|
2015-07-02 05:00:48 +00:00
|
|
|
|
|
|
|
sbx, err := ctrlr.NewSandbox("sandbox1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sid := sbx.ID()
|
|
|
|
|
2016-10-13 18:14:39 +00:00
|
|
|
ep1, err := nws[0].CreateEndpoint("ep1")
|
2015-07-02 05:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-13 18:14:39 +00:00
|
|
|
ep2, err := nws[1].CreateEndpoint("ep2")
|
2015-07-02 05:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-13 18:14:39 +00:00
|
|
|
ep3, err := nws[2].CreateEndpoint("ep3")
|
2015-07-02 05:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:02:30 +00:00
|
|
|
if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:02:30 +00:00
|
|
|
if err := ep2.Join(sbx, JoinOptionPriority(2)); err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:02:30 +00:00
|
|
|
if err := ep3.Join(sbx, JoinOptionPriority(3)); err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap")
|
|
|
|
}
|
|
|
|
|
2016-01-26 00:23:00 +00:00
|
|
|
if len(sbx.Endpoints()) != 3 {
|
|
|
|
t.Fatal("Expected 3 endpoints to be connected to the sandbox.")
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
if err := ep3.Leave(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal("Expected ep2 to be at the top of the heap after removing ep3. But did not find ep2 at the top of the heap")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep2.Leave(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal("Expected ep1 to be at the top of the heap after removing ep2. But did not find ep1 at the top of the heap")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-add ep3 back
|
2020-05-15 03:02:30 +00:00
|
|
|
if err := ep3.Join(sbx, JoinOptionPriority(3)); err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal("Expected ep3 to be at the top of the heap after adding ep3 back. But did not find ep3 at the top of the heap")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sbx.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
|
|
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
|
|
|
|
}
|
|
|
|
|
|
|
|
osl.GC()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSandboxAddSamePrio(t *testing.T) {
|
2023-07-26 19:06:35 +00:00
|
|
|
defer netnsutils.SetupTestOSContext(t)()
|
2015-07-02 05:00:48 +00:00
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
opts := [][]NetworkOption{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)},
|
|
|
|
{NetworkOptionInternalNetwork()},
|
|
|
|
}
|
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
ctrlr, nws := getTestEnv(t, opts...)
|
2015-07-02 05:00:48 +00:00
|
|
|
|
|
|
|
sbx, err := ctrlr.NewSandbox("sandbox1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sid := sbx.ID()
|
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
epNw1, err := nws[1].CreateEndpoint("ep1")
|
2015-07-02 05:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-28 14:15:55 +00:00
|
|
|
epIPv6, err := nws[2].CreateEndpoint("ep2")
|
2015-07-02 05:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
epInternal, err := nws[3].CreateEndpoint("ep3")
|
|
|
|
if err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
epNw0, err := nws[0].CreateEndpoint("ep4")
|
|
|
|
if err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
if err := epNw1.Join(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-07-02 05:00:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
if err := epIPv6.Join(sbx); err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
if err := epInternal.Join(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-07-02 05:00:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 14:15:55 +00:00
|
|
|
if err := epNw0.Join(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// order should now be: epIPv6, epNw0, epNw1, epInternal
|
|
|
|
if len(sbx.Endpoints()) != 4 {
|
|
|
|
t.Fatal("Expected 4 endpoints to be connected to the sandbox.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPv6 has precedence over IPv4
|
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != epIPv6.ID() {
|
|
|
|
t.Fatal("Expected epIPv6 to be at the top of the heap. But did not find epIPv6 at the top of the heap")
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal network has lowest precedence
|
|
|
|
if ctrlr.sandboxes[sid].endpoints[3].ID() != epInternal.ID() {
|
|
|
|
t.Fatal("Expected epInternal to be at the bottom of the heap. But did not find epInternal at the bottom of the heap")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := epIPv6.Leave(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'test_nw_0' has precedence over 'test_nw_1'
|
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != epNw0.ID() {
|
|
|
|
t.Fatal("Expected epNw0 to be at the top of the heap after removing epIPv6. But did not find epNw0 at the top of the heap")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := epNw1.Leave(sbx); err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sbx.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
|
|
t.Fatalf("controller containers is not empty. len = %d", len(ctrlr.sandboxes))
|
|
|
|
}
|
|
|
|
|
|
|
|
osl.GC()
|
|
|
|
}
|