-
Notifications
You must be signed in to change notification settings - Fork 0
/
banjax_base_test.go
227 lines (194 loc) · 5.49 KB
/
banjax_base_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
package main
import (
"flag"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const endpoint = "http://localhost:8081"
const fixtureConfigTest = "./fixtures/banjax-config-test.yaml"
const fixtureConfigTestReload = "./fixtures/banjax-config-test-reload.yaml"
const fixtureConfigTestShaInv = "./fixtures/banjax-config-test-sha-inv.yaml"
const fixtureConfigTestRegexBanner = "./fixtures/banjax-config-test-regex-banner.yaml"
const fixtureConfigTestReloadCIDR = "./fixtures/banjax-config-test-reload-cidr.yaml"
const fixtureConfigTestPersiteFail = "./fixtures/banjax-config-test-persite-fail.yaml"
var tmpDir string
var configFile string
func setUp() {
createTempDir()
copyConfigFile(fixtureConfigTest)
setCommandLineFlags()
log.SetFlags(log.LstdFlags | log.Lshortfile) // show line num in logs
go main()
time.Sleep(1 * time.Second)
}
func tearDown() {
os.RemoveAll(tmpDir)
}
func createTempDir() {
dir, err := ioutil.TempDir("", "banjax-tests")
if err != nil {
log.Fatal(err)
}
tmpDir = dir
}
func copyConfigFile(src string) {
source, err := os.Open(src)
if err != nil {
log.Fatal(err)
}
defer source.Close()
dst := filepath.Join(tmpDir, "banjax-config.yaml")
dest, err := os.Create(dst)
if err != nil {
log.Fatal(err)
}
defer dest.Close()
_, err = io.Copy(dest, source)
if err != nil {
log.Fatal(err)
}
configFile = dst
}
func setCommandLineFlags() {
flag.Parse()
os.Args = []string{os.Args[0]}
os.Args = append(os.Args, "-config-file", configFile)
os.Args = append(os.Args, "-standalone-testing")
os.Args = append(os.Args, "-debug")
}
type TestResource struct {
method string
name string
response_code int
headers http.Header
contains []string
}
func httpTester(t *testing.T, resources []TestResource) {
client := &http.Client{}
for _, resource := range resources {
test_name := "Test_" + resource.method + "_" + resource.name
t.Run(test_name, func(t *testing.T) {
httpCheck(client, &resource, t)
})
}
}
func httpCheck(client *http.Client, resource_ptr *TestResource, t *testing.T) {
resource := *resource_ptr
resp := httpRequest(client, resource)
assert.Equal(t, resource.response_code, resp.StatusCode, "Response code is not correct")
if len(resource.contains) > 0 {
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error when ready Body from ", resource.method, resource.name)
}
resp.Body.Close()
for _, lookup := range resource.contains {
if !strings.Contains(string(body), lookup) {
log.Fatalf("Expected string [[ %s ]] not found when testing: %s %s",
lookup, resource.method, resource.name)
}
}
}
}
type CookieMap map[string]*http.Cookie
func httpTesterWithCookie(t *testing.T, resources []TestResource) {
client := &http.Client{}
for _, resource := range resources {
test_name := "Test_" + resource.method + "_" + resource.name
t.Run(test_name, func(t *testing.T) {
cookies := httpCheckWithCookie(client, &resource, t)
assert.Contains(t, cookies, resource.contains[0])
if len(resource.contains) > 1 {
log.Print(cookies[resource.contains[0]])
expectedMaxAge, _ := strconv.Atoi(resource.contains[1])
assert.Equal(t, cookies[resource.contains[0]].MaxAge, expectedMaxAge)
}
})
}
}
func httpCheckWithCookie(client *http.Client, resource_ptr *TestResource, t *testing.T) (cookieMap CookieMap) {
resource := *resource_ptr
resp := httpRequest(client, resource)
assert.Equal(t, resource.response_code, resp.StatusCode, "Response code is not correct")
cookieMap = make(CookieMap)
if len(resp.Cookies()) > 0 {
for _, cookie := range resp.Cookies() {
cookieMap[cookie.Name] = cookie
}
}
return
}
func httpStress(resources []TestResource, repeat int) {
var resp *http.Response
client := http.Client{}
for _, resource := range resources {
for i := 0; i <= repeat; i++ {
resp = httpRequest(&client, resource)
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}
}
}
func httpRequest(client *http.Client, resource TestResource) *http.Response {
req, err := http.NewRequest(resource.method, endpoint+resource.name, nil)
if err != nil {
log.Fatal("Error when creating the request object",
resource.method, resource.name)
}
for key, values := range resource.headers {
for _, value := range values {
req.Header.Set(key, value)
}
}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error when doing the request ", resource.method, resource.name, err)
}
if req != nil && req.Body != nil {
req.Body.Close()
}
return resp
}
func randomXClientIP() http.Header {
return http.Header{"X-Client-IP": {randomIP()}}
}
func ClientIP(ip string) http.Header {
return http.Header{"X-Client-IP": {ip}}
}
func randomIP() string {
octets := []string{}
for i := 0; i < 4; i++ {
octet := rand.Intn(252)
octets = append(octets, strconv.Itoa(octet))
}
return strings.Join(octets, ".")
}
func reloadConfig(path string, randomReqCount int) {
done := make(chan bool)
// just to make a mark in log so we know when the reload is done
httpStress(
[]TestResource{{"GET", "/auth_request?path=/reloadConfig", 200, randomXClientIP(), nil}}, 1)
// Simulate activity of http requests when the config is reloaded
go func() {
httpStress(
[]TestResource{{"GET", "/auth_request?path=/", 200, randomXClientIP(), nil}},
randomReqCount)
done <- true
}()
copyConfigFile(path)
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
time.Sleep(1 * time.Second)
<-done
}