github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/test/e2e/network_test.go (about) 1 // +build !remoteclient 2 3 package integration 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "strings" 11 12 . "github.com/containers/libpod/test/utils" 13 "github.com/containers/storage/pkg/stringid" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 func writeConf(conf []byte, confPath string) { 19 if err := ioutil.WriteFile(confPath, conf, 777); err != nil { 20 fmt.Println(err) 21 } 22 } 23 func removeConf(confPath string) { 24 if err := os.Remove(confPath); err != nil { 25 fmt.Println(err) 26 } 27 } 28 29 var _ = Describe("Podman network", func() { 30 var ( 31 tempdir string 32 err error 33 podmanTest *PodmanTestIntegration 34 ) 35 36 BeforeEach(func() { 37 tempdir, err = CreateTempDirInTempDir() 38 if err != nil { 39 os.Exit(1) 40 } 41 podmanTest = PodmanTestCreate(tempdir) 42 podmanTest.Setup() 43 }) 44 45 AfterEach(func() { 46 podmanTest.Cleanup() 47 f := CurrentGinkgoTestDescription() 48 processTestResult(f) 49 50 }) 51 52 var ( 53 secondConf = `{ 54 "cniVersion": "0.3.0", 55 "name": "podman-integrationtest", 56 "plugins": [ 57 { 58 "type": "bridge", 59 "bridge": "cni1", 60 "isGateway": true, 61 "ipMasq": true, 62 "ipam": { 63 "type": "host-local", 64 "subnet": "10.99.0.0/16", 65 "routes": [ 66 { "dst": "0.0.0.0/0" } 67 ] 68 } 69 }, 70 { 71 "type": "portmap", 72 "capabilities": { 73 "portMappings": true 74 } 75 } 76 ] 77 }` 78 cniPath = "/etc/cni/net.d" 79 ) 80 81 It("podman network list", func() { 82 SkipIfRootless() 83 // Setup, use uuid to prevent conflict with other tests 84 uuid := stringid.GenerateNonCryptoID() 85 secondPath := filepath.Join(cniPath, fmt.Sprintf("%s.conflist", uuid)) 86 writeConf([]byte(secondConf), secondPath) 87 defer removeConf(secondPath) 88 89 session := podmanTest.Podman([]string{"network", "ls"}) 90 session.WaitWithDefaultTimeout() 91 Expect(session.ExitCode()).To(Equal(0)) 92 Expect(session.LineInOutputContains("podman-integrationtest")).To(BeTrue()) 93 }) 94 95 It("podman network list -q", func() { 96 SkipIfRootless() 97 // Setup, use uuid to prevent conflict with other tests 98 uuid := stringid.GenerateNonCryptoID() 99 secondPath := filepath.Join(cniPath, fmt.Sprintf("%s.conflist", uuid)) 100 writeConf([]byte(secondConf), secondPath) 101 defer removeConf(secondPath) 102 103 session := podmanTest.Podman([]string{"network", "ls", "--quiet"}) 104 session.WaitWithDefaultTimeout() 105 Expect(session.ExitCode()).To(Equal(0)) 106 Expect(session.LineInOutputContains("podman-integrationtest")).To(BeTrue()) 107 }) 108 109 It("podman network rm no args", func() { 110 SkipIfRootless() 111 session := podmanTest.Podman([]string{"network", "rm"}) 112 session.WaitWithDefaultTimeout() 113 Expect(session.ExitCode()).ToNot(BeZero()) 114 }) 115 116 It("podman network rm", func() { 117 SkipIfRootless() 118 // Setup, use uuid to prevent conflict with other tests 119 uuid := stringid.GenerateNonCryptoID() 120 secondPath := filepath.Join(cniPath, fmt.Sprintf("%s.conflist", uuid)) 121 writeConf([]byte(secondConf), secondPath) 122 defer removeConf(secondPath) 123 124 session := podmanTest.Podman([]string{"network", "ls", "--quiet"}) 125 session.WaitWithDefaultTimeout() 126 Expect(session.ExitCode()).To(Equal(0)) 127 Expect(session.LineInOutputContains("podman-integrationtest")).To(BeTrue()) 128 129 rm := podmanTest.Podman([]string{"network", "rm", "podman-integrationtest"}) 130 rm.WaitWithDefaultTimeout() 131 Expect(rm.ExitCode()).To(BeZero()) 132 133 results := podmanTest.Podman([]string{"network", "ls", "--quiet"}) 134 results.WaitWithDefaultTimeout() 135 Expect(results.ExitCode()).To(Equal(0)) 136 Expect(results.LineInOutputContains("podman-integrationtest")).To(BeFalse()) 137 }) 138 139 It("podman network inspect no args", func() { 140 SkipIfRootless() 141 session := podmanTest.Podman([]string{"network", "inspect"}) 142 session.WaitWithDefaultTimeout() 143 Expect(session.ExitCode()).ToNot(BeZero()) 144 }) 145 146 It("podman network inspect", func() { 147 SkipIfRootless() 148 // Setup, use uuid to prevent conflict with other tests 149 uuid := stringid.GenerateNonCryptoID() 150 secondPath := filepath.Join(cniPath, fmt.Sprintf("%s.conflist", uuid)) 151 writeConf([]byte(secondConf), secondPath) 152 defer removeConf(secondPath) 153 154 session := podmanTest.Podman([]string{"network", "inspect", "podman-integrationtest", "podman"}) 155 session.WaitWithDefaultTimeout() 156 Expect(session.ExitCode()).To(Equal(0)) 157 Expect(session.IsJSONOutputValid()).To(BeTrue()) 158 }) 159 160 It("podman inspect container single CNI network", func() { 161 SkipIfRootless() 162 netName := "testNetSingleCNI" 163 network := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.50.0/24", netName}) 164 network.WaitWithDefaultTimeout() 165 Expect(network.ExitCode()).To(BeZero()) 166 defer podmanTest.removeCNINetwork(netName) 167 168 ctrName := "testCtr" 169 container := podmanTest.Podman([]string{"run", "-dt", "--network", netName, "--name", ctrName, ALPINE, "top"}) 170 container.WaitWithDefaultTimeout() 171 Expect(container.ExitCode()).To(BeZero()) 172 173 inspect := podmanTest.Podman([]string{"inspect", ctrName}) 174 inspect.WaitWithDefaultTimeout() 175 Expect(inspect.ExitCode()).To(BeZero()) 176 conData := inspect.InspectContainerToJSON() 177 Expect(len(conData)).To(Equal(1)) 178 Expect(len(conData[0].NetworkSettings.Networks)).To(Equal(1)) 179 net, ok := conData[0].NetworkSettings.Networks[netName] 180 Expect(ok).To(BeTrue()) 181 Expect(net.NetworkID).To(Equal(netName)) 182 Expect(net.IPPrefixLen).To(Equal(24)) 183 Expect(strings.HasPrefix(net.IPAddress, "10.50.50.")).To(BeTrue()) 184 185 // Necessary to ensure the CNI network is removed cleanly 186 rmAll := podmanTest.Podman([]string{"rm", "-f", ctrName}) 187 rmAll.WaitWithDefaultTimeout() 188 Expect(rmAll.ExitCode()).To(BeZero()) 189 }) 190 191 It("podman inspect container two CNI networks", func() { 192 SkipIfRootless() 193 netName1 := "testNetTwoCNI1" 194 network1 := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.51.0/25", netName1}) 195 network1.WaitWithDefaultTimeout() 196 Expect(network1.ExitCode()).To(BeZero()) 197 defer podmanTest.removeCNINetwork(netName1) 198 199 netName2 := "testNetTwoCNI2" 200 network2 := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.51.128/26", netName2}) 201 network2.WaitWithDefaultTimeout() 202 Expect(network2.ExitCode()).To(BeZero()) 203 defer podmanTest.removeCNINetwork(netName2) 204 205 ctrName := "testCtr" 206 container := podmanTest.Podman([]string{"run", "-dt", "--network", fmt.Sprintf("%s,%s", netName1, netName2), "--name", ctrName, ALPINE, "top"}) 207 container.WaitWithDefaultTimeout() 208 Expect(container.ExitCode()).To(BeZero()) 209 210 inspect := podmanTest.Podman([]string{"inspect", ctrName}) 211 inspect.WaitWithDefaultTimeout() 212 Expect(inspect.ExitCode()).To(BeZero()) 213 conData := inspect.InspectContainerToJSON() 214 Expect(len(conData)).To(Equal(1)) 215 Expect(len(conData[0].NetworkSettings.Networks)).To(Equal(2)) 216 net1, ok := conData[0].NetworkSettings.Networks[netName1] 217 Expect(ok).To(BeTrue()) 218 Expect(net1.NetworkID).To(Equal(netName1)) 219 Expect(net1.IPPrefixLen).To(Equal(25)) 220 Expect(strings.HasPrefix(net1.IPAddress, "10.50.51.")).To(BeTrue()) 221 net2, ok := conData[0].NetworkSettings.Networks[netName2] 222 Expect(ok).To(BeTrue()) 223 Expect(net2.NetworkID).To(Equal(netName2)) 224 Expect(net2.IPPrefixLen).To(Equal(26)) 225 Expect(strings.HasPrefix(net2.IPAddress, "10.50.51.")).To(BeTrue()) 226 227 // Necessary to ensure the CNI network is removed cleanly 228 rmAll := podmanTest.Podman([]string{"rm", "-f", ctrName}) 229 rmAll.WaitWithDefaultTimeout() 230 Expect(rmAll.ExitCode()).To(BeZero()) 231 }) 232 })