github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/libnetwork/osl/sandbox_test.go (about)

     1  package osl
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/pkg/reexec"
     9  	"github.com/docker/libnetwork/ns"
    10  	"github.com/docker/libnetwork/testutils"
    11  )
    12  
    13  func TestMain(m *testing.M) {
    14  	if reexec.Init() {
    15  		return
    16  	}
    17  	os.Exit(m.Run())
    18  }
    19  
    20  func TestSandboxCreate(t *testing.T) {
    21  	defer testutils.SetupTestOSContext(t)()
    22  
    23  	key, err := newKey(t)
    24  	if err != nil {
    25  		t.Fatalf("Failed to obtain a key: %v", err)
    26  	}
    27  
    28  	s, err := NewSandbox(key, true, false)
    29  	if err != nil {
    30  		t.Fatalf("Failed to create a new sandbox: %v", err)
    31  	}
    32  
    33  	if s.Key() != key {
    34  		t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
    35  	}
    36  
    37  	tbox, err := newInfo(ns.NlHandle(), t)
    38  	if err != nil {
    39  		t.Fatalf("Failed to generate new sandbox info: %v", err)
    40  	}
    41  
    42  	for _, i := range tbox.Info().Interfaces() {
    43  		err = s.AddInterface(i.SrcName(), i.DstName(),
    44  			tbox.InterfaceOptions().Bridge(i.Bridge()),
    45  			tbox.InterfaceOptions().Address(i.Address()),
    46  			tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
    47  		if err != nil {
    48  			t.Fatalf("Failed to add interfaces to sandbox: %v", err)
    49  		}
    50  	}
    51  
    52  	err = s.SetGateway(tbox.Info().Gateway())
    53  	if err != nil {
    54  		t.Fatalf("Failed to set gateway to sandbox: %v", err)
    55  	}
    56  
    57  	err = s.SetGatewayIPv6(tbox.Info().GatewayIPv6())
    58  	if err != nil {
    59  		t.Fatalf("Failed to set ipv6 gateway to sandbox: %v", err)
    60  	}
    61  
    62  	verifySandbox(t, s, []string{"0", "1", "2"})
    63  
    64  	err = s.Destroy()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	verifyCleanup(t, s, true)
    69  }
    70  
    71  func TestSandboxCreateTwice(t *testing.T) {
    72  	defer testutils.SetupTestOSContext(t)()
    73  
    74  	key, err := newKey(t)
    75  	if err != nil {
    76  		t.Fatalf("Failed to obtain a key: %v", err)
    77  	}
    78  
    79  	_, err = NewSandbox(key, true, false)
    80  	if err != nil {
    81  		t.Fatalf("Failed to create a new sandbox: %v", err)
    82  	}
    83  	runtime.LockOSThread()
    84  
    85  	// Create another sandbox with the same key to see if we handle it
    86  	// gracefully.
    87  	s, err := NewSandbox(key, true, false)
    88  	if err != nil {
    89  		t.Fatalf("Failed to create a new sandbox: %v", err)
    90  	}
    91  	runtime.LockOSThread()
    92  
    93  	err = s.Destroy()
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	GC()
    98  	verifyCleanup(t, s, false)
    99  }
   100  
   101  func TestSandboxGC(t *testing.T) {
   102  	key, err := newKey(t)
   103  	if err != nil {
   104  		t.Fatalf("Failed to obtain a key: %v", err)
   105  	}
   106  
   107  	s, err := NewSandbox(key, true, false)
   108  	if err != nil {
   109  		t.Fatalf("Failed to create a new sandbox: %v", err)
   110  	}
   111  
   112  	err = s.Destroy()
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	GC()
   118  	verifyCleanup(t, s, false)
   119  }
   120  
   121  func TestAddRemoveInterface(t *testing.T) {
   122  	defer testutils.SetupTestOSContext(t)()
   123  
   124  	key, err := newKey(t)
   125  	if err != nil {
   126  		t.Fatalf("Failed to obtain a key: %v", err)
   127  	}
   128  
   129  	s, err := NewSandbox(key, true, false)
   130  	if err != nil {
   131  		t.Fatalf("Failed to create a new sandbox: %v", err)
   132  	}
   133  	runtime.LockOSThread()
   134  
   135  	if s.Key() != key {
   136  		t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
   137  	}
   138  
   139  	tbox, err := newInfo(ns.NlHandle(), t)
   140  	if err != nil {
   141  		t.Fatalf("Failed to generate new sandbox info: %v", err)
   142  	}
   143  
   144  	for _, i := range tbox.Info().Interfaces() {
   145  		err = s.AddInterface(i.SrcName(), i.DstName(),
   146  			tbox.InterfaceOptions().Bridge(i.Bridge()),
   147  			tbox.InterfaceOptions().Address(i.Address()),
   148  			tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
   149  		if err != nil {
   150  			t.Fatalf("Failed to add interfaces to sandbox: %v", err)
   151  		}
   152  	}
   153  
   154  	verifySandbox(t, s, []string{"0", "1", "2"})
   155  
   156  	interfaces := s.Info().Interfaces()
   157  	if err := interfaces[0].Remove(); err != nil {
   158  		t.Fatalf("Failed to remove interfaces from sandbox: %v", err)
   159  	}
   160  
   161  	verifySandbox(t, s, []string{"1", "2"})
   162  
   163  	i := tbox.Info().Interfaces()[0]
   164  	if err := s.AddInterface(i.SrcName(), i.DstName(),
   165  		tbox.InterfaceOptions().Bridge(i.Bridge()),
   166  		tbox.InterfaceOptions().Address(i.Address()),
   167  		tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6())); err != nil {
   168  		t.Fatalf("Failed to add interfaces to sandbox: %v", err)
   169  	}
   170  
   171  	verifySandbox(t, s, []string{"1", "2", "3"})
   172  
   173  	err = s.Destroy()
   174  	if err != nil {
   175  		t.Fatal(err)
   176  	}
   177  
   178  	GC()
   179  	verifyCleanup(t, s, false)
   180  }