github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/test/e2e/events_test.go (about) 1 package integration 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "sync" 8 "time" 9 10 "github.com/hanks177/podman/v4/libpod/events" 11 . "github.com/hanks177/podman/v4/test/utils" 12 "github.com/containers/storage/pkg/stringid" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gexec" 16 ) 17 18 var _ = Describe("Podman events", func() { 19 var ( 20 tempdir string 21 podmanTest *PodmanTestIntegration 22 ) 23 24 BeforeEach(func() { 25 var err error 26 tempdir, err = CreateTempDirInTempDir() 27 if err != nil { 28 os.Exit(1) 29 } 30 podmanTest = PodmanTestCreate(tempdir) 31 podmanTest.Setup() 32 }) 33 34 AfterEach(func() { 35 podmanTest.Cleanup() 36 f := CurrentGinkgoTestDescription() 37 processTestResult(f) 38 }) 39 40 // For most, all, of these tests we do not "live" test following a log because it may make a fragile test 41 // system more complex. Instead we run the "events" and then verify that the events are processed correctly. 42 // Perhaps a future version of this test would put events in a go func and send output back over a channel 43 // while events occur. 44 45 // These tests are only known to work on Fedora ATM. Other distributions 46 // will be skipped. 47 It("podman events", func() { 48 SkipIfNotFedora() 49 _, ec, _ := podmanTest.RunLsContainer("") 50 Expect(ec).To(Equal(0)) 51 result := podmanTest.Podman([]string{"events", "--stream=false"}) 52 result.WaitWithDefaultTimeout() 53 Expect(result).Should(Exit(0)) 54 }) 55 56 It("podman events with an event filter", func() { 57 SkipIfNotFedora() 58 _, ec, _ := podmanTest.RunLsContainer("") 59 Expect(ec).To(Equal(0)) 60 result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "event=start"}) 61 result.WaitWithDefaultTimeout() 62 Expect(result).Should(Exit(0)) 63 Expect(len(result.OutputToStringArray())).To(BeNumerically(">=", 1), "Number of events") 64 date := time.Now().Format("2006-01-02") 65 Expect(result.OutputToStringArray()).To(ContainElement(HavePrefix(date)), "event log has correct timestamp") 66 }) 67 68 It("podman events with an event filter and container=cid", func() { 69 _, ec, cid := podmanTest.RunLsContainer("") 70 Expect(ec).To(Equal(0)) 71 _, ec2, cid2 := podmanTest.RunLsContainer("") 72 Expect(ec2).To(Equal(0)) 73 time.Sleep(5 * time.Second) 74 result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "event=start", "--filter", fmt.Sprintf("container=%s", cid)}) 75 result.WaitWithDefaultTimeout() 76 Expect(result).Should(Exit(0)) 77 events := result.OutputToStringArray() 78 Expect(events).To(HaveLen(1), "number of events") 79 Expect(events[0]).To(ContainSubstring(cid), "event log includes CID") 80 Expect(events[0]).To(Not(ContainSubstring(cid2)), "event log does not include second CID") 81 }) 82 83 It("podman events with a type and filter container=id", func() { 84 SkipIfNotFedora() 85 _, ec, cid := podmanTest.RunLsContainer("") 86 Expect(ec).To(Equal(0)) 87 result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "type=pod", "--filter", fmt.Sprintf("container=%s", cid)}) 88 result.WaitWithDefaultTimeout() 89 Expect(result).Should(Exit(0)) 90 Expect(result.OutputToStringArray()).To(BeEmpty()) 91 }) 92 93 It("podman events with a type", func() { 94 SkipIfNotFedora() 95 setup := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:foobarpod", ALPINE, "top"}) 96 setup.WaitWithDefaultTimeout() 97 stop := podmanTest.Podman([]string{"pod", "stop", "foobarpod"}) 98 stop.WaitWithDefaultTimeout() 99 Expect(stop).Should(Exit(0)) 100 Expect(setup).Should(Exit(0)) 101 result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "type=pod", "--filter", "pod=foobarpod"}) 102 result.WaitWithDefaultTimeout() 103 Expect(result).Should(Exit(0)) 104 events := result.OutputToStringArray() 105 fmt.Println(events) 106 Expect(len(events)).To(BeNumerically(">=", 2), "Number of events") 107 Expect(events).To(ContainElement(ContainSubstring(" pod create "))) 108 Expect(events).To(ContainElement(ContainSubstring(" pod stop "))) 109 Expect(events).To(ContainElement(ContainSubstring("name=foobarpod"))) 110 }) 111 112 It("podman events --since", func() { 113 SkipIfNotFedora() 114 _, ec, _ := podmanTest.RunLsContainer("") 115 Expect(ec).To(Equal(0)) 116 result := podmanTest.Podman([]string{"events", "--stream=false", "--since", "1m"}) 117 result.WaitWithDefaultTimeout() 118 Expect(result).Should(Exit(0)) 119 }) 120 121 It("podman events --until", func() { 122 SkipIfNotFedora() 123 _, ec, _ := podmanTest.RunLsContainer("") 124 Expect(ec).To(Equal(0)) 125 result := podmanTest.Podman([]string{"events", "--stream=false", "--until", "1h"}) 126 result.WaitWithDefaultTimeout() 127 Expect(result).Should(Exit(0)) 128 }) 129 130 It("podman events format", func() { 131 SkipIfNotFedora() 132 _, ec, _ := podmanTest.RunLsContainer("") 133 Expect(ec).To(Equal(0)) 134 135 test := podmanTest.Podman([]string{"events", "--stream=false", "--format", "json"}) 136 test.WaitWithDefaultTimeout() 137 Expect(test).To(Exit(0)) 138 139 jsonArr := test.OutputToStringArray() 140 Expect(test.OutputToStringArray()).ShouldNot(BeEmpty()) 141 142 event := events.Event{} 143 err := json.Unmarshal([]byte(jsonArr[0]), &event) 144 Expect(err).ToNot(HaveOccurred()) 145 146 test = podmanTest.Podman([]string{"events", "--stream=false", "--format", "{{json.}}"}) 147 test.WaitWithDefaultTimeout() 148 Expect(test).To(Exit(0)) 149 150 jsonArr = test.OutputToStringArray() 151 Expect(test.OutputToStringArray()).ShouldNot(BeEmpty()) 152 153 event = events.Event{} 154 err = json.Unmarshal([]byte(jsonArr[0]), &event) 155 Expect(err).ToNot(HaveOccurred()) 156 }) 157 158 It("podman events --until future", func() { 159 name1 := stringid.GenerateNonCryptoID() 160 name2 := stringid.GenerateNonCryptoID() 161 name3 := stringid.GenerateNonCryptoID() 162 session := podmanTest.Podman([]string{"create", "--name", name1, ALPINE}) 163 session.WaitWithDefaultTimeout() 164 Expect(session).Should(Exit(0)) 165 166 var wg sync.WaitGroup 167 wg.Add(1) 168 go func() { 169 defer GinkgoRecover() 170 defer wg.Done() 171 172 // wait 2 seconds to be sure events is running 173 time.Sleep(time.Second * 2) 174 session = podmanTest.Podman([]string{"create", "--name", name2, ALPINE}) 175 session.WaitWithDefaultTimeout() 176 Expect(session).Should(Exit(0)) 177 session = podmanTest.Podman([]string{"create", "--name", name3, ALPINE}) 178 session.WaitWithDefaultTimeout() 179 Expect(session).Should(Exit(0)) 180 }() 181 182 // unix timestamp in 10 seconds 183 until := time.Now().Add(time.Second * 10).Unix() 184 result := podmanTest.Podman([]string{"events", "--since", "30s", "--until", fmt.Sprint(until)}) 185 result.Wait(11) 186 Expect(result).Should(Exit(0)) 187 Expect(result.OutputToString()).To(ContainSubstring(name1)) 188 Expect(result.OutputToString()).To(ContainSubstring(name2)) 189 Expect(result.OutputToString()).To(ContainSubstring(name3)) 190 191 // string duration in 10 seconds 192 untilT := time.Now().Add(time.Second * 9) 193 result = podmanTest.Podman([]string{"events", "--since", "30s", "--until", "10s"}) 194 result.Wait(11) 195 Expect(result).Should(Exit(0)) 196 tEnd := time.Now() 197 outDur := tEnd.Sub(untilT) 198 diff := outDur.Seconds() > 0 199 Expect(diff).To(Equal(true)) 200 Expect(result.OutputToString()).To(ContainSubstring(name1)) 201 Expect(result.OutputToString()).To(ContainSubstring(name2)) 202 Expect(result.OutputToString()).To(ContainSubstring(name3)) 203 204 wg.Wait() 205 }) 206 207 It("podman events pod creation", func() { 208 create := podmanTest.Podman([]string{"pod", "create", "--infra=false", "--name", "foobarpod"}) 209 create.WaitWithDefaultTimeout() 210 Expect(create).Should(Exit(0)) 211 id := create.OutputToString() 212 result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "pod=" + id}) 213 result.WaitWithDefaultTimeout() 214 Expect(result).Should(Exit(0)) 215 Expect(result.OutputToStringArray()).To(HaveLen(1)) 216 Expect(result.OutputToString()).To(ContainSubstring("create")) 217 }) 218 219 })