chore(cli): remove dbmem from tests (#18195)

This commit is contained in:
Hugo Dutka
2025-06-05 09:29:31 +02:00
committed by GitHub
parent 9ef44e6be2
commit bb2c19562b
2 changed files with 56 additions and 62 deletions

View File

@ -651,9 +651,9 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
AppHostname: appHostname, AppHostname: appHostname,
AppHostnameRegex: appHostnameRegex, AppHostnameRegex: appHostnameRegex,
Logger: logger.Named("coderd"), Logger: logger.Named("coderd"),
Database: dbmem.New(), Database: nil,
BaseDERPMap: derpMap, BaseDERPMap: derpMap,
Pubsub: pubsub.NewInMemory(), Pubsub: nil,
CacheDir: cacheDir, CacheDir: cacheDir,
GoogleTokenValidator: googleTokenValidator, GoogleTokenValidator: googleTokenValidator,
ExternalAuthConfigs: externalAuthConfigs, ExternalAuthConfigs: externalAuthConfigs,

View File

@ -58,6 +58,15 @@ import (
"github.com/coder/coder/v2/testutil" "github.com/coder/coder/v2/testutil"
) )
func dbArg(t *testing.T) string {
if !dbtestutil.WillUsePostgres() {
return "--in-memory"
}
dbURL, err := dbtestutil.Open(t)
require.NoError(t, err)
return "--postgres-url=" + dbURL
}
func TestReadExternalAuthProvidersFromEnv(t *testing.T) { func TestReadExternalAuthProvidersFromEnv(t *testing.T) {
t.Parallel() t.Parallel()
t.Run("Valid", func(t *testing.T) { t.Run("Valid", func(t *testing.T) {
@ -267,7 +276,7 @@ func TestServer(t *testing.T) {
t.Parallel() t.Parallel()
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://localhost:3000/", "--access-url", "http://localhost:3000/",
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
@ -475,7 +484,7 @@ func TestServer(t *testing.T) {
t.Parallel() t.Parallel()
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://localhost:3000/", "--access-url", "http://localhost:3000/",
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
@ -498,7 +507,7 @@ func TestServer(t *testing.T) {
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "https://foobarbaz.mydomain", "--access-url", "https://foobarbaz.mydomain",
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
@ -519,7 +528,7 @@ func TestServer(t *testing.T) {
t.Parallel() t.Parallel()
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "https://google.com", "--access-url", "https://google.com",
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
@ -541,7 +550,7 @@ func TestServer(t *testing.T) {
root, _ := clitest.New(t, root, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "google.com", "--access-url", "google.com",
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
@ -557,7 +566,7 @@ func TestServer(t *testing.T) {
root, _ := clitest.New(t, root, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", "", "--http-address", "",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--tls-enable", "--tls-enable",
@ -575,7 +584,7 @@ func TestServer(t *testing.T) {
root, _ := clitest.New(t, root, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", "", "--http-address", "",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--tls-enable", "--tls-enable",
@ -628,7 +637,7 @@ func TestServer(t *testing.T) {
args := []string{ args := []string{
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
@ -650,7 +659,7 @@ func TestServer(t *testing.T) {
certPath, keyPath := generateTLSCertificate(t) certPath, keyPath := generateTLSCertificate(t)
root, cfg := clitest.New(t, root, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", "", "--http-address", "",
"--access-url", "https://example.com", "--access-url", "https://example.com",
"--tls-enable", "--tls-enable",
@ -686,7 +695,7 @@ func TestServer(t *testing.T) {
cert2Path, key2Path := generateTLSCertificate(t, "*.llama.com") cert2Path, key2Path := generateTLSCertificate(t, "*.llama.com")
root, cfg := clitest.New(t, root, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", "", "--http-address", "",
"--access-url", "https://example.com", "--access-url", "https://example.com",
"--tls-enable", "--tls-enable",
@ -766,7 +775,7 @@ func TestServer(t *testing.T) {
certPath, keyPath := generateTLSCertificate(t) certPath, keyPath := generateTLSCertificate(t)
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "https://example.com", "--access-url", "https://example.com",
"--tls-enable", "--tls-enable",
@ -894,7 +903,7 @@ func TestServer(t *testing.T) {
certPath, keyPath := generateTLSCertificate(t) certPath, keyPath := generateTLSCertificate(t)
flags := []string{ flags := []string{
"server", "server",
"--in-memory", dbArg(t),
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
"--http-address", httpListenAddr, "--http-address", httpListenAddr,
} }
@ -1004,33 +1013,19 @@ func TestServer(t *testing.T) {
t.Run("CanListenUnspecifiedv4", func(t *testing.T) { t.Run("CanListenUnspecifiedv4", func(t *testing.T) {
t.Parallel() t.Parallel()
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
root, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", "0.0.0.0:0", "--http-address", "0.0.0.0:0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
) )
pty := ptytest.New(t) pty := ptytest.New(t).Attach(inv)
root.Stdout = pty.Output() clitest.Start(t, inv)
root.Stderr = pty.Output()
serverStop := make(chan error, 1)
go func() {
err := root.WithContext(ctx).Run()
if err != nil {
t.Error(err)
}
close(serverStop)
}()
pty.ExpectMatch("Started HTTP listener") pty.ExpectMatch("Started HTTP listener")
pty.ExpectMatch("http://0.0.0.0:") pty.ExpectMatch("http://0.0.0.0:")
cancelFunc()
<-serverStop
}) })
t.Run("CanListenUnspecifiedv6", func(t *testing.T) { t.Run("CanListenUnspecifiedv6", func(t *testing.T) {
@ -1038,7 +1033,7 @@ func TestServer(t *testing.T) {
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", "[::]:0", "--http-address", "[::]:0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
) )
@ -1057,7 +1052,7 @@ func TestServer(t *testing.T) {
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":80", "--http-address", ":80",
"--tls-enable=false", "--tls-enable=false",
"--tls-address", "", "--tls-address", "",
@ -1074,7 +1069,7 @@ func TestServer(t *testing.T) {
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--tls-enable=true", "--tls-enable=true",
"--tls-address", "", "--tls-address", "",
) )
@ -1097,7 +1092,7 @@ func TestServer(t *testing.T) {
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--address", ":0", "--address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
@ -1124,7 +1119,7 @@ func TestServer(t *testing.T) {
certPath, keyPath := generateTLSCertificate(t) certPath, keyPath := generateTLSCertificate(t)
root, cfg := clitest.New(t, root, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--address", ":0", "--address", ":0",
"--access-url", "https://example.com", "--access-url", "https://example.com",
"--tls-enable", "--tls-enable",
@ -1161,7 +1156,7 @@ func TestServer(t *testing.T) {
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--trace=true", "--trace=true",
@ -1180,7 +1175,7 @@ func TestServer(t *testing.T) {
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--telemetry", "--telemetry",
@ -1220,7 +1215,7 @@ func TestServer(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitLong) ctx := testutil.Context(t, testutil.WaitLong)
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--provisioner-daemons", "1", "--provisioner-daemons", "1",
@ -1282,7 +1277,7 @@ func TestServer(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitLong) ctx := testutil.Context(t, testutil.WaitLong)
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--provisioner-daemons", "1", "--provisioner-daemons", "1",
@ -1339,7 +1334,7 @@ func TestServer(t *testing.T) {
fakeRedirect := "https://fake-url.com" fakeRedirect := "https://fake-url.com"
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--oauth2-github-allow-everyone", "--oauth2-github-allow-everyone",
@ -1386,7 +1381,7 @@ func TestServer(t *testing.T) {
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--oidc-client-id", "fake", "--oidc-client-id", "fake",
@ -1462,7 +1457,7 @@ func TestServer(t *testing.T) {
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--oidc-client-id", "fake", "--oidc-client-id", "fake",
@ -1556,7 +1551,7 @@ func TestServer(t *testing.T) {
root, cfg := clitest.New(t, root, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
) )
@ -1584,7 +1579,7 @@ func TestServer(t *testing.T) {
val := "100" val := "100"
root, cfg := clitest.New(t, root, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--api-rate-limit", val, "--api-rate-limit", val,
@ -1612,7 +1607,7 @@ func TestServer(t *testing.T) {
root, cfg := clitest.New(t, root, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--api-rate-limit", "-1", "--api-rate-limit", "-1",
@ -1644,7 +1639,7 @@ func TestServer(t *testing.T) {
root, _ := clitest.New(t, root, _ := clitest.New(t,
"server", "server",
"--log-filter=.*", "--log-filter=.*",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--provisioner-daemons=3", "--provisioner-daemons=3",
@ -1663,7 +1658,7 @@ func TestServer(t *testing.T) {
root, _ := clitest.New(t, root, _ := clitest.New(t,
"server", "server",
"--log-filter=.*", "--log-filter=.*",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--provisioner-daemons=3", "--provisioner-daemons=3",
@ -1682,7 +1677,7 @@ func TestServer(t *testing.T) {
root, _ := clitest.New(t, root, _ := clitest.New(t,
"server", "server",
"--log-filter=.*", "--log-filter=.*",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--provisioner-daemons=3", "--provisioner-daemons=3",
@ -1706,7 +1701,7 @@ func TestServer(t *testing.T) {
args := []string{ args := []string{
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--log-human", filepath.Join(t.TempDir(), "coder-logging-test-human"), "--log-human", filepath.Join(t.TempDir(), "coder-logging-test-human"),
@ -1750,7 +1745,7 @@ func TestServer(t *testing.T) {
ctx = testutil.Context(t, testutil.WaitMedium) ctx = testutil.Context(t, testutil.WaitMedium)
// Finally, we restart the server with just the config and no flags // Finally, we restart the server with just the config and no flags
// and ensure that the live configuration is equivalent. // and ensure that the live configuration is equivalent.
inv, cfg = clitest.New(t, "server", "--config="+fi.Name()) inv, cfg = clitest.New(t, "server", "--config="+fi.Name(), dbArg(t))
w = clitest.StartWithWaiter(t, inv) w = clitest.StartWithWaiter(t, inv)
client = codersdk.New(waitAccessURL(t, cfg)) client = codersdk.New(waitAccessURL(t, cfg))
_ = coderdtest.CreateFirstUser(t, client) _ = coderdtest.CreateFirstUser(t, client)
@ -1820,7 +1815,7 @@ func TestServer_Logging_NoParallel(t *testing.T) {
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--log-filter=.*", "--log-filter=.*",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--provisioner-daemons=3", "--provisioner-daemons=3",
@ -1855,7 +1850,7 @@ func TestServer_Logging_NoParallel(t *testing.T) {
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--log-filter=.*", "--log-filter=.*",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--provisioner-daemons=3", "--provisioner-daemons=3",
@ -1906,8 +1901,6 @@ func TestServer_Production(t *testing.T) {
// Skip on non-Linux because it spawns a PostgreSQL instance. // Skip on non-Linux because it spawns a PostgreSQL instance.
t.SkipNow() t.SkipNow()
} }
connectionURL, err := dbtestutil.Open(t)
require.NoError(t, err)
// Postgres + race detector + CI = slow. // Postgres + race detector + CI = slow.
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitSuperLong*3) ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitSuperLong*3)
@ -1917,14 +1910,14 @@ func TestServer_Production(t *testing.T) {
"server", "server",
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--postgres-url", connectionURL, dbArg(t),
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
) )
clitest.Start(t, inv.WithContext(ctx)) clitest.Start(t, inv.WithContext(ctx))
accessURL := waitAccessURL(t, cfg) accessURL := waitAccessURL(t, cfg)
client := codersdk.New(accessURL) client := codersdk.New(accessURL)
_, err = client.CreateFirstUser(ctx, coderdtest.FirstUserParams) _, err := client.CreateFirstUser(ctx, coderdtest.FirstUserParams)
require.NoError(t, err) require.NoError(t, err)
} }
@ -1974,7 +1967,7 @@ func TestServer_InterruptShutdown(t *testing.T) {
root, cfg := clitest.New(t, root, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--provisioner-daemons", "1", "--provisioner-daemons", "1",
@ -2006,7 +1999,7 @@ func TestServer_GracefulShutdown(t *testing.T) {
root, cfg := clitest.New(t, root, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--provisioner-daemons", "1", "--provisioner-daemons", "1",
@ -2190,9 +2183,10 @@ func TestServer_InvalidDERP(t *testing.T) {
// Try to start a server with the built-in DERP server disabled and no // Try to start a server with the built-in DERP server disabled and no
// external DERP map. // external DERP map.
inv, _ := clitest.New(t, inv, _ := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--derp-server-enable=false", "--derp-server-enable=false",
@ -2220,7 +2214,7 @@ func TestServer_DisabledDERP(t *testing.T) {
// external DERP map. // external DERP map.
inv, cfg := clitest.New(t, inv, cfg := clitest.New(t,
"server", "server",
"--in-memory", dbArg(t),
"--http-address", ":0", "--http-address", ":0",
"--access-url", "http://example.com", "--access-url", "http://example.com",
"--derp-server-enable=false", "--derp-server-enable=false",