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