This repository has been archived by the owner on Dec 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_test.go
297 lines (210 loc) · 5.99 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
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
// +build integration
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/magneticio/vamp-router/haproxy"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"testing"
"time"
)
// This file contains the initial integration tests for Vamp Router. Integration tests rely on some
// external components:
// - Docker
// - Haproxy
const (
TEMPLATE_FILE = "configuration/templates/haproxy_config.template"
CONFIG_FILE = "haproxy_test.cfg"
JSON_FILE = "vamp_lb_test.json"
PID_FILE = "vamp_lb_test.pid"
TEST_FILES = "test/integration/"
WORK_DIR = "/tmp/vamp_router_integration_test/"
DOCKER_HOST = "192.168.59.103"
)
// TestHarnass is an object that holds the prerequisites for setting up a test scenario
// such as the config for Vamp Router and the containers that should be run.
type TestHarnass struct {
Name string `json:"name"`
Containers []*Container `json:"containers"`
Config haproxy.Config `json:"config"`
UseCookies bool `json:"useCookies"`
Cases []*Case `json:"cases"`
}
type Case struct {
Url string `json:"url"`
Verb string `json:"verb"`
Headers []*Header `json:"headers"`
Expect string `json:"expect"`
}
type Header struct {
Key string `json:"key"`
Value string `json:"value"`
}
type Container struct {
OutPort int `json:"outPort"`
InPort int `json:"inPort"`
Name string `json:"name"`
Image string `json:"image"`
Parameters string `json:"parameters"`
}
// Does the actual request and checks the result
func (th *TestHarnass) Assert() bool {
// create a cookie jar, http client and some helper variables
jar, _ := cookiejar.New(nil)
var cookies []*http.Cookie
result := true
client := &http.Client{}
// loop over all cases
for i, _case := range th.Cases {
req, err := http.NewRequest(_case.Verb, _case.Url, nil)
for _, header := range _case.Headers {
req.Header.Set(header.Key, header.Value)
}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// if the test needs to persist cookies between request, do so
if th.UseCookies {
cookies = resp.Cookies()
u, _ := url.Parse(_case.Url)
jar.SetCookies(u, cookies)
client.Jar = jar
}
body, _ := ioutil.ReadAll(resp.Body)
body = bytes.TrimRight(body, "\n")
if !(string(body) == _case.Expect) {
fmt.Printf("=== Error in request %s \n", strconv.Itoa(i+1))
fmt.Printf("--- Expected: %s \n", _case.Expect)
fmt.Printf("--- Result : %s \n", body)
result = false
break
} else {
fmt.Printf("--- Correct result : %s \n", body)
}
}
return result
}
// TestMain sets up Vamp Router for testing and kicks of the tests.
// After the tests are done, a destroyTestHarnass routine is run.
func TestMain(m *testing.M) {
setup()
fmt.Println("--- Starting tests...")
m.Run()
defer KillHaproxy()
}
//Tests follow a pattern of loading the harnass and then testing assumptions.
func TestAll(t *testing.T) {
files, _ := ioutil.ReadDir(TEST_FILES)
for _, file := range files {
if strings.HasSuffix(file.Name(), ".json") {
th := loadTestHarnass(file.Name(), t)
if !(th.Assert()) {
t.Error("Failed test: ", th.Name)
}
destroyTestHarnass(th)
}
}
}
func setup() {
fmt.Println("--- Running setup...")
go setupApi()
}
func setupApi() {
RemoveWorkingDir()
KillHaproxy()
main()
}
func runDocker(c *Container, wg *sync.WaitGroup) {
defer wg.Done()
name := "--name=\"" + c.Name + "\""
portMap := strconv.Itoa(c.OutPort) + ":" + strconv.Itoa(c.InPort)
fmt.Println("--- docker run -d " + name + " -p " + portMap + " " + c.Image + " " + c.Parameters)
Docker := exec.Command("docker", "run", "-d", name, "-p", portMap, c.Image, c.Parameters)
_ = Docker.Run()
// check if Docker container has started
for {
time.Sleep(1000 * time.Millisecond)
if resp, _ := http.Get("http://" + DOCKER_HOST + ":" + strconv.Itoa(c.OutPort) + "/ping"); resp.StatusCode == 200 {
fmt.Printf("--- Container %s is running\n", c.Name)
break
}
}
}
func stopDocker(c *Container, wg *sync.WaitGroup) {
defer wg.Done()
DockerStop := exec.Command("docker", "stop", c.Name)
_ = DockerStop.Run()
for {
DockerCheck := exec.Command("docker", "inspect", "-f", "{{.State.Running}}", c.Name)
out, err := DockerCheck.Output()
if err != nil {
panic(err.Error())
}
if string(bytes.TrimRight(out, "\n")) == "false" {
DockerRm := exec.Command("docker", "rm", c.Name)
_ = DockerRm.Run()
break
}
time.Sleep(1000 * time.Millisecond)
}
}
func loadTestHarnass(file string, t *testing.T) *TestHarnass {
var th *TestHarnass
file_loc := TEST_FILES + file
if f, err := ioutil.ReadFile(file_loc); err != nil {
t.Errorf("Failed to load test harnass: %s", err.Error())
} else {
if err := json.Unmarshal(f, &th); err != nil {
t.Errorf("Failed to load test harnass: %s", err.Error())
}
}
fmt.Println()
fmt.Println("--- Loading test harnass: ", th.Name)
fmt.Println("--- Setting up containers...")
var wg sync.WaitGroup
for _, c := range th.Containers {
wg.Add(1)
go runDocker(c, &wg)
}
wg.Wait()
fmt.Println("--- Setting up Vamp Router...")
config, err := json.Marshal(th.Config)
if err != nil {
t.Errorf("Failed to load test harnass: %s", err.Error())
}
if resp, err := http.Post("http://localhost:10001/v1/config", "application/json", bytes.NewBuffer(config)); err != nil || resp.StatusCode != 201 {
t.Errorf("Failed to load test harnass: %s", err.Error())
}
time.Sleep(4000 * time.Millisecond)
return th
}
func destroyTestHarnass(th *TestHarnass) {
fmt.Println("--- Destroying containers...")
fmt.Println()
var wg sync.WaitGroup
for _, c := range th.Containers {
wg.Add(1)
go stopDocker(c, &wg)
}
wg.Wait()
}
func KillHaproxy() {
fmt.Println("--- Destroying haproxy...")
KillHaproxy := exec.Command("killall", "haproxy")
_ = KillHaproxy.Run()
}
func RemoveWorkingDir() {
fmt.Println("--- Destroying working dir...")
os.RemoveAll(workDir.Dir())
}