github.com/containers/podman/v4@v4.9.4/pkg/machine/e2e/basic_test.go (about)

     1  package e2e_test
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"net/http"
     7  	"net/url"
     8  	"time"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("run basic podman commands", func() {
    16  	var (
    17  		mb      *machineTestBuilder
    18  		testDir string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		testDir, mb = setup()
    23  	})
    24  	AfterEach(func() {
    25  		teardown(originalHomeDir, testDir, mb)
    26  	})
    27  
    28  	It("Basic ops", func() {
    29  		// golangci-lint has trouble with actually skipping tests marked Skip
    30  		// so skip it on cirrus envs and where CIRRUS_CI isn't set.
    31  		name := randomString()
    32  		i := new(initMachine)
    33  		session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run()
    34  		Expect(err).ToNot(HaveOccurred())
    35  		Expect(session).To(Exit(0))
    36  
    37  		bm := basicMachine{}
    38  		imgs, err := mb.setCmd(bm.withPodmanCommand([]string{"images", "-q"})).run()
    39  		Expect(err).ToNot(HaveOccurred())
    40  		Expect(imgs).To(Exit(0))
    41  		Expect(imgs.outputToStringSlice()).To(BeEmpty())
    42  
    43  		newImgs, err := mb.setCmd(bm.withPodmanCommand([]string{"pull", "quay.io/libpod/alpine_nginx"})).run()
    44  		Expect(err).ToNot(HaveOccurred())
    45  		Expect(newImgs).To(Exit(0))
    46  		Expect(newImgs.outputToStringSlice()).To(HaveLen(1))
    47  
    48  		runAlp, err := mb.setCmd(bm.withPodmanCommand([]string{"run", "quay.io/libpod/alpine_nginx", "cat", "/etc/os-release"})).run()
    49  		Expect(err).ToNot(HaveOccurred())
    50  		Expect(runAlp).To(Exit(0))
    51  		Expect(runAlp.outputToString()).To(ContainSubstring("Alpine Linux"))
    52  
    53  		rmCon, err := mb.setCmd(bm.withPodmanCommand([]string{"rm", "-a"})).run()
    54  		Expect(err).ToNot(HaveOccurred())
    55  		Expect(rmCon).To(Exit(0))
    56  	})
    57  
    58  	It("Podman ops with port forwarding and gvproxy", func() {
    59  		name := randomString()
    60  		i := new(initMachine)
    61  		session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run()
    62  		Expect(err).ToNot(HaveOccurred())
    63  		Expect(session).To(Exit(0))
    64  
    65  		ctrName := "test"
    66  		bm := basicMachine{}
    67  		runAlp, err := mb.setCmd(bm.withPodmanCommand([]string{"run", "-dt", "--name", ctrName, "-p", "62544:80", "quay.io/libpod/alpine_nginx"})).run()
    68  		Expect(err).ToNot(HaveOccurred())
    69  		Expect(runAlp).To(Exit(0))
    70  		testHTTPServer("62544", false, "podman rulez")
    71  
    72  		// Test exec in machine scenario: https://github.com/containers/podman/issues/20821
    73  		exec, err := mb.setCmd(bm.withPodmanCommand([]string{"exec", ctrName, "true"})).run()
    74  		Expect(err).ToNot(HaveOccurred())
    75  		Expect(exec).To(Exit(0))
    76  
    77  		out, err := pgrep("gvproxy")
    78  		Expect(err).ToNot(HaveOccurred())
    79  		Expect(out).ToNot(BeEmpty())
    80  
    81  		rmCon, err := mb.setCmd(bm.withPodmanCommand([]string{"rm", "-af"})).run()
    82  		Expect(err).ToNot(HaveOccurred())
    83  		Expect(rmCon).To(Exit(0))
    84  		testHTTPServer("62544", true, "")
    85  
    86  		stop := new(stopMachine)
    87  		stopSession, err := mb.setCmd(stop).run()
    88  		Expect(err).ToNot(HaveOccurred())
    89  		Expect(stopSession).To(Exit(0))
    90  
    91  		// gxproxy should exit after machine is stopped
    92  		out, _ = pgrep("gvproxy")
    93  		Expect(out).ToNot(ContainSubstring("gvproxy"))
    94  	})
    95  
    96  })
    97  
    98  func testHTTPServer(port string, shouldErr bool, expectedResponse string) {
    99  	address := url.URL{
   100  		Scheme: "http",
   101  		Host:   net.JoinHostPort("localhost", port),
   102  	}
   103  
   104  	interval := 250 * time.Millisecond
   105  	var err error
   106  	var resp *http.Response
   107  	for i := 0; i < 6; i++ {
   108  		resp, err = http.Get(address.String())
   109  		if err != nil && shouldErr {
   110  			Expect(err.Error()).To(ContainSubstring(expectedResponse))
   111  			return
   112  		}
   113  		if err == nil {
   114  			defer resp.Body.Close()
   115  			break
   116  		}
   117  		time.Sleep(interval)
   118  		interval *= 2
   119  	}
   120  	Expect(err).ToNot(HaveOccurred())
   121  
   122  	body, err := io.ReadAll(resp.Body)
   123  	Expect(err).ToNot(HaveOccurred())
   124  	Expect(string(body)).Should(Equal(expectedResponse))
   125  }