test: share coderdtest instances to stop paying the startup tax 22 times (#23454)

Consolidates 6 tests that spun up separate coderdtest instances per sub-test into a single shared instance per parent. 

> 🤖 This PR was created with the help of Coder Agents, and has been
reviewed by my human. 🧑‍💻
This commit is contained in:
Cian Johnston
2026-03-23 19:54:43 +00:00
committed by GitHub
parent d2afda8191
commit 956f6b2473
3 changed files with 31 additions and 59 deletions
+10 -27
View File
@@ -21,11 +21,14 @@ import (
func TestPostFiles(t *testing.T) { func TestPostFiles(t *testing.T) {
t.Parallel() t.Parallel()
t.Run("BadContentType", func(t *testing.T) {
t.Parallel() // Single instance shared across all sub-tests. Each sub-test
// creates independent resources with unique IDs so parallel
// execution is safe.
client := coderdtest.New(t, nil) client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client) _ = coderdtest.CreateFirstUser(t, client)
t.Run("BadContentType", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel() defer cancel()
@@ -35,9 +38,6 @@ func TestPostFiles(t *testing.T) {
t.Run("Insert", func(t *testing.T) { t.Run("Insert", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel() defer cancel()
@@ -47,9 +47,6 @@ func TestPostFiles(t *testing.T) {
t.Run("InsertWindowsZip", func(t *testing.T) { t.Run("InsertWindowsZip", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel() defer cancel()
@@ -59,9 +56,6 @@ func TestPostFiles(t *testing.T) {
t.Run("InsertAlreadyExists", func(t *testing.T) { t.Run("InsertAlreadyExists", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel() defer cancel()
@@ -73,9 +67,6 @@ func TestPostFiles(t *testing.T) {
}) })
t.Run("InsertConcurrent", func(t *testing.T) { t.Run("InsertConcurrent", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel() defer cancel()
@@ -99,11 +90,12 @@ func TestPostFiles(t *testing.T) {
func TestDownload(t *testing.T) { func TestDownload(t *testing.T) {
t.Parallel() t.Parallel()
t.Run("NotFound", func(t *testing.T) {
t.Parallel() // Shared instance — see TestPostFiles for rationale.
client := coderdtest.New(t, nil) client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client) _ = coderdtest.CreateFirstUser(t, client)
t.Run("NotFound", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel() defer cancel()
@@ -115,9 +107,6 @@ func TestDownload(t *testing.T) {
t.Run("InsertTar_DownloadTar", func(t *testing.T) { t.Run("InsertTar_DownloadTar", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
// given // given
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel() defer cancel()
@@ -139,9 +128,6 @@ func TestDownload(t *testing.T) {
t.Run("InsertZip_DownloadTar", func(t *testing.T) { t.Run("InsertZip_DownloadTar", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
// given // given
zipContent := archivetest.TestZipFileBytes() zipContent := archivetest.TestZipFileBytes()
@@ -164,9 +150,6 @@ func TestDownload(t *testing.T) {
t.Run("InsertTar_DownloadZip", func(t *testing.T) { t.Run("InsertTar_DownloadZip", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
// given // given
tarball := archivetest.TestTarFileBytes() tarball := archivetest.TestTarFileBytes()
+5 -5
View File
@@ -14,9 +14,13 @@ import (
func TestInitScript(t *testing.T) { func TestInitScript(t *testing.T) {
t.Parallel() t.Parallel()
// Single instance shared across all sub-tests. All operations
// are read-only (fetching init scripts) so parallel execution
// is safe.
client := coderdtest.New(t, nil)
t.Run("OK Windows amd64", func(t *testing.T) { t.Run("OK Windows amd64", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
script, err := client.InitScript(context.Background(), "windows", "amd64") script, err := client.InitScript(context.Background(), "windows", "amd64")
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, script) require.NotEmpty(t, script)
@@ -26,7 +30,6 @@ func TestInitScript(t *testing.T) {
t.Run("OK Windows arm64", func(t *testing.T) { t.Run("OK Windows arm64", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
script, err := client.InitScript(context.Background(), "windows", "arm64") script, err := client.InitScript(context.Background(), "windows", "arm64")
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, script) require.NotEmpty(t, script)
@@ -36,7 +39,6 @@ func TestInitScript(t *testing.T) {
t.Run("OK Linux amd64", func(t *testing.T) { t.Run("OK Linux amd64", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
script, err := client.InitScript(context.Background(), "linux", "amd64") script, err := client.InitScript(context.Background(), "linux", "amd64")
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, script) require.NotEmpty(t, script)
@@ -46,7 +48,6 @@ func TestInitScript(t *testing.T) {
t.Run("OK Linux arm64", func(t *testing.T) { t.Run("OK Linux arm64", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
script, err := client.InitScript(context.Background(), "linux", "arm64") script, err := client.InitScript(context.Background(), "linux", "arm64")
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, script) require.NotEmpty(t, script)
@@ -56,7 +57,6 @@ func TestInitScript(t *testing.T) {
t.Run("BadRequest", func(t *testing.T) { t.Run("BadRequest", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
_, err := client.InitScript(context.Background(), "darwin", "armv7") _, err := client.InitScript(context.Background(), "darwin", "armv7")
require.Error(t, err) require.Error(t, err)
var apiErr *codersdk.Error var apiErr *codersdk.Error
+16 -27
View File
@@ -1272,10 +1272,14 @@ func TestTemplateVersionsByTemplate(t *testing.T) {
func TestTemplateVersionByName(t *testing.T) { func TestTemplateVersionByName(t *testing.T) {
t.Parallel() t.Parallel()
t.Run("NotFound", func(t *testing.T) {
t.Parallel() // Single instance shared across all sub-tests. Each sub-test
// creates its own template version and template with unique
// IDs so parallel execution is safe.
client := coderdtest.New(t, nil) client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client) user := coderdtest.CreateFirstUser(t, client)
t.Run("NotFound", func(t *testing.T) {
t.Parallel()
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
@@ -1290,8 +1294,6 @@ func TestTemplateVersionByName(t *testing.T) {
t.Run("Found", func(t *testing.T) { t.Run("Found", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
@@ -1935,10 +1937,12 @@ func TestPaginatedTemplateVersions(t *testing.T) {
func TestTemplateVersionByOrganizationTemplateAndName(t *testing.T) { func TestTemplateVersionByOrganizationTemplateAndName(t *testing.T) {
t.Parallel() t.Parallel()
t.Run("NotFound", func(t *testing.T) {
t.Parallel() // Shared instance — see TestTemplateVersionByName for rationale.
client := coderdtest.New(t, nil) client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client) user := coderdtest.CreateFirstUser(t, client)
t.Run("NotFound", func(t *testing.T) {
t.Parallel()
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
@@ -1953,8 +1957,6 @@ func TestTemplateVersionByOrganizationTemplateAndName(t *testing.T) {
t.Run("Found", func(t *testing.T) { t.Run("Found", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
@@ -2204,10 +2206,14 @@ func TestTemplateVersionVariables(t *testing.T) {
func TestTemplateVersionPatch(t *testing.T) { func TestTemplateVersionPatch(t *testing.T) {
t.Parallel() t.Parallel()
t.Run("Update the name", func(t *testing.T) {
t.Parallel() // Single instance shared across all 9 sub-tests. Each sub-test
// creates its own template version(s) and template(s) with
// unique IDs so parallel execution is safe.
client := coderdtest.New(t, nil) client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client) user := coderdtest.CreateFirstUser(t, client)
t.Run("Update the name", func(t *testing.T) {
t.Parallel()
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
@@ -2226,8 +2232,6 @@ func TestTemplateVersionPatch(t *testing.T) {
t.Run("Update the message", func(t *testing.T) { t.Run("Update the message", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) { version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) {
req.Message = "Example message" req.Message = "Example message"
}) })
@@ -2247,8 +2251,6 @@ func TestTemplateVersionPatch(t *testing.T) {
t.Run("Remove the message", func(t *testing.T) { t.Run("Remove the message", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) { version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) {
req.Message = "Example message" req.Message = "Example message"
}) })
@@ -2268,8 +2270,6 @@ func TestTemplateVersionPatch(t *testing.T) {
t.Run("Keep the message", func(t *testing.T) { t.Run("Keep the message", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
wantMessage := "Example message" wantMessage := "Example message"
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) { version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) {
req.Message = wantMessage req.Message = wantMessage
@@ -2291,8 +2291,6 @@ func TestTemplateVersionPatch(t *testing.T) {
t.Run("Use the same name if a new name is not passed", func(t *testing.T) { t.Run("Use the same name if a new name is not passed", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
@@ -2306,9 +2304,6 @@ func TestTemplateVersionPatch(t *testing.T) {
t.Run("Use the same name for two different templates", func(t *testing.T) { t.Run("Use the same name for two different templates", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.CreateTemplate(t, client, user.OrganizationID, version1.ID) coderdtest.CreateTemplate(t, client, user.OrganizationID, version1.ID)
version2 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version2 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
@@ -2334,8 +2329,6 @@ func TestTemplateVersionPatch(t *testing.T) {
t.Run("Use the same name for two versions for the same templates", func(t *testing.T) { t.Run("Use the same name for two versions for the same templates", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(ctvr *codersdk.CreateTemplateVersionRequest) { version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(ctvr *codersdk.CreateTemplateVersionRequest) {
ctvr.Name = "v1" ctvr.Name = "v1"
}) })
@@ -2356,8 +2349,6 @@ func TestTemplateVersionPatch(t *testing.T) {
t.Run("Rename the unassigned template", func(t *testing.T) { t.Run("Rename the unassigned template", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
@@ -2373,8 +2364,6 @@ func TestTemplateVersionPatch(t *testing.T) {
t.Run("Use incorrect template version name", func(t *testing.T) { t.Run("Use incorrect template version name", func(t *testing.T) {
t.Parallel() t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)