-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
116 lines (93 loc) · 3.46 KB
/
main_test.go
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
package main_test
import (
"net/http"
"net/http/httptest"
"os/exec"
"sync"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("Results", func() {
var (
server *httptest.Server
response string
)
BeforeEach(func() {
var m sync.Mutex
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
m.Lock()
w.Write([]byte(response))
m.Unlock()
}))
})
Context("when downtime is reported", func() {
Context("when the success rate is > 99", func() {
It("displays the results from the server", func() {
response = `{"totalrequests": 100, "responses": {"200": 99, "500": 1}}`
cmd := exec.Command(healthBinary, "-address", server.URL)
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
Expect(session.Out).Should(gbytes.Say("Response:\n "))
Expect(session.Out).Should(gbytes.Say(response))
Expect(session.Out).Should(gbytes.Say(`Success rate \(0.990000\)`))
})
})
Context("when the success rate is < 99", func() {
It("displays the results from the server", func() {
response = `{"totalrequests": 100, "responses": {"200": 98, "500": 2}}`
cmd := exec.Command(healthBinary, "-address", server.URL)
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Expect(session.Out).Should(gbytes.Say("Response:\n "))
Expect(session.Out).Should(gbytes.Say(response))
Expect(session.Err).Should(gbytes.Say(`Success rate \(0.980000\)`))
})
})
})
Context("when no downtime is reported", func() {
It("displays the results from the server", func() {
response = `{"totalrequests": 100, "responses": {"200": 100}}`
cmd := exec.Command(healthBinary, "-address", server.URL)
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
Expect(session.Out).Should(gbytes.Say("Response:\n "))
Expect(session.Out).Should(gbytes.Say(response))
Expect(session.Out).Should(gbytes.Say("No downtime for this app!\n"))
})
})
Context("when an error occurs", func() {
Context("when no server flag has been provided", func() {
It("returns an error", func() {
cmd := exec.Command(healthBinary)
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Expect(session.Err).Should(gbytes.Say("address not provided"))
})
})
Context("when the server address is unreachable", func() {
It("returns an error", func() {
cmd := exec.Command(healthBinary, "-address", "localhost:7890")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Expect(session.Err).Should(gbytes.Say("GET request failed"))
})
})
Context("when the server returns invalid json", func() {
It("returns an error", func() {
response = `""""""`
cmd := exec.Command(healthBinary, "-address", server.URL)
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Expect(session.Err.Contents()).Should((ContainSubstring("invalid character")))
})
})
})
})