1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
//go:build linux || freebsd
package integration
import (
"fmt"
. "github.com/containers/podman/v5/test/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
"go.podman.io/storage/pkg/stringid"
)
var _ = Describe("Podman network connect and disconnect", func() {
It("bad network name in disconnect should result in error", func() {
dis := podmanTest.Podman([]string{"network", "disconnect", "foobar", "test"})
dis.WaitWithDefaultTimeout()
Expect(dis).Should(ExitWithError(125, `no container with name or ID "test" found: no such container`))
})
It("bad container name in network disconnect should result in error", func() {
netName := "aliasTest" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
dis := podmanTest.Podman([]string{"network", "disconnect", netName, "foobar"})
dis.WaitWithDefaultTimeout()
Expect(dis).Should(ExitWithError(125, `no container with name or ID "foobar" found: no such container`))
})
It("network disconnect with net mode slirp4netns should result in error", func() {
netName := "slirp" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
session = podmanTest.Podman([]string{"create", "--name", "test", "--network", "slirp4netns", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
con := podmanTest.Podman([]string{"network", "disconnect", netName, "test"})
con.WaitWithDefaultTimeout()
Expect(con).Should(ExitWithError(125, `"slirp4netns" is not supported: invalid network mode`))
})
It("podman network disconnect", func() {
SkipIfRootlessCgroupsV1("stats not supported under rootless CgroupsV1")
netName := "aliasTest" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
gw := podmanTest.Podman([]string{"network", "inspect", netName, "--format", "{{(index .Subnets 0).Gateway}}"})
gw.WaitWithDefaultTimeout()
Expect(gw).Should(ExitCleanly())
ns := gw.OutputToString()
ctr := podmanTest.Podman([]string{"run", "-dt", "--name", "test", "--network", netName, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(ExitCleanly())
exec := podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth0"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitCleanly())
exec2 := podmanTest.Podman([]string{"exec", "test", "cat", "/etc/resolv.conf"})
exec2.WaitWithDefaultTimeout()
Expect(exec2).Should(ExitCleanly())
Expect(exec2.OutputToString()).To(ContainSubstring(ns))
dis := podmanTest.Podman([]string{"network", "disconnect", netName, "test"})
dis.WaitWithDefaultTimeout()
Expect(dis).Should(ExitCleanly())
Expect(dis.ErrorToString()).Should(Equal(""))
inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(ExitCleanly())
Expect(inspect.OutputToString()).To(Equal("0"))
exec = podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth0"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitWithError(1, "ip: can't find device 'eth0'"))
exec3 := podmanTest.Podman([]string{"exec", "test", "cat", "/etc/resolv.conf"})
exec3.WaitWithDefaultTimeout()
Expect(exec3).Should(ExitCleanly())
Expect(exec3.OutputToString()).ToNot(ContainSubstring(ns))
// make sure stats still works https://github.com/containers/podman/issues/13824
stats := podmanTest.Podman([]string{"stats", "test", "--no-stream"})
stats.WaitWithDefaultTimeout()
Expect(stats).Should(ExitCleanly())
})
It("bad network name in connect should result in error", func() {
session := podmanTest.Podman([]string{"create", "--name", "testContainer", "--network", "bridge", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
dis := podmanTest.Podman([]string{"network", "connect", "nonexistent-network", "testContainer"})
dis.WaitWithDefaultTimeout()
Expect(dis).Should(ExitWithError(125, "unable to find network with name or ID nonexistent-network: network not found"))
})
It("bad container name in network connect should result in error", func() {
netName := "aliasTest" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
dis := podmanTest.Podman([]string{"network", "connect", netName, "foobar"})
dis.WaitWithDefaultTimeout()
Expect(dis).Should(ExitWithError(125, `no container with name or ID "foobar" found: no such container`))
})
It("network connect with net mode slirp4netns should result in error", func() {
netName := "slirp" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
session = podmanTest.Podman([]string{"create", "--name", "test", "--network", "slirp4netns", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
con := podmanTest.Podman([]string{"network", "connect", netName, "test"})
con.WaitWithDefaultTimeout()
Expect(con).Should(ExitWithError(125, `"slirp4netns" is not supported: invalid network mode`))
})
It("podman connect on a container that already is connected to the network should error after init", func() {
netName := "aliasTest" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
ctr := podmanTest.Podman([]string{"create", "--name", "test", "--network", netName, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(ExitCleanly())
cid := ctr.OutputToString()
// network alias container short id is always added and shown in inspect
inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{(index .NetworkSettings.Networks \"" + netName + "\").Aliases}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(ExitCleanly())
Expect(inspect.OutputToString()).To(Equal("[" + cid[0:12] + "]"))
con := podmanTest.Podman([]string{"network", "connect", netName, "test"})
con.WaitWithDefaultTimeout()
Expect(con).Should(ExitCleanly())
init := podmanTest.Podman([]string{"init", "test"})
init.WaitWithDefaultTimeout()
Expect(init).Should(ExitCleanly())
con2 := podmanTest.Podman([]string{"network", "connect", netName, "test"})
con2.WaitWithDefaultTimeout()
if podmanTest.DatabaseBackend == "boltdb" {
Expect(con2).Should(ExitWithError(125, fmt.Sprintf("container %s is already connected to network %q: network is already connected", cid, netName)))
} else {
Expect(con2).Should(ExitWithError(125, fmt.Sprintf("container %s is already connected to network %s: network is already connected", cid, netName)))
}
})
It("podman network connect", func() {
SkipIfRootlessCgroupsV1("stats not supported under rootless CgroupsV1")
netName := "aliasTest" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
ctr := podmanTest.Podman([]string{"run", "-dt", "--name", "test", "--network", netName, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(ExitCleanly())
cid := ctr.OutputToString()
exec := podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth0"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitCleanly())
// Create a second network
newNetName := "aliasTest" + stringid.GenerateRandomID()
session = podmanTest.Podman([]string{"network", "create", newNetName, "--subnet", "10.11.100.0/24"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(newNetName)
gw := podmanTest.Podman([]string{"network", "inspect", newNetName, "--format", "{{(index .Subnets 0).Gateway}}"})
gw.WaitWithDefaultTimeout()
Expect(gw).Should(ExitCleanly())
ns := gw.OutputToString()
exec2 := podmanTest.Podman([]string{"exec", "test", "cat", "/etc/resolv.conf"})
exec2.WaitWithDefaultTimeout()
Expect(exec2).Should(ExitCleanly())
Expect(exec2.OutputToString()).ToNot(ContainSubstring(ns))
ip := "10.11.100.99"
mac := "44:11:44:11:44:11"
connect := podmanTest.Podman([]string{"network", "connect", "--ip", ip, "--mac-address", mac, newNetName, "test"})
connect.WaitWithDefaultTimeout()
Expect(connect).Should(ExitCleanly())
Expect(connect.ErrorToString()).Should(Equal(""))
inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(ExitCleanly())
Expect(inspect.OutputToString()).To(Equal("2"))
// network alias container short id is always added and shown in inspect
inspect = podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{(index .NetworkSettings.Networks \"" + newNetName + "\").Aliases}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(ExitCleanly())
Expect(inspect.OutputToString()).To(Equal("[" + cid[0:12] + "]"))
exec = podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth1"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitCleanly())
Expect(exec.OutputToString()).Should(ContainSubstring(ip))
Expect(exec.OutputToString()).Should(ContainSubstring(mac))
exec3 := podmanTest.Podman([]string{"exec", "test", "cat", "/etc/resolv.conf"})
exec3.WaitWithDefaultTimeout()
Expect(exec3).Should(ExitCleanly())
Expect(exec3.OutputToString()).To(ContainSubstring(ns))
// make sure stats works https://github.com/containers/podman/issues/13824
stats := podmanTest.Podman([]string{"stats", "test", "--no-stream"})
stats.WaitWithDefaultTimeout()
Expect(stats).Should(ExitCleanly())
// make sure no logrus errors are shown https://github.com/containers/podman/issues/9602
rm := podmanTest.Podman([]string{"rm", "--time=0", "-f", "test"})
rm.WaitWithDefaultTimeout()
Expect(rm).Should(ExitCleanly())
Expect(rm.ErrorToString()).To(Equal(""))
})
It("podman network connect when not running", func() {
netName1 := "connect1" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName1})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName1)
netName2 := "connect2" + stringid.GenerateRandomID()
session = podmanTest.Podman([]string{"network", "create", netName2})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName2)
ctr := podmanTest.Podman([]string{"create", "--name", "test", "--network", netName1, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(ExitCleanly())
dis := podmanTest.Podman([]string{"network", "connect", netName2, "test"})
dis.WaitWithDefaultTimeout()
Expect(dis).Should(ExitCleanly())
inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(ExitCleanly())
Expect(inspect.OutputToString()).To(Equal("2"))
start := podmanTest.Podman([]string{"start", "test"})
start.WaitWithDefaultTimeout()
Expect(start).Should(ExitCleanly())
exec := podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth0"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitCleanly())
exec = podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth1"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitCleanly())
})
It("podman network connect and run with network ID", func() {
netName := "ID" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.ID}}", "--filter", "name=" + netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
netID := session.OutputToString()
ctr := podmanTest.Podman([]string{"run", "-dt", "--name", "test", "--network", netID, "--network-alias", "somealias", ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(ExitCleanly())
exec := podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth0"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitCleanly())
// Create a second network
newNetName := "ID2" + stringid.GenerateRandomID()
session = podmanTest.Podman([]string{"network", "create", newNetName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(newNetName)
session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.ID}}", "--filter", "name=" + newNetName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
newNetID := session.OutputToString()
connect := podmanTest.Podman([]string{"network", "connect", "--alias", "secondalias", newNetID, "test"})
connect.WaitWithDefaultTimeout()
Expect(connect).Should(ExitCleanly())
inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{.NetworkSettings.Networks}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(ExitCleanly())
Expect(inspect.OutputToString()).To(ContainSubstring(netName))
Expect(inspect.OutputToString()).To(ContainSubstring(newNetName))
exec = podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth1"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitCleanly())
})
It("podman network disconnect when not running", func() {
netName1 := "aliasTest" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName1})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName1)
netName2 := "aliasTest" + stringid.GenerateRandomID()
session2 := podmanTest.Podman([]string{"network", "create", netName2})
session2.WaitWithDefaultTimeout()
Expect(session2).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName2)
ctr := podmanTest.Podman([]string{"create", "--name", "test", "--network", netName1 + "," + netName2, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(ExitCleanly())
dis := podmanTest.Podman([]string{"network", "disconnect", netName1, "test"})
dis.WaitWithDefaultTimeout()
Expect(dis).Should(ExitCleanly())
inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(ExitCleanly())
Expect(inspect.OutputToString()).To(Equal("1"))
start := podmanTest.Podman([]string{"start", "test"})
start.WaitWithDefaultTimeout()
Expect(start).Should(ExitCleanly())
exec := podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth0"})
exec.WaitWithDefaultTimeout()
// because the network interface order is not guaranteed to be the same, we have to check both eth0 and eth1.
// if eth0 did not exist, eth1 has to exist.
var exitMatcher types.GomegaMatcher = ExitWithError(1, "ip: can't find device 'eth1'")
if exec.ExitCode() > 0 {
Expect(exec).To(ExitWithError(1, "ip: can't find device 'eth0'"))
exitMatcher = ExitCleanly()
}
exec = podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth1"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(exitMatcher)
})
It("podman network disconnect and run with network ID", func() {
netName := "aliasTest" + stringid.GenerateRandomID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
defer podmanTest.removeNetwork(netName)
session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.ID}}", "--filter", "name=" + netName})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
netID := session.OutputToString()
ctr := podmanTest.Podman([]string{"run", "-dt", "--name", "test", "--network", netID, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(ExitCleanly())
exec := podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth0"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitCleanly())
dis := podmanTest.Podman([]string{"network", "disconnect", netID, "test"})
dis.WaitWithDefaultTimeout()
Expect(dis).Should(ExitCleanly())
inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(ExitCleanly())
Expect(inspect.OutputToString()).To(Equal("0"))
exec = podmanTest.Podman([]string{"exec", "test", "ip", "addr", "show", "eth0"})
exec.WaitWithDefaultTimeout()
Expect(exec).Should(ExitWithError(1, "ip: can't find device 'eth0'"))
})
})
|