diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/acceptance/acme_test.go | 6 | ||||
-rw-r--r-- | test/acceptance/helpers_test.go | 42 | ||||
-rw-r--r-- | test/acceptance/ratelimiter_test.go | 22 | ||||
-rw-r--r-- | test/acceptance/serving_test.go | 14 | ||||
-rw-r--r-- | test/acceptance/status_test.go | 3 |
5 files changed, 41 insertions, 46 deletions
diff --git a/test/acceptance/acme_test.go b/test/acceptance/acme_test.go index d743a4e1..40d98a21 100644 --- a/test/acceptance/acme_test.go +++ b/test/acceptance/acme_test.go @@ -7,8 +7,6 @@ import ( "testing" "github.com/stretchr/testify/require" - - "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) const ( @@ -43,10 +41,10 @@ func TestAcmeChallengesWhenItIsNotConfigured(t *testing.T) { rsp, err := GetRedirectPage(t, httpListener, "withacmechallenge.domain.com", test.token) - testhelpers.Close(t, rsp.Body) require.NoError(t, err) require.Equal(t, test.expectedStatus, rsp.StatusCode) body, err := io.ReadAll(rsp.Body) + require.NoError(t, rsp.Body.Close()) require.NoError(t, err) require.Contains(t, string(body), test.expectedContent) @@ -84,10 +82,10 @@ func TestAcmeChallengesWhenItIsConfigured(t *testing.T) { rsp, err := GetRedirectPage(t, httpListener, "withacmechallenge.domain.com", test.token) - testhelpers.Close(t, rsp.Body) require.NoError(t, err) require.Equal(t, test.expectedStatus, rsp.StatusCode) body, err := io.ReadAll(rsp.Body) + require.NoError(t, rsp.Body.Close()) require.NoError(t, err) require.Contains(t, string(body), test.expectedContent) diff --git a/test/acceptance/helpers_test.go b/test/acceptance/helpers_test.go index 4c365c5e..5ee409bf 100644 --- a/test/acceptance/helpers_test.go +++ b/test/acceptance/helpers_test.go @@ -190,33 +190,36 @@ func (l ListenSpec) QuickTimeoutClient() *http.Client { // Returns only once this spec points at a working TCP server func (l ListenSpec) WaitUntilRequestSucceeds(done chan struct{}) error { timeout := 5 * time.Second - for start := time.Now(); time.Since(start) < timeout; { + + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + ticker := time.NewTicker(100 * time.Millisecond) + defer ticker.Stop() + + for { select { case <-done: return fmt.Errorf("server has shut down already") - default: - } + case <-ctx.Done(): + return fmt.Errorf("ctx done: %w for listener %v", ctx.Err(), l) + case <-ticker.C: + req, err := http.NewRequestWithContext(ctx, http.MethodGet, l.URL("/@healthcheck"), nil) + if err != nil { + return err + } - req, err := http.NewRequest("GET", l.URL("/"), nil) - if err != nil { - return err - } + response, err := l.QuickTimeoutClient().Transport.RoundTrip(req) - response, err := l.QuickTimeoutClient().Transport.RoundTrip(req) - if err != nil { - time.Sleep(100 * time.Millisecond) - continue - } - response.Body.Close() + if err == nil { + response.Body.Close() - if code := response.StatusCode; code >= 200 && code < 500 { - return nil + if code := response.StatusCode; code >= 200 && code < 500 { + return nil + } + } } - - time.Sleep(100 * time.Millisecond) } - - return fmt.Errorf("timed out after %v waiting for listener %v", timeout, l) } func (l ListenSpec) JoinHostPort() string { @@ -339,6 +342,7 @@ func runPagesProcess(t *testing.T, wait bool, pagesBinary string, listeners []Li func getPagesArgs(t *testing.T, listeners []ListenSpec, promPort string, extraArgs []string) (args []string) { var hasHTTPS bool args = append(args, "-log-verbose=true") + args = append(args, "-pages-status=/@healthcheck") for _, spec := range listeners { if spec.Type == "unix" { diff --git a/test/acceptance/ratelimiter_test.go b/test/acceptance/ratelimiter_test.go index ef49aa19..85e0f6aa 100644 --- a/test/acceptance/ratelimiter_test.go +++ b/test/acceptance/ratelimiter_test.go @@ -16,19 +16,14 @@ var ratelimitedListeners = map[string]struct { listener ListenSpec header http.Header clientIP string - // We perform requests to server while we're waiting for it to boot up, - // successful request gets counted in IP rate limit - includeWaitRequest bool }{ "http_listener": { - listener: httpListener, - clientIP: "127.0.0.1", - includeWaitRequest: true, + listener: httpListener, + clientIP: "127.0.0.1", }, "https_listener": { - listener: httpsListener, - clientIP: "127.0.0.1", - includeWaitRequest: true, + listener: httpsListener, + clientIP: "127.0.0.1", }, "proxy_listener": { listener: proxyListener, @@ -39,9 +34,8 @@ var ratelimitedListeners = map[string]struct { clientIP: "172.16.123.1", }, "proxyv2_listener": { - listener: httpsProxyv2Listener, - clientIP: "10.1.1.1", - includeWaitRequest: true, + listener: httpsProxyv2Listener, + clientIP: "10.1.1.1", }, } @@ -57,10 +51,6 @@ func TestIPRateLimits(t *testing.T) { withExtraArgument("rate-limit-source-ip-burst", fmt.Sprint(rateLimit)), ) - if tc.includeWaitRequest { - rateLimit-- // we've already used one of requests while checking if server is up - } - for i := 0; i < 10; i++ { rsp, err := GetPageFromListenerWithHeaders(t, tc.listener, "group.gitlab-example.com", "project/", tc.header) require.NoError(t, err) diff --git a/test/acceptance/serving_test.go b/test/acceptance/serving_test.go index 92e9c8c7..410e5ca0 100644 --- a/test/acceptance/serving_test.go +++ b/test/acceptance/serving_test.go @@ -370,6 +370,7 @@ func TestDomainResolverError(t *testing.T) { }, "timeout": { timeout: 100 * time.Millisecond, + status: http.StatusTeapot, }, "server_fails": { panic: true, @@ -378,7 +379,7 @@ func TestDomainResolverError(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { - called := false + called := make(chan struct{}) // handler setup pagesHandler := func(w http.ResponseWriter, r *http.Request) { @@ -387,7 +388,7 @@ func TestDomainResolverError(t *testing.T) { return } - called = true + close(called) if test.panic { panic("server failed") @@ -413,7 +414,11 @@ func TestDomainResolverError(t *testing.T) { require.NoError(t, err) testhelpers.Close(t, response.Body) - require.True(t, called, "api must have been called") + select { + case <-called: + case <-time.After(2 * time.Second): + t.Fatal("timed out waiting for the pages handler") + } require.Equal(t, http.StatusBadGateway, response.StatusCode) @@ -569,7 +574,7 @@ func TestSlowRequests(t *testing.T) { defer cancel() url := httpListener.URL("/index.html") - req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) require.NoError(t, err) req.Host = "group.gitlab-example.com" @@ -578,7 +583,6 @@ func TestSlowRequests(t *testing.T) { require.Error(t, err, "cancelling the context should trigger this error") require.Eventually(t, func() bool { - require.Contains(t, logBuf.String(), "context done: context canceled", "error mismatch") require.Contains(t, logBuf.String(), "\"status\":404", "status mismatch") return true }, time.Second, time.Millisecond) diff --git a/test/acceptance/status_test.go b/test/acceptance/status_test.go index c48aaff7..a57ae043 100644 --- a/test/acceptance/status_test.go +++ b/test/acceptance/status_test.go @@ -12,10 +12,9 @@ import ( func TestStatusPage(t *testing.T) { RunPagesProcess(t, withListeners([]ListenSpec{httpListener}), - withExtraArgument("pages-status", "/@statuscheck"), ) - rsp, err := GetPageFromListener(t, httpListener, "group.gitlab-example.com", "@statuscheck") + rsp, err := GetPageFromListener(t, httpListener, "group.gitlab-example.com", "@healthcheck") require.NoError(t, err) testhelpers.Close(t, rsp.Body) require.Equal(t, http.StatusOK, rsp.StatusCode) |