mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
4ceec05f1b
Added support to add a bridge the same way as any other interface into the namespace. The only difference is linux does not support creating the bridge in one namespace and moving it into another namespace. So for a bridge the sandbox code also does the creation of the bridge inside the sandbox. Also added an optional argument to interface which can now select one of the already existing interfaces as it's master. For this option to succeed the master interface should be of type bridge. Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
172 lines
3.8 KiB
Go
172 lines
3.8 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
"github.com/docker/libnetwork/netutils"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
if reexec.Init() {
|
|
return
|
|
}
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func TestSandboxCreate(t *testing.T) {
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
key, err := newKey(t)
|
|
if err != nil {
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
}
|
|
|
|
s, err := NewSandbox(key, true)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
|
|
if s.Key() != key {
|
|
t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
|
|
}
|
|
|
|
tbox, err := newInfo(t)
|
|
if err != nil {
|
|
t.Fatalf("Failed to generate new sandbox info: %v", err)
|
|
}
|
|
|
|
for _, i := range tbox.Info().Interfaces() {
|
|
err = s.AddInterface(i.SrcName(), i.DstName(),
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
|
tbox.InterfaceOptions().Address(i.Address()),
|
|
tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
|
|
if err != nil {
|
|
t.Fatalf("Failed to add interfaces to sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
}
|
|
|
|
err = s.SetGateway(tbox.Info().Gateway())
|
|
if err != nil {
|
|
t.Fatalf("Failed to set gateway to sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
|
|
err = s.SetGatewayIPv6(tbox.Info().GatewayIPv6())
|
|
if err != nil {
|
|
t.Fatalf("Failed to set ipv6 gateway to sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
|
runtime.LockOSThread()
|
|
|
|
s.Destroy()
|
|
verifyCleanup(t, s, true)
|
|
}
|
|
|
|
func TestSandboxCreateTwice(t *testing.T) {
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
key, err := newKey(t)
|
|
if err != nil {
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
}
|
|
|
|
_, err = NewSandbox(key, true)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
|
|
// Create another sandbox with the same key to see if we handle it
|
|
// gracefully.
|
|
s, err := NewSandbox(key, true)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
s.Destroy()
|
|
}
|
|
|
|
func TestSandboxGC(t *testing.T) {
|
|
key, err := newKey(t)
|
|
if err != nil {
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
}
|
|
|
|
s, err := NewSandbox(key, true)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
|
|
s.Destroy()
|
|
|
|
GC()
|
|
verifyCleanup(t, s, false)
|
|
}
|
|
|
|
func TestAddRemoveInterface(t *testing.T) {
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
key, err := newKey(t)
|
|
if err != nil {
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
}
|
|
|
|
s, err := NewSandbox(key, true)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
|
|
if s.Key() != key {
|
|
t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
|
|
}
|
|
|
|
tbox, err := newInfo(t)
|
|
if err != nil {
|
|
t.Fatalf("Failed to generate new sandbox info: %v", err)
|
|
}
|
|
|
|
for _, i := range tbox.Info().Interfaces() {
|
|
err = s.AddInterface(i.SrcName(), i.DstName(),
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
|
tbox.InterfaceOptions().Address(i.Address()),
|
|
tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
|
|
if err != nil {
|
|
t.Fatalf("Failed to add interfaces to sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
}
|
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
|
runtime.LockOSThread()
|
|
|
|
interfaces := s.Info().Interfaces()
|
|
if err := interfaces[0].Remove(); err != nil {
|
|
t.Fatalf("Failed to remove interfaces from sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
|
|
verifySandbox(t, s, []string{"1", "2"})
|
|
runtime.LockOSThread()
|
|
|
|
i := tbox.Info().Interfaces()[0]
|
|
if err := s.AddInterface(i.SrcName(), i.DstName(),
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
|
tbox.InterfaceOptions().Address(i.Address()),
|
|
tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6())); err != nil {
|
|
t.Fatalf("Failed to add interfaces to sandbox: %v", err)
|
|
}
|
|
runtime.LockOSThread()
|
|
|
|
verifySandbox(t, s, []string{"1", "2", "3"})
|
|
runtime.LockOSThread()
|
|
|
|
s.Destroy()
|
|
}
|