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

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	. "github.com/containers/podman/v2/test/utils"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Podman port", 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  	})
    29  
    30  	AfterEach(func() {
    31  		podmanTest.Cleanup()
    32  		f := CurrentGinkgoTestDescription()
    33  		processTestResult(f)
    34  
    35  	})
    36  
    37  	It("podman port all and latest", func() {
    38  		result := podmanTest.Podman([]string{"port", "-a", "-l"})
    39  		result.WaitWithDefaultTimeout()
    40  		Expect(result).To(ExitWithError())
    41  	})
    42  
    43  	It("podman port all and extra", func() {
    44  		result := podmanTest.Podman([]string{"port", "-a", "foobar"})
    45  		result.WaitWithDefaultTimeout()
    46  		Expect(result).To(ExitWithError())
    47  	})
    48  
    49  	It("podman port -l nginx", func() {
    50  		session, cid := podmanTest.RunNginxWithHealthCheck("test1")
    51  		Expect(session.ExitCode()).To(Equal(0))
    52  
    53  		if err := podmanTest.RunHealthCheck(cid); err != nil {
    54  			Fail(err.Error())
    55  		}
    56  
    57  		if !IsRemote() {
    58  			cid = "-l"
    59  		}
    60  		result := podmanTest.Podman([]string{"port", cid})
    61  		result.WaitWithDefaultTimeout()
    62  		Expect(result.ExitCode()).To(Equal(0))
    63  		port := strings.Split(result.OutputToStringArray()[0], ":")[1]
    64  		Expect(result.LineInOutputStartsWith(fmt.Sprintf("80/tcp -> 0.0.0.0:%s", port))).To(BeTrue())
    65  	})
    66  
    67  	It("podman container port  -l nginx", func() {
    68  		session, cid := podmanTest.RunNginxWithHealthCheck("")
    69  		Expect(session.ExitCode()).To(Equal(0))
    70  
    71  		if err := podmanTest.RunHealthCheck(cid); err != nil {
    72  			Fail(err.Error())
    73  		}
    74  
    75  		if !IsRemote() {
    76  			cid = "-l"
    77  		}
    78  		result := podmanTest.Podman([]string{"container", "port", cid})
    79  		result.WaitWithDefaultTimeout()
    80  		Expect(result.ExitCode()).To(Equal(0))
    81  		port := strings.Split(result.OutputToStringArray()[0], ":")[1]
    82  		Expect(result.LineInOutputStartsWith(fmt.Sprintf("80/tcp -> 0.0.0.0:%s", port))).To(BeTrue())
    83  	})
    84  
    85  	It("podman port -l port nginx", func() {
    86  		session, cid := podmanTest.RunNginxWithHealthCheck("")
    87  		Expect(session.ExitCode()).To(Equal(0))
    88  
    89  		if err := podmanTest.RunHealthCheck(cid); err != nil {
    90  			Fail(err.Error())
    91  		}
    92  
    93  		if !IsRemote() {
    94  			cid = "-l"
    95  		}
    96  		result := podmanTest.Podman([]string{"port", cid, "80"})
    97  		result.WaitWithDefaultTimeout()
    98  		Expect(result.ExitCode()).To(Equal(0))
    99  		port := strings.Split(result.OutputToStringArray()[0], ":")[1]
   100  		Expect(result.LineInOutputStartsWith(fmt.Sprintf("0.0.0.0:%s", port))).To(BeTrue())
   101  	})
   102  
   103  	It("podman port -a nginx", func() {
   104  		session, cid := podmanTest.RunNginxWithHealthCheck("")
   105  		Expect(session.ExitCode()).To(Equal(0))
   106  
   107  		if err := podmanTest.RunHealthCheck(cid); err != nil {
   108  			Fail(err.Error())
   109  		}
   110  
   111  		result := podmanTest.Podman([]string{"port", "-a"})
   112  		result.WaitWithDefaultTimeout()
   113  		Expect(result.ExitCode()).To(Equal(0))
   114  	})
   115  
   116  	It("podman port nginx by name", func() {
   117  		session, cid := podmanTest.RunNginxWithHealthCheck("portcheck")
   118  		Expect(session.ExitCode()).To(Equal(0))
   119  
   120  		if err := podmanTest.RunHealthCheck(cid); err != nil {
   121  			Fail(err.Error())
   122  		}
   123  
   124  		result := podmanTest.Podman([]string{"port", "portcheck"})
   125  		result.WaitWithDefaultTimeout()
   126  		Expect(result.ExitCode()).To(Equal(0))
   127  		result.LineInOutputStartsWith("80/tcp -> 0.0.0.0:")
   128  	})
   129  
   130  	It("podman port multiple ports", func() {
   131  		// Acquire and release locks
   132  		lock1 := GetPortLock("5000")
   133  		defer lock1.Unlock()
   134  		lock2 := GetPortLock("5001")
   135  		defer lock2.Unlock()
   136  
   137  		setup := podmanTest.Podman([]string{"run", "--name", "test", "-dt", "-p", "5000:5000", "-p", "5001:5001", ALPINE, "top"})
   138  		setup.WaitWithDefaultTimeout()
   139  		Expect(setup.ExitCode()).To(BeZero())
   140  
   141  		// Check that the first port was honored
   142  		result1 := podmanTest.Podman([]string{"port", "test", "5000"})
   143  		result1.WaitWithDefaultTimeout()
   144  		Expect(result1.ExitCode()).To(BeZero())
   145  		Expect(result1.LineInOutputStartsWith("0.0.0.0:5000")).To(BeTrue())
   146  
   147  		// Check that the second port was honored
   148  		result2 := podmanTest.Podman([]string{"port", "test", "5001"})
   149  		result2.WaitWithDefaultTimeout()
   150  		Expect(result2.ExitCode()).To(BeZero())
   151  		Expect(result2.LineInOutputStartsWith("0.0.0.0:5001")).To(BeTrue())
   152  	})
   153  })