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
|
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/namsral/flag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var pagesBinary = flag.String("gitlab-pages-binary", "./gitlab-pages", "Path to the gitlab-pages binary")
// TODO: Use TCP port 0 everywhere to avoid conflicts. The binary could output
// the actual port (and type of listener) for us to read in place of the
// hardcoded values below.
var listeners = []ListenSpec{
{"http", "127.0.0.1", "37000"},
{"http", "::1", "37000"},
{"https", "127.0.0.1", "37001"},
{"https", "::1", "37001"},
{"proxy", "127.0.0.1", "37002"},
{"proxy", "::1", "37002"},
}
var (
httpListener = listeners[0]
httpsListener = listeners[2]
)
func skipUnlessEnabled(t *testing.T) {
if testing.Short() {
t.Log("Acceptance tests disabled")
t.SkipNow()
}
if _, err := os.Stat(*pagesBinary); os.IsNotExist(err) {
t.Errorf("Couldn't find gitlab-pages binary at %s", *pagesBinary)
t.FailNow()
}
}
func TestUnknownHostReturnsNotFound(t *testing.T) {
skipUnlessEnabled(t)
teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-redirect-http=false")
defer teardown()
for _, spec := range listeners {
rsp, err := GetPageFromListener(t, spec, "invalid.invalid", "")
assert.NoError(t, err)
rsp.Body.Close()
assert.Equal(t, http.StatusNotFound, rsp.StatusCode)
}
}
func TestKnownHostReturns200(t *testing.T) {
skipUnlessEnabled(t)
teardown := RunPagesProcess(t, *pagesBinary, listeners, "")
defer teardown()
for _, spec := range listeners {
rsp, err := GetPageFromListener(t, spec, "group.gitlab-example.com", "project/")
assert.NoError(t, err)
rsp.Body.Close()
assert.Equal(t, http.StatusOK, rsp.StatusCode)
}
}
func TestCORSWhenDisabled(t *testing.T) {
skipUnlessEnabled(t)
teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-disable-cross-origin-requests")
defer teardown()
for _, spec := range listeners {
for _, method := range []string{"GET", "OPTIONS"} {
rsp := doCrossOriginRequest(t, method, method, spec.URL("project/"))
assert.Equal(t, http.StatusOK, rsp.StatusCode)
assert.Equal(t, "", rsp.Header.Get("Access-Control-Allow-Origin"))
assert.Equal(t, "", rsp.Header.Get("Access-Control-Allow-Credentials"))
}
}
}
func TestCORSAllowsGET(t *testing.T) {
skipUnlessEnabled(t)
teardown := RunPagesProcess(t, *pagesBinary, listeners, "")
defer teardown()
for _, spec := range listeners {
for _, method := range []string{"GET", "OPTIONS"} {
rsp := doCrossOriginRequest(t, method, method, spec.URL("project/"))
assert.Equal(t, http.StatusOK, rsp.StatusCode)
assert.Equal(t, "*", rsp.Header.Get("Access-Control-Allow-Origin"))
assert.Equal(t, "", rsp.Header.Get("Access-Control-Allow-Credentials"))
}
}
}
func TestCORSForbidsPOST(t *testing.T) {
skipUnlessEnabled(t)
teardown := RunPagesProcess(t, *pagesBinary, listeners, "")
defer teardown()
for _, spec := range listeners {
rsp := doCrossOriginRequest(t, "OPTIONS", "POST", spec.URL("project/"))
assert.Equal(t, http.StatusOK, rsp.StatusCode)
assert.Equal(t, "", rsp.Header.Get("Access-Control-Allow-Origin"))
assert.Equal(t, "", rsp.Header.Get("Access-Control-Allow-Credentials"))
}
}
func doCrossOriginRequest(t *testing.T, method, reqMethod, url string) *http.Response {
req, err := http.NewRequest(method, url, nil)
require.NoError(t, err)
req.Host = "group.gitlab-example.com"
req.Header.Add("Origin", "example.com")
req.Header.Add("Access-Control-Request-Method", reqMethod)
rsp, err := DoPagesRequest(t, req)
require.NoError(t, err)
rsp.Body.Close()
return rsp
}
func TestKnownHostWithPortReturns200(t *testing.T) {
skipUnlessEnabled(t)
teardown := RunPagesProcess(t, *pagesBinary, listeners, "")
defer teardown()
for _, spec := range listeners {
rsp, err := GetPageFromListener(t, spec, "group.gitlab-example.com:"+spec.Port, "project/")
assert.NoError(t, err)
rsp.Body.Close()
assert.Equal(t, http.StatusOK, rsp.StatusCode)
}
}
func TestHttpToHttpsRedirectDisabled(t *testing.T) {
skipUnlessEnabled(t)
teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-redirect-http=false")
defer teardown()
rsp, err := GetRedirectPage(t, httpListener, "group.gitlab-example.com", "project/")
assert.NoError(t, err)
defer rsp.Body.Close()
assert.Equal(t, http.StatusOK, rsp.StatusCode)
rsp, err = GetPageFromListener(t, httpsListener, "group.gitlab-example.com", "project/")
assert.NoError(t, err)
defer rsp.Body.Close()
assert.Equal(t, http.StatusOK, rsp.StatusCode)
}
func TestHttpToHttpsRedirectEnabled(t *testing.T) {
skipUnlessEnabled(t)
teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-redirect-http=true")
defer teardown()
rsp, err := GetRedirectPage(t, httpListener, "group.gitlab-example.com", "project/")
assert.NoError(t, err)
defer rsp.Body.Close()
assert.Equal(t, http.StatusTemporaryRedirect, rsp.StatusCode)
assert.Equal(t, 1, len(rsp.Header["Location"]))
assert.Equal(t, "https://group.gitlab-example.com/project/", rsp.Header.Get("Location"))
rsp, err = GetPageFromListener(t, httpsListener, "group.gitlab-example.com", "project/")
assert.NoError(t, err)
defer rsp.Body.Close()
assert.Equal(t, http.StatusOK, rsp.StatusCode)
}
func TestPrometheusMetricsCanBeScraped(t *testing.T) {
skipUnlessEnabled(t)
listener := []ListenSpec{{"http", "127.0.0.1", "37003"}}
teardown := RunPagesProcess(t, *pagesBinary, listener, ":42345")
defer teardown()
resp, err := http.Get("http://localhost:42345/metrics")
if assert.NoError(t, err) {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
assert.Contains(t, string(body), "gitlab_pages_http_sessions_active 0")
assert.Contains(t, string(body), "gitlab_pages_domains_served_total 7")
}
}
func TestStatusPage(t *testing.T) {
skipUnlessEnabled(t)
teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-redirect-http=false", "-pages-status=/@statuscheck")
defer teardown()
rsp, err := GetPageFromListener(t, httpListener, "group.gitlab-example.com", "@statuscheck")
assert.NoError(t, err)
defer rsp.Body.Close()
assert.Equal(t, http.StatusOK, rsp.StatusCode)
}
func TestProxyRequest(t *testing.T) {
skipUnlessEnabled(t)
content := "<!DOCTYPE html><html><head><title>Title of the document</title></head><body></body></html>"
contentLength := int64(len(content))
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/projects/1/jobs/2/artifacts/delayed_200.html":
time.Sleep(2 * time.Second)
case "/projects/1/jobs/2/artifacts/200.html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, content)
case "/projects/1/jobs/2/artifacts/500.html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, content)
default:
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, content)
}
}))
defer testServer.Close()
cases := []struct {
Path string
Status int
BinaryOption string
Content string
Length int64
CacheControl string
ContentType string
Description string
}{
{
"200.html",
http.StatusOK,
"",
content,
contentLength,
"max-age=3600",
"text/html; charset=utf-8",
"basic proxied request",
},
{
"delayed_200.html",
http.StatusBadGateway,
"-artifacts-server-timeout=1",
"",
0,
"",
"text/html; charset=utf-8",
"502 error while attempting to proxy",
},
{
"404.html",
http.StatusNotFound,
"",
"",
0,
"",
"text/html; charset=utf-8",
"Proxying 404 from server",
},
{
"500.html",
http.StatusInternalServerError,
"",
"",
0,
"",
"text/html; charset=utf-8",
"Proxying 500 from server",
},
}
for _, c := range cases {
t.Run(fmt.Sprintf("Proxy Request Test: %s", c.Description), func(t *testing.T) {
teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-artifacts-server="+testServer.URL, c.BinaryOption)
defer teardown()
resp, err := GetPageFromListener(t, httpListener, "artifact~1~2.gitlab-example.com", c.Path)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, c.Status, resp.StatusCode)
assert.Equal(t, c.ContentType, resp.Header.Get("Content-Type"))
if !((c.Status == http.StatusBadGateway) || (c.Status == http.StatusNotFound) || (c.Status == http.StatusInternalServerError)) {
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, c.Content, string(body))
assert.Equal(t, c.Length, resp.ContentLength)
assert.Equal(t, c.CacheControl, resp.Header.Get("Cache-Control"))
}
})
}
}
func TestEnvironmentVariablesConfig(t *testing.T) {
skipUnlessEnabled(t)
os.Setenv("LISTEN_HTTP", net.JoinHostPort(httpListener.Host, httpListener.Port))
defer func() { os.Unsetenv("LISTEN_HTTP") }()
teardown := RunPagesProcess(t, *pagesBinary, []ListenSpec{}, "")
defer teardown()
rsp, err := GetPageFromListener(t, httpListener, "group.gitlab-example.com:", "project/")
assert.NoError(t, err)
rsp.Body.Close()
assert.Equal(t, http.StatusOK, rsp.StatusCode)
}
func TestMixedConfigSources(t *testing.T) {
skipUnlessEnabled(t)
os.Setenv("LISTEN_HTTP", net.JoinHostPort(httpListener.Host, httpListener.Port))
defer func() { os.Unsetenv("LISTEN_HTTP") }()
teardown := RunPagesProcess(t, *pagesBinary, []ListenSpec{httpsListener}, "")
defer teardown()
for _, listener := range []ListenSpec{httpListener, httpsListener} {
rsp, err := GetPageFromListener(t, listener, "group.gitlab-example.com", "project/")
assert.NoError(t, err)
rsp.Body.Close()
assert.Equal(t, http.StatusOK, rsp.StatusCode)
}
}
func TestMultiFlagEnvironmentVariables(t *testing.T) {
skipUnlessEnabled(t)
listenSpec := []ListenSpec{{"http", "127.0.0.1", "37001"}, {"http", "127.0.0.1", "37002"}}
envVarValue := fmt.Sprintf("%s,%s", net.JoinHostPort("127.0.0.1", "37001"), net.JoinHostPort("127.0.0.1", "37002"))
os.Setenv("LISTEN_HTTP", envVarValue)
defer func() { os.Unsetenv("LISTEN_HTTP") }()
teardown := RunPagesProcess(t, *pagesBinary, []ListenSpec{}, "")
defer teardown()
for _, listener := range listenSpec {
rsp, err := GetPageFromListener(t, listener, "group.gitlab-example.com", "project/")
assert.NoError(t, err)
rsp.Body.Close()
assert.Equal(t, http.StatusOK, rsp.StatusCode)
}
}
|