2015-06-02 13:22:28 -07:00
|
|
|
package libnetwork
|
|
|
|
|
2015-06-11 14:52:24 -07:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/libnetwork/sandbox"
|
|
|
|
)
|
2015-06-02 13:22:28 -07:00
|
|
|
|
|
|
|
func createEmptyCtrlr() *controller {
|
|
|
|
return &controller{sandboxes: sandboxTable{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEmptyEndpoint() *endpoint {
|
|
|
|
return &endpoint{
|
|
|
|
container: &containerInfo{},
|
|
|
|
joinInfo: &endpointJoinInfo{},
|
|
|
|
iFaces: []*endpointInterface{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSandboxAddEmpty(t *testing.T) {
|
|
|
|
ctrlr := createEmptyCtrlr()
|
|
|
|
ep := createEmptyEndpoint()
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if _, err := ctrlr.sandboxAdd(sandbox.GenerateKey("sandbox1"), true, ep); err != nil {
|
2015-06-02 13:22:28 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
ctrlr.sandboxRm(sandbox.GenerateKey("sandbox1"), ep)
|
2015-06-02 13:22:28 -07:00
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
ctrlr.LeaveAll("sandbox1")
|
2015-06-02 13:22:28 -07:00
|
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
|
|
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
|
|
|
|
}
|
2015-06-11 14:52:24 -07:00
|
|
|
|
|
|
|
sandbox.GC()
|
2015-06-02 13:22:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSandboxAddMultiPrio(t *testing.T) {
|
|
|
|
ctrlr := createEmptyCtrlr()
|
|
|
|
ep1 := createEmptyEndpoint()
|
|
|
|
ep2 := createEmptyEndpoint()
|
|
|
|
ep3 := createEmptyEndpoint()
|
|
|
|
|
|
|
|
ep1.container.config.prio = 1
|
|
|
|
ep2.container.config.prio = 2
|
|
|
|
ep3.container.config.prio = 3
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
sKey := sandbox.GenerateKey("sandbox1")
|
2015-06-02 13:22:28 -07:00
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep1); err != nil {
|
2015-06-02 13:22:28 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep2); err != nil {
|
2015-06-02 13:22:28 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep3); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-06-02 13:22:28 -07:00
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep3 {
|
2015-06-02 13:22:28 -07:00
|
|
|
t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap")
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
ctrlr.sandboxRm(sKey, ep3)
|
2015-06-02 13:22:28 -07:00
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep2 {
|
2015-06-02 13:22:28 -07: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")
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
ctrlr.sandboxRm(sKey, ep2)
|
2015-06-02 13:22:28 -07:00
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep1 {
|
2015-06-02 13:22:28 -07: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
|
2015-06-19 18:41:31 -07:00
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep3); err != nil {
|
2015-06-02 13:22:28 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep3 {
|
2015-06-02 13:22:28 -07: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")
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
ctrlr.sandboxRm(sKey, ep3)
|
|
|
|
ctrlr.sandboxRm(sKey, ep1)
|
|
|
|
|
|
|
|
if err := ctrlr.LeaveAll("sandbox1"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-02 13:22:28 -07:00
|
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
|
|
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
|
|
|
|
}
|
2015-06-11 14:52:24 -07:00
|
|
|
|
|
|
|
sandbox.GC()
|
2015-06-02 13:22:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSandboxAddSamePrio(t *testing.T) {
|
|
|
|
ctrlr := createEmptyCtrlr()
|
|
|
|
ep1 := createEmptyEndpoint()
|
|
|
|
ep2 := createEmptyEndpoint()
|
|
|
|
|
|
|
|
ep1.network = &network{name: "aaa"}
|
|
|
|
ep2.network = &network{name: "bbb"}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
sKey := sandbox.GenerateKey("sandbox1")
|
2015-06-02 13:22:28 -07:00
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep1); err != nil {
|
2015-06-02 13:22:28 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep2); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-06-02 13:22:28 -07:00
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep1 {
|
2015-06-02 13:22:28 -07:00
|
|
|
t.Fatal("Expected ep1 to be at the top of the heap. But did not find ep1 at the top of the heap")
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
ctrlr.sandboxRm(sKey, ep1)
|
2015-06-02 13:22:28 -07:00
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep2 {
|
2015-06-02 13:22:28 -07: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")
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:41:31 -07:00
|
|
|
ctrlr.sandboxRm(sKey, ep2)
|
|
|
|
|
|
|
|
if err := ctrlr.LeaveAll("sandbox1"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-02 13:22:28 -07:00
|
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
|
|
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
|
|
|
|
}
|
2015-06-11 14:52:24 -07:00
|
|
|
|
|
|
|
sandbox.GC()
|
2015-06-02 13:22:28 -07:00
|
|
|
}
|