Commit 2841b478 authored by Catalin Irimie's avatar Catalin Irimie

Add Geo assets proxy test, move static file setup to testhelper

parent c3501224
...@@ -167,3 +167,16 @@ func Retry(t testing.TB, timeout time.Duration, fn func() error) { ...@@ -167,3 +167,16 @@ func Retry(t testing.TB, timeout time.Duration, fn func() error) {
} }
t.Fatalf("test timeout after %v; last error: %v", timeout, err) t.Fatalf("test timeout after %v; last error: %v", timeout, err)
} }
func SetupStaticFileHelper(t *testing.T, fpath, content, directory string) string {
cwd, err := os.Getwd()
require.NoError(t, err, "get working directory")
absDocumentRoot := path.Join(cwd, directory)
require.NoError(t, os.MkdirAll(path.Join(absDocumentRoot, path.Dir(fpath)), 0755), "create document root")
staticFile := path.Join(absDocumentRoot, fpath)
require.NoError(t, ioutil.WriteFile(staticFile, []byte(content), 0666), "write file content")
return absDocumentRoot
}
...@@ -2,6 +2,8 @@ package upstream ...@@ -2,6 +2,8 @@ package upstream
import ( import (
"testing" "testing"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/testhelper"
) )
func TestProjectNotExistingGitHttpPullWithGeoProxy(t *testing.T) { func TestProjectNotExistingGitHttpPullWithGeoProxy(t *testing.T) {
...@@ -45,3 +47,15 @@ func TestProjectNotExistingGitSSHPushWithGeoProxy(t *testing.T) { ...@@ -45,3 +47,15 @@ func TestProjectNotExistingGitSSHPushWithGeoProxy(t *testing.T) {
runTestCasesWithGeoProxyEnabled(t, testCases) runTestCasesWithGeoProxyEnabled(t, testCases)
} }
func TestAssetsServedLocallyWithGeoProxy(t *testing.T) {
path := "/assets/static.txt"
content := "local geo asset"
testhelper.SetupStaticFileHelper(t, path, content, testDocumentRoot)
testCases := []testCase{
{"assets path", "/assets/static.txt", "local geo asset"},
}
runTestCasesWithGeoProxyEnabled(t, testCases)
}
...@@ -138,7 +138,7 @@ func TestDeniedXSendfileDownload(t *testing.T) { ...@@ -138,7 +138,7 @@ func TestDeniedXSendfileDownload(t *testing.T) {
func TestAllowedStaticFile(t *testing.T) { func TestAllowedStaticFile(t *testing.T) {
content := "PUBLIC" content := "PUBLIC"
require.NoError(t, setupStaticFile("static file.txt", content)) setupStaticFile(t, "static file.txt", content)
proxied := false proxied := false
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) { ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
...@@ -164,7 +164,7 @@ func TestAllowedStaticFile(t *testing.T) { ...@@ -164,7 +164,7 @@ func TestAllowedStaticFile(t *testing.T) {
func TestStaticFileRelativeURL(t *testing.T) { func TestStaticFileRelativeURL(t *testing.T) {
content := "PUBLIC" content := "PUBLIC"
require.NoError(t, setupStaticFile("static.txt", content), "create public/static.txt") setupStaticFile(t, "static.txt", content)
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), http.HandlerFunc(http.NotFound)) ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), http.HandlerFunc(http.NotFound))
defer ts.Close() defer ts.Close()
...@@ -182,7 +182,7 @@ func TestStaticFileRelativeURL(t *testing.T) { ...@@ -182,7 +182,7 @@ func TestStaticFileRelativeURL(t *testing.T) {
func TestAllowedPublicUploadsFile(t *testing.T) { func TestAllowedPublicUploadsFile(t *testing.T) {
content := "PRIVATE but allowed" content := "PRIVATE but allowed"
require.NoError(t, setupStaticFile("uploads/static file.txt", content), "create public/uploads/static file.txt") setupStaticFile(t, "uploads/static file.txt", content)
proxied := false proxied := false
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) { ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
...@@ -208,7 +208,7 @@ func TestAllowedPublicUploadsFile(t *testing.T) { ...@@ -208,7 +208,7 @@ func TestAllowedPublicUploadsFile(t *testing.T) {
func TestDeniedPublicUploadsFile(t *testing.T) { func TestDeniedPublicUploadsFile(t *testing.T) {
content := "PRIVATE" content := "PRIVATE"
require.NoError(t, setupStaticFile("uploads/static.txt", content), "create public/uploads/static.txt") setupStaticFile(t, "uploads/static.txt", content)
proxied := false proxied := false
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, _ *http.Request) { ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, _ *http.Request) {
...@@ -241,7 +241,7 @@ This is a static error page for code 499 ...@@ -241,7 +241,7 @@ This is a static error page for code 499
</body> </body>
</html> </html>
` `
require.NoError(t, setupStaticFile("499.html", errorPageBody)) setupStaticFile(t, "499.html", errorPageBody)
ts := testhelper.TestServerWithHandler(nil, func(w http.ResponseWriter, _ *http.Request) { ts := testhelper.TestServerWithHandler(nil, func(w http.ResponseWriter, _ *http.Request) {
upstreamError := "499" upstreamError := "499"
// This is the point of the test: the size of the upstream response body // This is the point of the test: the size of the upstream response body
...@@ -266,7 +266,7 @@ This is a static error page for code 499 ...@@ -266,7 +266,7 @@ This is a static error page for code 499
func TestGzipAssets(t *testing.T) { func TestGzipAssets(t *testing.T) {
path := "/assets/static.txt" path := "/assets/static.txt"
content := "asset" content := "asset"
require.NoError(t, setupStaticFile(path, content)) setupStaticFile(t, path, content)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
gzipWriter := gzip.NewWriter(buf) gzipWriter := gzip.NewWriter(buf)
...@@ -274,7 +274,7 @@ func TestGzipAssets(t *testing.T) { ...@@ -274,7 +274,7 @@ func TestGzipAssets(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, gzipWriter.Close()) require.NoError(t, gzipWriter.Close())
contentGzip := buf.String() contentGzip := buf.String()
require.NoError(t, setupStaticFile(path+".gz", contentGzip)) setupStaticFile(t, path+".gz", contentGzip)
proxied := false proxied := false
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) { ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
...@@ -319,7 +319,7 @@ func TestGzipAssets(t *testing.T) { ...@@ -319,7 +319,7 @@ func TestGzipAssets(t *testing.T) {
func TestAltDocumentAssets(t *testing.T) { func TestAltDocumentAssets(t *testing.T) {
path := "/assets/static.txt" path := "/assets/static.txt"
content := "asset" content := "asset"
require.NoError(t, setupAltStaticFile(path, content)) setupAltStaticFile(t, path, content)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
gzipWriter := gzip.NewWriter(buf) gzipWriter := gzip.NewWriter(buf)
...@@ -327,7 +327,7 @@ func TestAltDocumentAssets(t *testing.T) { ...@@ -327,7 +327,7 @@ func TestAltDocumentAssets(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, gzipWriter.Close()) require.NoError(t, gzipWriter.Close())
contentGzip := buf.String() contentGzip := buf.String()
require.NoError(t, setupAltStaticFile(path+".gz", contentGzip)) setupAltStaticFile(t, path+".gz", contentGzip)
proxied := false proxied := false
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) { ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
...@@ -712,25 +712,12 @@ func TestRejectUnknownMethod(t *testing.T) { ...@@ -712,25 +712,12 @@ func TestRejectUnknownMethod(t *testing.T) {
require.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) require.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
} }
func setupStaticFile(fpath, content string) error { func setupStaticFile(t *testing.T, fpath, content string) {
return setupStaticFileHelper(fpath, content, testDocumentRoot) absDocumentRoot = testhelper.SetupStaticFileHelper(t, fpath, content, testDocumentRoot)
} }
func setupAltStaticFile(fpath, content string) error { func setupAltStaticFile(t *testing.T, fpath, content string) {
return setupStaticFileHelper(fpath, content, testAltDocumentRoot) absDocumentRoot = testhelper.SetupStaticFileHelper(t, fpath, content, testAltDocumentRoot)
}
func setupStaticFileHelper(fpath, content, directory string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
absDocumentRoot = path.Join(cwd, directory)
if err := os.MkdirAll(path.Join(absDocumentRoot, path.Dir(fpath)), 0755); err != nil {
return err
}
staticFile := path.Join(absDocumentRoot, fpath)
return ioutil.WriteFile(staticFile, []byte(content), 0666)
} }
func prepareDownloadDir(t *testing.T) { func prepareDownloadDir(t *testing.T) {
...@@ -896,7 +883,7 @@ This is a static error page for code 503 ...@@ -896,7 +883,7 @@ This is a static error page for code 503
</body> </body>
</html> </html>
` `
require.NoError(t, setupStaticFile("503.html", errorPageBody)) setupStaticFile(t, "503.html", errorPageBody)
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, _ *http.Request) { ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("X-Gitlab-Custom-Error", "1") w.Header().Set("X-Gitlab-Custom-Error", "1")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment