github.com/AbhinandanKurakure/podman/v3@v3.4.10/test/e2e/network_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/containers/podman/v3/pkg/rootless"
    10  	. "github.com/containers/podman/v3/test/utils"
    11  	"github.com/containers/storage/pkg/stringid"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gexec"
    15  )
    16  
    17  var _ = Describe("Podman network", func() {
    18  	var (
    19  		tempdir    string
    20  		err        error
    21  		podmanTest *PodmanTestIntegration
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		tempdir, err = CreateTempDirInTempDir()
    26  		if err != nil {
    27  			os.Exit(1)
    28  		}
    29  		podmanTest = PodmanTestCreate(tempdir)
    30  		podmanTest.Setup()
    31  	})
    32  
    33  	AfterEach(func() {
    34  		podmanTest.Cleanup()
    35  		f := CurrentGinkgoTestDescription()
    36  		processTestResult(f)
    37  
    38  	})
    39  
    40  	It("podman network list", func() {
    41  		name, path := generateNetworkConfig(podmanTest)
    42  		defer removeConf(path)
    43  
    44  		session := podmanTest.Podman([]string{"network", "ls"})
    45  		session.WaitWithDefaultTimeout()
    46  		Expect(session).Should(Exit(0))
    47  		Expect(session.LineInOutputContains(name)).To(BeTrue())
    48  	})
    49  
    50  	It("podman network list -q", func() {
    51  		name, path := generateNetworkConfig(podmanTest)
    52  		defer removeConf(path)
    53  
    54  		session := podmanTest.Podman([]string{"network", "ls", "--quiet"})
    55  		session.WaitWithDefaultTimeout()
    56  		Expect(session).Should(Exit(0))
    57  		Expect(session.LineInOutputContains(name)).To(BeTrue())
    58  	})
    59  
    60  	It("podman network list --filter success", func() {
    61  		name, path := generateNetworkConfig(podmanTest)
    62  		defer removeConf(path)
    63  
    64  		session := podmanTest.Podman([]string{"network", "ls", "--filter", "plugin=bridge"})
    65  		session.WaitWithDefaultTimeout()
    66  		Expect(session).Should(Exit(0))
    67  		Expect(session.LineInOutputContains(name)).To(BeTrue())
    68  	})
    69  
    70  	It("podman network list --filter plugin and name", func() {
    71  		name, path := generateNetworkConfig(podmanTest)
    72  		defer removeConf(path)
    73  
    74  		session := podmanTest.Podman([]string{"network", "ls", "--filter", "plugin=bridge", "--filter", "name=" + name})
    75  		session.WaitWithDefaultTimeout()
    76  		Expect(session).Should(Exit(0))
    77  		Expect(session.OutputToString()).To(ContainSubstring(name))
    78  	})
    79  
    80  	It("podman network list --filter two names", func() {
    81  		name1, path1 := generateNetworkConfig(podmanTest)
    82  		defer removeConf(path1)
    83  
    84  		name2, path2 := generateNetworkConfig(podmanTest)
    85  		defer removeConf(path2)
    86  
    87  		session := podmanTest.Podman([]string{"network", "ls", "--filter", "name=" + name1, "--filter", "name=" + name2})
    88  		session.WaitWithDefaultTimeout()
    89  		Expect(session).Should(Exit(0))
    90  		Expect(session.OutputToString()).To(ContainSubstring(name1))
    91  		Expect(session.OutputToString()).To(ContainSubstring(name2))
    92  	})
    93  
    94  	It("podman network list --filter labels", func() {
    95  		net1 := "labelnet" + stringid.GenerateNonCryptoID()
    96  		label1 := "testlabel1=abc"
    97  		label2 := "abcdef"
    98  		session := podmanTest.Podman([]string{"network", "create", "--label", label1, net1})
    99  		session.WaitWithDefaultTimeout()
   100  		defer podmanTest.removeCNINetwork(net1)
   101  		Expect(session).Should(Exit(0))
   102  
   103  		net2 := "labelnet" + stringid.GenerateNonCryptoID()
   104  		session = podmanTest.Podman([]string{"network", "create", "--label", label1, "--label", label2, net2})
   105  		session.WaitWithDefaultTimeout()
   106  		defer podmanTest.removeCNINetwork(net2)
   107  		Expect(session).Should(Exit(0))
   108  
   109  		session = podmanTest.Podman([]string{"network", "ls", "--filter", "label=" + label1})
   110  		session.WaitWithDefaultTimeout()
   111  		Expect(session).Should(Exit(0))
   112  		Expect(session.OutputToString()).To(ContainSubstring(net1))
   113  		Expect(session.OutputToString()).To(ContainSubstring(net2))
   114  
   115  		session = podmanTest.Podman([]string{"network", "ls", "--filter", "label=" + label1, "--filter", "label=" + label2})
   116  		session.WaitWithDefaultTimeout()
   117  		Expect(session).Should(Exit(0))
   118  		Expect(session.OutputToString()).ToNot(ContainSubstring(net1))
   119  		Expect(session.OutputToString()).To(ContainSubstring(net2))
   120  	})
   121  
   122  	It("podman network list --filter invalid value", func() {
   123  		net := "net" + stringid.GenerateNonCryptoID()
   124  		session := podmanTest.Podman([]string{"network", "create", net})
   125  		session.WaitWithDefaultTimeout()
   126  		defer podmanTest.removeCNINetwork(net)
   127  		Expect(session).Should(Exit(0))
   128  
   129  		session = podmanTest.Podman([]string{"network", "ls", "--filter", "namr=ab"})
   130  		session.WaitWithDefaultTimeout()
   131  		Expect(session).To(ExitWithError())
   132  		Expect(session.ErrorToString()).To(ContainSubstring(`invalid filter "namr"`))
   133  	})
   134  
   135  	It("podman network list --filter failure", func() {
   136  		name, path := generateNetworkConfig(podmanTest)
   137  		defer removeConf(path)
   138  
   139  		session := podmanTest.Podman([]string{"network", "ls", "--filter", "plugin=test"})
   140  		session.WaitWithDefaultTimeout()
   141  		Expect(session).Should(Exit(0))
   142  		Expect(session.LineInOutputContains(name)).To(BeFalse())
   143  	})
   144  
   145  	It("podman network ID test", func() {
   146  		net := "networkIDTest"
   147  		// the network id should be the sha256 hash of the network name
   148  		netID := "6073aefe03cdf8f29be5b23ea9795c431868a3a22066a6290b187691614fee84"
   149  		session := podmanTest.Podman([]string{"network", "create", net})
   150  		session.WaitWithDefaultTimeout()
   151  		defer podmanTest.removeCNINetwork(net)
   152  		Expect(session).Should(Exit(0))
   153  
   154  		// Tests Default Table Output
   155  		session = podmanTest.Podman([]string{"network", "ls", "--filter", "id=" + netID})
   156  		session.WaitWithDefaultTimeout()
   157  		Expect(session).Should(Exit(0))
   158  		expectedTable := "NETWORK ID NAME VERSION PLUGINS"
   159  		Expect(session.OutputToString()).To(ContainSubstring(expectedTable))
   160  
   161  		session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}} {{.ID}}", "--filter", "id=" + netID})
   162  		session.WaitWithDefaultTimeout()
   163  		Expect(session).Should(Exit(0))
   164  		Expect(session.OutputToString()).To(ContainSubstring(net + " " + netID[:12]))
   165  
   166  		session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}} {{.ID}}", "--filter", "id=" + netID[10:50], "--no-trunc"})
   167  		session.WaitWithDefaultTimeout()
   168  		Expect(session).Should(Exit(0))
   169  		Expect(session.OutputToString()).To(ContainSubstring(net + " " + netID))
   170  
   171  		session = podmanTest.Podman([]string{"network", "inspect", netID[:40]})
   172  		session.WaitWithDefaultTimeout()
   173  		Expect(session).Should(Exit(0))
   174  		Expect(session.OutputToString()).To(ContainSubstring(net))
   175  
   176  		session = podmanTest.Podman([]string{"network", "inspect", netID[1:]})
   177  		session.WaitWithDefaultTimeout()
   178  		Expect(session).Should(ExitWithError())
   179  		Expect(session.ErrorToString()).To(ContainSubstring("no such network"))
   180  
   181  		session = podmanTest.Podman([]string{"network", "rm", netID})
   182  		session.WaitWithDefaultTimeout()
   183  		Expect(session).Should(Exit(0))
   184  	})
   185  
   186  	rm_func := func(rm string) {
   187  		It(fmt.Sprintf("podman network %s no args", rm), func() {
   188  			session := podmanTest.Podman([]string{"network", rm})
   189  			session.WaitWithDefaultTimeout()
   190  			Expect(session).Should(ExitWithError())
   191  
   192  		})
   193  
   194  		It(fmt.Sprintf("podman network %s", rm), func() {
   195  			name, path := generateNetworkConfig(podmanTest)
   196  			defer removeConf(path)
   197  
   198  			session := podmanTest.Podman([]string{"network", "ls", "--quiet"})
   199  			session.WaitWithDefaultTimeout()
   200  			Expect(session).Should(Exit(0))
   201  			Expect(session.LineInOutputContains(name)).To(BeTrue())
   202  
   203  			rm := podmanTest.Podman([]string{"network", rm, name})
   204  			rm.WaitWithDefaultTimeout()
   205  			Expect(rm).Should(Exit(0))
   206  
   207  			results := podmanTest.Podman([]string{"network", "ls", "--quiet"})
   208  			results.WaitWithDefaultTimeout()
   209  			Expect(results).Should(Exit(0))
   210  			Expect(results.LineInOutputContains(name)).To(BeFalse())
   211  		})
   212  	}
   213  
   214  	rm_func("rm")
   215  	rm_func("remove")
   216  
   217  	It("podman network inspect no args", func() {
   218  		session := podmanTest.Podman([]string{"network", "inspect"})
   219  		session.WaitWithDefaultTimeout()
   220  		Expect(session).Should(ExitWithError())
   221  	})
   222  
   223  	It("podman network inspect", func() {
   224  		name, path := generateNetworkConfig(podmanTest)
   225  		defer removeConf(path)
   226  
   227  		expectedNetworks := []string{name}
   228  		if !rootless.IsRootless() {
   229  			// rootful image contains "podman/cni/87-podman-bridge.conflist" for "podman" network
   230  			expectedNetworks = append(expectedNetworks, "podman")
   231  		}
   232  		session := podmanTest.Podman(append([]string{"network", "inspect"}, expectedNetworks...))
   233  		session.WaitWithDefaultTimeout()
   234  		Expect(session).Should(Exit(0))
   235  		Expect(session.IsJSONOutputValid()).To(BeTrue())
   236  	})
   237  
   238  	It("podman network inspect", func() {
   239  		name, path := generateNetworkConfig(podmanTest)
   240  		defer removeConf(path)
   241  
   242  		session := podmanTest.Podman([]string{"network", "inspect", name, "--format", "{{.cniVersion}}"})
   243  		session.WaitWithDefaultTimeout()
   244  		Expect(session).Should(Exit(0))
   245  		Expect(session.LineInOutputContains("0.3.0")).To(BeTrue())
   246  	})
   247  
   248  	It("podman inspect container single CNI network", func() {
   249  		netName := "net-" + stringid.GenerateNonCryptoID()
   250  		network := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.50.0/24", netName})
   251  		network.WaitWithDefaultTimeout()
   252  		defer podmanTest.removeCNINetwork(netName)
   253  		Expect(network).Should(Exit(0))
   254  
   255  		ctrName := "testCtr"
   256  		container := podmanTest.Podman([]string{"run", "-dt", "--network", netName, "--name", ctrName, ALPINE, "top"})
   257  		container.WaitWithDefaultTimeout()
   258  		Expect(container).Should(Exit(0))
   259  
   260  		inspect := podmanTest.Podman([]string{"inspect", ctrName})
   261  		inspect.WaitWithDefaultTimeout()
   262  		Expect(inspect).Should(Exit(0))
   263  		conData := inspect.InspectContainerToJSON()
   264  		Expect(len(conData)).To(Equal(1))
   265  		Expect(len(conData[0].NetworkSettings.Networks)).To(Equal(1))
   266  		net, ok := conData[0].NetworkSettings.Networks[netName]
   267  		Expect(ok).To(BeTrue())
   268  		Expect(net.NetworkID).To(Equal(netName))
   269  		Expect(net.IPPrefixLen).To(Equal(24))
   270  		Expect(strings.HasPrefix(net.IPAddress, "10.50.50.")).To(BeTrue())
   271  
   272  		// Necessary to ensure the CNI network is removed cleanly
   273  		rmAll := podmanTest.Podman([]string{"rm", "-f", ctrName})
   274  		rmAll.WaitWithDefaultTimeout()
   275  		Expect(rmAll).Should(Exit(0))
   276  	})
   277  
   278  	It("podman inspect container two CNI networks (container not running)", func() {
   279  		netName1 := "net1-" + stringid.GenerateNonCryptoID()
   280  		network1 := podmanTest.Podman([]string{"network", "create", netName1})
   281  		network1.WaitWithDefaultTimeout()
   282  		defer podmanTest.removeCNINetwork(netName1)
   283  		Expect(network1).Should(Exit(0))
   284  
   285  		netName2 := "net2-" + stringid.GenerateNonCryptoID()
   286  		network2 := podmanTest.Podman([]string{"network", "create", netName2})
   287  		network2.WaitWithDefaultTimeout()
   288  		defer podmanTest.removeCNINetwork(netName2)
   289  		Expect(network2).Should(Exit(0))
   290  
   291  		ctrName := "testCtr"
   292  		container := podmanTest.Podman([]string{"create", "--network", fmt.Sprintf("%s,%s", netName1, netName2), "--name", ctrName, ALPINE, "top"})
   293  		container.WaitWithDefaultTimeout()
   294  		Expect(container).Should(Exit(0))
   295  
   296  		inspect := podmanTest.Podman([]string{"inspect", ctrName})
   297  		inspect.WaitWithDefaultTimeout()
   298  		Expect(inspect).Should(Exit(0))
   299  		conData := inspect.InspectContainerToJSON()
   300  		Expect(len(conData)).To(Equal(1))
   301  		Expect(len(conData[0].NetworkSettings.Networks)).To(Equal(2))
   302  		net1, ok := conData[0].NetworkSettings.Networks[netName1]
   303  		Expect(ok).To(BeTrue())
   304  		Expect(net1.NetworkID).To(Equal(netName1))
   305  		net2, ok := conData[0].NetworkSettings.Networks[netName2]
   306  		Expect(ok).To(BeTrue())
   307  		Expect(net2.NetworkID).To(Equal(netName2))
   308  
   309  		// Necessary to ensure the CNI network is removed cleanly
   310  		rmAll := podmanTest.Podman([]string{"rm", "-f", ctrName})
   311  		rmAll.WaitWithDefaultTimeout()
   312  		Expect(rmAll).Should(Exit(0))
   313  	})
   314  
   315  	It("podman inspect container two CNI networks", func() {
   316  		netName1 := "net1-" + stringid.GenerateNonCryptoID()
   317  		network1 := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.51.0/25", netName1})
   318  		network1.WaitWithDefaultTimeout()
   319  		defer podmanTest.removeCNINetwork(netName1)
   320  		Expect(network1).Should(Exit(0))
   321  
   322  		netName2 := "net2-" + stringid.GenerateNonCryptoID()
   323  		network2 := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.51.128/26", netName2})
   324  		network2.WaitWithDefaultTimeout()
   325  		defer podmanTest.removeCNINetwork(netName2)
   326  		Expect(network2).Should(Exit(0))
   327  
   328  		ctrName := "testCtr"
   329  		container := podmanTest.Podman([]string{"run", "-dt", "--network", fmt.Sprintf("%s,%s", netName1, netName2), "--name", ctrName, ALPINE, "top"})
   330  		container.WaitWithDefaultTimeout()
   331  		Expect(container).Should(Exit(0))
   332  
   333  		inspect := podmanTest.Podman([]string{"inspect", ctrName})
   334  		inspect.WaitWithDefaultTimeout()
   335  		Expect(inspect).Should(Exit(0))
   336  		conData := inspect.InspectContainerToJSON()
   337  		Expect(len(conData)).To(Equal(1))
   338  		Expect(len(conData[0].NetworkSettings.Networks)).To(Equal(2))
   339  		net1, ok := conData[0].NetworkSettings.Networks[netName1]
   340  		Expect(ok).To(BeTrue())
   341  		Expect(net1.NetworkID).To(Equal(netName1))
   342  		Expect(net1.IPPrefixLen).To(Equal(25))
   343  		Expect(strings.HasPrefix(net1.IPAddress, "10.50.51.")).To(BeTrue())
   344  		net2, ok := conData[0].NetworkSettings.Networks[netName2]
   345  		Expect(ok).To(BeTrue())
   346  		Expect(net2.NetworkID).To(Equal(netName2))
   347  		Expect(net2.IPPrefixLen).To(Equal(26))
   348  		Expect(strings.HasPrefix(net2.IPAddress, "10.50.51.")).To(BeTrue())
   349  
   350  		// Necessary to ensure the CNI network is removed cleanly
   351  		rmAll := podmanTest.Podman([]string{"rm", "-f", ctrName})
   352  		rmAll.WaitWithDefaultTimeout()
   353  		Expect(rmAll).Should(Exit(0))
   354  	})
   355  
   356  	It("podman network remove after disconnect when container initially created with the network", func() {
   357  		SkipIfRootless("disconnect works only in non rootless container")
   358  
   359  		container := "test"
   360  		network := "foo"
   361  
   362  		session := podmanTest.Podman([]string{"network", "create", network})
   363  		session.WaitWithDefaultTimeout()
   364  		Expect(session).Should(Exit(0))
   365  
   366  		session = podmanTest.Podman([]string{"run", "--name", container, "--network", network, "-d", ALPINE, "top"})
   367  		session.WaitWithDefaultTimeout()
   368  		Expect(session).Should(Exit(0))
   369  
   370  		session = podmanTest.Podman([]string{"network", "disconnect", network, container})
   371  		session.WaitWithDefaultTimeout()
   372  		Expect(session).Should(Exit(0))
   373  
   374  		session = podmanTest.Podman([]string{"network", "rm", network})
   375  		session.WaitWithDefaultTimeout()
   376  		Expect(session).Should(Exit(0))
   377  	})
   378  
   379  	It("podman network remove bogus", func() {
   380  		session := podmanTest.Podman([]string{"network", "rm", "bogus"})
   381  		session.WaitWithDefaultTimeout()
   382  		Expect(session).Should(Exit(1))
   383  	})
   384  
   385  	It("podman network remove --force with pod", func() {
   386  		netName := "net-" + stringid.GenerateNonCryptoID()
   387  		session := podmanTest.Podman([]string{"network", "create", netName})
   388  		session.WaitWithDefaultTimeout()
   389  		defer podmanTest.removeCNINetwork(netName)
   390  		Expect(session).Should(Exit(0))
   391  
   392  		session = podmanTest.Podman([]string{"pod", "create", "--network", netName})
   393  		session.WaitWithDefaultTimeout()
   394  		Expect(session).Should(Exit(0))
   395  		podID := session.OutputToString()
   396  
   397  		session = podmanTest.Podman([]string{"create", "--pod", podID, ALPINE})
   398  		session.WaitWithDefaultTimeout()
   399  		Expect(session).Should(Exit(0))
   400  
   401  		session = podmanTest.Podman([]string{"network", "rm", netName})
   402  		session.WaitWithDefaultTimeout()
   403  		Expect(session).Should(Exit(2))
   404  
   405  		session = podmanTest.Podman([]string{"network", "rm", "--force", netName})
   406  		session.WaitWithDefaultTimeout()
   407  		Expect(session).Should(Exit(0))
   408  
   409  		// check if pod is deleted
   410  		session = podmanTest.Podman([]string{"pod", "exists", podID})
   411  		session.WaitWithDefaultTimeout()
   412  		Expect(session).Should(Exit(1))
   413  
   414  		// check if net is deleted
   415  		session = podmanTest.Podman([]string{"network", "ls"})
   416  		session.WaitWithDefaultTimeout()
   417  		Expect(session).Should(Exit(0))
   418  		Expect(session.OutputToString()).To(Not(ContainSubstring(netName)))
   419  	})
   420  
   421  	It("podman network remove with two networks", func() {
   422  		netName1 := "net1-" + stringid.GenerateNonCryptoID()
   423  		session := podmanTest.Podman([]string{"network", "create", netName1})
   424  		session.WaitWithDefaultTimeout()
   425  		defer podmanTest.removeCNINetwork(netName1)
   426  		Expect(session).Should(Exit(0))
   427  
   428  		netName2 := "net2-" + stringid.GenerateNonCryptoID()
   429  		session = podmanTest.Podman([]string{"network", "create", netName2})
   430  		session.WaitWithDefaultTimeout()
   431  		defer podmanTest.removeCNINetwork(netName2)
   432  		Expect(session).Should(Exit(0))
   433  
   434  		session = podmanTest.Podman([]string{"network", "rm", netName1, netName2})
   435  		session.WaitWithDefaultTimeout()
   436  		Expect(session).Should(Exit(0))
   437  		lines := session.OutputToStringArray()
   438  		Expect(lines[0]).To(Equal(netName1))
   439  		Expect(lines[1]).To(Equal(netName2))
   440  	})
   441  
   442  	It("podman network with multiple aliases", func() {
   443  		var worked bool
   444  		netName := "aliasTest" + stringid.GenerateNonCryptoID()
   445  		session := podmanTest.Podman([]string{"network", "create", netName})
   446  		session.WaitWithDefaultTimeout()
   447  		defer podmanTest.removeCNINetwork(netName)
   448  		Expect(session).Should(Exit(0))
   449  
   450  		interval := time.Duration(250 * time.Millisecond)
   451  		for i := 0; i < 6; i++ {
   452  			n := podmanTest.Podman([]string{"network", "exists", netName})
   453  			n.WaitWithDefaultTimeout()
   454  			worked = n.ExitCode() == 0
   455  			if worked {
   456  				break
   457  			}
   458  			time.Sleep(interval)
   459  			interval *= 2
   460  		}
   461  
   462  		top := podmanTest.Podman([]string{"run", "-dt", "--name=web", "--network=" + netName, "--network-alias=web1", "--network-alias=web2", nginx})
   463  		top.WaitWithDefaultTimeout()
   464  		Expect(top).Should(Exit(0))
   465  		interval = time.Duration(250 * time.Millisecond)
   466  		// Wait for the nginx service to be running
   467  		for i := 0; i < 6; i++ {
   468  			// Test curl against the container's name
   469  			c1 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, nginx, "curl", "web"})
   470  			c1.WaitWithDefaultTimeout()
   471  			worked = c1.ExitCode() == 0
   472  			if worked {
   473  				break
   474  			}
   475  			time.Sleep(interval)
   476  			interval *= 2
   477  		}
   478  		Expect(worked).To(BeTrue())
   479  
   480  		// Nginx is now running so no need to do a loop
   481  		// Test against the first alias
   482  		c2 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, nginx, "curl", "web1"})
   483  		c2.WaitWithDefaultTimeout()
   484  		Expect(c2).Should(Exit(0))
   485  
   486  		// Test against the second alias
   487  		c3 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, nginx, "curl", "web2"})
   488  		c3.WaitWithDefaultTimeout()
   489  		Expect(c3).Should(Exit(0))
   490  	})
   491  
   492  	It("podman network create/remove macvlan", func() {
   493  		net := "macvlan" + stringid.GenerateNonCryptoID()
   494  		nc := podmanTest.Podman([]string{"network", "create", "--macvlan", "lo", net})
   495  		nc.WaitWithDefaultTimeout()
   496  		defer podmanTest.removeCNINetwork(net)
   497  		Expect(nc).Should(Exit(0))
   498  
   499  		nc = podmanTest.Podman([]string{"network", "rm", net})
   500  		nc.WaitWithDefaultTimeout()
   501  		Expect(nc).Should(Exit(0))
   502  	})
   503  
   504  	It("podman network create/remove macvlan as driver (-d) no device name", func() {
   505  		net := "macvlan" + stringid.GenerateNonCryptoID()
   506  		nc := podmanTest.Podman([]string{"network", "create", "-d", "macvlan", net})
   507  		nc.WaitWithDefaultTimeout()
   508  		defer podmanTest.removeCNINetwork(net)
   509  		Expect(nc).Should(Exit(0))
   510  
   511  		inspect := podmanTest.Podman([]string{"network", "inspect", net})
   512  		inspect.WaitWithDefaultTimeout()
   513  		Expect(inspect).Should(Exit(0))
   514  
   515  		out, err := inspect.jq(".[0].plugins[0].master")
   516  		Expect(err).To(BeNil())
   517  		Expect(out).To(Equal("\"\""))
   518  
   519  		nc = podmanTest.Podman([]string{"network", "rm", net})
   520  		nc.WaitWithDefaultTimeout()
   521  		Expect(nc).Should(Exit(0))
   522  	})
   523  
   524  	It("podman network create/remove macvlan as driver (-d) with device name", func() {
   525  		net := "macvlan" + stringid.GenerateNonCryptoID()
   526  		nc := podmanTest.Podman([]string{"network", "create", "-d", "macvlan", "-o", "parent=lo", net})
   527  		nc.WaitWithDefaultTimeout()
   528  		defer podmanTest.removeCNINetwork(net)
   529  		Expect(nc).Should(Exit(0))
   530  
   531  		inspect := podmanTest.Podman([]string{"network", "inspect", net})
   532  		inspect.WaitWithDefaultTimeout()
   533  		Expect(inspect).Should(Exit(0))
   534  
   535  		out, err := inspect.jq(".[0].plugins[0].master")
   536  		Expect(err).To(BeNil())
   537  		Expect(out).To(Equal(`"lo"`))
   538  
   539  		ipamType, err := inspect.jq(".[0].plugins[0].ipam.type")
   540  		Expect(err).To(BeNil())
   541  		Expect(ipamType).To(Equal(`"dhcp"`))
   542  
   543  		nc = podmanTest.Podman([]string{"network", "rm", net})
   544  		nc.WaitWithDefaultTimeout()
   545  		Expect(nc).Should(Exit(0))
   546  	})
   547  
   548  	It("podman network exists", func() {
   549  		net := "net" + stringid.GenerateNonCryptoID()
   550  		session := podmanTest.Podman([]string{"network", "create", net})
   551  		session.WaitWithDefaultTimeout()
   552  		defer podmanTest.removeCNINetwork(net)
   553  		Expect(session).Should(Exit(0))
   554  
   555  		session = podmanTest.Podman([]string{"network", "exists", net})
   556  		session.WaitWithDefaultTimeout()
   557  		Expect(session).Should(Exit(0))
   558  
   559  		session = podmanTest.Podman([]string{"network", "exists", stringid.GenerateNonCryptoID()})
   560  		session.WaitWithDefaultTimeout()
   561  		Expect(session).Should(Exit(1))
   562  	})
   563  
   564  	It("podman network create macvlan with network info and options", func() {
   565  		net := "macvlan" + stringid.GenerateNonCryptoID()
   566  		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})
   567  		nc.WaitWithDefaultTimeout()
   568  		defer podmanTest.removeCNINetwork(net)
   569  		Expect(nc).Should(Exit(0))
   570  
   571  		inspect := podmanTest.Podman([]string{"network", "inspect", net})
   572  		inspect.WaitWithDefaultTimeout()
   573  		Expect(inspect).Should(Exit(0))
   574  
   575  		mtu, err := inspect.jq(".[0].plugins[0].mtu")
   576  		Expect(err).To(BeNil())
   577  		Expect(mtu).To(Equal("1500"))
   578  
   579  		name, err := inspect.jq(".[0].plugins[0].type")
   580  		Expect(err).To(BeNil())
   581  		Expect(name).To(Equal(`"macvlan"`))
   582  
   583  		netInt, err := inspect.jq(".[0].plugins[0].master")
   584  		Expect(err).To(BeNil())
   585  		Expect(netInt).To(Equal(`"lo"`))
   586  
   587  		ipamType, err := inspect.jq(".[0].plugins[0].ipam.type")
   588  		Expect(err).To(BeNil())
   589  		Expect(ipamType).To(Equal(`"host-local"`))
   590  
   591  		gw, err := inspect.jq(".[0].plugins[0].ipam.ranges[0][0].gateway")
   592  		Expect(err).To(BeNil())
   593  		Expect(gw).To(Equal(`"192.168.1.254"`))
   594  
   595  		subnet, err := inspect.jq(".[0].plugins[0].ipam.ranges[0][0].subnet")
   596  		Expect(err).To(BeNil())
   597  		Expect(subnet).To(Equal(`"192.168.1.0/24"`))
   598  
   599  		routes, err := inspect.jq(".[0].plugins[0].ipam.routes[0].dst")
   600  		Expect(err).To(BeNil())
   601  		Expect(routes).To(Equal(`"0.0.0.0/0"`))
   602  
   603  		nc = podmanTest.Podman([]string{"network", "rm", net})
   604  		nc.WaitWithDefaultTimeout()
   605  		Expect(nc).Should(Exit(0))
   606  	})
   607  
   608  	It("podman network prune --filter", func() {
   609  		net1 := "macvlan" + stringid.GenerateNonCryptoID() + "net1"
   610  
   611  		nc := podmanTest.Podman([]string{"network", "create", net1})
   612  		nc.WaitWithDefaultTimeout()
   613  		defer podmanTest.removeCNINetwork(net1)
   614  		Expect(nc).Should(Exit(0))
   615  
   616  		list := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
   617  		list.WaitWithDefaultTimeout()
   618  		Expect(list).Should(Exit(0))
   619  
   620  		Expect(StringInSlice(net1, list.OutputToStringArray())).To(BeTrue())
   621  		if !isRootless() {
   622  			Expect(StringInSlice("podman", list.OutputToStringArray())).To(BeTrue())
   623  		}
   624  
   625  		// -f needed only to skip y/n question
   626  		prune := podmanTest.Podman([]string{"network", "prune", "-f", "--filter", "until=50"})
   627  		prune.WaitWithDefaultTimeout()
   628  		Expect(prune).Should(Exit(0))
   629  
   630  		listAgain := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
   631  		listAgain.WaitWithDefaultTimeout()
   632  		Expect(listAgain).Should(Exit(0))
   633  
   634  		Expect(StringInSlice(net1, listAgain.OutputToStringArray())).To(BeTrue())
   635  		if !isRootless() {
   636  			Expect(StringInSlice("podman", list.OutputToStringArray())).To(BeTrue())
   637  		}
   638  
   639  		// -f needed only to skip y/n question
   640  		prune = podmanTest.Podman([]string{"network", "prune", "-f", "--filter", "until=5000000000000"})
   641  		prune.WaitWithDefaultTimeout()
   642  		Expect(prune).Should(Exit(0))
   643  
   644  		listAgain = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
   645  		listAgain.WaitWithDefaultTimeout()
   646  		Expect(listAgain).Should(Exit(0))
   647  
   648  		Expect(StringInSlice(net1, listAgain.OutputToStringArray())).To(BeFalse())
   649  		if !isRootless() {
   650  			Expect(StringInSlice("podman", list.OutputToStringArray())).To(BeTrue())
   651  		}
   652  	})
   653  
   654  	It("podman network prune", func() {
   655  		// Create two networks
   656  		// Check they are there
   657  		// Run a container on one of them
   658  		// Network Prune
   659  		// Check that one has been pruned, other remains
   660  		net := "macvlan" + stringid.GenerateNonCryptoID()
   661  		net1 := net + "1"
   662  		net2 := net + "2"
   663  		nc := podmanTest.Podman([]string{"network", "create", net1})
   664  		nc.WaitWithDefaultTimeout()
   665  		defer podmanTest.removeCNINetwork(net1)
   666  		Expect(nc).Should(Exit(0))
   667  
   668  		nc2 := podmanTest.Podman([]string{"network", "create", net2})
   669  		nc2.WaitWithDefaultTimeout()
   670  		defer podmanTest.removeCNINetwork(net2)
   671  		Expect(nc2).Should(Exit(0))
   672  
   673  		list := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
   674  		list.WaitWithDefaultTimeout()
   675  		Expect(list).Should(Exit(0))
   676  
   677  		Expect(StringInSlice(net1, list.OutputToStringArray())).To(BeTrue())
   678  		Expect(StringInSlice(net2, list.OutputToStringArray())).To(BeTrue())
   679  		if !isRootless() {
   680  			Expect(StringInSlice("podman", list.OutputToStringArray())).To(BeTrue())
   681  		}
   682  
   683  		session := podmanTest.Podman([]string{"run", "-dt", "--net", net2, ALPINE, "top"})
   684  		session.WaitWithDefaultTimeout()
   685  		Expect(session).Should(Exit(0))
   686  
   687  		prune := podmanTest.Podman([]string{"network", "prune", "-f"})
   688  		prune.WaitWithDefaultTimeout()
   689  		Expect(prune).Should(Exit(0))
   690  
   691  		listAgain := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"})
   692  		listAgain.WaitWithDefaultTimeout()
   693  		Expect(listAgain).Should(Exit(0))
   694  
   695  		Expect(StringInSlice(net1, listAgain.OutputToStringArray())).To(BeFalse())
   696  		Expect(StringInSlice(net2, listAgain.OutputToStringArray())).To(BeTrue())
   697  		// Make sure default network 'podman' still exists
   698  		if !isRootless() {
   699  			Expect(StringInSlice("podman", list.OutputToStringArray())).To(BeTrue())
   700  		}
   701  
   702  	})
   703  })