2015-07-02 01:00:48 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2016-10-13 14:14:39 -04:00
|
|
|
"fmt"
|
2015-07-02 01:00:48 -04:00
|
|
|
"testing"
|
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-07-02 01:00:48 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
|
|
|
"github.com/docker/libnetwork/osl"
|
2015-09-07 13:33:28 -04:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-07-02 01:00:48 -04:00
|
|
|
)
|
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
func getTestEnv(t *testing.T, numNetworks int) (NetworkController, []Network) {
|
2015-09-18 17:00:36 -04:00
|
|
|
netType := "bridge"
|
2015-07-02 01:00:48 -04:00
|
|
|
|
|
|
|
option := options.Generic{
|
|
|
|
"EnableIPForwarding": true,
|
|
|
|
}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = option
|
2015-09-18 17:00:36 -04:00
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
cfgOptions, err := OptionBoltdbWithRandomDBFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
c, err := New(append(cfgOptions, config.OptionDriverConfig(netType, genericOption))...)
|
2015-09-18 17:00:36 -04:00
|
|
|
if err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
if numNetworks == 0 {
|
|
|
|
return c, nil
|
2016-05-12 17:33:41 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
nwList := make([]Network, 0, numNetworks)
|
|
|
|
for i := 0; i < numNetworks; i++ {
|
|
|
|
name := fmt.Sprintf("test_nw_%d", i)
|
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
|
|
|
"BridgeName": name,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
n, err := c.NewNetwork(netType, name, "", NetworkOptionGeneric(netOption))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
nwList = append(nwList, n)
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
return c, nwList
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSandboxAddEmpty(t *testing.T) {
|
2016-10-13 14:14:39 -04:00
|
|
|
c, _ := getTestEnv(t, 0)
|
2016-05-12 17:33:41 -04:00
|
|
|
ctrlr := c.(*controller)
|
2015-07-02 01:00:48 -04: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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSandboxAddMultiPrio(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
c, nws := getTestEnv(t, 3)
|
2015-07-02 01:00:48 -04:00
|
|
|
ctrlr := c.(*controller)
|
|
|
|
|
|
|
|
sbx, err := ctrlr.NewSandbox("sandbox1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sid := sbx.ID()
|
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
ep1, err := nws[0].CreateEndpoint("ep1")
|
2015-07-02 01:00:48 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-13 14:14:39 -04:00
|
|
|
ep2, err := nws[1].CreateEndpoint("ep2")
|
2015-07-02 01:00:48 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-13 14:14:39 -04:00
|
|
|
ep3, err := nws[2].CreateEndpoint("ep3")
|
2015-07-02 01:00:48 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep1.Join(sbx, JoinOptionPriority(ep1, 1)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep2.Join(sbx, JoinOptionPriority(ep2, 2)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep3.Join(sbx, JoinOptionPriority(ep3, 3)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
|
2015-07-02 01:00:48 -04: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-25 19:23:00 -05:00
|
|
|
if len(sbx.Endpoints()) != 3 {
|
|
|
|
t.Fatal("Expected 3 endpoints to be connected to the sandbox.")
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := ep3.Leave(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() {
|
2015-07-02 01:00:48 -04: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 07:21:15 -04:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() {
|
2015-07-02 01:00:48 -04: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
|
|
|
|
if err := ep3.Join(sbx, JoinOptionPriority(ep3, 3)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
|
2015-07-02 01:00:48 -04: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) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
c, nws := getTestEnv(t, 2)
|
2015-07-02 01:00:48 -04:00
|
|
|
|
|
|
|
ctrlr := c.(*controller)
|
|
|
|
|
|
|
|
sbx, err := ctrlr.NewSandbox("sandbox1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sid := sbx.ID()
|
|
|
|
|
2016-10-13 14:14:39 -04:00
|
|
|
ep1, err := nws[0].CreateEndpoint("ep1")
|
2015-07-02 01:00:48 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-13 14:14:39 -04:00
|
|
|
ep2, err := nws[1].CreateEndpoint("ep2")
|
2015-07-02 01:00:48 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep1.Join(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep2.Join(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatal("Expected ep1 to be at the top of the heap. But did not find ep1 at the top of the heap")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep1.Leave(sbx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() {
|
2015-07-02 01:00:48 -04: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|