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

     1  // +build !remoteclient
     2  
     3  package integration
     4  
     5  import (
     6  	"fmt"
     7  	"net/http"
     8  	"os"
     9  	"time"
    10  
    11  	. "github.com/containers/libpod/test/utils"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    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()
    25  		tempdir, err = CreateTempDirInTempDir()
    26  		if err != nil {
    27  			os.Exit(1)
    28  		}
    29  		podmanTest = PodmanTestCreate(tempdir)
    30  		podmanTest.Setup()
    31  		podmanTest.SeedImages()
    32  		// Cleanup the CNI networks used by the tests
    33  		os.RemoveAll("/var/lib/cni/networks/podman")
    34  	})
    35  
    36  	AfterEach(func() {
    37  		podmanTest.Cleanup()
    38  		f := CurrentGinkgoTestDescription()
    39  		processTestResult(f)
    40  
    41  	})
    42  
    43  	It("Podman run --ip with garbage address", func() {
    44  		result := podmanTest.Podman([]string{"run", "-ti", "--ip", "114232346", ALPINE, "ls"})
    45  		result.WaitWithDefaultTimeout()
    46  		Expect(result).To(ExitWithError())
    47  	})
    48  
    49  	It("Podman run --ip with v6 address", func() {
    50  		result := podmanTest.Podman([]string{"run", "-ti", "--ip", "2001:db8:bad:beef::1", ALPINE, "ls"})
    51  		result.WaitWithDefaultTimeout()
    52  		Expect(result).To(ExitWithError())
    53  	})
    54  
    55  	It("Podman run --ip with non-allocatable IP", func() {
    56  		result := podmanTest.Podman([]string{"run", "-ti", "--ip", "203.0.113.124", ALPINE, "ls"})
    57  		result.WaitWithDefaultTimeout()
    58  		Expect(result).To(ExitWithError())
    59  	})
    60  
    61  	It("Podman run with specified static IP has correct IP", func() {
    62  		ip := GetRandomIPAddress()
    63  		result := podmanTest.Podman([]string{"run", "-ti", "--ip", ip, ALPINE, "ip", "addr"})
    64  		result.WaitWithDefaultTimeout()
    65  		Expect(result.ExitCode()).To(Equal(0))
    66  		Expect(result.OutputToString()).To(ContainSubstring(ip + "/16"))
    67  	})
    68  
    69  	It("Podman run two containers with the same IP", func() {
    70  		ip := GetRandomIPAddress()
    71  		result := podmanTest.Podman([]string{"run", "-dt", "--ip", ip, nginx})
    72  		result.WaitWithDefaultTimeout()
    73  		Expect(result.ExitCode()).To(Equal(0))
    74  		for i := 0; i < 10; i++ {
    75  			fmt.Println("Waiting for nginx", err)
    76  			time.Sleep(1 * time.Second)
    77  			response, err := http.Get(fmt.Sprintf("http://%s", ip))
    78  			if err != nil {
    79  				continue
    80  			}
    81  			if response.StatusCode == http.StatusOK {
    82  				break
    83  			}
    84  		}
    85  		result = podmanTest.Podman([]string{"run", "-ti", "--ip", ip, ALPINE, "ip", "addr"})
    86  		result.WaitWithDefaultTimeout()
    87  		Expect(result).To(ExitWithError())
    88  	})
    89  })