github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/test/e2e/network_create_test.go (about) 1 // +build !remoteclient 2 3 package integration 4 5 import ( 6 "encoding/json" 7 "io/ioutil" 8 "net" 9 "os" 10 "strings" 11 12 cniversion "github.com/containernetworking/cni/pkg/version" 13 "github.com/containers/libpod/pkg/network" 14 . "github.com/containers/libpod/test/utils" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 "github.com/pkg/errors" 18 ) 19 20 var ErrPluginNotFound = errors.New("plugin not found") 21 22 func findPluginByName(plugins interface{}, pluginType string) (interface{}, error) { 23 for _, p := range plugins.([]interface{}) { 24 r := p.(map[string]interface{}) 25 if pluginType == r["type"] { 26 return p, nil 27 } 28 } 29 return nil, errors.Wrap(ErrPluginNotFound, pluginType) 30 } 31 32 func genericPluginsToBridge(plugins interface{}, pluginType string) (network.HostLocalBridge, error) { 33 var bridge network.HostLocalBridge 34 generic, err := findPluginByName(plugins, pluginType) 35 if err != nil { 36 return bridge, err 37 } 38 b, err := json.Marshal(generic) 39 if err != nil { 40 return bridge, err 41 } 42 err = json.Unmarshal(b, &bridge) 43 return bridge, err 44 } 45 46 func genericPluginsToPortMap(plugins interface{}, pluginType string) (network.PortMapConfig, error) { 47 var portMap network.PortMapConfig 48 generic, err := findPluginByName(plugins, "portmap") 49 if err != nil { 50 return portMap, err 51 } 52 b, err := json.Marshal(generic) 53 if err != nil { 54 return portMap, err 55 } 56 err = json.Unmarshal(b, &portMap) 57 return portMap, err 58 } 59 60 func (p *PodmanTestIntegration) removeCNINetwork(name string) { 61 session := p.Podman([]string{"network", "rm", "-f", name}) 62 session.WaitWithDefaultTimeout() 63 Expect(session.ExitCode()).To(BeZero()) 64 } 65 66 func removeNetworkDevice(name string) { 67 session := SystemExec("ip", []string{"link", "delete", name}) 68 session.WaitWithDefaultTimeout() 69 } 70 71 var _ = Describe("Podman network create", func() { 72 var ( 73 tempdir string 74 err error 75 podmanTest *PodmanTestIntegration 76 ) 77 78 BeforeEach(func() { 79 SkipIfRootless() 80 tempdir, err = CreateTempDirInTempDir() 81 if err != nil { 82 os.Exit(1) 83 } 84 podmanTest = PodmanTestCreate(tempdir) 85 podmanTest.Setup() 86 podmanTest.SeedImages() 87 }) 88 89 AfterEach(func() { 90 podmanTest.Cleanup() 91 f := CurrentGinkgoTestDescription() 92 processTestResult(f) 93 }) 94 95 It("podman network create with no input", func() { 96 var result network.NcList 97 98 nc := podmanTest.Podman([]string{"network", "create"}) 99 nc.WaitWithDefaultTimeout() 100 Expect(nc.ExitCode()).To(BeZero()) 101 102 fileContent, err := ioutil.ReadFile(nc.OutputToString()) 103 Expect(err).To(BeNil()) 104 err = json.Unmarshal(fileContent, &result) 105 Expect(err).To(BeNil()) 106 defer podmanTest.removeCNINetwork(result["name"].(string)) 107 Expect(result["cniVersion"]).To(Equal(cniversion.Current())) 108 Expect(strings.HasPrefix(result["name"].(string), "cni-podman")).To(BeTrue()) 109 110 bridgePlugin, err := genericPluginsToBridge(result["plugins"], "bridge") 111 Expect(err).To(BeNil()) 112 portMapPlugin, err := genericPluginsToPortMap(result["plugins"], "portmap") 113 Expect(err).To(BeNil()) 114 115 Expect(bridgePlugin.IPAM.Routes[0].Dest).To(Equal("0.0.0.0/0")) 116 Expect(bridgePlugin.IsGW).To(BeTrue()) 117 Expect(bridgePlugin.IPMasq).To(BeTrue()) 118 Expect(portMapPlugin.Capabilities["portMappings"]).To(BeTrue()) 119 120 }) 121 122 It("podman network create with name", func() { 123 var ( 124 results []network.NcList 125 ) 126 127 nc := podmanTest.Podman([]string{"network", "create", "newname"}) 128 nc.WaitWithDefaultTimeout() 129 Expect(nc.ExitCode()).To(BeZero()) 130 defer podmanTest.removeCNINetwork("newname") 131 132 inspect := podmanTest.Podman([]string{"network", "inspect", "newname"}) 133 inspect.WaitWithDefaultTimeout() 134 135 err := json.Unmarshal([]byte(inspect.OutputToString()), &results) 136 Expect(err).To(BeNil()) 137 result := results[0] 138 Expect(result["name"]).To(Equal("newname")) 139 140 }) 141 142 It("podman network create with name and subnet", func() { 143 var ( 144 results []network.NcList 145 ) 146 nc := podmanTest.Podman([]string{"network", "create", "--subnet", "10.11.12.0/24", "newnetwork"}) 147 nc.WaitWithDefaultTimeout() 148 Expect(nc.ExitCode()).To(BeZero()) 149 150 defer podmanTest.removeCNINetwork("newnetwork") 151 152 // Inspect the network configuration 153 inspect := podmanTest.Podman([]string{"network", "inspect", "newnetwork"}) 154 inspect.WaitWithDefaultTimeout() 155 156 // JSON the network configuration into something usable 157 err := json.Unmarshal([]byte(inspect.OutputToString()), &results) 158 Expect(err).To(BeNil()) 159 result := results[0] 160 Expect(result["name"]).To(Equal("newnetwork")) 161 162 // JSON the bridge info 163 bridgePlugin, err := genericPluginsToBridge(result["plugins"], "bridge") 164 Expect(err).To(BeNil()) 165 166 // Once a container executes a new network, the nic will be created. We should clean those up 167 // best we can 168 defer removeNetworkDevice(bridgePlugin.BrName) 169 170 try := podmanTest.Podman([]string{"run", "-it", "--rm", "--network", "newnetwork", ALPINE, "sh", "-c", "ip addr show eth0 | awk ' /inet / {print $2}'"}) 171 try.WaitWithDefaultTimeout() 172 173 _, subnet, err := net.ParseCIDR("10.11.12.0/24") 174 Expect(err).To(BeNil()) 175 // Note this is an IPv4 test only! 176 containerIP, _, err := net.ParseCIDR(try.OutputToString()) 177 Expect(err).To(BeNil()) 178 // Ensure that the IP the container got is within the subnet the user asked for 179 Expect(subnet.Contains(containerIP)).To(BeTrue()) 180 }) 181 182 It("podman network create with invalid subnet", func() { 183 nc := podmanTest.Podman([]string{"network", "create", "--subnet", "10.11.12.0/17000", "fail"}) 184 nc.WaitWithDefaultTimeout() 185 Expect(nc).To(ExitWithError()) 186 }) 187 188 It("podman network create with invalid IP", func() { 189 nc := podmanTest.Podman([]string{"network", "create", "--subnet", "10.11.0/17000", "fail"}) 190 nc.WaitWithDefaultTimeout() 191 Expect(nc).To(ExitWithError()) 192 }) 193 194 It("podman network create with invalid gateway for subnet", func() { 195 nc := podmanTest.Podman([]string{"network", "create", "--subnet", "10.11.12.0/24", "--gateway", "192.168.1.1", "fail"}) 196 nc.WaitWithDefaultTimeout() 197 Expect(nc).To(ExitWithError()) 198 }) 199 200 It("podman network create two networks with same name should fail", func() { 201 nc := podmanTest.Podman([]string{"network", "create", "samename"}) 202 nc.WaitWithDefaultTimeout() 203 Expect(nc.ExitCode()).To(BeZero()) 204 defer podmanTest.removeCNINetwork("samename") 205 206 ncFail := podmanTest.Podman([]string{"network", "create", "samename"}) 207 ncFail.WaitWithDefaultTimeout() 208 Expect(ncFail).To(ExitWithError()) 209 }) 210 211 It("podman network create with invalid network name", func() { 212 nc := podmanTest.Podman([]string{"network", "create", "foo "}) 213 nc.WaitWithDefaultTimeout() 214 Expect(nc).To(ExitWithError()) 215 }) 216 217 })