Commit ea5869cc authored by Catalin Irimie's avatar Catalin Irimie

Introduce Geo proxy bypass handler for Workhorse tests

This removes the timing expectation (polling interval) from the
tests and instead introduces a middleware that ignores requests
to `/api/v4/geo/proxy`.
parent e5096b9e
...@@ -49,7 +49,7 @@ func getGeoProxyURLGivenResponse(t *testing.T, givenInternalApiResponse string) ...@@ -49,7 +49,7 @@ func getGeoProxyURLGivenResponse(t *testing.T, givenInternalApiResponse string)
} }
func testRailsServer(url *regexp.Regexp, code int, body string) *httptest.Server { func testRailsServer(url *regexp.Regexp, code int, body string) *httptest.Server {
return testhelper.TestServerWithHandler(url, func(w http.ResponseWriter, r *http.Request) { return testhelper.TestServerWithHandlerWithGeoPolling(url, func(w http.ResponseWriter, r *http.Request) {
// return a 204 No Content response if we don't receive the JWT header // return a 204 No Content response if we don't receive the JWT header
if r.Header.Get(secret.RequestHeader) == "" { if r.Header.Get(secret.RequestHeader) == "" {
w.WriteHeader(204) w.WriteHeader(204)
......
...@@ -22,6 +22,10 @@ import ( ...@@ -22,6 +22,10 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/secret" "gitlab.com/gitlab-org/gitlab/workhorse/internal/secret"
) )
const (
geoProxyEndpointPath = "/api/v4/geo/proxy"
)
func ConfigureSecret() { func ConfigureSecret() {
secret.SetPath(path.Join(RootDir(), "testdata/test-secret")) secret.SetPath(path.Join(RootDir(), "testdata/test-secret"))
} }
...@@ -50,7 +54,20 @@ func RequireResponseHeader(t *testing.T, w interface{}, header string, expected ...@@ -50,7 +54,20 @@ func RequireResponseHeader(t *testing.T, w interface{}, header string, expected
require.Equal(t, expected, actual, "values for HTTP header %s", header) require.Equal(t, expected, actual, "values for HTTP header %s", header)
} }
// TestServerWithHandler skips Geo API polling for a proxy URL by default,
// use TestServerWithHandlerWithGeoPolling if you need to explicitly
// handle Geo API polling request as well.
func TestServerWithHandler(url *regexp.Regexp, handler http.HandlerFunc) *httptest.Server { func TestServerWithHandler(url *regexp.Regexp, handler http.HandlerFunc) *httptest.Server {
return TestServerWithHandlerWithGeoPolling(url, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if geoProxyEndpointPath == r.URL.Path {
return
}
handler(w, r)
}))
}
func TestServerWithHandlerWithGeoPolling(url *regexp.Regexp, handler http.HandlerFunc) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logEntry := log.WithFields(log.Fields{ logEntry := log.WithFields(log.Fields{
"method": r.Method, "method": r.Method,
......
...@@ -207,8 +207,8 @@ func (u *upstream) findGeoProxyRoute(cleanedPath string, r *http.Request) *route ...@@ -207,8 +207,8 @@ func (u *upstream) findGeoProxyRoute(cleanedPath string, r *http.Request) *route
func (u *upstream) pollGeoProxyAPI() { func (u *upstream) pollGeoProxyAPI() {
for { for {
u.geoProxyPollSleep(geoProxyApiPollingInterval)
u.callGeoProxyAPI() u.callGeoProxyAPI()
u.geoProxyPollSleep(geoProxyApiPollingInterval)
} }
} }
......
...@@ -314,9 +314,5 @@ func startWorkhorseServer(railsServerURL string, enableGeoProxyFeature bool) (*h ...@@ -314,9 +314,5 @@ func startWorkhorseServer(railsServerURL string, enableGeoProxyFeature bool) (*h
} }
} }
// Since the first sleep happens before any API call, this ensures
// we call the API at least once.
waitForNextApiPoll()
return ws, ws.Close, waitForNextApiPoll return ws, ws.Close, waitForNextApiPoll
} }
...@@ -5,13 +5,15 @@ import ( ...@@ -5,13 +5,15 @@ import (
"io/ioutil" "io/ioutil"
"mime" "mime"
"net/http" "net/http"
"net/http/httptest"
"os" "os"
"path" "path"
"regexp"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/labkit/log" "gitlab.com/gitlab-org/labkit/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/testhelper"
) )
func TestDeniedLfsDownload(t *testing.T) { func TestDeniedLfsDownload(t *testing.T) {
...@@ -35,7 +37,7 @@ func allowedXSendfileDownload(t *testing.T, contentFilename string, filePath str ...@@ -35,7 +37,7 @@ func allowedXSendfileDownload(t *testing.T, contentFilename string, filePath str
prepareDownloadDir(t) prepareDownloadDir(t)
// Prepare test server and backend // Prepare test server and backend
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{"method": r.Method, "url": r.URL}).Info("UPSTREAM") log.WithFields(log.Fields{"method": r.Method, "url": r.URL}).Info("UPSTREAM")
require.Equal(t, "X-Sendfile", r.Header.Get("X-Sendfile-Type")) require.Equal(t, "X-Sendfile", r.Header.Get("X-Sendfile-Type"))
...@@ -69,7 +71,7 @@ func deniedXSendfileDownload(t *testing.T, contentFilename string, filePath stri ...@@ -69,7 +71,7 @@ func deniedXSendfileDownload(t *testing.T, contentFilename string, filePath stri
prepareDownloadDir(t) prepareDownloadDir(t)
// Prepare test server and backend // Prepare test server and backend
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{"method": r.Method, "url": r.URL}).Info("UPSTREAM") log.WithFields(log.Fields{"method": r.Method, "url": r.URL}).Info("UPSTREAM")
require.Equal(t, "X-Sendfile", r.Header.Get("X-Sendfile-Type")) require.Equal(t, "X-Sendfile", r.Header.Get("X-Sendfile-Type"))
......
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