github.com/containers/podman/v4@v4.9.4/pkg/bindings/test/exec_test.go (about)

     1  package bindings_test
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/containers/podman/v4/pkg/api/handlers"
     7  	"github.com/containers/podman/v4/pkg/bindings/containers"
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("Podman containers exec", func() {
    14  	var (
    15  		bt *bindingTest
    16  		s  *gexec.Session
    17  	)
    18  
    19  	BeforeEach(func() {
    20  		bt = newBindingTest()
    21  		bt.RestoreImagesFromCache()
    22  		s = bt.startAPIService()
    23  		time.Sleep(1 * time.Second)
    24  		err := bt.NewConnection()
    25  		Expect(err).ToNot(HaveOccurred())
    26  	})
    27  
    28  	AfterEach(func() {
    29  		s.Kill()
    30  		bt.cleanup()
    31  	})
    32  
    33  	It("Podman exec create+start makes an exec session", func() {
    34  		name := "testCtr"
    35  		cid, err := bt.RunTopContainer(&name, nil)
    36  		Expect(err).ToNot(HaveOccurred())
    37  
    38  		execConfig := new(handlers.ExecCreateConfig)
    39  		execConfig.Cmd = []string{"echo", "hello world"}
    40  
    41  		sessionID, err := containers.ExecCreate(bt.conn, name, execConfig)
    42  		Expect(err).ToNot(HaveOccurred())
    43  		Expect(sessionID).To(Not(Equal("")))
    44  
    45  		inspectOut, err := containers.ExecInspect(bt.conn, sessionID, nil)
    46  		Expect(err).ToNot(HaveOccurred())
    47  		Expect(inspectOut.ContainerID).To(Equal(cid))
    48  		Expect(inspectOut.ProcessConfig.Entrypoint).To(Equal("echo"))
    49  		Expect(inspectOut.ProcessConfig.Arguments).To(HaveLen(1))
    50  		Expect(inspectOut.ProcessConfig.Arguments[0]).To(Equal("hello world"))
    51  
    52  		err = containers.ExecStart(bt.conn, sessionID, nil)
    53  		Expect(err).ToNot(HaveOccurred())
    54  
    55  		inspectOut, err = containers.ExecInspect(bt.conn, sessionID, nil)
    56  		Expect(err).ToNot(HaveOccurred())
    57  		Expect(inspectOut.ContainerID).To(Equal(cid))
    58  		Expect(inspectOut.Running).To(BeFalse(), "session should not be running")
    59  		Expect(inspectOut.ExitCode).To(Equal(0), "exit code from echo")
    60  	})
    61  
    62  	It("Podman exec create with bad command fails", func() {
    63  		name := "testCtr"
    64  		_, err := bt.RunTopContainer(&name, nil)
    65  		Expect(err).ToNot(HaveOccurred())
    66  
    67  		execConfig := new(handlers.ExecCreateConfig)
    68  
    69  		_, err = containers.ExecCreate(bt.conn, name, execConfig)
    70  		Expect(err).To(HaveOccurred())
    71  	})
    72  
    73  	It("Podman exec create with invalid container fails", func() {
    74  		execConfig := new(handlers.ExecCreateConfig)
    75  		execConfig.Cmd = []string{"echo", "hello world"}
    76  
    77  		_, err := containers.ExecCreate(bt.conn, "doesnotexist", execConfig)
    78  		Expect(err).To(HaveOccurred())
    79  	})
    80  
    81  	It("Podman exec inspect on invalid session fails", func() {
    82  		_, err := containers.ExecInspect(bt.conn, "0000000000000000000000000000000000000000000000000000000000000000", nil)
    83  		Expect(err).To(HaveOccurred())
    84  	})
    85  })