github.com/containers/podman/v4@v4.9.4/pkg/bindings/test/attach_test.go (about) 1 package bindings_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "time" 7 8 "github.com/containers/podman/v4/libpod/define" 9 "github.com/containers/podman/v4/pkg/bindings/containers" 10 "github.com/containers/podman/v4/pkg/specgen" 11 . "github.com/onsi/ginkgo/v2" 12 . "github.com/onsi/gomega" 13 "github.com/onsi/gomega/gexec" 14 ) 15 16 var _ = Describe("Podman containers attach", func() { 17 var ( 18 bt *bindingTest 19 s *gexec.Session 20 ) 21 22 BeforeEach(func() { 23 bt = newBindingTest() 24 bt.RestoreImagesFromCache() 25 s = bt.startAPIService() 26 time.Sleep(1 * time.Second) 27 err := bt.NewConnection() 28 Expect(err).ShouldNot(HaveOccurred()) 29 }) 30 31 AfterEach(func() { 32 s.Kill() 33 bt.cleanup() 34 }) 35 36 It("can run top in container", func() { 37 name := "TopAttachTest" 38 id, err := bt.RunTopContainer(&name, nil) 39 Expect(err).ShouldNot(HaveOccurred()) 40 41 tickTock := time.NewTimer(2 * time.Second) 42 go func() { 43 <-tickTock.C 44 timeout := uint(5) 45 err := containers.Stop(bt.conn, id, new(containers.StopOptions).WithTimeout(timeout)) 46 if err != nil { 47 _, writeErr := GinkgoWriter.Write([]byte(err.Error())) 48 Expect(writeErr).ShouldNot(HaveOccurred()) 49 } 50 }() 51 52 stdout := &bytes.Buffer{} 53 stderr := &bytes.Buffer{} 54 go func() { 55 defer GinkgoRecover() 56 options := new(containers.AttachOptions).WithLogs(true).WithStream(true) 57 err := containers.Attach(bt.conn, id, nil, stdout, stderr, nil, options) 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, nil) 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, new(containers.WaitOptions).WithCondition([]define.ContainerStatus{wait})) 80 Expect(err).ShouldNot(HaveOccurred()) 81 82 tickTock := time.NewTimer(2 * time.Second) 83 go func() { 84 <-tickTock.C 85 err := containers.Stop(bt.conn, ctnr.ID, new(containers.StopOptions).WithTimeout(uint(5))) 86 if err != nil { 87 fmt.Fprint(GinkgoWriter, err.Error()) 88 } 89 }() 90 91 msg := "Hello, World" 92 stdin := &bytes.Buffer{} 93 stdin.WriteString(msg + "\n") 94 95 stdout := &bytes.Buffer{} 96 stderr := &bytes.Buffer{} 97 go func() { 98 defer GinkgoRecover() 99 options := new(containers.AttachOptions).WithStream(true) 100 err := containers.Attach(bt.conn, ctnr.ID, stdin, stdout, stderr, nil, options) 101 Expect(err).ShouldNot(HaveOccurred()) 102 }() 103 104 time.Sleep(5 * time.Second) 105 // Tty==true so we get echo'ed stdin + expected output 106 Expect(stdout.String()).Should(Equal(fmt.Sprintf("%[1]s\r\n%[1]s\r\n", msg))) 107 Expect(stderr.String()).Should(BeEmpty()) 108 }) 109 })