github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/test/e2e/run_staticip_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"time"
     8  
     9  	. "github.com/hanks177/podman/v4/test/utils"
    10  	"github.com/containers/storage/pkg/stringid"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("Podman run with --ip flag", func() {
    17  	var (
    18  		tempdir    string
    19  		err        error
    20  		podmanTest *PodmanTestIntegration
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		SkipIfRootless("rootless does not support --ip without network")
    25  		tempdir, err = CreateTempDirInTempDir()
    26  		if err != nil {
    27  			os.Exit(1)
    28  		}
    29  		podmanTest = PodmanTestCreate(tempdir)
    30  		podmanTest.Setup()
    31  		// Cleanup the CNI networks used by the tests
    32  		os.RemoveAll("/var/lib/cni/networks/podman")
    33  	})
    34  
    35  	AfterEach(func() {
    36  		podmanTest.Cleanup()
    37  		f := CurrentGinkgoTestDescription()
    38  		processTestResult(f)
    39  
    40  	})
    41  
    42  	It("Podman run --ip with garbage address", func() {
    43  		result := podmanTest.Podman([]string{"run", "-ti", "--ip", "114232346", ALPINE, "ls"})
    44  		result.WaitWithDefaultTimeout()
    45  		Expect(result).To(ExitWithError())
    46  	})
    47  
    48  	It("Podman run --ip with v6 address", func() {
    49  		result := podmanTest.Podman([]string{"run", "-ti", "--ip", "2001:db8:bad:beef::1", ALPINE, "ls"})
    50  		result.WaitWithDefaultTimeout()
    51  		Expect(result).To(ExitWithError())
    52  	})
    53  
    54  	It("Podman run --ip with non-allocatable IP", func() {
    55  		result := podmanTest.Podman([]string{"run", "-ti", "--ip", "203.0.113.124", ALPINE, "ls"})
    56  		result.WaitWithDefaultTimeout()
    57  		Expect(result).To(ExitWithError())
    58  	})
    59  
    60  	It("Podman run with specified static IP has correct IP", func() {
    61  		ip := GetRandomIPAddress()
    62  		result := podmanTest.Podman([]string{"run", "-ti", "--ip", ip, ALPINE, "ip", "addr"})
    63  		result.WaitWithDefaultTimeout()
    64  		Expect(result).Should(Exit(0))
    65  		Expect(result.OutputToString()).To(ContainSubstring(ip + "/16"))
    66  	})
    67  
    68  	It("Podman run with specified static IPv6 has correct IP", func() {
    69  		netName := "ipv6-" + stringid.GenerateNonCryptoID()
    70  		ipv6 := "fd46:db93:aa76:ac37::10"
    71  		net := podmanTest.Podman([]string{"network", "create", "--subnet", "fd46:db93:aa76:ac37::/64", netName})
    72  		net.WaitWithDefaultTimeout()
    73  		defer podmanTest.removeNetwork(netName)
    74  		Expect(net).To(Exit(0))
    75  
    76  		result := podmanTest.Podman([]string{"run", "-ti", "--network", netName, "--ip6", ipv6, ALPINE, "ip", "addr"})
    77  		result.WaitWithDefaultTimeout()
    78  		Expect(result).Should(Exit(0))
    79  		Expect(result.OutputToString()).To(ContainSubstring(ipv6 + "/64"))
    80  	})
    81  
    82  	It("Podman run with --network bridge:ip=", func() {
    83  		ip := GetRandomIPAddress()
    84  		result := podmanTest.Podman([]string{"run", "-ti", "--network", "bridge:ip=" + ip, ALPINE, "ip", "addr"})
    85  		result.WaitWithDefaultTimeout()
    86  		Expect(result).Should(Exit(0))
    87  		Expect(result.OutputToString()).To(ContainSubstring(ip + "/16"))
    88  	})
    89  
    90  	It("Podman run with --network net:ip=,mac=,interface_name=", func() {
    91  		ip := GetRandomIPAddress()
    92  		mac := "44:33:22:11:00:99"
    93  		intName := "myeth"
    94  		result := podmanTest.Podman([]string{"run", "-ti", "--network", "bridge:ip=" + ip + ",mac=" + mac + ",interface_name=" + intName, ALPINE, "ip", "addr"})
    95  		result.WaitWithDefaultTimeout()
    96  		Expect(result).Should(Exit(0))
    97  		Expect(result.OutputToString()).To(ContainSubstring(ip + "/16"))
    98  		Expect(result.OutputToString()).To(ContainSubstring(mac))
    99  		Expect(result.OutputToString()).To(ContainSubstring(intName))
   100  	})
   101  
   102  	It("Podman run two containers with the same IP", func() {
   103  		ip := GetRandomIPAddress()
   104  		result := podmanTest.Podman([]string{"run", "-d", "--name", "nginx", "--ip", ip, nginx})
   105  		result.WaitWithDefaultTimeout()
   106  		Expect(result).Should(Exit(0))
   107  
   108  		for retries := 20; retries > 0; retries-- {
   109  			response, err := http.Get(fmt.Sprintf("http://%s", ip))
   110  			if err == nil && response.StatusCode == http.StatusOK {
   111  				break
   112  			}
   113  			if retries == 1 {
   114  				logps := podmanTest.Podman([]string{"ps", "-a"})
   115  				logps.WaitWithDefaultTimeout()
   116  				logps = podmanTest.Podman([]string{"logs", "nginx"})
   117  				logps.WaitWithDefaultTimeout()
   118  				Fail("Timed out waiting for nginx container, see ps & log above.")
   119  			}
   120  
   121  			if err != nil {
   122  				fmt.Printf("nginx not ready yet; error=%v; %d retries left...\n", err, retries)
   123  			} else {
   124  				fmt.Printf("nginx not ready yet; response=%v; %d retries left...\n", response.StatusCode, retries)
   125  			}
   126  			time.Sleep(1 * time.Second)
   127  		}
   128  		result = podmanTest.Podman([]string{"run", "--ip", ip, ALPINE, "ip", "addr"})
   129  		result.WaitWithDefaultTimeout()
   130  		Expect(result).To(ExitWithError())
   131  		Expect(result.ErrorToString()).To(ContainSubstring(" address %s ", ip))
   132  	})
   133  })