diff options
Diffstat (limited to 'test/integration/container_test.go')
| -rw-r--r-- | test/integration/container_test.go | 72 |
1 files changed, 25 insertions, 47 deletions
diff --git a/test/integration/container_test.go b/test/integration/container_test.go index fcf2752..6739807 100644 --- a/test/integration/container_test.go +++ b/test/integration/container_test.go @@ -6,7 +6,6 @@ package test import ( "context" "net/http" - "net/url" "testing" "time" @@ -14,62 +13,46 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/wait" "github.com/xlgmokha/x/pkg/env" - "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc" ) -type TestLogConsumer struct { - t *testing.T -} - -func (lc *TestLogConsumer) Accept(l testcontainers.Log) { - lc.t.Logf("%s", l.Content) +func environmentVariables(srv *mockoidc.MockOIDC) map[string]string { + config := srv.Config() + return map[string]string{ + "APP_ENV": "test", + "BIND_ADDR": ":8080", + "DEBUG": env.Fetch("DEBUG", ""), + "HMAC_SESSION_SECRET": "secret", + "OAUTH_CLIENT_ID": config.ClientID, + "OAUTH_CLIENT_SECRET": config.ClientSecret, + "OAUTH_REDIRECT_URL": "", + "OIDC_ISSUER": srv.Issuer(), + } } func TestContainer(t *testing.T) { - srv := oidc.NewTestServer(t) - defer srv.Close() - ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() - address, err := url.Parse(srv.MockOIDC.Addr()) - require.NoError(t, err) - - issuer := srv.Issuer() - t.Logf("mockoidc: %v %v %v\n", address.String(), issuer, testcontainers.HostInternal) - container, err := testcontainers.Run( - ctx, - env.Fetch("IMAGE_TAG", "sparkled:invalid"), - testcontainers.WithEnv(map[string]string{ - "APP_ENV": "test", - "BIND_ADDR": ":8080", - "DEBUG": env.Fetch("DEBUG", ""), - "HMAC_SESSION_SECRET": "secret", - "OAUTH_CLIENT_ID": srv.MockOIDC.Config().ClientID, - "OAUTH_CLIENT_SECRET": srv.MockOIDC.Config().ClientSecret, - "OAUTH_REDIRECT_URL": "", - "OIDC_ISSUER": issuer, - }), - testcontainers.WithExposedPorts("8080/tcp", "9901/tcp", "10000/tcp"), - testcontainers.WithLogConsumers(&TestLogConsumer{t: t}), - testcontainers.WithWaitStrategy(wait.ForLog("Listening on").WithStartupTimeout(10*time.Second)), - ) - require.NoError(t, err) + srv := NewOIDCTestServer(t) + defer srv.Shutdown() + container := NewContainer(t, ctx, environmentVariables(srv)) defer testcontainers.TerminateContainer(container) - oidcProviderEndpoint := address.String() + require.True(t, container.IsRunning()) + sparkleEndpoint, err := container.PortEndpoint(ctx, "8080", "http") require.NoError(t, err) envoyEndpoint, err := container.PortEndpoint(ctx, "10000", "http") require.NoError(t, err) - client := &http.Client{Timeout: 5 * time.Second} + envoyAdminEndpoint, err := container.PortEndpoint(ctx, "9901", "http") + require.NoError(t, err) - publicPaths := []string{ + for _, publicPath := range []string{ + envoyAdminEndpoint + "/", envoyEndpoint + "/", envoyEndpoint + "/application.js", envoyEndpoint + "/favicon.ico", @@ -77,18 +60,13 @@ func TestContainer(t *testing.T) { envoyEndpoint + "/health", envoyEndpoint + "/index.html", envoyEndpoint + "/logo.png", - oidcProviderEndpoint + mockoidc.DiscoveryEndpoint, sparkleEndpoint + "/", sparkleEndpoint + "/favicon.ico", sparkleEndpoint + "/health", - } - - for _, path := range publicPaths { - t.Run(path, func(t *testing.T) { - request, err := http.NewRequestWithContext(ctx, http.MethodGet, path, nil) - response, err := client.Do(request) - require.NoError(t, err) - assert.Equal(t, http.StatusOK, response.StatusCode) + srv.DiscoveryEndpoint(), + } { + t.Run(publicPath, func(t *testing.T) { + assert.Equal(t, http.StatusOK, HttpGet(t, ctx, publicPath).StatusCode) }) } } |
