Skip to content

Commit 1bfa7d4

Browse files
authored
chore: add postgres template caching for tests (#15336)
This PR is the first in a series aimed at closing [#15109](#15109). ### Changes - **Template Database Creation:** `dbtestutil.Open` now has the ability to create a template database if none is provided via `DB_FROM`. The template database’s name is derived from a hash of the migration files, ensuring that it can be reused across tests and is automatically updated whenever migrations change. - **Optimized Database Handling:** Previously, `dbtestutil.Open` would spin up a new container for each test when `DB_FROM` was unset. Now, it first checks for an active PostgreSQL instance on `localhost:5432`. If none is found, it creates a single container that remains available for subsequent tests, eliminating repeated container startups. These changes address the long individual test times (10+ seconds) reported by some users, likely due to the time Docker took to start and complete migrations.
1 parent 1c29944 commit 1bfa7d4

File tree

15 files changed

+629
-144
lines changed

15 files changed

+629
-144
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ sqlc-vet: test-postgres-docker
765765
test-postgres: test-postgres-docker
766766
# The postgres test is prone to failure, so we limit parallelism for
767767
# more consistent execution.
768-
$(GIT_FLAGS) DB=ci DB_FROM=$(shell go run scripts/migrate-ci/main.go) gotestsum \
768+
$(GIT_FLAGS) DB=ci gotestsum \
769769
--junitfile="gotests.xml" \
770770
--jsonfile="gotests.json" \
771771
--packages="./..." -- \

cli/resetpassword_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ func TestResetPassword(t *testing.T) {
3232
const newPassword = "MyNewPassword!"
3333

3434
// start postgres and coder server processes
35-
connectionURL, closeFunc, err := dbtestutil.Open()
35+
connectionURL, err := dbtestutil.Open(t)
3636
require.NoError(t, err)
37-
defer closeFunc()
3837
ctx, cancelFunc := context.WithCancel(context.Background())
3938
serverDone := make(chan struct{})
4039
serverinv, cfg := clitest.New(t,

cli/server_createadminuser_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ func TestServerCreateAdminUser(t *testing.T) {
8585
// Skip on non-Linux because it spawns a PostgreSQL instance.
8686
t.SkipNow()
8787
}
88-
connectionURL, closeFunc, err := dbtestutil.Open()
88+
connectionURL, err := dbtestutil.Open(t)
8989
require.NoError(t, err)
90-
defer closeFunc()
9190

9291
sqlDB, err := sql.Open("postgres", connectionURL)
9392
require.NoError(t, err)
@@ -151,9 +150,8 @@ func TestServerCreateAdminUser(t *testing.T) {
151150
// Skip on non-Linux because it spawns a PostgreSQL instance.
152151
t.SkipNow()
153152
}
154-
connectionURL, closeFunc, err := dbtestutil.Open()
153+
connectionURL, err := dbtestutil.Open(t)
155154
require.NoError(t, err)
156-
defer closeFunc()
157155

158156
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
159157
defer cancel()
@@ -185,9 +183,8 @@ func TestServerCreateAdminUser(t *testing.T) {
185183
// Skip on non-Linux because it spawns a PostgreSQL instance.
186184
t.SkipNow()
187185
}
188-
connectionURL, closeFunc, err := dbtestutil.Open()
186+
connectionURL, err := dbtestutil.Open(t)
189187
require.NoError(t, err)
190-
defer closeFunc()
191188

192189
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
193190
defer cancel()
@@ -225,9 +222,8 @@ func TestServerCreateAdminUser(t *testing.T) {
225222
// Skip on non-Linux because it spawns a PostgreSQL instance.
226223
t.SkipNow()
227224
}
228-
connectionURL, closeFunc, err := dbtestutil.Open()
225+
connectionURL, err := dbtestutil.Open(t)
229226
require.NoError(t, err)
230-
defer closeFunc()
231227
ctx, cancelFunc := context.WithCancel(context.Background())
232228
defer cancelFunc()
233229

cli/server_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,9 +1598,8 @@ func TestServer_Production(t *testing.T) {
15981598
// Skip on non-Linux because it spawns a PostgreSQL instance.
15991599
t.SkipNow()
16001600
}
1601-
connectionURL, closeFunc, err := dbtestutil.Open()
1601+
connectionURL, err := dbtestutil.Open(t)
16021602
require.NoError(t, err)
1603-
defer closeFunc()
16041603

16051604
// Postgres + race detector + CI = slow.
16061605
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitSuperLong*3)
@@ -1803,9 +1802,8 @@ func TestConnectToPostgres(t *testing.T) {
18031802

18041803
log := slogtest.Make(t, nil)
18051804

1806-
dbURL, closeFunc, err := dbtestutil.Open()
1805+
dbURL, err := dbtestutil.Open(t)
18071806
require.NoError(t, err)
1808-
t.Cleanup(closeFunc)
18091807

18101808
sqlDB, err := cli.ConnectToPostgres(ctx, log, "postgres", dbURL)
18111809
require.NoError(t, err)

coderd/database/db_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ func TestNestedInTx(t *testing.T) {
8787
func testSQLDB(t testing.TB) *sql.DB {
8888
t.Helper()
8989

90-
connection, closeFn, err := dbtestutil.Open()
90+
connection, err := dbtestutil.Open(t)
9191
require.NoError(t, err)
92-
t.Cleanup(closeFn)
9392

9493
db, err := sql.Open("postgres", connection)
9594
require.NoError(t, err)

coderd/database/dbtestutil/db.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,17 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
9595
opt(&o)
9696
}
9797

98-
db := dbmem.New()
99-
ps := pubsub.NewInMemory()
98+
var db database.Store
99+
var ps pubsub.Pubsub
100100
if WillUsePostgres() {
101101
connectionURL := os.Getenv("CODER_PG_CONNECTION_URL")
102102
if connectionURL == "" && o.url != "" {
103103
connectionURL = o.url
104104
}
105105
if connectionURL == "" {
106-
var (
107-
err error
108-
closePg func()
109-
)
110-
connectionURL, closePg, err = Open()
106+
var err error
107+
connectionURL, err = Open(t)
111108
require.NoError(t, err)
112-
t.Cleanup(closePg)
113109
}
114110

115111
if o.fixedTimezone == "" {
@@ -143,6 +139,9 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
143139
t.Cleanup(func() {
144140
_ = ps.Close()
145141
})
142+
} else {
143+
db = dbmem.New()
144+
ps = pubsub.NewInMemory()
146145
}
147146

148147
return db, ps

0 commit comments

Comments
 (0)