Skip to content

Commit

Permalink
backend: Add waitServerReady for integration tests
Browse files Browse the repository at this point in the history
Because on some systems the server isn't ready right away
so we need to wait for it to be ready.
  • Loading branch information
illume committed Feb 1, 2022
1 parent 635e919 commit dbeed12
Showing 1 changed file with 66 additions and 24 deletions.
90 changes: 66 additions & 24 deletions backend/test/auth/oidc/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth_test

import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -73,6 +74,35 @@ func TestOIDCAuthModeSetup(t *testing.T) {
})
}

var ErrOutOfRetries = errors.New("test: out of retries")

func waitServerReady() (bool, error) {
retries := 5
for i := 0; i < retries; i++ {
if i != 0 {
time.Sleep(100 * time.Millisecond)
}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/health", testServerURL), nil)
if err != nil {
continue
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
continue
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
continue
}

if (http.StatusOK == resp.StatusCode) && ("OK" == string(bodyBytes)) {
return true, nil
}
}
return false, ErrOutOfRetries
}
func TestLogin(t *testing.T) {
t.Run("no_login_redirect_url", func(t *testing.T) {
// establish db connection
Expand All @@ -90,13 +120,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login", testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -129,13 +160,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=http://localhost:9000", testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -168,13 +200,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=%s/", testServerURL, testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -216,13 +249,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=%s/", testServerURL, testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -264,13 +298,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=%s/", testServerURL, testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -322,13 +357,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=%s/", testServerURL, testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -366,13 +402,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=%s/", testServerURL, testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -427,13 +464,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=%s/", testServerURL, testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -497,13 +535,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=%s/", testServerURL, testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -569,13 +608,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/apps", testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -621,13 +661,14 @@ func TestLogin(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=%s/", testServerURL, testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down Expand Up @@ -698,13 +739,14 @@ func TestValidateToken(t *testing.T) {
//nolint:errcheck
go server.Start(serverPortStr)

time.Sleep(100 * time.Millisecond)

//nolint:errcheck
defer server.Shutdown(context.Background())
//nolint:errcheck
defer oidcServer.Shutdown()

_, err = waitServerReady()
require.NoError(t, err)

req, err := http.NewRequest("GET", fmt.Sprintf("%s/login?login_redirect_url=%s/", testServerURL, testServerURL), nil)
require.NoError(t, err)
require.NotNil(t, req)
Expand Down

0 comments on commit dbeed12

Please sign in to comment.