From 332db55755592fdd4fa725f68c6e28881a2e3832 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Thu, 11 Apr 2024 13:09:45 -0600 Subject: [PATCH] chore: move `MustDefaultConfigWithRandomPorts` and `TCPRandomPort` to `testutils` package (#1529) Signed-off-by: Juan Antonio Osorio --- cmd/run/run_test.go | 40 ++++++++++++++++---------------- internal/server/config/config.go | 30 ------------------------ pkg/testutils/testutils.go | 32 +++++++++++++++++++++++++ tests/check/check_test.go | 2 +- tests/tests.go | 4 ++-- 5 files changed, 55 insertions(+), 53 deletions(-) diff --git a/cmd/run/run_test.go b/cmd/run/run_test.go index b0019143bc..5b462c47e7 100644 --- a/cmd/run/run_test.go +++ b/cmd/run/run_test.go @@ -188,7 +188,7 @@ func runServer(ctx context.Context, cfg *serverconfig.Config) error { } func TestBuildServiceWithPresharedKeyAuthenticationFailsIfZeroKeys(t *testing.T) { - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.Authn.Method = "preshared" cfg.Authn.AuthnPresharedKeyConfig = &serverconfig.AuthnPresharedKeyConfig{} @@ -197,7 +197,7 @@ func TestBuildServiceWithPresharedKeyAuthenticationFailsIfZeroKeys(t *testing.T) } func TestBuildServiceWithNoAuth(t *testing.T) { - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -219,7 +219,7 @@ func TestBuildServiceWithNoAuth(t *testing.T) { } func TestBuildServiceWithPresharedKeyAuthentication(t *testing.T) { - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.Authn.Method = "preshared" cfg.Authn.AuthnPresharedKeyConfig = &serverconfig.AuthnPresharedKeyConfig{ Keys: []string{"KEYONE", "KEYTWO"}, @@ -276,13 +276,13 @@ func TestBuildServiceWithPresharedKeyAuthentication(t *testing.T) { func TestBuildServiceWithTracingEnabled(t *testing.T) { // create mock OTLP server - otlpServerPort, otlpServerPortReleaser := serverconfig.TCPRandomPort() + otlpServerPort, otlpServerPortReleaser := testutils.TCPRandomPort() localOTLPServerURL := fmt.Sprintf("localhost:%d", otlpServerPort) otlpServerPortReleaser() otlpServer := mocks.NewMockTracingServer(t, otlpServerPort) // create OpenFGA server with tracing enabled - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.Trace.Enabled = true cfg.Trace.SampleRatio = 1 cfg.Trace.OTLP.Endpoint = localOTLPServerURL @@ -404,7 +404,7 @@ func tryGetStores(t *testing.T, test authTest, httpAddr string, retryClient *ret } func TestHTTPServerWithCORS(t *testing.T) { - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.Authn.Method = "preshared" cfg.Authn.AuthnPresharedKeyConfig = &serverconfig.AuthnPresharedKeyConfig{ Keys: []string{"KEYONE", "KEYTWO"}, @@ -500,10 +500,10 @@ func TestHTTPServerWithCORS(t *testing.T) { } func TestBuildServerWithOIDCAuthentication(t *testing.T) { - oidcServerPort, oidcServerPortReleaser := serverconfig.TCPRandomPort() + oidcServerPort, oidcServerPortReleaser := testutils.TCPRandomPort() localOIDCServerURL := fmt.Sprintf("http://localhost:%d", oidcServerPort) - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.Authn.Method = "oidc" cfg.Authn.AuthnOIDCConfig = &serverconfig.AuthnOIDCConfig{ Audience: "openfga.dev", @@ -568,12 +568,12 @@ func TestBuildServerWithOIDCAuthentication(t *testing.T) { } func TestBuildServerWithOIDCAuthenticationAlias(t *testing.T) { - oidcServerPort1, oidcServerPortReleaser1 := serverconfig.TCPRandomPort() - oidcServerPort2, oidcServerPortReleaser2 := serverconfig.TCPRandomPort() + oidcServerPort1, oidcServerPortReleaser1 := testutils.TCPRandomPort() + oidcServerPort2, oidcServerPortReleaser2 := testutils.TCPRandomPort() oidcServerURL1 := fmt.Sprintf("http://localhost:%d", oidcServerPort1) oidcServerURL2 := fmt.Sprintf("http://localhost:%d", oidcServerPort2) - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.Authn.Method = "oidc" cfg.Authn.AuthnOIDCConfig = &serverconfig.AuthnOIDCConfig{ Audience: "openfga.dev", @@ -623,7 +623,7 @@ func TestHTTPServingTLS(t *testing.T) { certsAndKeys := createCertsAndKeys(t) defer certsAndKeys.Clean() - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.HTTP.TLS = &serverconfig.TLSConfig{ CertPath: certsAndKeys.serverCertFile, KeyPath: certsAndKeys.serverKeyFile, @@ -645,7 +645,7 @@ func TestHTTPServingTLS(t *testing.T) { certsAndKeys := createCertsAndKeys(t) defer certsAndKeys.Clean() - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.HTTP.TLS = &serverconfig.TLSConfig{ Enabled: true, CertPath: certsAndKeys.serverCertFile, @@ -682,7 +682,7 @@ func TestGRPCServingTLS(t *testing.T) { certsAndKeys := createCertsAndKeys(t) defer certsAndKeys.Clean() - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.HTTP.Enabled = false cfg.GRPC.TLS = &serverconfig.TLSConfig{ CertPath: certsAndKeys.serverCertFile, @@ -705,7 +705,7 @@ func TestGRPCServingTLS(t *testing.T) { certsAndKeys := createCertsAndKeys(t) defer certsAndKeys.Clean() - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.HTTP.Enabled = false cfg.GRPC.TLS = &serverconfig.TLSConfig{ Enabled: true, @@ -744,13 +744,13 @@ func TestServerMetricsReporting(t *testing.T) { func testServerMetricsReporting(t *testing.T, engine string) { testDatastore := storagefixtures.RunDatastoreTestContainer(t, engine) - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.Datastore.Engine = engine cfg.Datastore.URI = testDatastore.GetConnectionURI(true) cfg.Datastore.Metrics.Enabled = true cfg.Metrics.Enabled = true cfg.Metrics.EnableRPCHistograms = true - metricsPort, metricsPortReleaser := serverconfig.TCPRandomPort() + metricsPort, metricsPortReleaser := testutils.TCPRandomPort() metricsPortReleaser() cfg.Metrics.Addr = fmt.Sprintf("0.0.0.0:%d", metricsPort) @@ -896,7 +896,7 @@ func testServerMetricsReporting(t *testing.T, engine string) { } func TestHTTPServerDisabled(t *testing.T) { - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() cfg.HTTP.Enabled = false ctx, cancel := context.WithCancel(context.Background()) @@ -916,7 +916,7 @@ func TestHTTPServerDisabled(t *testing.T) { } func TestHTTPServerEnabled(t *testing.T) { - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -1221,7 +1221,7 @@ func TestRunCommandConfigIsMerged(t *testing.T) { func TestHTTPHeaders(t *testing.T) { t.Parallel() - cfg := serverconfig.MustDefaultConfigWithRandomPorts() + cfg := testutils.MustDefaultConfigWithRandomPorts() ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) diff --git a/internal/server/config/config.go b/internal/server/config/config.go index dcfee166e2..79211c4e1f 100644 --- a/internal/server/config/config.go +++ b/internal/server/config/config.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "math" - "net" "strconv" "time" ) @@ -427,32 +426,3 @@ func MustDefaultConfig() *Config { return config } - -// MustDefaultConfigWithRandomPorts returns default server config but with random ports for the grpc and http addresses -// and with the playground, tracing and metrics turned off. -// This function may panic if somehow a random port cannot be chosen. -func MustDefaultConfigWithRandomPorts() *Config { - config := MustDefaultConfig() - - httpPort, httpPortReleaser := TCPRandomPort() - defer httpPortReleaser() - grpcPort, grpcPortReleaser := TCPRandomPort() - defer grpcPortReleaser() - - config.GRPC.Addr = fmt.Sprintf("0.0.0.0:%d", grpcPort) - config.HTTP.Addr = fmt.Sprintf("0.0.0.0:%d", httpPort) - - return config -} - -// TCPRandomPort tries to find a random TCP Port. If it can't find one, it panics. Else, it returns the port and a function that releases the port. -// It is the responsibility of the caller to call the release function right before trying to listen on the given port. -func TCPRandomPort() (int, func()) { - l, err := net.Listen("tcp", "") - if err != nil { - panic(err) - } - return l.Addr().(*net.TCPAddr).Port, func() { - l.Close() - } -} diff --git a/pkg/testutils/testutils.go b/pkg/testutils/testutils.go index b0362c9d24..562e8bf823 100644 --- a/pkg/testutils/testutils.go +++ b/pkg/testutils/testutils.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "math/rand" + "net" "net/http" "sort" "strconv" @@ -25,6 +26,8 @@ import ( "google.golang.org/grpc/credentials/insecure" healthv1pb "google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/protobuf/types/known/structpb" + + serverconfig "github.com/openfga/openfga/internal/server/config" ) const ( @@ -208,3 +211,32 @@ func EnsureServiceHealthy(t testing.TB, grpcAddr, httpAddr string, transportCred require.Equal(t, http.StatusOK, resp.StatusCode, "unexpected status code received from server") } } + +// MustDefaultConfigWithRandomPorts returns default server config but with random ports for the grpc and http addresses +// and with the playground, tracing and metrics turned off. +// This function may panic if somehow a random port cannot be chosen. +func MustDefaultConfigWithRandomPorts() *serverconfig.Config { + config := serverconfig.MustDefaultConfig() + + httpPort, httpPortReleaser := TCPRandomPort() + defer httpPortReleaser() + grpcPort, grpcPortReleaser := TCPRandomPort() + defer grpcPortReleaser() + + config.GRPC.Addr = fmt.Sprintf("0.0.0.0:%d", grpcPort) + config.HTTP.Addr = fmt.Sprintf("0.0.0.0:%d", httpPort) + + return config +} + +// TCPRandomPort tries to find a random TCP Port. If it can't find one, it panics. Else, it returns the port and a function that releases the port. +// It is the responsibility of the caller to call the release function right before trying to listen on the given port. +func TCPRandomPort() (int, func()) { + l, err := net.Listen("tcp", "") + if err != nil { + panic(err) + } + return l.Addr().(*net.TCPAddr).Port, func() { + l.Close() + } +} diff --git a/tests/check/check_test.go b/tests/check/check_test.go index 0801315adf..1b86f5a597 100644 --- a/tests/check/check_test.go +++ b/tests/check/check_test.go @@ -55,7 +55,7 @@ func TestCheckLogs(t *testing.T) { // defer goleak.VerifyNone(t) // create mock OTLP server - otlpServerPort, otlpServerPortReleaser := config.TCPRandomPort() + otlpServerPort, otlpServerPortReleaser := testutils.TCPRandomPort() localOTLPServerURL := fmt.Sprintf("localhost:%d", otlpServerPort) otlpServerPortReleaser() _ = mocks.NewMockTracingServer(t, otlpServerPort) diff --git a/tests/tests.go b/tests/tests.go index dbae818add..171abbf00e 100644 --- a/tests/tests.go +++ b/tests/tests.go @@ -41,9 +41,9 @@ func StartServerWithContext(t testing.TB, cfg *serverconfig.Config, serverCtx *r ctx, cancel := context.WithCancel(context.Background()) - httpPort, httpPortReleaser := serverconfig.TCPRandomPort() + httpPort, httpPortReleaser := testutils.TCPRandomPort() cfg.HTTP.Addr = fmt.Sprintf("0.0.0.0:%d", httpPort) - grpcPort, grpcPortReleaser := serverconfig.TCPRandomPort() + grpcPort, grpcPortReleaser := testutils.TCPRandomPort() cfg.GRPC.Addr = fmt.Sprintf("0.0.0.0:%d", grpcPort) // these two functions release the ports so that the server can start listening on them