Skip to content

Commit

Permalink
attempted test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fheinecke committed Dec 9, 2024
1 parent b31b065 commit b1a3eae
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions integrations/operator/controllers/resources/testlib/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package testlib
import (
"context"
"math/rand/v2"
"net"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -57,11 +58,9 @@ import (
"github.com/gravitational/teleport/integrations/operator/controllers"
"github.com/gravitational/teleport/integrations/operator/controllers/resources"
"github.com/gravitational/teleport/lib/modules"
"github.com/gravitational/teleport/lib/service"
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/srv/db/common"
"github.com/gravitational/teleport/lib/srv/db/postgres"
"github.com/gravitational/teleport/tool/teleport/testenv"
)

// scheme is our own test-specific scheme to avoid using the global
Expand Down Expand Up @@ -236,25 +235,24 @@ func (s *TestSetup) StopKubernetesOperator() {
}

// Spec matches https://goteleport.com/docs/enroll-resources/database-access/guides/dynamic-registration/
func setupMockPostgresServer(t *testing.T, process *service.TeleportProcess, setup *TestSetup) {
rootClient := testenv.MakeDefaultAuthClient(t, process)

func setupMockPostgresServer(t *testing.T, setup *TestSetup) {
postgresTestServer, err := postgres.NewTestServer(common.TestServerConfig{
AuthClient: rootClient,
AuthClient: setup.TeleportClient,
AllowAnyUser: true,
})
require.NoError(t, err)

go func() {
t.Logf("Postgres Fake server running at %s port", postgresTestServer.Port())
require.NoError(t, postgresTestServer.Serve())
t.Logf("Postgres Fake server running at %s port", postgresTestServer.Port())
}()
t.Cleanup(func() {
postgresTestServer.Close()
})

setup.DatabaseConfig = types.DatabaseSpecV3{
Protocol: "postgres",
URI: "localhost:" + postgresTestServer.Port(),
URI: net.JoinHostPort("localhost", postgresTestServer.Port()),
}
}

Expand Down Expand Up @@ -287,10 +285,6 @@ func setupTeleportClient(t *testing.T, setup *TestSetup) {
err := setup.TeleportClient.Close()
require.NoError(t, err)
})

// This will not work when OPERATOR_TEST_TELEPORT_ADDR is set.
// Not sure how to work around this.
setupMockPostgresServer(t, teleportServer.Process, setup)
}

type TestOption func(*TestSetup)
Expand Down Expand Up @@ -343,6 +337,7 @@ func SetupTestEnv(t *testing.T, opts ...TestOption) *TestSetup {
}

setupTeleportClient(t, setup)
setupMockPostgresServer(t, setup)

// If the test wants to do step by step reconciliation, we don't start
// an operator in the background.
Expand Down

0 comments on commit b1a3eae

Please sign in to comment.