github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/sandbox_test.go (about) 1 package libnetwork 2 3 import ( 4 "fmt" 5 "runtime" 6 "testing" 7 8 "github.com/docker/docker/libnetwork/config" 9 "github.com/docker/docker/libnetwork/ipamapi" 10 "github.com/docker/docker/libnetwork/netlabel" 11 "github.com/docker/docker/libnetwork/options" 12 "github.com/docker/docker/libnetwork/osl" 13 "github.com/docker/docker/libnetwork/testutils" 14 "gotest.tools/v3/skip" 15 ) 16 17 func getTestEnv(t *testing.T, opts ...[]NetworkOption) (NetworkController, []Network) { 18 skip.If(t, runtime.GOOS == "windows", "test only works on linux") 19 20 netType := "bridge" 21 22 option := options.Generic{ 23 "EnableIPForwarding": true, 24 } 25 genericOption := make(map[string]interface{}) 26 genericOption[netlabel.GenericData] = option 27 28 cfgOptions, err := OptionBoltdbWithRandomDBFile() 29 if err != nil { 30 t.Fatal(err) 31 } 32 c, err := New(append(cfgOptions, config.OptionDriverConfig(netType, genericOption))...) 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 if len(opts) == 0 { 38 return c, nil 39 } 40 41 nwList := make([]Network, 0, len(opts)) 42 for i, opt := range opts { 43 name := fmt.Sprintf("test_nw_%d", i) 44 netOption := options.Generic{ 45 netlabel.GenericData: options.Generic{ 46 "BridgeName": name, 47 }, 48 } 49 newOptions := make([]NetworkOption, 1, len(opt)+1) 50 newOptions[0] = NetworkOptionGeneric(netOption) 51 newOptions = append(newOptions, opt...) 52 n, err := c.NewNetwork(netType, name, "", newOptions...) 53 if err != nil { 54 t.Fatal(err) 55 } 56 57 nwList = append(nwList, n) 58 } 59 60 return c, nwList 61 } 62 63 func TestSandboxAddEmpty(t *testing.T) { 64 c, _ := getTestEnv(t) 65 ctrlr := c.(*controller) 66 67 sbx, err := ctrlr.NewSandbox("sandbox0") 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 if err := sbx.Delete(); err != nil { 73 t.Fatal(err) 74 } 75 76 if len(ctrlr.sandboxes) != 0 { 77 t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes)) 78 } 79 80 osl.GC() 81 } 82 83 // // If different priorities are specified, internal option and ipv6 addresses mustn't influence endpoint order 84 func TestSandboxAddMultiPrio(t *testing.T) { 85 if !testutils.IsRunningInContainer() { 86 defer testutils.SetupTestOSContext(t)() 87 } 88 89 opts := [][]NetworkOption{ 90 {NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)}, 91 {NetworkOptionInternalNetwork()}, 92 {}, 93 } 94 95 c, nws := getTestEnv(t, opts...) 96 ctrlr := c.(*controller) 97 98 sbx, err := ctrlr.NewSandbox("sandbox1") 99 if err != nil { 100 t.Fatal(err) 101 } 102 sid := sbx.ID() 103 104 ep1, err := nws[0].CreateEndpoint("ep1") 105 if err != nil { 106 t.Fatal(err) 107 } 108 ep2, err := nws[1].CreateEndpoint("ep2") 109 if err != nil { 110 t.Fatal(err) 111 } 112 ep3, err := nws[2].CreateEndpoint("ep3") 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil { 118 t.Fatal(err) 119 } 120 121 if err := ep2.Join(sbx, JoinOptionPriority(2)); err != nil { 122 t.Fatal(err) 123 } 124 125 if err := ep3.Join(sbx, JoinOptionPriority(3)); err != nil { 126 t.Fatal(err) 127 } 128 129 if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() { 130 t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap") 131 } 132 133 if len(sbx.Endpoints()) != 3 { 134 t.Fatal("Expected 3 endpoints to be connected to the sandbox.") 135 } 136 137 if err := ep3.Leave(sbx); err != nil { 138 t.Fatal(err) 139 } 140 if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() { 141 t.Fatal("Expected ep2 to be at the top of the heap after removing ep3. But did not find ep2 at the top of the heap") 142 } 143 144 if err := ep2.Leave(sbx); err != nil { 145 t.Fatal(err) 146 } 147 if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() { 148 t.Fatal("Expected ep1 to be at the top of the heap after removing ep2. But did not find ep1 at the top of the heap") 149 } 150 151 // Re-add ep3 back 152 if err := ep3.Join(sbx, JoinOptionPriority(3)); err != nil { 153 t.Fatal(err) 154 } 155 156 if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() { 157 t.Fatal("Expected ep3 to be at the top of the heap after adding ep3 back. But did not find ep3 at the top of the heap") 158 } 159 160 if err := sbx.Delete(); err != nil { 161 t.Fatal(err) 162 } 163 164 if len(ctrlr.sandboxes) != 0 { 165 t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes)) 166 } 167 168 osl.GC() 169 } 170 171 func TestSandboxAddSamePrio(t *testing.T) { 172 if !testutils.IsRunningInContainer() { 173 defer testutils.SetupTestOSContext(t)() 174 } 175 176 opts := [][]NetworkOption{ 177 {}, 178 {}, 179 {NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)}, 180 {NetworkOptionInternalNetwork()}, 181 } 182 183 c, nws := getTestEnv(t, opts...) 184 185 ctrlr := c.(*controller) 186 187 sbx, err := ctrlr.NewSandbox("sandbox1") 188 if err != nil { 189 t.Fatal(err) 190 } 191 sid := sbx.ID() 192 193 epNw1, err := nws[1].CreateEndpoint("ep1") 194 if err != nil { 195 t.Fatal(err) 196 } 197 epIPv6, err := nws[2].CreateEndpoint("ep2") 198 if err != nil { 199 t.Fatal(err) 200 } 201 202 epInternal, err := nws[3].CreateEndpoint("ep3") 203 if err != nil { 204 t.Fatal(err) 205 } 206 207 epNw0, err := nws[0].CreateEndpoint("ep4") 208 if err != nil { 209 t.Fatal(err) 210 } 211 212 if err := epNw1.Join(sbx); err != nil { 213 t.Fatal(err) 214 } 215 216 if err := epIPv6.Join(sbx); err != nil { 217 t.Fatal(err) 218 } 219 220 if err := epInternal.Join(sbx); err != nil { 221 t.Fatal(err) 222 } 223 224 if err := epNw0.Join(sbx); err != nil { 225 t.Fatal(err) 226 } 227 228 // order should now be: epIPv6, epNw0, epNw1, epInternal 229 if len(sbx.Endpoints()) != 4 { 230 t.Fatal("Expected 4 endpoints to be connected to the sandbox.") 231 } 232 233 // IPv6 has precedence over IPv4 234 if ctrlr.sandboxes[sid].endpoints[0].ID() != epIPv6.ID() { 235 t.Fatal("Expected epIPv6 to be at the top of the heap. But did not find epIPv6 at the top of the heap") 236 } 237 238 // internal network has lowest precedence 239 if ctrlr.sandboxes[sid].endpoints[3].ID() != epInternal.ID() { 240 t.Fatal("Expected epInternal to be at the bottom of the heap. But did not find epInternal at the bottom of the heap") 241 } 242 243 if err := epIPv6.Leave(sbx); err != nil { 244 t.Fatal(err) 245 } 246 247 // 'test_nw_0' has precedence over 'test_nw_1' 248 if ctrlr.sandboxes[sid].endpoints[0].ID() != epNw0.ID() { 249 t.Fatal("Expected epNw0 to be at the top of the heap after removing epIPv6. But did not find epNw0 at the top of the heap") 250 } 251 252 if err := epNw1.Leave(sbx); err != nil { 253 t.Fatal(err) 254 } 255 256 if err := sbx.Delete(); err != nil { 257 t.Fatal(err) 258 } 259 260 if len(ctrlr.sandboxes) != 0 { 261 t.Fatalf("controller containers is not empty. len = %d", len(ctrlr.sandboxes)) 262 } 263 264 osl.GC() 265 }