1package integration_test
2
3import (
4	"strings"
5
6	. "github.com/onsi/ginkgo"
7	. "github.com/onsi/gomega"
8	"github.com/onsi/gomega/gexec"
9)
10
11var _ = Describe("SuiteSetup", func() {
12	var pathToTest string
13
14	Context("when the BeforeSuite and AfterSuite pass", func() {
15		BeforeEach(func() {
16			pathToTest = tmpPath("suite_setup")
17			copyIn(fixturePath("passing_suite_setup"), pathToTest, false)
18		})
19
20		It("should run the BeforeSuite once, then run all the tests", func() {
21			session := startGinkgo(pathToTest, "--noColor")
22			Eventually(session).Should(gexec.Exit(0))
23			output := string(session.Out.Contents())
24
25			Ω(strings.Count(output, "BEFORE SUITE")).Should(Equal(1))
26			Ω(strings.Count(output, "AFTER SUITE")).Should(Equal(1))
27		})
28
29		It("should run the BeforeSuite once per parallel node, then run all the tests", func() {
30			session := startGinkgo(pathToTest, "--noColor", "--nodes=2")
31			Eventually(session).Should(gexec.Exit(0))
32			output := string(session.Out.Contents())
33
34			Ω(strings.Count(output, "BEFORE SUITE")).Should(Equal(2))
35			Ω(strings.Count(output, "AFTER SUITE")).Should(Equal(2))
36		})
37	})
38
39	Context("when the BeforeSuite fails", func() {
40		BeforeEach(func() {
41			pathToTest = tmpPath("suite_setup")
42			copyIn(fixturePath("failing_before_suite"), pathToTest, false)
43		})
44
45		It("should run the BeforeSuite once, none of the tests, but it should run the AfterSuite", func() {
46			session := startGinkgo(pathToTest, "--noColor")
47			Eventually(session).Should(gexec.Exit(1))
48			output := string(session.Out.Contents())
49
50			Ω(strings.Count(output, "BEFORE SUITE")).Should(Equal(1))
51			Ω(strings.Count(output, "Test Panicked")).Should(Equal(1))
52			Ω(strings.Count(output, "AFTER SUITE")).Should(Equal(1))
53			Ω(output).ShouldNot(ContainSubstring("NEVER SEE THIS"))
54		})
55
56		It("should run the BeforeSuite once per parallel node, none of the tests, but it should run the AfterSuite for each node", func() {
57			session := startGinkgo(pathToTest, "--noColor", "--nodes=2")
58			Eventually(session).Should(gexec.Exit(1))
59			output := string(session.Out.Contents())
60
61			Ω(strings.Count(output, "BEFORE SUITE")).Should(Equal(2))
62			Ω(strings.Count(output, "Test Panicked")).Should(Equal(2))
63			Ω(strings.Count(output, "AFTER SUITE")).Should(Equal(2))
64			Ω(output).ShouldNot(ContainSubstring("NEVER SEE THIS"))
65		})
66	})
67
68	Context("when the AfterSuite fails", func() {
69		BeforeEach(func() {
70			pathToTest = tmpPath("suite_setup")
71			copyIn(fixturePath("failing_after_suite"), pathToTest, false)
72		})
73
74		It("should run the BeforeSuite once, none of the tests, but it should run the AfterSuite", func() {
75			session := startGinkgo(pathToTest, "--noColor")
76			Eventually(session).Should(gexec.Exit(1))
77			output := string(session.Out.Contents())
78
79			Ω(strings.Count(output, "BEFORE SUITE")).Should(Equal(1))
80			Ω(strings.Count(output, "AFTER SUITE")).Should(Equal(1))
81			Ω(strings.Count(output, "Test Panicked")).Should(Equal(1))
82			Ω(strings.Count(output, "A TEST")).Should(Equal(2))
83		})
84
85		It("should run the BeforeSuite once per parallel node, none of the tests, but it should run the AfterSuite for each node", func() {
86			session := startGinkgo(pathToTest, "--noColor", "--nodes=2")
87			Eventually(session).Should(gexec.Exit(1))
88			output := string(session.Out.Contents())
89
90			Ω(strings.Count(output, "BEFORE SUITE")).Should(Equal(2))
91			Ω(strings.Count(output, "AFTER SUITE")).Should(Equal(2))
92			Ω(strings.Count(output, "Test Panicked")).Should(Equal(2))
93			Ω(strings.Count(output, "A TEST")).Should(Equal(2))
94		})
95	})
96
97	Context("With passing synchronized before and after suites", func() {
98		BeforeEach(func() {
99			pathToTest = tmpPath("suite_setup")
100			copyIn(fixturePath("synchronized_setup_tests"), pathToTest, false)
101		})
102
103		Context("when run with one node", func() {
104			It("should do all the work on that one node", func() {
105				session := startGinkgo(pathToTest, "--noColor")
106				Eventually(session).Should(gexec.Exit(0))
107				output := string(session.Out.Contents())
108
109				Ω(output).Should(ContainSubstring("BEFORE_A_1\nBEFORE_B_1: DATA"))
110				Ω(output).Should(ContainSubstring("AFTER_A_1\nAFTER_B_1"))
111			})
112		})
113
114		Context("when run across multiple nodes", func() {
115			It("should run the first BeforeSuite function (BEFORE_A) on node 1, the second (BEFORE_B) on all the nodes, the first AfterSuite (AFTER_A) on all the nodes, and then the second (AFTER_B) on Node 1 *after* everything else is finished", func() {
116				session := startGinkgo(pathToTest, "--noColor", "--nodes=3")
117				Eventually(session).Should(gexec.Exit(0))
118				output := string(session.Out.Contents())
119
120				Ω(output).Should(ContainSubstring("BEFORE_A_1"))
121				Ω(output).Should(ContainSubstring("BEFORE_B_1: DATA"))
122				Ω(output).Should(ContainSubstring("BEFORE_B_2: DATA"))
123				Ω(output).Should(ContainSubstring("BEFORE_B_3: DATA"))
124
125				Ω(output).ShouldNot(ContainSubstring("BEFORE_A_2"))
126				Ω(output).ShouldNot(ContainSubstring("BEFORE_A_3"))
127
128				Ω(output).Should(ContainSubstring("AFTER_A_1"))
129				Ω(output).Should(ContainSubstring("AFTER_A_2"))
130				Ω(output).Should(ContainSubstring("AFTER_A_3"))
131				Ω(output).Should(ContainSubstring("AFTER_B_1"))
132
133				Ω(output).ShouldNot(ContainSubstring("AFTER_B_2"))
134				Ω(output).ShouldNot(ContainSubstring("AFTER_B_3"))
135			})
136		})
137
138		Context("when streaming across multiple nodes", func() {
139			It("should run the first BeforeSuite function (BEFORE_A) on node 1, the second (BEFORE_B) on all the nodes, the first AfterSuite (AFTER_A) on all the nodes, and then the second (AFTER_B) on Node 1 *after* everything else is finished", func() {
140				session := startGinkgo(pathToTest, "--noColor", "--nodes=3", "--stream")
141				Eventually(session).Should(gexec.Exit(0))
142				output := string(session.Out.Contents())
143
144				Ω(output).Should(ContainSubstring("[1] BEFORE_A_1"))
145				Ω(output).Should(ContainSubstring("[1] BEFORE_B_1: DATA"))
146				Ω(output).Should(ContainSubstring("[2] BEFORE_B_2: DATA"))
147				Ω(output).Should(ContainSubstring("[3] BEFORE_B_3: DATA"))
148
149				Ω(output).ShouldNot(ContainSubstring("BEFORE_A_2"))
150				Ω(output).ShouldNot(ContainSubstring("BEFORE_A_3"))
151
152				Ω(output).Should(ContainSubstring("[1] AFTER_A_1"))
153				Ω(output).Should(ContainSubstring("[2] AFTER_A_2"))
154				Ω(output).Should(ContainSubstring("[3] AFTER_A_3"))
155				Ω(output).Should(ContainSubstring("[1] AFTER_B_1"))
156
157				Ω(output).ShouldNot(ContainSubstring("AFTER_B_2"))
158				Ω(output).ShouldNot(ContainSubstring("AFTER_B_3"))
159			})
160		})
161	})
162
163	Context("With a failing synchronized before suite", func() {
164		BeforeEach(func() {
165			pathToTest = tmpPath("suite_setup")
166			copyIn(fixturePath("exiting_synchronized_setup_tests"), pathToTest, false)
167		})
168
169		It("should fail and let the user know that node 1 disappeared prematurely", func() {
170			session := startGinkgo(pathToTest, "--noColor", "--nodes=3")
171			Eventually(session).Should(gexec.Exit(1))
172			output := string(session.Out.Contents())
173
174			Ω(output).Should(ContainSubstring("Node 1 disappeared before completing BeforeSuite"))
175			Ω(output).Should(ContainSubstring("Ginkgo timed out waiting for all parallel nodes to report back!"))
176		})
177	})
178})
179