github.com/containers/podman/v4@v4.9.4/test/e2e/create_staticip_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"time"
     5  
     6  	. "github.com/containers/podman/v4/test/utils"
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gexec"
    10  )
    11  
    12  var _ = Describe("Podman create with --ip flag", func() {
    13  
    14  	It("Podman create --ip with garbage address", func() {
    15  		result := podmanTest.Podman([]string{"create", "--name", "test", "--ip", "114232346", ALPINE, "ls"})
    16  		result.WaitWithDefaultTimeout()
    17  		Expect(result).To(ExitWithError())
    18  	})
    19  
    20  	It("Podman create --ip with non-allocatable IP", func() {
    21  		SkipIfRootless("--ip not supported without network in rootless mode")
    22  		result := podmanTest.Podman([]string{"create", "--name", "test", "--ip", "203.0.113.124", ALPINE, "ls"})
    23  		result.WaitWithDefaultTimeout()
    24  		Expect(result).Should(ExitCleanly())
    25  
    26  		result = podmanTest.Podman([]string{"start", "test"})
    27  		result.WaitWithDefaultTimeout()
    28  		Expect(result).To(ExitWithError())
    29  	})
    30  
    31  	It("Podman create with specified static IP has correct IP", func() {
    32  		ip := GetSafeIPAddress()
    33  		result := podmanTest.Podman([]string{"create", "--name", "test", "--ip", ip, ALPINE, "ip", "addr"})
    34  		result.WaitWithDefaultTimeout()
    35  		// Rootless static ip assignment without network should error
    36  		if isRootless() {
    37  			Expect(result).Should(Exit(125))
    38  		} else {
    39  			Expect(result).Should(ExitCleanly())
    40  
    41  			result = podmanTest.Podman([]string{"start", "-a", "test"})
    42  			result.WaitWithDefaultTimeout()
    43  			Expect(result).Should(ExitCleanly())
    44  			Expect(result.OutputToString()).To(ContainSubstring(ip + "/16"))
    45  		}
    46  	})
    47  
    48  	It("Podman create two containers with the same IP", func() {
    49  		SkipIfRootless("--ip not supported without network in rootless mode")
    50  		ip := GetSafeIPAddress()
    51  		result := podmanTest.Podman([]string{"create", "--log-driver", "k8s-file", "--name", "test1", "--ip", ip, ALPINE, "sleep", "999"})
    52  		result.WaitWithDefaultTimeout()
    53  		Expect(result).Should(ExitCleanly())
    54  		result = podmanTest.Podman([]string{"create", "--log-driver", "k8s-file", "--name", "test2", "--ip", ip, ALPINE, "ip", "addr"})
    55  		result.WaitWithDefaultTimeout()
    56  		Expect(result).Should(ExitCleanly())
    57  		result = podmanTest.Podman([]string{"start", "test1"})
    58  		result.WaitWithDefaultTimeout()
    59  		Expect(result).Should(ExitCleanly())
    60  
    61  		// race prevention: wait until IP address is assigned and
    62  		// container is running.
    63  		for i := 0; i < 5; i++ {
    64  			result = podmanTest.Podman([]string{"inspect", "--format", "{{.State.Status}} {{.NetworkSettings.IPAddress}}", "test1"})
    65  			result.WaitWithDefaultTimeout()
    66  			Expect(result).Should(ExitCleanly())
    67  			if result.OutputToString() == "running "+ip {
    68  				break
    69  			}
    70  			time.Sleep(1 * time.Second)
    71  		}
    72  		Expect(result.OutputToString()).To(Equal("running " + ip))
    73  
    74  		// test1 container is running with the given IP.
    75  		result = podmanTest.Podman([]string{"start", "-a", "test2"})
    76  		result.WaitWithDefaultTimeout()
    77  		Expect(result).To(ExitWithError())
    78  		if podmanTest.NetworkBackend == CNI {
    79  			Expect(result.ErrorToString()).To(ContainSubstring("requested IP address %s is not available", ip))
    80  		} else if podmanTest.NetworkBackend == Netavark {
    81  			Expect(result.ErrorToString()).To(ContainSubstring("requested ip address %s is already allocated", ip))
    82  		}
    83  	})
    84  })