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