github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/test/e2e/create_staticip_test.go (about)

     1  // +build !remoteclient
     2  
     3  package integration
     4  
     5  import (
     6  	"os"
     7  	"time"
     8  
     9  	. "github.com/containers/libpod/test/utils"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Podman create with --ip flag", func() {
    15  	var (
    16  		tempdir    string
    17  		err        error
    18  		podmanTest *PodmanTestIntegration
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		SkipIfRootless()
    23  		tempdir, err = CreateTempDirInTempDir()
    24  		if err != nil {
    25  			os.Exit(1)
    26  		}
    27  		podmanTest = PodmanTestCreate(tempdir)
    28  		podmanTest.Setup()
    29  		podmanTest.SeedImages()
    30  		// Cleanup the CNI networks used by the tests
    31  		os.RemoveAll("/var/lib/cni/networks/podman")
    32  	})
    33  
    34  	AfterEach(func() {
    35  		podmanTest.Cleanup()
    36  		f := CurrentGinkgoTestDescription()
    37  		processTestResult(f)
    38  
    39  	})
    40  
    41  	It("Podman create --ip with garbage address", func() {
    42  		result := podmanTest.Podman([]string{"create", "--name", "test", "--ip", "114232346", ALPINE, "ls"})
    43  		result.WaitWithDefaultTimeout()
    44  		Expect(result).To(ExitWithError())
    45  	})
    46  
    47  	It("Podman create --ip with v6 address", func() {
    48  		result := podmanTest.Podman([]string{"create", "--name", "test", "--ip", "2001:db8:bad:beef::1", ALPINE, "ls"})
    49  		result.WaitWithDefaultTimeout()
    50  		Expect(result).To(ExitWithError())
    51  	})
    52  
    53  	It("Podman create --ip with non-allocatable IP", func() {
    54  		result := podmanTest.Podman([]string{"create", "--name", "test", "--ip", "203.0.113.124", ALPINE, "ls"})
    55  		result.WaitWithDefaultTimeout()
    56  		Expect(result.ExitCode()).To(Equal(0))
    57  
    58  		result = podmanTest.Podman([]string{"start", "test"})
    59  		result.WaitWithDefaultTimeout()
    60  		Expect(result).To(ExitWithError())
    61  	})
    62  
    63  	It("Podman create with specified static IP has correct IP", func() {
    64  		ip := GetRandomIPAddress()
    65  		result := podmanTest.Podman([]string{"create", "--name", "test", "--ip", ip, ALPINE, "ip", "addr"})
    66  		result.WaitWithDefaultTimeout()
    67  		Expect(result.ExitCode()).To(Equal(0))
    68  
    69  		result = podmanTest.Podman([]string{"start", "test"})
    70  		result.WaitWithDefaultTimeout()
    71  		Expect(result.ExitCode()).To(Equal(0))
    72  
    73  		result = podmanTest.Podman([]string{"logs", "test"})
    74  		result.WaitWithDefaultTimeout()
    75  		Expect(result.ExitCode()).To(Equal(0))
    76  		Expect(result.OutputToString()).To(ContainSubstring(ip + "/16"))
    77  	})
    78  
    79  	It("Podman create two containers with the same IP", func() {
    80  		ip := GetRandomIPAddress()
    81  		result := podmanTest.Podman([]string{"create", "--name", "test1", "--ip", ip, ALPINE, "sleep", "999"})
    82  		result.WaitWithDefaultTimeout()
    83  		Expect(result.ExitCode()).To(Equal(0))
    84  		result = podmanTest.Podman([]string{"create", "--name", "test2", "--ip", ip, ALPINE, "ip", "addr"})
    85  		result.WaitWithDefaultTimeout()
    86  		Expect(result.ExitCode()).To(Equal(0))
    87  		result = podmanTest.Podman([]string{"start", "test1"})
    88  		result.WaitWithDefaultTimeout()
    89  		Expect(result.ExitCode()).To(Equal(0))
    90  
    91  		// race prevention: wait until IP address is assigned
    92  		for i := 0; i < 5; i++ {
    93  			result = podmanTest.Podman([]string{"inspect", "--format", "{{.NetworkSettings.IPAddress}}", "test1"})
    94  			result.WaitWithDefaultTimeout()
    95  			Expect(result.ExitCode()).To(Equal(0))
    96  			if result.OutputToString() != "" {
    97  				break
    98  			}
    99  			time.Sleep(1 * time.Second)
   100  		}
   101  		Expect(result.OutputToString()).To(Equal(ip))
   102  
   103  		// test1 container is running with the given IP.
   104  		result = podmanTest.Podman([]string{"start", "test2"})
   105  		result.WaitWithDefaultTimeout()
   106  		Expect(result).To(ExitWithError())
   107  		Expect(result.ErrorToString()).To(ContainSubstring("requested IP address " + ip + " is not available"))
   108  	})
   109  })