mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
1ac72c85cb
Currently container can join one endpoint when it is started. More endpoints can be attached at a later point in time. But when that happens this attachment should only have meaning only as long as the container is alive. The attachment should lose it's meaning when the container goes away. Cuurently there is no way for the container management code to tell libnetwork to detach the container from all attached endpoints. This PR provides an additional API `LeaveAll` which adds this functionality, To facilitate this and make the sanbox lifecycle consistent some slight changes have been made to the behavior of sandbox management code. The sandbox is no longer destroyed when the last endpoint is detached from the container. Instead the sandbox ie kept alive and can only be destroyed with a `LeaveAll` call. This gives better control of sandbox lifecycle by the container management code and the sandbox doesn't get destroyed from under the carpet while the container is still using it. Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
141 lines
3.3 KiB
Go
141 lines
3.3 KiB
Go
package libnetwork
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/docker/libnetwork/sandbox"
|
|
)
|
|
|
|
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()
|
|
|
|
if _, err := ctrlr.sandboxAdd(sandbox.GenerateKey("sandbox1"), true, ep); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ctrlr.sandboxRm(sandbox.GenerateKey("sandbox1"), ep)
|
|
|
|
ctrlr.LeaveAll("sandbox1")
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
|
|
}
|
|
|
|
sandbox.GC()
|
|
}
|
|
|
|
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
|
|
|
|
sKey := sandbox.GenerateKey("sandbox1")
|
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep3); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep3 {
|
|
t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap")
|
|
}
|
|
|
|
ctrlr.sandboxRm(sKey, ep3)
|
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep2 {
|
|
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")
|
|
}
|
|
|
|
ctrlr.sandboxRm(sKey, ep2)
|
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep1 {
|
|
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 := ctrlr.sandboxAdd(sKey, true, ep3); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep3 {
|
|
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")
|
|
}
|
|
|
|
ctrlr.sandboxRm(sKey, ep3)
|
|
ctrlr.sandboxRm(sKey, ep1)
|
|
|
|
if err := ctrlr.LeaveAll("sandbox1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
|
|
}
|
|
|
|
sandbox.GC()
|
|
}
|
|
|
|
func TestSandboxAddSamePrio(t *testing.T) {
|
|
ctrlr := createEmptyCtrlr()
|
|
ep1 := createEmptyEndpoint()
|
|
ep2 := createEmptyEndpoint()
|
|
|
|
ep1.network = &network{name: "aaa"}
|
|
ep2.network = &network{name: "bbb"}
|
|
|
|
sKey := sandbox.GenerateKey("sandbox1")
|
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := ctrlr.sandboxAdd(sKey, true, ep2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep1 {
|
|
t.Fatal("Expected ep1 to be at the top of the heap. But did not find ep1 at the top of the heap")
|
|
}
|
|
|
|
ctrlr.sandboxRm(sKey, ep1)
|
|
|
|
if ctrlr.sandboxes[sKey].endpoints[0] != ep2 {
|
|
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")
|
|
}
|
|
|
|
ctrlr.sandboxRm(sKey, ep2)
|
|
|
|
if err := ctrlr.LeaveAll("sandbox1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
|
|
}
|
|
|
|
sandbox.GC()
|
|
}
|