Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PostgreSQL Test Containers for Integration Testing #602

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ linters-settings:
- "golang.org/x/text/language"
- "github.com/redis/go-redis/v9"
- "github.com/spaolacci/murmur3"
- "github.com/testcontainers/testcontainers-go"
- "github.com/stretchr/testify/require"
- "github.com/docker/go-connections/nat"
test:
files:
- $test
Expand All @@ -86,6 +89,7 @@ linters-settings:
- "github.com/jackc/pgx/v5/pgproto3"
- "github.com/testcontainers/testcontainers-go"
- "github.com/redis/go-redis/v9"
- "github.com/docker/go-connections/nat"
tagalign:
align: false
sort: false
Expand Down
17 changes: 11 additions & 6 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/gatewayd-io/gatewayd/network"
"github.com/gatewayd-io/gatewayd/plugin"
"github.com/gatewayd-io/gatewayd/pool"
"github.com/gatewayd-io/gatewayd/testhelpers"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -239,9 +240,12 @@ func TestPoolsWithEmptyPools(t *testing.T) {
}

func TestGetProxies(t *testing.T) {
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port()

clientConfig := &config.Client{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
Address: postgresAddress,
}
client := network.NewClient(context.TODO(), clientConfig, zerolog.Logger{}, nil)
require.NotNil(t, client)
Expand All @@ -255,7 +259,7 @@ func TestGetProxies(t *testing.T) {
HealthCheckPeriod: config.DefaultHealthCheckPeriod,
ClientConfig: &config.Client{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
Address: postgresAddress,
},
Logger: zerolog.Logger{},
PluginTimeout: config.DefaultPluginTimeout,
Expand Down Expand Up @@ -289,9 +293,11 @@ func TestGetProxies(t *testing.T) {
}

func TestGetServers(t *testing.T) {
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port()
clientConfig := &config.Client{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
Address: postgresAddress,
}
client := network.NewClient(context.TODO(), clientConfig, zerolog.Logger{}, nil)
newPool := pool.NewPool(context.TODO(), 1)
Expand All @@ -305,7 +311,7 @@ func TestGetServers(t *testing.T) {
HealthCheckPeriod: config.DefaultHealthCheckPeriod,
ClientConfig: &config.Client{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
Address: postgresAddress,
},
Logger: zerolog.Logger{},
PluginTimeout: config.DefaultPluginTimeout,
Expand Down Expand Up @@ -337,7 +343,7 @@ func TestGetServers(t *testing.T) {
context.TODO(),
network.Server{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
Address: postgresAddress,
TickInterval: config.DefaultTickInterval,
Options: network.Option{
EnableTicker: false,
Expand Down Expand Up @@ -370,7 +376,6 @@ func TestGetServers(t *testing.T) {

if defaultServer, ok := servers.AsMap()[config.Default].(map[string]interface{}); ok {
assert.Equal(t, config.DefaultNetwork, defaultServer["network"])
assert.Equal(t, config.DefaultAddress, "localhost:5432")
statusFloat, isStatusFloat := defaultServer["status"].(float64)
assert.True(t, isStatusFloat, "status should be of type float64")
status := config.Status(statusFloat)
Expand Down
9 changes: 6 additions & 3 deletions api/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import (
"github.com/gatewayd-io/gatewayd/network"
"github.com/gatewayd-io/gatewayd/plugin"
"github.com/gatewayd-io/gatewayd/pool"
"github.com/gatewayd-io/gatewayd/testhelpers"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/health/grpc_health_v1"
)

func Test_Healthchecker(t *testing.T) {
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port()
clientConfig := &config.Client{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
Address: postgresAddress,
}
client := network.NewClient(context.TODO(), clientConfig, zerolog.Logger{}, nil)
newPool := pool.NewPool(context.TODO(), 1)
Expand All @@ -32,7 +35,7 @@ func Test_Healthchecker(t *testing.T) {
HealthCheckPeriod: config.DefaultHealthCheckPeriod,
ClientConfig: &config.Client{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
Address: postgresAddress,
},
Logger: zerolog.Logger{},
PluginTimeout: config.DefaultPluginTimeout,
Expand Down Expand Up @@ -64,7 +67,7 @@ func Test_Healthchecker(t *testing.T) {
context.TODO(),
network.Server{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
Address: postgresAddress,
TickInterval: config.DefaultTickInterval,
Options: network.Option{
EnableTicker: false,
Expand Down
21 changes: 20 additions & 1 deletion cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/gatewayd-io/gatewayd/config"
"github.com/gatewayd-io/gatewayd/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -18,6 +19,10 @@ var (
)

func Test_runCmd(t *testing.T) {
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port()
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress)

globalTestConfigFile := "./test_global_runCmd.yaml"
pluginTestConfigFile := "./test_plugins_runCmd.yaml"
// Create a test plugins config file.
Expand Down Expand Up @@ -78,6 +83,10 @@ func Test_runCmd(t *testing.T) {

// Test_runCmdWithTLS tests the run command with TLS enabled on the server.
func Test_runCmdWithTLS(t *testing.T) {
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port()
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress)

globalTLSTestConfigFile := "./testdata/gatewayd_tls.yaml"
pluginTestConfigFile := "./test_plugins_runCmdWithTLS.yaml"
// Create a test plugins config file.
Expand Down Expand Up @@ -133,8 +142,14 @@ func Test_runCmdWithTLS(t *testing.T) {
}

// Test_runCmdWithMultiTenancy tests the run command with multi-tenancy enabled.
// Note: This test needs two instances of PostgreSQL running on ports 5432 and 5433.
func Test_runCmdWithMultiTenancy(t *testing.T) {
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port()
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress)
postgresHostIP2, postgresMappedPort2 := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
postgredAddress2 := postgresHostIP2 + ":" + postgresMappedPort2.Port()
t.Setenv("GATEWAYD_CLIENTS_TEST_WRITE_ADDRESS", postgredAddress2)

globalTestConfigFile := "./testdata/gatewayd.yaml"
pluginTestConfigFile := "./test_plugins_runCmdWithMultiTenancy.yaml"
// Create a test plugins config file.
Expand Down Expand Up @@ -192,6 +207,10 @@ func Test_runCmdWithMultiTenancy(t *testing.T) {
}

func Test_runCmdWithCachePlugin(t *testing.T) {
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port()
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress)

globalTestConfigFile := "./test_global_runCmdWithCachePlugin.yaml"
pluginTestConfigFile := "./test_plugins_runCmdWithCachePlugin.yaml"
// TODO: Remove this once these global variables are removed from cmd/run.go.
Expand Down
16 changes: 14 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/NYTimes/gziphandler v1.1.1
github.com/codingsince1985/checksum v1.3.0
github.com/cybercyst/go-scaffold v0.0.0-20240606114431-816e6211c151
github.com/docker/go-connections v0.5.0
github.com/envoyproxy/protoc-gen-validate v1.1.0
github.com/gatewayd-io/gatewayd-plugin-sdk v0.3.1
github.com/getsentry/sentry-go v0.28.1
Expand Down Expand Up @@ -36,7 +37,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.29.0
go.opentelemetry.io/otel/sdk v1.29.0
go.opentelemetry.io/otel/trace v1.29.0
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
golang.org/x/text v0.18.0
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1
google.golang.org/grpc v1.66.0
Expand All @@ -57,17 +58,20 @@ require (
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cilium/ebpf v0.16.0 // indirect
github.com/cloudflare/circl v1.4.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/cosiner/argv v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/creack/pty v1.1.20 // indirect
github.com/cyphar/filepath-securejoin v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/derekparker/trie v0.0.0-20230829180723-39f4de51ef7d // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v27.2.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/expr-lang/expr v1.16.9 // indirect
Expand All @@ -77,6 +81,8 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/flosch/pongo2/v6 v6.0.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-delve/delve v1.23.0 // indirect
github.com/go-delve/liner v1.2.3-0.20231231155935-4726ab1d7f62 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.12.0 // indirect
Expand All @@ -86,8 +92,10 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-dap v0.12.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -100,6 +108,7 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
Expand All @@ -123,6 +132,7 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
github.com/qri-io/jsonpointer v0.1.1 // indirect
github.com/qri-io/jsonschema v0.2.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
Expand All @@ -144,7 +154,9 @@ require (
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.starlark.net v0.0.0-20240725214946-42030a7cedce // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/arch v0.10.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
Expand Down
Loading