Skip to content

Commit 100110f

Browse files
committed
chore: try using postgres testcontainer to wrap pgbouncer
1 parent 86b9554 commit 100110f

20 files changed

+150
-193
lines changed

cmd/spicedb/integration/migrate_integration_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build docker && image
1+
// //go:build docker && image
22

33
package integration_test
44

@@ -41,7 +41,7 @@ func TestMigrate(t *testing.T) {
4141
t.Run(engineKey, func(t *testing.T) {
4242
engineKey := engineKey
4343

44-
r := testdatastore.RunDatastoreEngineWithBridge(t, engineKey, bridgeNetworkName)
44+
r := testdatastore.RunDatastoreEngine(t, engineKey)
4545
db := r.NewDatabase(t)
4646

4747
envVars := map[string]string{}
@@ -55,6 +55,7 @@ func TestMigrate(t *testing.T) {
5555
}
5656

5757
// Run the migrate command and wait for it to complete.
58+
// TODO:
5859
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
5960
ContainerRequest: testcontainers.ContainerRequest{
6061
Image: "authzed/spicedb:ci",
@@ -65,9 +66,7 @@ func TestMigrate(t *testing.T) {
6566
Started: true,
6667
})
6768
require.NoError(t, err)
68-
t.Cleanup(func() {
69-
_ = container.Terminate(ctx)
70-
})
69+
testcontainers.CleanupContainer(t, container)
7170

7271
// Ensure the command completed successfully.
7372
state, err := container.State(ctx)
@@ -76,6 +75,7 @@ func TestMigrate(t *testing.T) {
7675
if state.ExitCode != 0 {
7776
stream := new(bytes.Buffer)
7877

78+
// TODO: use logs
7979
logReader, lerr := container.Logs(ctx)
8080
require.NoError(t, lerr)
8181
defer logReader.Close()

cmd/spicedb/integration/schemawatch_integration_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build docker && image
1+
// //go:build docker && image
22

33
package integration_test
44

@@ -45,7 +45,7 @@ func TestSchemaWatch(t *testing.T) {
4545
_ = net.Remove(ctx)
4646
})
4747

48-
engine := testdatastore.RunDatastoreEngineWithBridge(t, driverName, bridgeNetworkName)
48+
engine := testdatastore.RunDatastoreEngine(t, driverName)
4949

5050
envVars := map[string]string{}
5151
if wev, ok := engine.(testdatastore.RunningEngineForTestWithEnvVars); ok {
@@ -79,6 +79,7 @@ func TestSchemaWatch(t *testing.T) {
7979
require.Equal(t, 0, exitCode.ExitCode)
8080

8181
// Run a serve and immediately close, ensuring it shuts down gracefully.
82+
// TODO;
8283
serveContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
8384
ContainerRequest: testcontainers.ContainerRequest{
8485
Image: "authzed/spicedb:ci",
@@ -89,14 +90,13 @@ func TestSchemaWatch(t *testing.T) {
8990
Started: true,
9091
})
9192
require.NoError(t, err)
92-
t.Cleanup(func() {
93-
_ = serveContainer.Terminate(ctx)
94-
})
93+
testcontainers.CleanupContainer(t, serveContainer)
9594

9695
ww := &watchingWriter{make(chan bool, 1), "starting watching cache"}
9796

9897
// Grab logs and ensure schema watch has started before graceful shutdown.
9998
go (func() {
99+
// TODO: do logging directly
100100
logReader, err := serveContainer.Logs(ctx)
101101
if err != nil {
102102
assert.NoError(t, err)
@@ -107,15 +107,14 @@ func TestSchemaWatch(t *testing.T) {
107107
assert.NoError(t, err)
108108
})()
109109

110+
// TODO: what?
110111
select {
111112
case <-ww.c:
112113
break
113114

114115
case <-time.After(10 * time.Second):
115116
require.Fail(t, "timed out waiting for schema watch to run")
116117
}
117-
118-
require.True(t, gracefulShutdown(ctx, serveContainer))
119118
})
120119
}
121120
}

cmd/spicedb/integration/serve_integration_test.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build docker && image
1+
// //go:build docker && image
22

33
package integration_test
44

@@ -26,11 +26,13 @@ import (
2626

2727
testdatastore "github.com/authzed/spicedb/internal/testserver/datastore"
2828
"github.com/authzed/spicedb/pkg/datastore"
29+
"github.com/authzed/spicedb/pkg/migrate"
2930
)
3031

3132
func TestServe(t *testing.T) {
3233
requireParent := require.New(t)
3334

35+
// TODO:
3436
tester, err := newTester(t,
3537
testcontainers.ContainerRequest{
3638
Image: "authzed/spicedb:ci",
@@ -112,7 +114,7 @@ func gracefulShutdown(ctx context.Context, container testcontainers.Container) b
112114
}
113115

114116
func TestGracefulShutdownInMemory(t *testing.T) {
115-
ctx := context.Background()
117+
ctx := t.Context()
116118

117119
// Run a serve and immediately close, ensuring it shuts down gracefully.
118120
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
@@ -154,17 +156,15 @@ func TestGracefulShutdown(t *testing.T) {
154156

155157
for driverName, awaitGC := range engines {
156158
t.Run(driverName, func(t *testing.T) {
157-
ctx := context.Background()
158-
bridgeNetworkName := fmt.Sprintf("bridge-%s", uuid.New().String())
159+
ctx := t.Context()
159160

160-
// Create a bridge network for testing.
161-
net, err := network.New(ctx, network.WithDriver("bridge"), network.WithLabels(map[string]string{"name": bridgeNetworkName}))
161+
// Create a network for testing.
162+
net, err := network.New(ctx)
162163
require.NoError(t, err)
163-
t.Cleanup(func() {
164-
_ = net.Remove(ctx)
165-
})
164+
testcontainers.CleanupNetwork(t, net)
166165

167-
engine := testdatastore.RunDatastoreEngineWithBridge(t, driverName, bridgeNetworkName)
166+
// TODO: figure out how to supply the network in this case.
167+
engine := testdatastore.RunDatastoreEngine(t, driverName)
168168

169169
envVars := map[string]string{}
170170
if wev, ok := engine.(testdatastore.RunningEngineForTestWithEnvVars); ok {
@@ -177,20 +177,18 @@ func TestGracefulShutdown(t *testing.T) {
177177
}
178178

179179
// Run the migrate command and wait for it to complete.
180+
// TODO: probably handled by spicedb testcontainer
180181
db := engine.NewDatabase(t)
181182
migrateContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
182183
ContainerRequest: testcontainers.ContainerRequest{
183184
Image: "authzed/spicedb:ci",
184185
Cmd: []string{"migrate", "head", "--datastore-engine", driverName, "--datastore-conn-uri", db},
185-
Networks: []string{bridgeNetworkName},
186186
Env: envVars,
187187
},
188188
Started: true,
189189
})
190190
require.NoError(t, err)
191-
t.Cleanup(func() {
192-
_ = migrateContainer.Terminate(ctx)
193-
})
191+
testcontainers.CleanupContainer(t, migrateContainer)
194192

195193
// Ensure the command completed successfully.
196194
exitCode, err := migrateContainer.State(ctx)
@@ -202,15 +200,12 @@ func TestGracefulShutdown(t *testing.T) {
202200
ContainerRequest: testcontainers.ContainerRequest{
203201
Image: "authzed/spicedb:ci",
204202
Cmd: []string{"serve", "--grpc-preshared-key", "firstkey", "--datastore-engine", driverName, "--datastore-conn-uri", db, "--datastore-gc-interval", "1s", "--telemetry-endpoint", ""},
205-
Networks: []string{bridgeNetworkName},
206203
Env: envVars,
207204
},
208205
Started: true,
209206
})
210207
require.NoError(t, err)
211-
t.Cleanup(func() {
212-
_ = serveContainer.Terminate(ctx)
213-
})
208+
testcontainers.CleanupContainer(t, serveContainer)
214209

215210
if awaitGC {
216211
ww := &watchingWriter{make(chan bool, 1), "running garbage collection worker"}

cmd/spicedb/integration/servetesting_integration_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build docker && image
1+
// //go:build docker && image
22

33
package integration_test
44

@@ -190,6 +190,7 @@ const retryCount = 8
190190
func newTester(t *testing.T, containerReq testcontainers.ContainerRequest, token string, withExistingSchema bool) (*spicedbHandle, error) {
191191
ctx := context.Background()
192192

193+
// TODO:
193194
for i := 0; i < retryCount; i++ {
194195
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
195196
ContainerRequest: containerReq,

cmd/spicedb/integration/servetesting_race_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestCheckPermissionOnTesterNoFlakes(t *testing.T) {
3333
{
3434
HostFilePath: path.Join(basepath, "testdata/bootstrap.yaml"),
3535
ContainerFilePath: "/mnt/spicedb_bootstrap.yaml",
36-
FileMode: 0644,
36+
FileMode: 0o644,
3737
},
3838
},
3939
ExposedPorts: []string{"50051/tcp", "50052/tcp", "8443/tcp", "8444/tcp"},

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ require (
412412
github.com/stoewer/go-strcase v1.3.0 // indirect
413413
github.com/stretchr/objx v0.5.2 // indirect
414414
github.com/subosito/gotenv v1.6.0 // indirect
415+
github.com/testcontainers/testcontainers-go/modules/mysql v0.40.0 // indirect
415416
github.com/tetafro/godot v1.5.4 // indirect
416417
github.com/tetratelabs/wazero v1.9.0 // indirect
417418
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,8 @@ github.com/testcontainers/testcontainers-go v0.40.0 h1:pSdJYLOVgLE8YdUY2FHQ1Fxu+
17191719
github.com/testcontainers/testcontainers-go v0.40.0/go.mod h1:FSXV5KQtX2HAMlm7U3APNyLkkap35zNLxukw9oBi/MY=
17201720
github.com/testcontainers/testcontainers-go/modules/cockroachdb v0.40.0 h1:UNYfrnFV9mkO93Sw6hqRA5KbE9DsAvDeYKD4GDConiE=
17211721
github.com/testcontainers/testcontainers-go/modules/cockroachdb v0.40.0/go.mod h1:O8By1J/1y726YYk7obTIXxfv2OzonVe+ORq9Z+K+fDg=
1722+
github.com/testcontainers/testcontainers-go/modules/mysql v0.40.0 h1:P9Txfy5Jothx2wFdcus0QoSmX/PKSIXZxrTbZPVJswA=
1723+
github.com/testcontainers/testcontainers-go/modules/mysql v0.40.0/go.mod h1:oZPHHqJqXG7FD8OB/yWH7gLnDvZUlFHAVJNrGftL+eg=
17221724
github.com/testcontainers/testcontainers-go/modules/postgres v0.40.0 h1:s2bIayFXlbDFexo96y+htn7FzuhpXLYJNnIuglNKqOk=
17231725
github.com/testcontainers/testcontainers-go/modules/postgres v0.40.0/go.mod h1:h+u/2KoREGTnTl9UwrQ/g+XhasAT8E6dClclAADeXoQ=
17241726
github.com/tetafro/godot v1.5.4 h1:u1ww+gqpRLiIA16yF2PV1CV1n/X3zhyezbNXC3E14Sg=

internal/datastore/crdb/crdb_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func crdbTestVersion() string {
7676

7777
func TestCRDBDatastoreWithoutIntegrity(t *testing.T) {
7878
t.Parallel()
79-
b := testdatastore.RunCRDBForTesting(t, "", crdbTestVersion())
80-
test.All(t, test.DatastoreTesterFunc(func(_ testing.TB, revisionQuantization, gcInterval, gcWindow time.Duration, watchBufferLength uint16) (datastore.Datastore, error) {
79+
b := testdatastore.RunCRDBForTesting(t, crdbTestVersion())
80+
test.All(t, test.DatastoreTesterFunc(func(revisionQuantization, gcInterval, gcWindow time.Duration, watchBufferLength uint16) (datastore.Datastore, error) {
8181
ctx := context.Background()
8282
ds := b.NewDatastore(t, func(engine, uri string) datastore.Datastore {
8383
ds, err := NewCRDBDatastore(
@@ -140,7 +140,7 @@ func TestCRDBDatastoreWithFollowerReads(t *testing.T) {
140140
followerReadDelay := time.Duration(4.8 * float64(time.Second))
141141
gcWindow := 100 * time.Second
142142

143-
engine := testdatastore.RunCRDBForTesting(t, "", crdbTestVersion())
143+
engine := testdatastore.RunCRDBForTesting(t, crdbTestVersion())
144144

145145
quantizationDurations := []time.Duration{
146146
0 * time.Second,
@@ -203,7 +203,7 @@ var defaultKeyForTesting = proxy.KeyConfig{
203203

204204
func TestCRDBDatastoreWithIntegrity(t *testing.T) { //nolint:tparallel
205205
t.Parallel()
206-
b := testdatastore.RunCRDBForTesting(t, "", crdbTestVersion())
206+
b := testdatastore.RunCRDBForTesting(t, crdbTestVersion())
207207

208208
test.All(t, test.DatastoreTesterFunc(func(_ testing.TB, revisionQuantization, gcInterval, gcWindow time.Duration, watchBufferLength uint16) (datastore.Datastore, error) {
209209
ctx := context.Background()
@@ -303,7 +303,6 @@ func TestWatchFeatureDetection(t *testing.T) {
303303
},
304304
}
305305
for _, tt := range cases {
306-
tt := tt
307306
t.Run(tt.name, func(t *testing.T) {
308307
t.Parallel()
309308
ctx, cancel := context.WithCancel(context.Background())
@@ -455,6 +454,7 @@ func newCRDBWithUser(t *testing.T) (adminConn *pgx.Conn, connStrings map[provisi
455454
}))
456455
require.NoError(t, rootCertFile.Close())
457456

457+
// TODO: fix
458458
container, err := cockroachdb.Run(t.Context(),
459459
"cockroachdb/cockroach:v"+crdbTestVersion(),
460460
cockroachdb.WithInsecure(),
@@ -479,8 +479,9 @@ func newCRDBWithUser(t *testing.T) (adminConn *pgx.Conn, connStrings map[provisi
479479

480480
// Retry connection
481481
maxRetries := 10
482+
// TODO: require.EventuallyWithT
482483
for i := 0; i < maxRetries; i++ {
483-
_, err = pgxpool.New(context.Background(), fmt.Sprintf("postgres://root@localhost:%[1]s/defaultdb?sslmode=verify-full&sslrootcert=%[2]s/ca.crt&sslcert=%[2]s/client.root.crt&sslkey=%[2]s/client.root.key", port, certDir))
484+
_, err = pgxpool.New(t.Context(), fmt.Sprintf("postgres://root@localhost:%[1]s/defaultdb?sslmode=verify-full&sslrootcert=%[2]s/ca.crt&sslcert=%[2]s/client.root.crt&sslkey=%[2]s/client.root.key", port, certDir))
484485
if err == nil {
485486
break
486487
}

internal/datastore/crdb/pool_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
var testUserNS = namespace.Namespace(testUserNamespace)
2626

2727
func TestTxReset(t *testing.T) {
28-
b := testdatastore.RunCRDBForTesting(t, "", crdbTestVersion())
28+
b := testdatastore.RunCRDBForTesting(t, crdbTestVersion())
2929

3030
cases := []struct {
3131
name string
@@ -97,7 +97,6 @@ func TestTxReset(t *testing.T) {
9797
},
9898
}
9999
for _, tt := range cases {
100-
tt := tt
101100
t.Run(tt.name, func(t *testing.T) {
102101
ctx := t.Context()
103102

internal/datastore/mysql/datastore_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,13 @@ func TestMySQLDatastoreDSNWithoutParseTime(t *testing.T) {
121121
}
122122

123123
func TestMySQL8Datastore(t *testing.T) {
124-
b := testdatastore.RunMySQLForTestingWithOptions(t, testdatastore.MySQLTesterOptions{MigrateForNewDatastore: true}, "")
125124
dst := datastoreTester{b: b}
126125
test.AllWithExceptions(t, test.DatastoreTesterFunc(dst.createDatastore), test.WithCategories(test.WatchSchemaCategory, test.WatchCheckpointsCategory), true)
127126
additionalMySQLTests(t, b)
128127
}
129128

130129
func TestMySQLRevisionTimestamps(t *testing.T) {
131-
b := testdatastore.RunMySQLForTestingWithOptions(t, testdatastore.MySQLTesterOptions{MigrateForNewDatastore: true}, "")
130+
b := testdatastore.RunMySQLForTestingWithOptions(t, testdatastore.MySQLTesterOptions{MigrateForNewDatastore: true})
132131
t.Run("TransactionTimestamps", createDatastoreTest(b, TransactionTimestampsTest, defaultOptions...))
133132
}
134133

@@ -845,7 +844,7 @@ func TestMySQLWithAWSIAMCredentialsProvider(t *testing.T) {
845844

846845
func datastoreDB(t *testing.T, migrate bool) *sql.DB {
847846
var databaseURI string
848-
testdatastore.RunMySQLForTestingWithOptions(t, testdatastore.MySQLTesterOptions{MigrateForNewDatastore: migrate}, "").NewDatastore(t, func(engine, uri string) datastore.Datastore {
847+
testdatastore.RunMySQLForTestingWithOptions(t, testdatastore.MySQLTesterOptions{MigrateForNewDatastore: migrate}).NewDatastore(t, func(engine, uri string) datastore.Datastore {
849848
databaseURI = uri
850849
return nil
851850
})

0 commit comments

Comments
 (0)