aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/onsi/gomega/gexec/session_test.go
blob: b7841a090fb7bdd8e626058d3052d76f2beb7584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package gexec_test

import (
	"os/exec"
	"syscall"
	"time"

	. "github.com/onsi/gomega/gbytes"
	. "github.com/onsi/gomega/gexec"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Session", func() {
	var command *exec.Cmd
	var session *Session

	var outWriter, errWriter *Buffer

	BeforeEach(func() {
		outWriter = nil
		errWriter = nil
	})

	JustBeforeEach(func() {
		command = exec.Command(fireflyPath)
		var err error
		session, err = Start(command, outWriter, errWriter)
		Ω(err).ShouldNot(HaveOccurred())
	})

	Context("running a command", func() {
		It("should start the process", func() {
			Ω(command.Process).ShouldNot(BeNil())
		})

		It("should wrap the process's stdout and stderr with gbytes buffers", func(done Done) {
			Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
			Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))
			defer session.Out.CancelDetects()

			select {
			case <-session.Out.Detect("Can we maybe vote on the whole murdering people issue"):
				Eventually(session).Should(Exit(0))
			case <-session.Out.Detect("I swear by my pretty floral bonnet, I will end you."):
				Eventually(session).Should(Exit(1))
			case <-session.Out.Detect("My work's illegal, but at least it's honest."):
				Eventually(session).Should(Exit(2))
			}

			close(done)
		})

		It("should satisfy the gbytes.BufferProvider interface, passing Stdout", func() {
			Eventually(session).Should(Say("We've done the impossible, and that makes us mighty"))
			Eventually(session).Should(Exit())
		})
	})

	Describe("providing the exit code", func() {
		It("should provide the app's exit code", func() {
			Ω(session.ExitCode()).Should(Equal(-1))

			Eventually(session).Should(Exit())
			Ω(session.ExitCode()).Should(BeNumerically(">=", 0))
			Ω(session.ExitCode()).Should(BeNumerically("<", 3))
		})
	})

	Describe("wait", func() {
		It("should wait till the command exits", func() {
			Ω(session.ExitCode()).Should(Equal(-1))
			Ω(session.Wait().ExitCode()).Should(BeNumerically(">=", 0))
			Ω(session.Wait().ExitCode()).Should(BeNumerically("<", 3))
		})
	})

	Describe("exited", func() {
		It("should close when the command exits", func() {
			Eventually(session.Exited).Should(BeClosed())
			Ω(session.ExitCode()).ShouldNot(Equal(-1))
		})
	})

	Describe("kill", func() {
		It("should kill the command and don't wait for it to exit", func() {
			session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
			Ω(err).ShouldNot(HaveOccurred())

			session.Kill()
			Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
			Eventually(session).Should(Exit(128 + 9))
		})
	})

	Describe("interrupt", func() {
		It("should interrupt the command", func() {
			session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
			Ω(err).ShouldNot(HaveOccurred())

			session.Interrupt()
			Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
			Eventually(session).Should(Exit(128 + 2))
		})
	})

	Describe("terminate", func() {
		It("should terminate the command", func() {
			session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
			Ω(err).ShouldNot(HaveOccurred())

			session.Terminate()
			Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
			Eventually(session).Should(Exit(128 + 15))
		})
	})

	Describe("signal", func() {
		It("should send the signal to the command", func() {
			session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
			Ω(err).ShouldNot(HaveOccurred())

			session.Signal(syscall.SIGABRT)
			Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
			Eventually(session).Should(Exit(128 + 6))
		})
	})

	Context("tracking sessions", func() {
		BeforeEach(func() {
			KillAndWait()
		})

		Describe("kill", func() {
			It("should kill all the started sessions", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				Kill()

				Eventually(session1).Should(Exit(128 + 9))
				Eventually(session2).Should(Exit(128 + 9))
				Eventually(session3).Should(Exit(128 + 9))
			})

			It("should not wait for exit", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				Kill()
				Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")

				Eventually(session1).Should(Exit(128 + 9))
			})

			It("should not track unstarted sessions", func() {
				_, err := Start(exec.Command("does not exist", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).Should(HaveOccurred())

				session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				Kill()

				Eventually(session2).Should(Exit(128 + 9))
				Eventually(session3).Should(Exit(128 + 9))
			})

		})

		Describe("killAndWait", func() {
			It("should kill all the started sessions and wait for them to finish", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				KillAndWait()
				Ω(session1).Should(Exit(128+9), "Should have exited")
				Ω(session2).Should(Exit(128+9), "Should have exited")
				Ω(session3).Should(Exit(128+9), "Should have exited")
			})
		})

		Describe("terminate", func() {
			It("should terminate all the started sessions", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				Terminate()

				Eventually(session1).Should(Exit(128 + 15))
				Eventually(session2).Should(Exit(128 + 15))
				Eventually(session3).Should(Exit(128 + 15))
			})

			It("should not wait for exit", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				Terminate()

				Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
			})
		})

		Describe("terminateAndWait", func() {
			It("should terminate all the started sessions, and wait for them to exit", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				TerminateAndWait()

				Ω(session1).Should(Exit(128+15), "Should have exited")
				Ω(session2).Should(Exit(128+15), "Should have exited")
				Ω(session3).Should(Exit(128+15), "Should have exited")
			})
		})

		Describe("signal", func() {
			It("should signal all the started sessions", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				Signal(syscall.SIGABRT)

				Eventually(session1).Should(Exit(128 + 6))
				Eventually(session2).Should(Exit(128 + 6))
				Eventually(session3).Should(Exit(128 + 6))
			})

			It("should not wait", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				Signal(syscall.SIGABRT)

				Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
			})
		})

		Describe("interrupt", func() {
			It("should interrupt all the started sessions, and not wait", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				Interrupt()

				Eventually(session1).Should(Exit(128 + 2))
				Eventually(session2).Should(Exit(128 + 2))
				Eventually(session3).Should(Exit(128 + 2))
			})

			It("should not wait", func() {
				session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
				Ω(err).ShouldNot(HaveOccurred())

				Interrupt()

				Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
			})
		})
	})

	Context("when the command exits", func() {
		It("should close the buffers", func() {
			Eventually(session).Should(Exit())

			Ω(session.Out.Closed()).Should(BeTrue())
			Ω(session.Err.Closed()).Should(BeTrue())

			Ω(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
		})

		var So = It

		So("this means that eventually should short circuit", func() {
			t := time.Now()
			failures := InterceptGomegaFailures(func() {
				Eventually(session).Should(Say("blah blah blah blah blah"))
			})
			Ω(time.Since(t)).Should(BeNumerically("<=", 500*time.Millisecond))
			Ω(failures).Should(HaveLen(1))
		})
	})

	Context("when wrapping out and err", func() {
		BeforeEach(func() {
			outWriter = NewBuffer()
			errWriter = NewBuffer()
		})

		It("should route to both the provided writers and the gbytes buffers", func() {
			Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
			Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))

			Ω(outWriter.Contents()).Should(ContainSubstring("We've done the impossible, and that makes us mighty"))
			Ω(errWriter.Contents()).Should(ContainSubstring("Ah, curse your sudden but inevitable betrayal!"))

			Eventually(session).Should(Exit())

			Ω(outWriter.Contents()).Should(Equal(session.Out.Contents()))
			Ω(errWriter.Contents()).Should(Equal(session.Err.Contents()))
		})
	})

	Describe("when the command fails to start", func() {
		It("should return an error", func() {
			_, err := Start(exec.Command("agklsjdfas"), nil, nil)
			Ω(err).Should(HaveOccurred())
		})
	})
})