Skip to content

Commit

Permalink
chore: move MustDefaultConfigWithRandomPorts and TCPRandomPort to…
Browse files Browse the repository at this point in the history
… `testutils` package (#1529)

Signed-off-by: Juan Antonio Osorio <ozz@stacklok.com>
  • Loading branch information
JAORMX authored Apr 11, 2024
1 parent 48b5992 commit 332db55
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 53 deletions.
40 changes: 20 additions & 20 deletions cmd/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -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()

Expand All @@ -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"},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand Down
30 changes: 0 additions & 30 deletions internal/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"math"
"net"
"strconv"
"time"
)
Expand Down Expand Up @@ -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()
}
}
32 changes: 32 additions & 0 deletions pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"net/http"
"sort"
"strconv"
Expand All @@ -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 (
Expand Down Expand Up @@ -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()
}
}
2 changes: 1 addition & 1 deletion tests/check/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 332db55

Please sign in to comment.