-
Notifications
You must be signed in to change notification settings - Fork 205
/
functional_tests.go
436 lines (347 loc) · 10.6 KB
/
functional_tests.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package functional_tests
import (
"bytes"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"io"
"github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/dghubble/sling"
"github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/phayes/freeport"
)
var HoverflyUsername = "benjih"
var HoverflyPassword = "password"
var BinaryPrefix = ""
func DoRequest(r *sling.Sling) *http.Response {
response, err := doRequest(r)
Expect(err).To(BeNil())
return response
}
func doRequest(r *sling.Sling) (*http.Response, error) {
req, err := r.Request()
if err != nil {
return nil, err
}
response, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
return response, nil
}
func Unmarshal(data []byte, to interface{}) {
Expect(json.Unmarshal(data, to)).To(BeNil())
}
func UnmarshalFromResponse(res *http.Response, to interface{}) {
responseJson, err := ioutil.ReadAll(res.Body)
Expect(err).To(BeNil())
Unmarshal(responseJson, to)
}
type Hoverfly struct {
adminPort int
adminUrl string
proxyPort int
proxyUrl string
process *exec.Cmd
commands []string
}
func NewHoverfly() *Hoverfly {
return &Hoverfly{
adminPort: freeport.GetPort(),
proxyPort: freeport.GetPort(),
}
}
func NewHoverflyWithAdminPort(adminPort int) *Hoverfly {
return &Hoverfly{
adminPort: adminPort,
adminUrl: fmt.Sprintf("http://localhost:%v", adminPort),
}
}
func (this *Hoverfly) Start(commands ...string) {
this.process = this.startHoverflyInternal(this.adminPort, this.proxyPort, commands...)
this.adminUrl = fmt.Sprintf("http://localhost:%v", this.adminPort)
this.proxyUrl = fmt.Sprintf("http://localhost:%v", this.proxyPort)
}
func (this Hoverfly) Stop() {
this.process.Process.Kill()
}
func (this Hoverfly) StopAPIAuthenticated(username, password string) {
token, err := this.getAPIToken(username, password)
if err != nil {
return
}
_, err = doRequest(sling.New().Delete(this.adminUrl+"/api/v2/shutdown").Add("Authorization", "Bearer "+token))
if err != nil {
panic(err)
}
}
func (this Hoverfly) DeleteBoltDb() {
workingDirectory, _ := os.Getwd()
Expect(os.Remove(workingDirectory + "requests.db")).To(BeNil())
}
func (this Hoverfly) GetMode() *v2.ModeView {
currentState := &v2.ModeView{}
resp := DoRequest(sling.New().Get(fmt.Sprintf("http://localhost:%v/api/v2/hoverfly/mode", this.adminPort)))
body, err := ioutil.ReadAll(resp.Body)
Expect(err).To(BeNil())
err = json.Unmarshal(body, currentState)
Expect(err).To(BeNil())
return currentState
}
func (this Hoverfly) SetMode(mode string) {
this.SetModeWithArgs(mode, v2.ModeArgumentsView{})
}
func (this Hoverfly) SetModeWithArgs(mode string, arguments v2.ModeArgumentsView) {
newMode := &v2.ModeView{
Mode: mode,
Arguments: arguments,
}
DoRequest(sling.New().Put(this.adminUrl + "/api/v2/hoverfly/mode").BodyJSON(newMode))
}
func (this Hoverfly) SetDestination(destination string) {
newDestination := &v2.DestinationView{
Destination: destination,
}
DoRequest(sling.New().Put(this.adminUrl + "/api/v2/hoverfly/destination").BodyJSON(newDestination))
}
func (this Hoverfly) SetMiddleware(binary, script string) {
newMiddleware := v2.MiddlewareView{
Binary: binary,
Script: script,
}
DoRequest(sling.New().Put(fmt.Sprintf("http://localhost:%v/api/v2/hoverfly/middleware", this.adminPort)).BodyJSON(newMiddleware))
}
func (this Hoverfly) GetSimulation() io.Reader {
res := sling.New().Get(this.adminUrl + "/api/v2/simulation")
req := DoRequest(res)
Expect(req.StatusCode).To(Equal(200))
return req.Body
}
func (this Hoverfly) ImportSimulation(simulation string) {
req := sling.New().Put(this.adminUrl + "/api/v2/simulation").Body(bytes.NewBufferString(simulation))
response := DoRequest(req)
Expect(response.StatusCode).To(Equal(http.StatusOK), "Failed to import simulation")
importedSimulationBytes, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())
ginkgo.GinkgoWriter.Write(importedSimulationBytes)
}
// Used for debugging when trying to find out why a functional test is failing
func (this Hoverfly) WriteLogsIfError() {
req := sling.New().Get(this.adminUrl+"/api/v2/logs").Add("Accept", "text/plain")
res := DoRequest(req)
Expect(res.StatusCode).To(Equal(200))
logs, err := ioutil.ReadAll(res.Body)
Expect(err).To(BeNil())
ginkgo.GinkgoWriter.Write(logs) // Only writes when test fails
}
func (this Hoverfly) ExportSimulation() v2.SimulationViewV5 {
reader := this.GetSimulation()
simulationBytes, err := ioutil.ReadAll(reader)
Expect(err).To(BeNil())
var simulation v2.SimulationViewV5
err = json.Unmarshal(simulationBytes, &simulation)
Expect(err).To(BeNil())
return simulation
}
func (this Hoverfly) GetCache() v2.CacheView {
req := sling.New().Get(this.adminUrl + "/api/v2/cache")
response := DoRequest(req)
Expect(response.StatusCode).To(Equal(http.StatusOK))
cacheBytes, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())
var cache v2.CacheView
err = json.Unmarshal(cacheBytes, &cache)
Expect(err).To(BeNil())
return cache
}
func (this Hoverfly) FlushCache() v2.CacheView {
req := sling.New().Delete(this.adminUrl + "/api/v2/cache")
res := DoRequest(req)
Expect(res.StatusCode).To(Equal(200))
cacheBytes, err := ioutil.ReadAll(res.Body)
Expect(err).To(BeNil())
var cache v2.CacheView
err = json.Unmarshal(cacheBytes, &cache)
Expect(err).To(BeNil())
return cache
}
func (this Hoverfly) SetPACFile(pacFile string) {
req := sling.New().Put(this.adminUrl + "/api/v2/hoverfly/pac").Body(bytes.NewBufferString(pacFile))
response := DoRequest(req)
Expect(response.StatusCode).To(Equal(http.StatusOK), "Failed to set PAC file")
_, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())
}
func (this Hoverfly) Proxy(r *sling.Sling) *http.Response {
req, err := r.Request()
Expect(err).To(BeNil())
return this.ProxyRequest(req)
}
func (this Hoverfly) ProxyRequest(req *http.Request) *http.Response {
proxy, _ := url.Parse(this.proxyUrl)
proxyHttpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxy),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
response, err := proxyHttpClient.Do(req)
Expect(err).To(BeNil())
return response
}
func (this Hoverfly) ProxyWithAuth(r *sling.Sling, user, password string) *http.Response {
req, err := r.Request()
Expect(err).To(BeNil())
proxy, _ := url.Parse(fmt.Sprintf("http://%s:%s@localhost:%v", user, password, this.proxyPort))
proxyHttpClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy), TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}, CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }}
response, err := proxyHttpClient.Do(req)
Expect(err).To(BeNil())
return response
}
func (this Hoverfly) GetAPIToken(username, password string) string {
token, err := this.getAPIToken(username, password)
Expect(err).To(BeNil())
return token
}
func (this Hoverfly) getAPIToken(username, password string) (string, error) {
response, err := doRequest(
sling.New().Post(this.adminUrl + "/api/token-auth").BodyJSON(map[string]interface{}{
"username": username,
"password": password,
}),
)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body, &bodyMap)
if err != nil {
return "", err
}
return bodyMap["token"].(string), nil
}
func (this Hoverfly) GetAdminPort() string {
return strconv.Itoa(this.adminPort)
}
func (this Hoverfly) GetProxyPort() string {
return strconv.Itoa(this.proxyPort)
}
func (this Hoverfly) GetPid() int {
return this.process.Process.Pid
}
func (this Hoverfly) startHoverflyInternal(adminPort, proxyPort int, additionalCommands ...string) *exec.Cmd {
hoverflyBinaryUri := BuildBinaryPath()
commands := []string{
"-ap",
strconv.Itoa(adminPort),
"-pp",
strconv.Itoa(proxyPort),
}
commands = append(commands, additionalCommands...)
this.commands = commands
hoverflyCmd := exec.Command(hoverflyBinaryUri, commands...)
err := hoverflyCmd.Start()
BinaryErrorCheck(err, hoverflyBinaryUri)
for _, command := range commands {
if command == "-add" {
time.Sleep(time.Second * 3)
return hoverflyCmd
}
}
this.healthcheck()
return hoverflyCmd
}
func BuildBinaryPath() string {
workingDirectory, _ := os.Getwd()
return filepath.Join(workingDirectory, BinaryPrefix, "bin/hoverfly")
}
func BinaryErrorCheck(err error, binaryPath string) {
if err != nil {
fmt.Println("Unable to start Hoverfly")
fmt.Println(os.Getwd())
fmt.Println(binaryPath)
fmt.Println("Is the binary there?")
os.Exit(1)
}
}
func (this Hoverfly) healthcheck() {
Eventually(func() int {
resp, err := http.Get(fmt.Sprintf("http://localhost:%v/api/health", this.adminPort))
if err == nil {
return resp.StatusCode
} else {
return 0
}
}, time.Second*5).Should(BeNumerically("==", http.StatusOK), "Hoverfly not running on %d", this.adminPort, this.commands)
}
func Healthcheck(adminPort int) {
var err error
var resp *http.Response
Eventually(func() int {
resp, err = http.Get(fmt.Sprintf("http://localhost:%v/api/health", adminPort))
if err == nil {
return resp.StatusCode
} else {
return 0
}
}, time.Second*5).Should(BeNumerically("==", http.StatusOK), "Hoverfly not running on %d but have no extra information", adminPort)
}
func Run(binary string, commands ...string) string {
cmd := exec.Command(binary, commands...)
out, err := cmd.Output()
if err != nil {
exitError, ok := err.(*exec.ExitError)
if ok {
return string(exitError.Stderr)
}
}
return strings.TrimSpace(string(out))
}
func GenerateFileName() string {
rb := make([]byte, 6)
rand.Read(rb)
rs := base64.URLEncoding.EncodeToString(rb)
return "testdata-gen/" + rs + ".json"
}
func TableToSliceMapStringString(table string) map[string]map[string]string {
results := map[string]map[string]string{}
tableRows := strings.Split(table, "\n")
headings := []string{}
for _, heading := range strings.Split(tableRows[1], "|") {
headings = append(headings, strings.TrimSpace(heading))
}
for _, row := range tableRows[2:] {
if !strings.Contains(row, "-+-") {
rowValues := strings.Split(row, "|")
result := map[string]string{}
for i, value := range rowValues {
if value != "" {
result[headings[i]] = strings.TrimSpace(value)
}
}
results[result["TARGET NAME"]] = result
}
}
return results
}