github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/network_create_linux_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "net" 22 "strings" 23 "testing" 24 25 "github.com/containerd/nerdctl/pkg/testutil" 26 "gotest.tools/v3/assert" 27 ) 28 29 func TestNetworkCreateWithMTU(t *testing.T) { 30 testNetwork := testutil.Identifier(t) 31 base := testutil.NewBase(t) 32 33 args := []string{ 34 "network", "create", testNetwork, 35 "--driver", "bridge", "--opt", "com.docker.network.driver.mtu=9216", 36 } 37 base.Cmd(args...).AssertOK() 38 defer base.Cmd("network", "rm", testNetwork).AssertOK() 39 40 base.Cmd("run", "--rm", "--net", testNetwork, testutil.AlpineImage, "ifconfig", "eth0").AssertOutContains("MTU:9216") 41 } 42 43 func TestNetworkCreate(t *testing.T) { 44 base := testutil.NewBase(t) 45 testNetwork := testutil.Identifier(t) 46 47 base.Cmd("network", "create", testNetwork).AssertOK() 48 defer base.Cmd("network", "rm", testNetwork).AssertOK() 49 50 net := base.InspectNetwork(testNetwork) 51 assert.Equal(t, len(net.IPAM.Config), 1) 52 53 base.Cmd("run", "--rm", "--net", testNetwork, testutil.CommonImage, "ip", "route").AssertOutContains(net.IPAM.Config[0].Subnet) 54 55 base.Cmd("network", "create", testNetwork+"-1").AssertOK() 56 defer base.Cmd("network", "rm", testNetwork+"-1").AssertOK() 57 58 base.Cmd("run", "--rm", "--net", testNetwork+"-1", testutil.CommonImage, "ip", "route").AssertNoOut(net.IPAM.Config[0].Subnet) 59 } 60 61 func TestNetworkCreateIPv6(t *testing.T) { 62 base := testutil.NewBaseWithIPv6Compatible(t) 63 testNetwork := testutil.Identifier(t) 64 65 subnetStr := "2001:db8:8::/64" 66 _, subnet, err := net.ParseCIDR(subnetStr) 67 assert.Assert(t, err == nil) 68 69 base.Cmd("network", "create", "--ipv6", "--subnet", subnetStr, testNetwork).AssertOK() 70 t.Cleanup(func() { 71 base.Cmd("network", "rm", testNetwork).Run() 72 }) 73 74 base.Cmd("run", "--rm", "--net", testNetwork, testutil.CommonImage, "ip", "addr", "show", "dev", "eth0").AssertOutWithFunc(func(stdout string) error { 75 ip := findIPv6(stdout) 76 if subnet.Contains(ip) { 77 return nil 78 } 79 return fmt.Errorf("expected subnet %s include ip %s", subnet, ip) 80 }) 81 } 82 83 func findIPv6(output string) net.IP { 84 var ipv6 string 85 lines := strings.Split(output, "\n") 86 for _, line := range lines { 87 if strings.Contains(line, "inet6") { 88 fields := strings.Fields(line) 89 if len(fields) > 1 { 90 ipv6 = strings.Split(fields[1], "/")[0] 91 break 92 } 93 } 94 } 95 return net.ParseIP(ipv6) 96 }