mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
chore: add postgres template caching for tests (#15336)
This PR is the first in a series aimed at closing [#15109](https://github.com/coder/coder/issues/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.
This commit is contained in:
@@ -32,9 +32,8 @@ func TestResetPassword(t *testing.T) {
|
||||
const newPassword = "MyNewPassword!"
|
||||
|
||||
// start postgres and coder server processes
|
||||
connectionURL, closeFunc, err := dbtestutil.Open()
|
||||
connectionURL, err := dbtestutil.Open(t)
|
||||
require.NoError(t, err)
|
||||
defer closeFunc()
|
||||
ctx, cancelFunc := context.WithCancel(context.Background())
|
||||
serverDone := make(chan struct{})
|
||||
serverinv, cfg := clitest.New(t,
|
||||
|
||||
@@ -85,9 +85,8 @@ func TestServerCreateAdminUser(t *testing.T) {
|
||||
// Skip on non-Linux because it spawns a PostgreSQL instance.
|
||||
t.SkipNow()
|
||||
}
|
||||
connectionURL, closeFunc, err := dbtestutil.Open()
|
||||
connectionURL, err := dbtestutil.Open(t)
|
||||
require.NoError(t, err)
|
||||
defer closeFunc()
|
||||
|
||||
sqlDB, err := sql.Open("postgres", connectionURL)
|
||||
require.NoError(t, err)
|
||||
@@ -151,9 +150,8 @@ func TestServerCreateAdminUser(t *testing.T) {
|
||||
// Skip on non-Linux because it spawns a PostgreSQL instance.
|
||||
t.SkipNow()
|
||||
}
|
||||
connectionURL, closeFunc, err := dbtestutil.Open()
|
||||
connectionURL, err := dbtestutil.Open(t)
|
||||
require.NoError(t, err)
|
||||
defer closeFunc()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
|
||||
defer cancel()
|
||||
@@ -185,9 +183,8 @@ func TestServerCreateAdminUser(t *testing.T) {
|
||||
// Skip on non-Linux because it spawns a PostgreSQL instance.
|
||||
t.SkipNow()
|
||||
}
|
||||
connectionURL, closeFunc, err := dbtestutil.Open()
|
||||
connectionURL, err := dbtestutil.Open(t)
|
||||
require.NoError(t, err)
|
||||
defer closeFunc()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
|
||||
defer cancel()
|
||||
@@ -225,9 +222,8 @@ func TestServerCreateAdminUser(t *testing.T) {
|
||||
// Skip on non-Linux because it spawns a PostgreSQL instance.
|
||||
t.SkipNow()
|
||||
}
|
||||
connectionURL, closeFunc, err := dbtestutil.Open()
|
||||
connectionURL, err := dbtestutil.Open(t)
|
||||
require.NoError(t, err)
|
||||
defer closeFunc()
|
||||
ctx, cancelFunc := context.WithCancel(context.Background())
|
||||
defer cancelFunc()
|
||||
|
||||
|
||||
+2
-4
@@ -1598,9 +1598,8 @@ func TestServer_Production(t *testing.T) {
|
||||
// Skip on non-Linux because it spawns a PostgreSQL instance.
|
||||
t.SkipNow()
|
||||
}
|
||||
connectionURL, closeFunc, err := dbtestutil.Open()
|
||||
connectionURL, err := dbtestutil.Open(t)
|
||||
require.NoError(t, err)
|
||||
defer closeFunc()
|
||||
|
||||
// Postgres + race detector + CI = slow.
|
||||
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitSuperLong*3)
|
||||
@@ -1803,9 +1802,8 @@ func TestConnectToPostgres(t *testing.T) {
|
||||
|
||||
log := slogtest.Make(t, nil)
|
||||
|
||||
dbURL, closeFunc, err := dbtestutil.Open()
|
||||
dbURL, err := dbtestutil.Open(t)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(closeFunc)
|
||||
|
||||
sqlDB, err := cli.ConnectToPostgres(ctx, log, "postgres", dbURL)
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user