2015-07-01 22:00:48 -07:00
|
|
|
package osl
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-04-20 08:44:06 -07:00
|
|
|
import (
|
2015-05-26 21:14:01 +00:00
|
|
|
"os"
|
2015-06-04 20:21:23 -07:00
|
|
|
"runtime"
|
2015-04-20 08:44:06 -07:00
|
|
|
"testing"
|
2015-05-26 21:14:01 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2016-05-16 11:51:40 -07:00
|
|
|
"github.com/docker/libnetwork/ns"
|
2015-09-07 13:33:28 -04:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-04-20 08:44:06 -07: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 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-04 20:21:23 -07: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)
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:32:19 -07:00
|
|
|
s, err := NewSandbox(key, true, false)
|
2015-04-13 18:40:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 20:21:23 -07: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)
|
|
|
|
}
|
|
|
|
|
2016-05-16 11:51:40 -07:00
|
|
|
tbox, err := newInfo(ns.NlHandle(), t)
|
2015-04-24 00:37:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate new sandbox info: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-04 20:21:23 -07:00
|
|
|
for _, i := range tbox.Info().Interfaces() {
|
|
|
|
err = s.AddInterface(i.SrcName(), i.DstName(),
|
2015-06-04 23:45:04 -07:00
|
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
2015-06-04 20:21:23 -07: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-04 20:21:23 -07: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-04 20:21:23 -07: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-04 20:21:23 -07:00
|
|
|
|
2015-06-04 23:45:04 -07:00
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
2015-04-24 00:37:19 +00:00
|
|
|
|
2015-06-11 14:52:24 -07:00
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-05 11:46:33 -07:00
|
|
|
verifyCleanup(t, s, true)
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-04-20 08:44:06 -07:00
|
|
|
|
2015-05-19 22:08:58 +00:00
|
|
|
func TestSandboxCreateTwice(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-04 20:21:23 -07: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)
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:32:19 -07:00
|
|
|
_, err = NewSandbox(key, true, false)
|
2015-05-19 22:08:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 20:21:23 -07: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.
|
2016-06-10 17:32:19 -07:00
|
|
|
s, err := NewSandbox(key, true, false)
|
2015-05-19 22:08:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 20:21:23 -07:00
|
|
|
runtime.LockOSThread()
|
2015-06-11 14:52:24 -07: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 11:46:33 -07:00
|
|
|
func TestSandboxGC(t *testing.T) {
|
|
|
|
key, err := newKey(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:32:19 -07:00
|
|
|
s, err := NewSandbox(key, true, false)
|
2015-06-05 11:46:33 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-11 14:52:24 -07:00
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-05 11:46:33 -07:00
|
|
|
|
|
|
|
GC()
|
|
|
|
verifyCleanup(t, s, false)
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:02:57 +00:00
|
|
|
func TestAddRemoveInterface(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-04 20:21:23 -07: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)
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:32:19 -07:00
|
|
|
s, err := NewSandbox(key, true, false)
|
2015-05-19 03:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
|
|
}
|
2015-06-04 20:21:23 -07: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)
|
|
|
|
}
|
|
|
|
|
2016-05-16 11:51:40 -07:00
|
|
|
tbox, err := newInfo(ns.NlHandle(), t)
|
2015-05-19 03:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to generate new sandbox info: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-04 20:21:23 -07:00
|
|
|
for _, i := range tbox.Info().Interfaces() {
|
|
|
|
err = s.AddInterface(i.SrcName(), i.DstName(),
|
2015-06-04 23:45:04 -07:00
|
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
2015-06-04 20:21:23 -07: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-04 23:45:04 -07:00
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
2015-05-19 03:02:57 +00:00
|
|
|
|
2015-06-04 20:21:23 -07: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-04 23:45:04 -07:00
|
|
|
verifySandbox(t, s, []string{"1", "2"})
|
2015-05-19 03:02:57 +00:00
|
|
|
|
2015-06-04 20:21:23 -07:00
|
|
|
i := tbox.Info().Interfaces()[0]
|
|
|
|
if err := s.AddInterface(i.SrcName(), i.DstName(),
|
2015-06-04 23:45:04 -07:00
|
|
|
tbox.InterfaceOptions().Bridge(i.Bridge()),
|
2015-06-04 20:21:23 -07: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-04 23:45:04 -07:00
|
|
|
verifySandbox(t, s, []string{"1", "2", "3"})
|
2015-05-19 03:02:57 +00:00
|
|
|
|
2015-06-11 14:52:24 -07:00
|
|
|
err = s.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
GC()
|
|
|
|
verifyCleanup(t, s, false)
|
2015-05-19 03:02:57 +00:00
|
|
|
}
|