forked from cloudfoundry/rep
/
client_test.go
193 lines (167 loc) · 5.52 KB
/
client_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
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
package rep_test
import (
"net/http"
"time"
"github.com/cloudfoundry-incubator/bbs/models"
"github.com/cloudfoundry-incubator/cf_http"
"github.com/cloudfoundry-incubator/rep"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
)
var _ = Describe("Client", func() {
var fakeServer *ghttp.Server
var client rep.Client
BeforeEach(func() {
fakeServer = ghttp.NewServer()
client = rep.NewClient(cf_http.NewClient(), fakeServer.URL())
})
AfterEach(func() {
fakeServer.Close()
})
Describe("StopLRPInstance", func() {
const cellAddr = "cell.example.com"
var stopErr error
var actualLRP = models.ActualLRP{
ActualLRPKey: models.NewActualLRPKey("some-process-guid", 2, "test-domain"),
ActualLRPInstanceKey: models.NewActualLRPInstanceKey("some-instance-guid", "some-cell-id"),
}
JustBeforeEach(func() {
stopErr = client.StopLRPInstance(actualLRP.ActualLRPKey, actualLRP.ActualLRPInstanceKey)
})
Context("when the request is successful", func() {
BeforeEach(func() {
fakeServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/v1/lrps/some-process-guid/instances/some-instance-guid/stop"),
ghttp.RespondWith(http.StatusAccepted, ""),
),
)
})
It("makes the request and does not return an error", func() {
Expect(stopErr).NotTo(HaveOccurred())
Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
})
})
Context("when the request returns 500", func() {
BeforeEach(func() {
fakeServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/v1/lrps/some-process-guid/instances/some-instance-guid/stop"),
ghttp.RespondWith(http.StatusInternalServerError, ""),
),
)
})
It("makes the request and returns an error", func() {
Expect(stopErr).To(HaveOccurred())
Expect(stopErr.Error()).To(ContainSubstring("http error: status code 500"))
Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
})
})
Context("when the connection fails", func() {
BeforeEach(func() {
fakeServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/v1/lrps/some-process-guid/instances/some-instance-guid/stop"),
func(w http.ResponseWriter, r *http.Request) {
fakeServer.CloseClientConnections()
},
),
)
})
It("makes the request and returns an error", func() {
Expect(stopErr).To(HaveOccurred())
Expect(stopErr.Error()).To(ContainSubstring("EOF"))
Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
})
})
Context("when the connection times out", func() {
BeforeEach(func() {
fakeServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/v1/lrps/some-process-guid/instances/some-instance-guid/stop"),
func(w http.ResponseWriter, r *http.Request) {
time.Sleep(cfHttpTimeout + 100*time.Millisecond)
},
),
)
})
It("makes the request and returns an error", func() {
Expect(stopErr).To(HaveOccurred())
Expect(stopErr.Error()).To(MatchRegexp("use of closed network connection|Client.Timeout exceeded while awaiting headers"))
Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
})
})
})
Describe("CancelTask", func() {
const cellAddr = "cell.example.com"
var cancelErr error
var taskGuid = "some-task-guid"
JustBeforeEach(func() {
cancelErr = client.CancelTask(taskGuid)
})
Context("when the request is successful", func() {
BeforeEach(func() {
fakeServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/v1/tasks/some-task-guid/cancel"),
ghttp.RespondWith(http.StatusAccepted, ""),
),
)
})
It("makes the request and does not return an error", func() {
Expect(cancelErr).NotTo(HaveOccurred())
Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
})
})
Context("when the request returns 500", func() {
BeforeEach(func() {
fakeServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/v1/tasks/some-task-guid/cancel"),
ghttp.RespondWith(http.StatusInternalServerError, ""),
),
)
})
It("makes the request and returns an error", func() {
Expect(cancelErr).To(HaveOccurred())
Expect(cancelErr.Error()).To(ContainSubstring("http error: status code 500"))
Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
})
})
Context("when the connection fails", func() {
BeforeEach(func() {
fakeServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/v1/tasks/some-task-guid/cancel"),
func(w http.ResponseWriter, r *http.Request) {
fakeServer.CloseClientConnections()
},
),
)
})
It("makes the request and returns an error", func() {
Expect(cancelErr).To(HaveOccurred())
Expect(cancelErr.Error()).To(ContainSubstring("EOF"))
Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
})
})
Context("when the connection times out", func() {
BeforeEach(func() {
fakeServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/v1/tasks/some-task-guid/cancel"),
func(w http.ResponseWriter, r *http.Request) {
time.Sleep(cfHttpTimeout + 100*time.Millisecond)
},
),
)
})
It("makes the request and returns an error", func() {
Expect(cancelErr).To(HaveOccurred())
Expect(cancelErr.Error()).To(MatchRegexp("use of closed network connection|Client.Timeout exceeded while awaiting headers"))
Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
})
})
})
})