mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00

Go 1.10 fixed the problem related to thread and namespaces.
Details:
2595fe7fb6
In few words there is no more the possibility to have a go routine
running on a thread that is another namespace.
In this commit some cleanup is done and the method SetNamespace is
being removed. This will save tons of setns syscall, that were happening
way too frequently possibily to make sure that each operation was being
done in the host namespace.
I suspect that also all the drivers not running in a different
namespace would be able to drop also the lock of the OS Thread but
will address it in a different commit
Removed useless LockOSThreads around
Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>
176 lines
3.8 KiB
Go
176 lines
3.8 KiB
Go
package osl
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
"github.com/docker/libnetwork/ns"
|
|
"github.com/docker/libnetwork/testutils"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
if reexec.Init() {
|
|
return
|
|
}
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func TestSandboxCreate(t *testing.T) {
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
key, err := newKey(t)
|
|
if err != nil {
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
}
|
|
|
|
s, err := NewSandbox(key, true, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
|
|
if s.Key() != key {
|
|
t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
|
|
}
|
|
|
|
tbox, err := newInfo(ns.NlHandle(), 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)
|
|
}
|
|
}
|
|
|
|
err = s.SetGateway(tbox.Info().Gateway())
|
|
if err != nil {
|
|
t.Fatalf("Failed to set gateway to sandbox: %v", err)
|
|
}
|
|
|
|
err = s.SetGatewayIPv6(tbox.Info().GatewayIPv6())
|
|
if err != nil {
|
|
t.Fatalf("Failed to set ipv6 gateway to sandbox: %v", err)
|
|
}
|
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
|
|
|
err = s.Destroy()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
verifyCleanup(t, s, true)
|
|
}
|
|
|
|
func TestSandboxCreateTwice(t *testing.T) {
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
key, err := newKey(t)
|
|
if err != nil {
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
}
|
|
|
|
_, err = NewSandbox(key, true, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
|
|
// Create another sandbox with the same key to see if we handle it
|
|
// gracefully.
|
|
s, err := NewSandbox(key, true, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
|
|
err = s.Destroy()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
GC()
|
|
verifyCleanup(t, s, false)
|
|
}
|
|
|
|
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, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
|
|
err = s.Destroy()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
GC()
|
|
verifyCleanup(t, s, false)
|
|
}
|
|
|
|
func TestAddRemoveInterface(t *testing.T) {
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
key, err := newKey(t)
|
|
if err != nil {
|
|
t.Fatalf("Failed to obtain a key: %v", err)
|
|
}
|
|
|
|
s, err := NewSandbox(key, true, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create a new sandbox: %v", err)
|
|
}
|
|
|
|
if s.Key() != key {
|
|
t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
|
|
}
|
|
|
|
tbox, err := newInfo(ns.NlHandle(), 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)
|
|
}
|
|
}
|
|
|
|
verifySandbox(t, s, []string{"0", "1", "2"})
|
|
|
|
interfaces := s.Info().Interfaces()
|
|
if err := interfaces[0].Remove(); err != nil {
|
|
t.Fatalf("Failed to remove interfaces from sandbox: %v", err)
|
|
}
|
|
|
|
verifySandbox(t, s, []string{"1", "2"})
|
|
|
|
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)
|
|
}
|
|
|
|
verifySandbox(t, s, []string{"1", "2", "3"})
|
|
|
|
err = s.Destroy()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
GC()
|
|
verifyCleanup(t, s, false)
|
|
}
|