github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/bindings/test/attach_test.go (about)

     1  package test_bindings
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/containers/podman/v2/libpod/define"
     9  	"github.com/containers/podman/v2/pkg/bindings"
    10  	"github.com/containers/podman/v2/pkg/bindings/containers"
    11  	"github.com/containers/podman/v2/pkg/specgen"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/onsi/gomega/gexec"
    15  )
    16  
    17  var _ = Describe("Podman containers attach", func() {
    18  	var (
    19  		bt *bindingTest
    20  		s  *gexec.Session
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		bt = newBindingTest()
    25  		bt.RestoreImagesFromCache()
    26  		s = bt.startAPIService()
    27  		time.Sleep(1 * time.Second)
    28  		err := bt.NewConnection()
    29  		Expect(err).ShouldNot(HaveOccurred())
    30  	})
    31  
    32  	AfterEach(func() {
    33  		s.Kill()
    34  		bt.cleanup()
    35  	})
    36  
    37  	It("can run top in container", func() {
    38  		name := "TopAttachTest"
    39  		id, err := bt.RunTopContainer(&name, nil, nil)
    40  		Expect(err).ShouldNot(HaveOccurred())
    41  
    42  		tickTock := time.NewTimer(2 * time.Second)
    43  		go func() {
    44  			<-tickTock.C
    45  			timeout := uint(5)
    46  			err := containers.Stop(bt.conn, id, &timeout)
    47  			if err != nil {
    48  				GinkgoWriter.Write([]byte(err.Error()))
    49  			}
    50  		}()
    51  
    52  		stdout := &bytes.Buffer{}
    53  		stderr := &bytes.Buffer{}
    54  		go func() {
    55  			defer GinkgoRecover()
    56  
    57  			err := containers.Attach(bt.conn, id, nil, bindings.PTrue, bindings.PTrue, nil, stdout, stderr, nil)
    58  			Expect(err).ShouldNot(HaveOccurred())
    59  		}()
    60  
    61  		time.Sleep(5 * time.Second)
    62  
    63  		// First character/First line of top output
    64  		Expect(stdout.String()).Should(ContainSubstring("Mem: "))
    65  	})
    66  
    67  	It("can echo data via cat in container", func() {
    68  		s := specgen.NewSpecGenerator(alpine.name, false)
    69  		s.Name = "CatAttachTest"
    70  		s.Terminal = true
    71  		s.Command = []string{"/bin/cat"}
    72  		ctnr, err := containers.CreateWithSpec(bt.conn, s)
    73  		Expect(err).ShouldNot(HaveOccurred())
    74  
    75  		err = containers.Start(bt.conn, ctnr.ID, nil)
    76  		Expect(err).ShouldNot(HaveOccurred())
    77  
    78  		wait := define.ContainerStateRunning
    79  		_, err = containers.Wait(bt.conn, ctnr.ID, &wait)
    80  		Expect(err).ShouldNot(HaveOccurred())
    81  
    82  		tickTock := time.NewTimer(2 * time.Second)
    83  		go func() {
    84  			<-tickTock.C
    85  			timeout := uint(5)
    86  			err := containers.Stop(bt.conn, ctnr.ID, &timeout)
    87  			if err != nil {
    88  				GinkgoWriter.Write([]byte(err.Error()))
    89  			}
    90  		}()
    91  
    92  		msg := "Hello, World"
    93  		stdin := &bytes.Buffer{}
    94  		stdin.WriteString(msg + "\n")
    95  
    96  		stdout := &bytes.Buffer{}
    97  		stderr := &bytes.Buffer{}
    98  		go func() {
    99  			defer GinkgoRecover()
   100  
   101  			err := containers.Attach(bt.conn, ctnr.ID, nil, bindings.PFalse, bindings.PTrue, stdin, stdout, stderr, nil)
   102  			Expect(err).ShouldNot(HaveOccurred())
   103  		}()
   104  
   105  		time.Sleep(5 * time.Second)
   106  		// Tty==true so we get echo'ed stdin + expected output
   107  		Expect(stdout.String()).Should(Equal(fmt.Sprintf("%[1]s\r\n%[1]s\r\n", msg)))
   108  		Expect(stderr.String()).Should(BeEmpty())
   109  	})
   110  })