podman

Форк
0
/
network_test.go 
739 строк · 28.3 Кб
1
package integration
2

3
import (
4
	"encoding/json"
5
	"fmt"
6
	"path/filepath"
7
	"time"
8

9
	"github.com/containers/podman/v5/pkg/domain/entities"
10
	. "github.com/containers/podman/v5/test/utils"
11
	"github.com/containers/storage/pkg/stringid"
12
	. "github.com/onsi/ginkgo/v2"
13
	. "github.com/onsi/gomega"
14
	. "github.com/onsi/gomega/gexec"
15
)
16

17
var _ = Describe("Podman network", func() {
18

19
	It("podman --cni-config-dir backwards compat", func() {
20
		SkipIfRemote("--cni-config-dir only works locally")
21
		netDir := filepath.Join(podmanTest.TempDir, "networks123")
22
		session := podmanTest.Podman([]string{"--cni-config-dir", netDir, "network", "ls", "--noheading"})
23
		session.WaitWithDefaultTimeout()
24
		Expect(session).Should(ExitCleanly())
25
		// default network always exists
26
		Expect(session.OutputToStringArray()).To(HaveLen(1))
27
	})
28

29
	It("podman network list", func() {
30
		name, path := generateNetworkConfig(podmanTest)
31
		defer removeConf(path)
32

33
		session := podmanTest.Podman([]string{"network", "ls"})
34
		session.WaitWithDefaultTimeout()
35
		Expect(session).Should(ExitCleanly())
36
		Expect(session.OutputToString()).To(ContainSubstring(name))
37
	})
38

39
	It("podman network list -q", func() {
40
		name, path := generateNetworkConfig(podmanTest)
41
		defer removeConf(path)
42

43
		session := podmanTest.Podman([]string{"network", "ls", "--quiet"})
44
		session.WaitWithDefaultTimeout()
45
		Expect(session).Should(ExitCleanly())
46
		Expect(session.OutputToString()).To(ContainSubstring(name))
47
	})
48

49
	It("podman network list --filter success", func() {
50
		name, path := generateNetworkConfig(podmanTest)
51
		defer removeConf(path)
52

53
		session := podmanTest.Podman([]string{"network", "ls", "--filter", "driver=bridge"})
54
		session.WaitWithDefaultTimeout()
55
		// Cannot ExitCleanly(): "stat ~/.config/.../*.conflist: ENOENT"
56
		Expect(session).Should(Exit(0))
57
		Expect(session.OutputToString()).To(ContainSubstring(name))
58
	})
59

60
	It("podman network list --filter driver and name", func() {
61
		name, path := generateNetworkConfig(podmanTest)
62
		defer removeConf(path)
63

64
		session := podmanTest.Podman([]string{"network", "ls", "--filter", "driver=bridge", "--filter", "name=" + name})
65
		session.WaitWithDefaultTimeout()
66
		Expect(session).Should(ExitCleanly())
67
		Expect(session.OutputToString()).To(ContainSubstring(name))
68
	})
69

70
	It("podman network list --filter two names", func() {
71
		name1, path1 := generateNetworkConfig(podmanTest)
72
		defer removeConf(path1)
73

74
		name2, path2 := generateNetworkConfig(podmanTest)
75
		defer removeConf(path2)
76

77
		session := podmanTest.Podman([]string{"network", "ls", "--filter", "name=" + name1, "--filter", "name=" + name2})
78
		session.WaitWithDefaultTimeout()
79
		Expect(session).Should(ExitCleanly())
80
		Expect(session.OutputToString()).To(ContainSubstring(name1))
81
		Expect(session.OutputToString()).To(ContainSubstring(name2))
82
	})
83

84
	It("podman network list --filter labels", func() {
85
		net1 := "labelnet" + stringid.GenerateRandomID()
86
		label1 := "testlabel1=abc"
87
		label2 := "abcdef"
88
		session := podmanTest.Podman([]string{"network", "create", "--label", label1, net1})
89
		session.WaitWithDefaultTimeout()
90
		defer podmanTest.removeNetwork(net1)
91
		Expect(session).Should(ExitCleanly())
92

93
		net2 := "labelnet" + stringid.GenerateRandomID()
94
		session = podmanTest.Podman([]string{"network", "create", "--label", label1, "--label", label2, net2})
95
		session.WaitWithDefaultTimeout()
96
		defer podmanTest.removeNetwork(net2)
97
		Expect(session).Should(ExitCleanly())
98

99
		session = podmanTest.Podman([]string{"network", "ls", "--filter", "label=" + label1})
100
		session.WaitWithDefaultTimeout()
101
		Expect(session).Should(ExitCleanly())
102
		Expect(session.OutputToString()).To(ContainSubstring(net1))
103
		Expect(session.OutputToString()).To(ContainSubstring(net2))
104

105
		session = podmanTest.Podman([]string{"network", "ls", "--filter", "label=" + label1, "--filter", "label=" + label2})
106
		session.WaitWithDefaultTimeout()
107
		Expect(session).Should(ExitCleanly())
108
		Expect(session.OutputToString()).ToNot(ContainSubstring(net1))
109
		Expect(session.OutputToString()).To(ContainSubstring(net2))
110
	})
111

112
	It("podman network list --filter invalid value", func() {
113
		net := "net" + stringid.GenerateRandomID()
114
		session := podmanTest.Podman([]string{"network", "create", net})
115
		session.WaitWithDefaultTimeout()
116
		defer podmanTest.removeNetwork(net)
117
		Expect(session).Should(ExitCleanly())
118

119
		session = podmanTest.Podman([]string{"network", "ls", "--filter", "namr=ab"})
120
		session.WaitWithDefaultTimeout()
121
		Expect(session).To(ExitWithError())
122
		Expect(session.ErrorToString()).To(ContainSubstring(`invalid filter "namr"`))
123
	})
124

125
	It("podman network list --filter failure", func() {
126
		name, path := generateNetworkConfig(podmanTest)
127
		defer removeConf(path)
128

129
		session := podmanTest.Podman([]string{"network", "ls", "--filter", "label=abc"})
130
		session.WaitWithDefaultTimeout()
131
		Expect(session).Should(ExitCleanly())
132
		Expect(session.OutputToString()).To(Not(ContainSubstring(name)))
133
	})
134

135
	It("podman network list --filter dangling", func() {
136
		name, path := generateNetworkConfig(podmanTest)
137
		defer removeConf(path)
138

139
		session := podmanTest.Podman([]string{"network", "ls", "--filter", "dangling=true"})
140
		session.WaitWithDefaultTimeout()
141
		Expect(session).Should(ExitCleanly())
142
		Expect(session.OutputToString()).To(ContainSubstring(name))
143

144
		session = podmanTest.Podman([]string{"network", "ls", "--filter", "dangling=false"})
145
		session.WaitWithDefaultTimeout()
146
		Expect(session).Should(ExitCleanly())
147
		Expect(session.OutputToString()).NotTo(ContainSubstring(name))
148

149
		session = podmanTest.Podman([]string{"network", "ls", "--filter", "dangling=foo"})
150
		session.WaitWithDefaultTimeout()
151
		Expect(session).To(ExitWithError())
152
		Expect(session.ErrorToString()).To(ContainSubstring(`invalid dangling filter value "foo"`))
153
	})
154

155
	It("podman network ID test", func() {
156
		net := "networkIDTest"
157
		// the network id should be the sha256 hash of the network name
158
		netID := "6073aefe03cdf8f29be5b23ea9795c431868a3a22066a6290b187691614fee84"
159
		session := podmanTest.Podman([]string{"network", "create", net})
160
		session.WaitWithDefaultTimeout()
161
		defer podmanTest.removeNetwork(net)
162
		Expect(session).Should(ExitCleanly())
163

164
		if podmanTest.NetworkBackend == Netavark {
165
			// netavark uses a different algo for determining the id and it is not repeatable
166
			getid := podmanTest.Podman([]string{"network", "inspect", net, "--format", "{{.ID}}"})
167
			getid.WaitWithDefaultTimeout()
168
			Expect(getid).Should(ExitCleanly())
169
			netID = getid.OutputToString()
170
		}
171
		// Tests Default Table Output
172
		session = podmanTest.Podman([]string{"network", "ls", "--filter", "id=" + netID})
173
		session.WaitWithDefaultTimeout()
174
		Expect(session).Should(ExitCleanly())
175
		expectedTable := "NETWORK ID NAME DRIVER"
176
		Expect(session.OutputToString()).To(ContainSubstring(expectedTable))
177

178
		session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}} {{.ID}}", "--filter", "id=" + netID})
179
		session.WaitWithDefaultTimeout()
180
		Expect(session).Should(ExitCleanly())
181
		Expect(session.OutputToString()).To(ContainSubstring(net + " " + netID[:12]))
182

183
		session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}} {{.ID}}", "--filter", "id=" + netID[:50], "--no-trunc"})
184
		session.WaitWithDefaultTimeout()
185
		Expect(session).Should(ExitCleanly())
186
		Expect(session.OutputToString()).To(ContainSubstring(net + " " + netID))
187

188
		session = podmanTest.Podman([]string{"network", "inspect", netID[:40]})
189
		session.WaitWithDefaultTimeout()
190
		Expect(session).Should(ExitCleanly())
191
		Expect(session.OutputToString()).To(ContainSubstring(net))
192

193
		session = podmanTest.Podman([]string{"network", "inspect", netID[1:]})
194
		session.WaitWithDefaultTimeout()
195
		Expect(session).Should(ExitWithError())
196
		Expect(session.ErrorToString()).To(ContainSubstring("network not found"))
197

198
		session = podmanTest.Podman([]string{"network", "rm", netID})
199
		session.WaitWithDefaultTimeout()
200
		Expect(session).Should(ExitCleanly())
201
	})
202

203
	rmFunc := func(rm string) {
204
		It(fmt.Sprintf("podman network %s no args", rm), func() {
205
			session := podmanTest.Podman([]string{"network", rm})
206
			session.WaitWithDefaultTimeout()
207
			Expect(session).Should(ExitWithError())
208

209
		})
210

211
		It(fmt.Sprintf("podman network %s", rm), func() {
212
			name, path := generateNetworkConfig(podmanTest)
213
			defer removeConf(path)
214

215
			session := podmanTest.Podman([]string{"network", "ls", "--quiet"})
216
			session.WaitWithDefaultTimeout()
217
			Expect(session).Should(ExitCleanly())
218
			Expect(session.OutputToString()).To(ContainSubstring(name))
219

220
			rm := podmanTest.Podman([]string{"network", rm, name})
221
			rm.WaitWithDefaultTimeout()
222
			Expect(rm).Should(ExitCleanly())
223

224
			results := podmanTest.Podman([]string{"network", "ls", "--quiet"})
225
			results.WaitWithDefaultTimeout()
226
			Expect(results).Should(ExitCleanly())
227
			Expect(results.OutputToString()).To(Not(ContainSubstring(name)))
228
		})
229
	}
230

231
	rmFunc("rm")
232
	rmFunc("remove")
233

234
	It("podman network inspect no args", func() {
235
		session := podmanTest.Podman([]string{"network", "inspect"})
236
		session.WaitWithDefaultTimeout()
237
		Expect(session).Should(ExitWithError())
238
	})
239

240
	It("podman network inspect", func() {
241
		name, path := generateNetworkConfig(podmanTest)
242
		defer removeConf(path)
243

244
		expectedNetworks := []string{name}
245
		if !isRootless() {
246
			// rootful image contains "podman/cni/87-podman-bridge.conflist" for "podman" network
247
			expectedNetworks = append(expectedNetworks, "podman")
248
		}
249
		session := podmanTest.Podman(append([]string{"network", "inspect"}, expectedNetworks...))
250
		session.WaitWithDefaultTimeout()
251
		Expect(session).Should(ExitCleanly())
252
		Expect(session.OutputToString()).To(BeValidJSON())
253
	})
254

255
	It("podman network inspect", func() {
256
		name, path := generateNetworkConfig(podmanTest)
257
		defer removeConf(path)
258

259
		session := podmanTest.Podman([]string{"network", "inspect", name, "--format", "{{.Driver}}"})
260
		session.WaitWithDefaultTimeout()
261
		Expect(session).Should(ExitCleanly())
262
		Expect(session.OutputToString()).To(ContainSubstring("bridge"))
263
	})
264

265
	It("podman inspect container single CNI network", func() {
266
		netName := "net-" + stringid.GenerateRandomID()
267
		network := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.50.0/24", netName})
268
		network.WaitWithDefaultTimeout()
269
		defer podmanTest.removeNetwork(netName)
270
		Expect(network).Should(ExitCleanly())
271

272
		ctrName := "testCtr"
273
		container := podmanTest.Podman([]string{"run", "-dt", "--network", netName, "--name", ctrName, ALPINE, "top"})
274
		container.WaitWithDefaultTimeout()
275
		Expect(container).Should(ExitCleanly())
276

277
		inspect := podmanTest.Podman([]string{"inspect", ctrName})
278
		inspect.WaitWithDefaultTimeout()
279
		Expect(inspect).Should(ExitCleanly())
280
		conData := inspect.InspectContainerToJSON()
281
		Expect(conData).To(HaveLen(1))
282
		Expect(conData[0].NetworkSettings.Networks).To(HaveLen(1))
283
		Expect(conData[0].NetworkSettings.Networks).To(HaveKey(netName))
284
		net := conData[0].NetworkSettings.Networks[netName]
285
		Expect(net).To(HaveField("NetworkID", netName))
286
		Expect(net).To(HaveField("IPPrefixLen", 24))
287
		Expect(net.IPAddress).To(HavePrefix("10.50.50."))
288

289
		// Necessary to ensure the CNI network is removed cleanly
290
		rmAll := podmanTest.Podman([]string{"rm", "-t", "0", "-f", ctrName})
291
		rmAll.WaitWithDefaultTimeout()
292
		Expect(rmAll).Should(ExitCleanly())
293
	})
294

295
	It("podman inspect container two CNI networks (container not running)", func() {
296
		netName1 := "net1-" + stringid.GenerateRandomID()
297
		network1 := podmanTest.Podman([]string{"network", "create", netName1})
298
		network1.WaitWithDefaultTimeout()
299
		defer podmanTest.removeNetwork(netName1)
300
		Expect(network1).Should(ExitCleanly())
301

302
		netName2 := "net2-" + stringid.GenerateRandomID()
303
		network2 := podmanTest.Podman([]string{"network", "create", netName2})
304
		network2.WaitWithDefaultTimeout()
305
		defer podmanTest.removeNetwork(netName2)
306
		Expect(network2).Should(ExitCleanly())
307

308
		ctrName := "testCtr"
309
		container := podmanTest.Podman([]string{"create", "--network", fmt.Sprintf("%s,%s", netName1, netName2), "--name", ctrName, ALPINE, "top"})
310
		container.WaitWithDefaultTimeout()
311
		Expect(container).Should(ExitCleanly())
312

313
		inspect := podmanTest.Podman([]string{"inspect", ctrName})
314
		inspect.WaitWithDefaultTimeout()
315
		Expect(inspect).Should(ExitCleanly())
316
		conData := inspect.InspectContainerToJSON()
317
		Expect(conData).To(HaveLen(1))
318
		Expect(conData[0].NetworkSettings.Networks).To(HaveLen(2))
319
		Expect(conData[0].NetworkSettings.Networks).To(HaveKey(netName1))
320
		Expect(conData[0].NetworkSettings.Networks).To(HaveKey(netName2))
321
		net1 := conData[0].NetworkSettings.Networks[netName1]
322
		Expect(net1).To(HaveField("NetworkID", netName1))
323
		net2 := conData[0].NetworkSettings.Networks[netName2]
324
		Expect(net2).To(HaveField("NetworkID", netName2))
325

326
		// Necessary to ensure the CNI network is removed cleanly
327
		rmAll := podmanTest.Podman([]string{"rm", "-t", "0", "-f", ctrName})
328
		rmAll.WaitWithDefaultTimeout()
329
		Expect(rmAll).Should(ExitCleanly())
330
	})
331

332
	It("podman inspect container two CNI networks", func() {
333
		netName1 := "net1-" + stringid.GenerateRandomID()
334
		network1 := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.51.0/25", netName1})
335
		network1.WaitWithDefaultTimeout()
336
		defer podmanTest.removeNetwork(netName1)
337
		Expect(network1).Should(ExitCleanly())
338

339
		netName2 := "net2-" + stringid.GenerateRandomID()
340
		network2 := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.51.128/26", netName2})
341
		network2.WaitWithDefaultTimeout()
342
		defer podmanTest.removeNetwork(netName2)
343
		Expect(network2).Should(ExitCleanly())
344

345
		ctrName := "testCtr"
346
		container := podmanTest.Podman([]string{"run", "-dt", "--network", fmt.Sprintf("%s,%s", netName1, netName2), "--name", ctrName, ALPINE, "top"})
347
		container.WaitWithDefaultTimeout()
348
		Expect(container).Should(ExitCleanly())
349

350
		inspect := podmanTest.Podman([]string{"inspect", ctrName})
351
		inspect.WaitWithDefaultTimeout()
352
		Expect(inspect).Should(ExitCleanly())
353
		conData := inspect.InspectContainerToJSON()
354
		Expect(conData).To(HaveLen(1))
355
		Expect(conData[0].NetworkSettings.Networks).To(HaveLen(2))
356
		Expect(conData[0].NetworkSettings.Networks).To(HaveKey(netName1))
357
		Expect(conData[0].NetworkSettings.Networks).To(HaveKey(netName2))
358
		net1 := conData[0].NetworkSettings.Networks[netName1]
359
		Expect(net1).To(HaveField("NetworkID", netName1))
360
		Expect(net1).To(HaveField("IPPrefixLen", 25))
361
		Expect(net1.IPAddress).To(HavePrefix("10.50.51."))
362
		net2 := conData[0].NetworkSettings.Networks[netName2]
363
		Expect(net2).To(HaveField("NetworkID", netName2))
364
		Expect(net2).To(HaveField("IPPrefixLen", 26))
365
		Expect(net2.IPAddress).To(HavePrefix("10.50.51."))
366

367
		// Necessary to ensure the CNI network is removed cleanly
368
		rmAll := podmanTest.Podman([]string{"rm", "-t", "0", "-f", ctrName})
369
		rmAll.WaitWithDefaultTimeout()
370
		Expect(rmAll).Should(ExitCleanly())
371
	})
372

373
	It("podman network remove after disconnect when container initially created with the network", func() {
374
		container := "test"
375
		network := "foo" + stringid.GenerateRandomID()
376

377
		session := podmanTest.Podman([]string{"network", "create", network})
378
		session.WaitWithDefaultTimeout()
379
		defer podmanTest.removeNetwork(network)
380
		Expect(session).Should(ExitCleanly())
381

382
		session = podmanTest.Podman([]string{"run", "--name", container, "--network", network, "-d", ALPINE, "top"})
383
		session.WaitWithDefaultTimeout()
384
		Expect(session).Should(ExitCleanly())
385

386
		session = podmanTest.Podman([]string{"network", "disconnect", network, container})
387
		session.WaitWithDefaultTimeout()
388
		Expect(session).Should(ExitCleanly())
389

390
		session = podmanTest.Podman([]string{"network", "rm", network})
391
		session.WaitWithDefaultTimeout()
392
		Expect(session).Should(ExitCleanly())
393
	})
394

395
	It("podman network remove bogus", func() {
396
		session := podmanTest.Podman([]string{"network", "rm", "bogus"})
397
		session.WaitWithDefaultTimeout()
398
		Expect(session).Should(Exit(1))
399
	})
400

401
	It("podman network remove --force with pod", func() {
402
		netName := "net-" + stringid.GenerateRandomID()
403
		session := podmanTest.Podman([]string{"network", "create", netName})
404
		session.WaitWithDefaultTimeout()
405
		defer podmanTest.removeNetwork(netName)
406
		Expect(session).Should(ExitCleanly())
407

408
		session = podmanTest.Podman([]string{"pod", "create", "--network", netName})
409
		session.WaitWithDefaultTimeout()
410
		Expect(session).Should(ExitCleanly())
411
		podID := session.OutputToString()
412

413
		session = podmanTest.Podman([]string{"create", "--pod", podID, ALPINE})
414
		session.WaitWithDefaultTimeout()
415
		Expect(session).Should(ExitCleanly())
416

417
		session = podmanTest.Podman([]string{"network", "rm", netName})
418
		session.WaitWithDefaultTimeout()
419
		Expect(session).Should(Exit(2))
420

421
		session = podmanTest.Podman([]string{"network", "rm", "-t", "0", "--force", netName})
422
		session.WaitWithDefaultTimeout()
423
		Expect(session).Should(ExitCleanly())
424

425
		// check if pod is deleted
426
		session = podmanTest.Podman([]string{"pod", "exists", podID})
427
		session.WaitWithDefaultTimeout()
428
		Expect(session).Should(Exit(1))
429

430
		// check if net is deleted
431
		session = podmanTest.Podman([]string{"network", "ls"})
432
		session.WaitWithDefaultTimeout()
433
		Expect(session).Should(ExitCleanly())
434
		Expect(session.OutputToString()).To(Not(ContainSubstring(netName)))
435
	})
436

437
	It("podman network remove with two networks", func() {
438
		netName1 := "net1-" + stringid.GenerateRandomID()
439
		session := podmanTest.Podman([]string{"network", "create", netName1})
440
		session.WaitWithDefaultTimeout()
441
		defer podmanTest.removeNetwork(netName1)
442
		Expect(session).Should(ExitCleanly())
443

444
		netName2 := "net2-" + stringid.GenerateRandomID()
445
		session = podmanTest.Podman([]string{"network", "create", netName2})
446
		session.WaitWithDefaultTimeout()
447
		defer podmanTest.removeNetwork(netName2)
448
		Expect(session).Should(ExitCleanly())
449

450
		session = podmanTest.Podman([]string{"network", "rm", netName1, netName2})
451
		session.WaitWithDefaultTimeout()
452
		Expect(session).Should(ExitCleanly())
453
		lines := session.OutputToStringArray()
454
		Expect(lines[0]).To(Equal(netName1))
455
		Expect(lines[1]).To(Equal(netName2))
456
	})
457

458
	It("podman network with multiple aliases", func() {
459
		var worked bool
460
		netName := createNetworkName("aliasTest")
461
		session := podmanTest.Podman([]string{"network", "create", netName})
462
		session.WaitWithDefaultTimeout()
463
		defer podmanTest.removeNetwork(netName)
464
		Expect(session).Should(ExitCleanly())
465

466
		interval := 250 * time.Millisecond
467
		for i := 0; i < 6; i++ {
468
			n := podmanTest.Podman([]string{"network", "exists", netName})
469
			n.WaitWithDefaultTimeout()
470
			worked = n.ExitCode() == 0
471
			if worked {
472
				break
473
			}
474
			time.Sleep(interval)
475
			interval *= 2
476
		}
477

478
		top := podmanTest.Podman([]string{"run", "-dt", "--name=web", "--network=" + netName, "--network-alias=web1", "--network-alias=web2", NGINX_IMAGE})
479
		top.WaitWithDefaultTimeout()
480
		Expect(top).Should(ExitCleanly())
481
		interval = 250 * time.Millisecond
482
		// Wait for the nginx service to be running
483
		for i := 0; i < 6; i++ {
484
			// Test curl against the container's name
485
			c1 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, NGINX_IMAGE, "curl", "web"})
486
			c1.WaitWithDefaultTimeout()
487
			worked = c1.ExitCode() == 0
488
			if worked {
489
				break
490
			}
491
			time.Sleep(interval)
492
			interval *= 2
493
		}
494
		Expect(worked).To(BeTrue(), "nginx came up")
495

496
		// Nginx is now running so no need to do a loop
497
		// Test against the first alias
498
		c2 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, NGINX_IMAGE, "curl", "-s", "web1"})
499
		c2.WaitWithDefaultTimeout()
500
		Expect(c2).Should(ExitCleanly())
501

502
		// Test against the second alias
503
		c3 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, NGINX_IMAGE, "curl", "-s", "web2"})
504
		c3.WaitWithDefaultTimeout()
505
		Expect(c3).Should(ExitCleanly())
506
	})
507

508
	It("podman network create/remove macvlan", func() {
509
		net := "macvlan" + stringid.GenerateRandomID()
510
		nc := podmanTest.Podman([]string{"network", "create", "--macvlan", "lo", net})
511
		nc.WaitWithDefaultTimeout()
512
		defer podmanTest.removeNetwork(net)
513
		// Cannot ExitCleanly(): "The --macvlan option is deprecated..."
514
		Expect(nc).Should(Exit(0))
515

516
		nc = podmanTest.Podman([]string{"network", "rm", net})
517
		nc.WaitWithDefaultTimeout()
518
		Expect(nc).Should(ExitCleanly())
519
	})
520

521
	It("podman network create/remove macvlan as driver (-d) no device name", func() {
522
		net := "macvlan" + stringid.GenerateRandomID()
523
		nc := podmanTest.Podman([]string{"network", "create", "-d", "macvlan", net})
524
		nc.WaitWithDefaultTimeout()
525
		defer podmanTest.removeNetwork(net)
526
		Expect(nc).Should(ExitCleanly())
527

528
		inspect := podmanTest.Podman([]string{"network", "inspect", net})
529
		inspect.WaitWithDefaultTimeout()
530
		Expect(inspect).Should(ExitCleanly())
531

532
		// JSON the network configuration into something usable
533
		var results []entities.NetworkInspectReport
534
		err := json.Unmarshal([]byte(inspect.OutputToString()), &results)
535
		Expect(err).ToNot(HaveOccurred())
536
		Expect(results).To(HaveLen(1))
537
		result := results[0]
538
		Expect(result).To(HaveField("NetworkInterface", ""))
539
		Expect(result.IPAMOptions).To(HaveKeyWithValue("driver", "dhcp"))
540

541
		nc = podmanTest.Podman([]string{"network", "rm", net})
542
		nc.WaitWithDefaultTimeout()
543
		Expect(nc).Should(ExitCleanly())
544
	})
545

546
	for _, opt := range []string{"-o=parent=lo", "--interface-name=lo"} {
547
		opt := opt
548
		It(fmt.Sprintf("podman network create/remove macvlan as driver (-d) with %s", opt), func() {
549
			net := "macvlan" + stringid.GenerateRandomID()
550
			nc := podmanTest.Podman([]string{"network", "create", "-d", "macvlan", opt, net})
551
			nc.WaitWithDefaultTimeout()
552
			defer podmanTest.removeNetwork(net)
553
			Expect(nc).Should(ExitCleanly())
554

555
			inspect := podmanTest.Podman([]string{"network", "inspect", net})
556
			inspect.WaitWithDefaultTimeout()
557
			Expect(inspect).Should(ExitCleanly())
558

559
			var results []entities.NetworkInspectReport
560
			err := json.Unmarshal([]byte(inspect.OutputToString()), &results)
561
			Expect(err).ToNot(HaveOccurred())
562
			Expect(results).To(HaveLen(1))
563
			result := results[0]
564

565
			Expect(result).To(HaveField("Driver", "macvlan"))
566
			Expect(result).To(HaveField("NetworkInterface", "lo"))
567
			Expect(result.IPAMOptions).To(HaveKeyWithValue("driver", "dhcp"))
568
			Expect(result.Subnets).To(BeEmpty())
569

570
			nc = podmanTest.Podman([]string{"network", "rm", net})
571
			nc.WaitWithDefaultTimeout()
572
			Expect(nc).Should(ExitCleanly())
573
		})
574
	}
575

576
	It("podman network create/remove ipvlan as driver (-d) with device name", func() {
577
		net := "ipvlan" + stringid.GenerateRandomID()
578
		nc := podmanTest.Podman([]string{"network", "create", "-d", "ipvlan", "-o", "parent=lo", "--subnet", "10.0.2.0/24", net})
579
		nc.WaitWithDefaultTimeout()
580
		defer podmanTest.removeNetwork(net)
581
		Expect(nc).Should(ExitCleanly())
582

583
		inspect := podmanTest.Podman([]string{"network", "inspect", net})
584
		inspect.WaitWithDefaultTimeout()
585
		Expect(inspect).Should(ExitCleanly())
586

587
		var results []entities.NetworkInspectReport
588
		err := json.Unmarshal([]byte(inspect.OutputToString()), &results)
589
		Expect(err).ToNot(HaveOccurred())
590
		Expect(results).To(HaveLen(1))
591
		result := results[0]
592

593
		Expect(result).To(HaveField("Driver", "ipvlan"))
594
		Expect(result).To(HaveField("NetworkInterface", "lo"))
595
		Expect(result.IPAMOptions).To(HaveKeyWithValue("driver", "host-local"))
596
		Expect(result.Subnets).To(HaveLen(1))
597

598
		nc = podmanTest.Podman([]string{"network", "rm", net})
599
		nc.WaitWithDefaultTimeout()
600
		Expect(nc).Should(ExitCleanly())
601
	})
602

603
	It("podman network exists", func() {
604
		net := "net" + stringid.GenerateRandomID()
605
		session := podmanTest.Podman([]string{"network", "create", net})
606
		session.WaitWithDefaultTimeout()
607
		defer podmanTest.removeNetwork(net)
608
		Expect(session).Should(ExitCleanly())
609

610
		session = podmanTest.Podman([]string{"network", "exists", net})
611
		session.WaitWithDefaultTimeout()
612
		Expect(session).Should(ExitCleanly())
613

614
		session = podmanTest.Podman([]string{"network", "exists", stringid.GenerateRandomID()})
615
		session.WaitWithDefaultTimeout()
616
		Expect(session).Should(Exit(1))
617
	})
618

619
	It("podman network create macvlan with network info and options", func() {
620
		net := "macvlan" + stringid.GenerateRandomID()
621
		nc := podmanTest.Podman([]string{"network", "create", "-d", "macvlan", "-o", "parent=lo", "-o", "mtu=1500", "--gateway", "192.168.1.254", "--subnet", "192.168.1.0/24", net})
622
		nc.WaitWithDefaultTimeout()
623
		defer podmanTest.removeNetwork(net)
624
		Expect(nc).Should(ExitCleanly())
625

626
		inspect := podmanTest.Podman([]string{"network", "inspect", net})
627
		inspect.WaitWithDefaultTimeout()
628
		Expect(inspect).Should(ExitCleanly())
629

630
		var results []entities.NetworkInspectReport
631
		err := json.Unmarshal([]byte(inspect.OutputToString()), &results)
632
		Expect(err).ToNot(HaveOccurred())
633
		Expect(results).To(HaveLen(1))
634
		result := results[0]
635

636
		Expect(result.Options).To(HaveKeyWithValue("mtu", "1500"))
637
		Expect(result).To(HaveField("Driver", "macvlan"))
638
		Expect(result).To(HaveField("NetworkInterface", "lo"))
639
		Expect(result.IPAMOptions).To(HaveKeyWithValue("driver", "host-local"))
640

641
		Expect(result.Subnets).To(HaveLen(1))
642
		Expect(result.Subnets[0].Subnet.String()).To(Equal("192.168.1.0/24"))
643
		Expect(result.Subnets[0].Gateway.String()).To(Equal("192.168.1.254"))
644

645
		nc = podmanTest.Podman([]string{"network", "rm", net})
646
		nc.WaitWithDefaultTimeout()
647
		Expect(nc).Should(ExitCleanly())
648
	})
649

650
	It("podman network prune --filter", func() {
651
		useCustomNetworkDir(podmanTest, tempdir)
652
		net1 := "macvlan" + stringid.GenerateRandomID() + "net1"
653

654
		nc := podmanTest.Podman([]string{"network", "create", net1})
655
		nc.WaitWithDefaultTimeout()
656
		defer podmanTest.removeNetwork(net1)
657
		Expect(nc).Should(ExitCleanly())
658

659
		list := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
660
		list.WaitWithDefaultTimeout()
661
		Expect(list).Should(ExitCleanly())
662
		Expect(list.OutputToStringArray()).Should(HaveLen(2))
663

664
		Expect(list.OutputToStringArray()).Should(ContainElement(net1))
665
		Expect(list.OutputToStringArray()).Should(ContainElement("podman"))
666

667
		// -f needed only to skip y/n question
668
		prune := podmanTest.Podman([]string{"network", "prune", "-f", "--filter", "until=50"})
669
		prune.WaitWithDefaultTimeout()
670
		Expect(prune).Should(ExitCleanly())
671

672
		listAgain := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
673
		listAgain.WaitWithDefaultTimeout()
674
		Expect(listAgain).Should(ExitCleanly())
675
		Expect(listAgain.OutputToStringArray()).Should(HaveLen(2))
676

677
		Expect(listAgain.OutputToStringArray()).Should(ContainElement(net1))
678
		Expect(listAgain.OutputToStringArray()).Should(ContainElement("podman"))
679

680
		// -f needed only to skip y/n question
681
		prune = podmanTest.Podman([]string{"network", "prune", "-f", "--filter", "until=5000000000000"})
682
		prune.WaitWithDefaultTimeout()
683
		Expect(prune).Should(ExitCleanly())
684

685
		listAgain = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
686
		listAgain.WaitWithDefaultTimeout()
687
		Expect(listAgain).Should(ExitCleanly())
688
		Expect(listAgain.OutputToStringArray()).Should(HaveLen(1))
689

690
		Expect(listAgain.OutputToStringArray()).ShouldNot(ContainElement(net1))
691
		Expect(listAgain.OutputToStringArray()).Should(ContainElement("podman"))
692
	})
693

694
	It("podman network prune", func() {
695
		useCustomNetworkDir(podmanTest, tempdir)
696
		// Create two networks
697
		// Check they are there
698
		// Run a container on one of them
699
		// Network Prune
700
		// Check that one has been pruned, other remains
701
		net := "macvlan" + stringid.GenerateRandomID()
702
		net1 := net + "1"
703
		net2 := net + "2"
704
		nc := podmanTest.Podman([]string{"network", "create", net1})
705
		nc.WaitWithDefaultTimeout()
706
		defer podmanTest.removeNetwork(net1)
707
		Expect(nc).Should(ExitCleanly())
708

709
		nc2 := podmanTest.Podman([]string{"network", "create", net2})
710
		nc2.WaitWithDefaultTimeout()
711
		defer podmanTest.removeNetwork(net2)
712
		Expect(nc2).Should(ExitCleanly())
713

714
		list := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
715
		list.WaitWithDefaultTimeout()
716
		Expect(list.OutputToStringArray()).Should(HaveLen(3))
717

718
		Expect(list.OutputToStringArray()).Should(ContainElement(net1))
719
		Expect(list.OutputToStringArray()).Should(ContainElement(net2))
720
		Expect(list.OutputToStringArray()).Should(ContainElement("podman"))
721

722
		session := podmanTest.Podman([]string{"run", "-dt", "--net", net2, ALPINE, "top"})
723
		session.WaitWithDefaultTimeout()
724
		Expect(session).Should(ExitCleanly())
725

726
		prune := podmanTest.Podman([]string{"network", "prune", "-f"})
727
		prune.WaitWithDefaultTimeout()
728
		Expect(prune).Should(ExitCleanly())
729

730
		listAgain := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
731
		listAgain.WaitWithDefaultTimeout()
732
		Expect(listAgain).Should(ExitCleanly())
733
		Expect(listAgain.OutputToStringArray()).Should(HaveLen(2))
734

735
		Expect(listAgain.OutputToStringArray()).ShouldNot(ContainElement(net1))
736
		Expect(listAgain.OutputToStringArray()).Should(ContainElement(net2))
737
		Expect(listAgain.OutputToStringArray()).Should(ContainElement("podman"))
738
	})
739
})
740

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.