2015-07-02 01:00:48 -04:00
|
|
|
package osl
|
2015-04-13 14:40:42 -04:00
|
|
|
|
2015-04-20 11:44:06 -04:00
|
|
|
import (
|
2015-05-26 17:14:01 -04:00
|
|
|
"os"
|
2015-06-04 23:21:23 -04:00
|
|
|
"runtime"
|
2015-04-20 11:44:06 -04:00
|
|
|
"testing"
|
2015-05-26 17:14:01 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-09-07 13:33:28 -04:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-04-20 11:44:06 -04:00
|
|
|
)
|
2015-04-13 14:40:42 -04:00
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
func TestSandboxCreate(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-04 23:21:23 -04:00
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
key, err := newKey(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:29:43 -04:00
|
|
|
s, err := NewSandbox(key, true)
|
2015-04-13 14:40:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-04-13 14:40:42 -04:00
|
|
|
|
|
|
|
if s.Key() != key {
|
|
|
|
t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
|
|
|
|
}
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
tbox, err := newInfo(t)
|
2015-04-23 20:37:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate new sandbox info: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
for _, i := range tbox.Info().Interfaces() {
|
|
|
|
err = s.AddInterface(i.SrcName(), i.DstName(),
|
2015-06-05 02:45:04 -04:00
|
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
2015-06-04 23:21:23 -04:00
|
|
|
tbox.InterfaceOptions().Address(i.Address()),
|
|
|
|
tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
|
2015-04-23 20:37:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to add interfaces to sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-04-23 20:37:19 -04:00
|
|
|
}
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
err = s.SetGateway(tbox.Info().Gateway())
|
2015-04-23 20:37:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to set gateway to sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-04-23 20:37:19 -04:00
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
err = s.SetGatewayIPv6(tbox.Info().GatewayIPv6())
|
2015-04-23 20:37:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to set ipv6 gateway to sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
|
|
|
|
2015-06-05 02:45:04 -04:00
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-04-23 20:37:19 -04:00
|
|
|
|
2015-06-11 17:52:24 -04:00
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-05 14:46:33 -04:00
|
|
|
verifyCleanup(t, s, true)
|
2015-04-13 14:40:42 -04:00
|
|
|
}
|
2015-04-20 11:44:06 -04:00
|
|
|
|
2015-05-19 18:08:58 -04:00
|
|
|
func TestSandboxCreateTwice(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-04 23:21:23 -04:00
|
|
|
|
2015-05-19 18:08:58 -04:00
|
|
|
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)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-19 18:08:58 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-06-11 17:52:24 -04:00
|
|
|
|
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
GC()
|
|
|
|
verifyCleanup(t, s, false)
|
2015-05-19 18:08:58 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 14:46:33 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-06-11 17:52:24 -04:00
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-05 14:46:33 -04:00
|
|
|
|
|
|
|
GC()
|
|
|
|
verifyCleanup(t, s, false)
|
|
|
|
}
|
|
|
|
|
2015-05-18 23:02:57 -04:00
|
|
|
func TestAddRemoveInterface(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-04 23:21:23 -04:00
|
|
|
|
2015-05-18 23:02:57 -04:00
|
|
|
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)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-18 23:02:57 -04:00
|
|
|
|
|
|
|
if s.Key() != key {
|
|
|
|
t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
|
|
|
|
}
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
tbox, err := newInfo(t)
|
2015-05-18 23:02:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate new sandbox info: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
for _, i := range tbox.Info().Interfaces() {
|
|
|
|
err = s.AddInterface(i.SrcName(), i.DstName(),
|
2015-06-05 02:45:04 -04:00
|
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
2015-06-04 23:21:23 -04:00
|
|
|
tbox.InterfaceOptions().Address(i.Address()),
|
|
|
|
tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
|
2015-05-18 23:02:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to add interfaces to sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-18 23:02:57 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 02:45:04 -04:00
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-18 23:02:57 -04:00
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
interfaces := s.Info().Interfaces()
|
|
|
|
if err := interfaces[0].Remove(); err != nil {
|
2015-05-18 23:02:57 -04:00
|
|
|
t.Fatalf("Failed to remove interfaces from sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-18 23:02:57 -04:00
|
|
|
|
2015-06-05 02:45:04 -04:00
|
|
|
verifySandbox(t, s, []string{"1", "2"})
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-18 23:02:57 -04:00
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
i := tbox.Info().Interfaces()[0]
|
|
|
|
if err := s.AddInterface(i.SrcName(), i.DstName(),
|
2015-06-05 02:45:04 -04:00
|
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
2015-06-04 23:21:23 -04:00
|
|
|
tbox.InterfaceOptions().Address(i.Address()),
|
|
|
|
tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6())); err != nil {
|
2015-05-18 23:02:57 -04:00
|
|
|
t.Fatalf("Failed to add interfaces to sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-18 23:02:57 -04:00
|
|
|
|
2015-06-05 02:45:04 -04:00
|
|
|
verifySandbox(t, s, []string{"1", "2", "3"})
|
2015-06-04 23:21:23 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-18 23:02:57 -04:00
|
|
|
|
2015-06-11 17:52:24 -04:00
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
GC()
|
|
|
|
verifyCleanup(t, s, false)
|
2015-05-18 23:02:57 -04:00
|
|
|
}
|