diff --git a/coderd/files_test.go b/coderd/files_test.go index b7f981d5e5..e1a87aad29 100644 --- a/coderd/files_test.go +++ b/coderd/files_test.go @@ -21,11 +21,14 @@ import ( func TestPostFiles(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) + _ = coderdtest.CreateFirstUser(t, client) t.Run("BadContentType", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) defer cancel() @@ -35,9 +38,6 @@ func TestPostFiles(t *testing.T) { t.Run("Insert", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) defer cancel() @@ -47,9 +47,6 @@ func TestPostFiles(t *testing.T) { t.Run("InsertWindowsZip", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) defer cancel() @@ -59,9 +56,6 @@ func TestPostFiles(t *testing.T) { t.Run("InsertAlreadyExists", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) defer cancel() @@ -73,9 +67,6 @@ func TestPostFiles(t *testing.T) { }) t.Run("InsertConcurrent", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) defer cancel() @@ -99,11 +90,12 @@ func TestPostFiles(t *testing.T) { func TestDownload(t *testing.T) { t.Parallel() + + // Shared instance — see TestPostFiles for rationale. + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) t.Run("NotFound", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) defer cancel() @@ -115,9 +107,6 @@ func TestDownload(t *testing.T) { t.Run("InsertTar_DownloadTar", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - // given ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) defer cancel() @@ -139,9 +128,6 @@ func TestDownload(t *testing.T) { t.Run("InsertZip_DownloadTar", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - // given zipContent := archivetest.TestZipFileBytes() @@ -164,9 +150,6 @@ func TestDownload(t *testing.T) { t.Run("InsertTar_DownloadZip", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - // given tarball := archivetest.TestTarFileBytes() diff --git a/coderd/initscript_test.go b/coderd/initscript_test.go index bad0577f02..0fa125aa1d 100644 --- a/coderd/initscript_test.go +++ b/coderd/initscript_test.go @@ -14,9 +14,13 @@ import ( func TestInitScript(t *testing.T) { 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.Parallel() - client := coderdtest.New(t, nil) script, err := client.InitScript(context.Background(), "windows", "amd64") require.NoError(t, err) require.NotEmpty(t, script) @@ -26,7 +30,6 @@ func TestInitScript(t *testing.T) { t.Run("OK Windows arm64", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) script, err := client.InitScript(context.Background(), "windows", "arm64") require.NoError(t, err) require.NotEmpty(t, script) @@ -36,7 +39,6 @@ func TestInitScript(t *testing.T) { t.Run("OK Linux amd64", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) script, err := client.InitScript(context.Background(), "linux", "amd64") require.NoError(t, err) require.NotEmpty(t, script) @@ -46,7 +48,6 @@ func TestInitScript(t *testing.T) { t.Run("OK Linux arm64", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) script, err := client.InitScript(context.Background(), "linux", "arm64") require.NoError(t, err) require.NotEmpty(t, script) @@ -56,7 +57,6 @@ func TestInitScript(t *testing.T) { t.Run("BadRequest", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) _, err := client.InitScript(context.Background(), "darwin", "armv7") require.Error(t, err) var apiErr *codersdk.Error diff --git a/coderd/templateversions_test.go b/coderd/templateversions_test.go index 99c32c0d5c..4520d37a10 100644 --- a/coderd/templateversions_test.go +++ b/coderd/templateversions_test.go @@ -1272,10 +1272,14 @@ func TestTemplateVersionsByTemplate(t *testing.T) { func TestTemplateVersionByName(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) + user := coderdtest.CreateFirstUser(t, client) t.Run("NotFound", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) 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.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) @@ -1935,10 +1937,12 @@ func TestPaginatedTemplateVersions(t *testing.T) { func TestTemplateVersionByOrganizationTemplateAndName(t *testing.T) { t.Parallel() + + // Shared instance — see TestTemplateVersionByName for rationale. + client := coderdtest.New(t, nil) + user := coderdtest.CreateFirstUser(t, client) t.Run("NotFound", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) 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.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) @@ -2204,10 +2206,14 @@ func TestTemplateVersionVariables(t *testing.T) { func TestTemplateVersionPatch(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) + user := coderdtest.CreateFirstUser(t, client) t.Run("Update the name", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) 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.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) { req.Message = "Example message" }) @@ -2247,8 +2251,6 @@ func TestTemplateVersionPatch(t *testing.T) { t.Run("Remove the message", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) { req.Message = "Example message" }) @@ -2268,8 +2270,6 @@ func TestTemplateVersionPatch(t *testing.T) { t.Run("Keep the message", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) wantMessage := "Example message" version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(req *codersdk.CreateTemplateVersionRequest) { 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.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) 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.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) - version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) coderdtest.CreateTemplate(t, client, user.OrganizationID, version1.ID) 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.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil, func(ctvr *codersdk.CreateTemplateVersionRequest) { ctvr.Name = "v1" }) @@ -2356,8 +2349,6 @@ func TestTemplateVersionPatch(t *testing.T) { t.Run("Rename the unassigned template", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) 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.Parallel() - client := coderdtest.New(t, nil) - user := coderdtest.CreateFirstUser(t, client) version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)