github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/test/e2e/create_staticip_test.go (about)

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