initial commit
[govpp.git] / vendor / github.com / onsi / gomega / gexec / session_test.go
1 package gexec_test
2
3 import (
4         "os/exec"
5         "syscall"
6         "time"
7
8         . "github.com/onsi/gomega/gbytes"
9         . "github.com/onsi/gomega/gexec"
10
11         . "github.com/onsi/ginkgo"
12         . "github.com/onsi/gomega"
13 )
14
15 var _ = Describe("Session", func() {
16         var command *exec.Cmd
17         var session *Session
18
19         var outWriter, errWriter *Buffer
20
21         BeforeEach(func() {
22                 outWriter = nil
23                 errWriter = nil
24         })
25
26         JustBeforeEach(func() {
27                 command = exec.Command(fireflyPath)
28                 var err error
29                 session, err = Start(command, outWriter, errWriter)
30                 Ω(err).ShouldNot(HaveOccurred())
31         })
32
33         Context("running a command", func() {
34                 It("should start the process", func() {
35                         Ω(command.Process).ShouldNot(BeNil())
36                 })
37
38                 It("should wrap the process's stdout and stderr with gbytes buffers", func(done Done) {
39                         Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
40                         Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))
41                         defer session.Out.CancelDetects()
42
43                         select {
44                         case <-session.Out.Detect("Can we maybe vote on the whole murdering people issue"):
45                                 Eventually(session).Should(Exit(0))
46                         case <-session.Out.Detect("I swear by my pretty floral bonnet, I will end you."):
47                                 Eventually(session).Should(Exit(1))
48                         case <-session.Out.Detect("My work's illegal, but at least it's honest."):
49                                 Eventually(session).Should(Exit(2))
50                         }
51
52                         close(done)
53                 })
54
55                 It("should satisfy the gbytes.BufferProvider interface, passing Stdout", func() {
56                         Eventually(session).Should(Say("We've done the impossible, and that makes us mighty"))
57                         Eventually(session).Should(Exit())
58                 })
59         })
60
61         Describe("providing the exit code", func() {
62                 It("should provide the app's exit code", func() {
63                         Ω(session.ExitCode()).Should(Equal(-1))
64
65                         Eventually(session).Should(Exit())
66                         Ω(session.ExitCode()).Should(BeNumerically(">=", 0))
67                         Ω(session.ExitCode()).Should(BeNumerically("<", 3))
68                 })
69         })
70
71         Describe("wait", func() {
72                 It("should wait till the command exits", func() {
73                         Ω(session.ExitCode()).Should(Equal(-1))
74                         Ω(session.Wait().ExitCode()).Should(BeNumerically(">=", 0))
75                         Ω(session.Wait().ExitCode()).Should(BeNumerically("<", 3))
76                 })
77         })
78
79         Describe("exited", func() {
80                 It("should close when the command exits", func() {
81                         Eventually(session.Exited).Should(BeClosed())
82                         Ω(session.ExitCode()).ShouldNot(Equal(-1))
83                 })
84         })
85
86         Describe("kill", func() {
87                 It("should kill the command and don't wait for it to exit", func() {
88                         session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
89                         Ω(err).ShouldNot(HaveOccurred())
90
91                         session.Kill()
92                         Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
93                         Eventually(session).Should(Exit(128 + 9))
94                 })
95         })
96
97         Describe("interrupt", func() {
98                 It("should interrupt the command", func() {
99                         session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
100                         Ω(err).ShouldNot(HaveOccurred())
101
102                         session.Interrupt()
103                         Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
104                         Eventually(session).Should(Exit(128 + 2))
105                 })
106         })
107
108         Describe("terminate", func() {
109                 It("should terminate the command", func() {
110                         session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
111                         Ω(err).ShouldNot(HaveOccurred())
112
113                         session.Terminate()
114                         Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
115                         Eventually(session).Should(Exit(128 + 15))
116                 })
117         })
118
119         Describe("signal", func() {
120                 It("should send the signal to the command", func() {
121                         session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
122                         Ω(err).ShouldNot(HaveOccurred())
123
124                         session.Signal(syscall.SIGABRT)
125                         Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
126                         Eventually(session).Should(Exit(128 + 6))
127                 })
128         })
129
130         Context("tracking sessions", func() {
131                 BeforeEach(func() {
132                         KillAndWait()
133                 })
134
135                 Describe("kill", func() {
136                         It("should kill all the started sessions", func() {
137                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
138                                 Ω(err).ShouldNot(HaveOccurred())
139
140                                 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
141                                 Ω(err).ShouldNot(HaveOccurred())
142
143                                 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
144                                 Ω(err).ShouldNot(HaveOccurred())
145
146                                 Kill()
147
148                                 Eventually(session1).Should(Exit(128 + 9))
149                                 Eventually(session2).Should(Exit(128 + 9))
150                                 Eventually(session3).Should(Exit(128 + 9))
151                         })
152
153                         It("should not wait for exit", func() {
154                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
155                                 Ω(err).ShouldNot(HaveOccurred())
156
157                                 Kill()
158                                 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
159
160                                 Eventually(session1).Should(Exit(128 + 9))
161                         })
162
163                         It("should not track unstarted sessions", func() {
164                                 _, err := Start(exec.Command("does not exist", "10000000"), GinkgoWriter, GinkgoWriter)
165                                 Ω(err).Should(HaveOccurred())
166
167                                 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
168                                 Ω(err).ShouldNot(HaveOccurred())
169
170                                 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
171                                 Ω(err).ShouldNot(HaveOccurred())
172
173                                 Kill()
174
175                                 Eventually(session2).Should(Exit(128 + 9))
176                                 Eventually(session3).Should(Exit(128 + 9))
177                         })
178
179                 })
180
181                 Describe("killAndWait", func() {
182                         It("should kill all the started sessions and wait for them to finish", func() {
183                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
184                                 Ω(err).ShouldNot(HaveOccurred())
185
186                                 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
187                                 Ω(err).ShouldNot(HaveOccurred())
188
189                                 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
190                                 Ω(err).ShouldNot(HaveOccurred())
191
192                                 KillAndWait()
193                                 Ω(session1).Should(Exit(128+9), "Should have exited")
194                                 Ω(session2).Should(Exit(128+9), "Should have exited")
195                                 Ω(session3).Should(Exit(128+9), "Should have exited")
196                         })
197                 })
198
199                 Describe("terminate", func() {
200                         It("should terminate all the started sessions", func() {
201                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
202                                 Ω(err).ShouldNot(HaveOccurred())
203
204                                 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
205                                 Ω(err).ShouldNot(HaveOccurred())
206
207                                 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
208                                 Ω(err).ShouldNot(HaveOccurred())
209
210                                 Terminate()
211
212                                 Eventually(session1).Should(Exit(128 + 15))
213                                 Eventually(session2).Should(Exit(128 + 15))
214                                 Eventually(session3).Should(Exit(128 + 15))
215                         })
216
217                         It("should not wait for exit", func() {
218                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
219                                 Ω(err).ShouldNot(HaveOccurred())
220
221                                 Terminate()
222
223                                 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
224                         })
225                 })
226
227                 Describe("terminateAndWait", func() {
228                         It("should terminate all the started sessions, and wait for them to exit", func() {
229                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
230                                 Ω(err).ShouldNot(HaveOccurred())
231
232                                 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
233                                 Ω(err).ShouldNot(HaveOccurred())
234
235                                 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
236                                 Ω(err).ShouldNot(HaveOccurred())
237
238                                 TerminateAndWait()
239
240                                 Ω(session1).Should(Exit(128+15), "Should have exited")
241                                 Ω(session2).Should(Exit(128+15), "Should have exited")
242                                 Ω(session3).Should(Exit(128+15), "Should have exited")
243                         })
244                 })
245
246                 Describe("signal", func() {
247                         It("should signal all the started sessions", func() {
248                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
249                                 Ω(err).ShouldNot(HaveOccurred())
250
251                                 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
252                                 Ω(err).ShouldNot(HaveOccurred())
253
254                                 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
255                                 Ω(err).ShouldNot(HaveOccurred())
256
257                                 Signal(syscall.SIGABRT)
258
259                                 Eventually(session1).Should(Exit(128 + 6))
260                                 Eventually(session2).Should(Exit(128 + 6))
261                                 Eventually(session3).Should(Exit(128 + 6))
262                         })
263
264                         It("should not wait", func() {
265                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
266                                 Ω(err).ShouldNot(HaveOccurred())
267
268                                 Signal(syscall.SIGABRT)
269
270                                 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
271                         })
272                 })
273
274                 Describe("interrupt", func() {
275                         It("should interrupt all the started sessions, and not wait", func() {
276                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
277                                 Ω(err).ShouldNot(HaveOccurred())
278
279                                 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
280                                 Ω(err).ShouldNot(HaveOccurred())
281
282                                 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
283                                 Ω(err).ShouldNot(HaveOccurred())
284
285                                 Interrupt()
286
287                                 Eventually(session1).Should(Exit(128 + 2))
288                                 Eventually(session2).Should(Exit(128 + 2))
289                                 Eventually(session3).Should(Exit(128 + 2))
290                         })
291
292                         It("should not wait", func() {
293                                 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
294                                 Ω(err).ShouldNot(HaveOccurred())
295
296                                 Interrupt()
297
298                                 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
299                         })
300                 })
301         })
302
303         Context("when the command exits", func() {
304                 It("should close the buffers", func() {
305                         Eventually(session).Should(Exit())
306
307                         Ω(session.Out.Closed()).Should(BeTrue())
308                         Ω(session.Err.Closed()).Should(BeTrue())
309
310                         Ω(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
311                 })
312
313                 var So = It
314
315                 So("this means that eventually should short circuit", func() {
316                         t := time.Now()
317                         failures := InterceptGomegaFailures(func() {
318                                 Eventually(session).Should(Say("blah blah blah blah blah"))
319                         })
320                         Ω(time.Since(t)).Should(BeNumerically("<=", 500*time.Millisecond))
321                         Ω(failures).Should(HaveLen(1))
322                 })
323         })
324
325         Context("when wrapping out and err", func() {
326                 BeforeEach(func() {
327                         outWriter = NewBuffer()
328                         errWriter = NewBuffer()
329                 })
330
331                 It("should route to both the provided writers and the gbytes buffers", func() {
332                         Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
333                         Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))
334
335                         Ω(outWriter.Contents()).Should(ContainSubstring("We've done the impossible, and that makes us mighty"))
336                         Ω(errWriter.Contents()).Should(ContainSubstring("Ah, curse your sudden but inevitable betrayal!"))
337
338                         Eventually(session).Should(Exit())
339
340                         Ω(outWriter.Contents()).Should(Equal(session.Out.Contents()))
341                         Ω(errWriter.Contents()).Should(Equal(session.Err.Contents()))
342                 })
343         })
344
345         Describe("when the command fails to start", func() {
346                 It("should return an error", func() {
347                         _, err := Start(exec.Command("agklsjdfas"), nil, nil)
348                         Ω(err).Should(HaveOccurred())
349                 })
350         })
351 })