Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

test(enterprise/coderd): fix ctx init in multiple workspace tests#16176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mafredri merged 1 commit intomainfrommafredri/fix-workspace-test
Jan 17, 2025
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 38 additions & 26 deletionsenterprise/coderd/workspaces_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -73,8 +73,7 @@ func TestCreateWorkspace(t *testing.T) {

other, _ := coderdtest.CreateAnotherUser(t, client, first.OrganizationID, rbac.RoleMember(), rbac.RoleOwner())

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
ctx := testutil.Context(t, testutil.WaitLong)

org, err := other.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "another",
Expand All@@ -83,6 +82,8 @@ func TestCreateWorkspace(t *testing.T) {
version := coderdtest.CreateTemplateVersion(t, other, org.ID, nil)
template := coderdtest.CreateTemplate(t, other, org.ID, version.ID)

ctx = testutil.Context(t, testutil.WaitLong) // Reset the context to avoid timeouts.

_, err = client.CreateWorkspace(ctx, first.OrganizationID, codersdk.Me, codersdk.CreateWorkspaceRequest{
TemplateID: template.ID,
Name: "workspace",
Expand All@@ -108,8 +109,7 @@ func TestCreateWorkspace(t *testing.T) {
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
ctx := testutil.Context(t, testutil.WaitLong)

acl, err := templateAdminClient.TemplateACL(ctx, template.ID)
require.NoError(t, err)
Expand DownExpand Up@@ -163,8 +163,7 @@ func TestCreateWorkspace(t *testing.T) {
coderdtest.AwaitTemplateVersionJobCompleted(t, templateAdmin, version.ID)
template := coderdtest.CreateTemplate(t, templateAdmin, owner.OrganizationID, version.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
ctx := testutil.Context(t, testutil.WaitLong)

// Remove everyone access
err := templateAdmin.UpdateTemplateACL(ctx, template.ID, codersdk.UpdateTemplateACL{
Expand DownExpand Up@@ -213,8 +212,7 @@ func TestCreateUserWorkspace(t *testing.T) {

other, _ := coderdtest.CreateAnotherUser(t, client, first.OrganizationID, rbac.RoleMember(), rbac.RoleOwner())

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
ctx := testutil.Context(t, testutil.WaitLong)

org, err := other.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "another",
Expand All@@ -223,6 +221,8 @@ func TestCreateUserWorkspace(t *testing.T) {
version := coderdtest.CreateTemplateVersion(t, other, org.ID, nil)
template := coderdtest.CreateTemplate(t, other, org.ID, version.ID)

ctx = testutil.Context(t, testutil.WaitLong) // Reset the context to avoid timeouts.

_, err = client.CreateUserWorkspace(ctx, codersdk.Me, codersdk.CreateWorkspaceRequest{
TemplateID: template.ID,
Name: "workspace",
Expand All@@ -248,8 +248,7 @@ func TestCreateUserWorkspace(t *testing.T) {
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
ctx := testutil.Context(t, testutil.WaitLong)

acl, err := templateAdminClient.TemplateACL(ctx, template.ID)
require.NoError(t, err)
Expand DownExpand Up@@ -431,7 +430,6 @@ func TestWorkspaceAutobuild(t *testing.T) {
t.Parallel()

var (
ctx = testutil.Context(t, testutil.WaitMedium)
ticker = make(chan time.Time)
statCh = make(chan autobuild.Stats)
inactiveTTL = time.Minute
Expand DownExpand Up@@ -492,6 +490,8 @@ func TestWorkspaceAutobuild(t *testing.T) {
require.Equal(t, database.AuditActionWrite, alog.Action)
require.Equal(t, workspace.Name, alog.ResourceTarget)

ctx := testutil.Context(t, testutil.WaitMedium)

dormantLastUsedAt := ws.LastUsedAt
// nolint:gocritic // this test is not testing RBAC.
err := client.UpdateWorkspaceDormancy(ctx, ws.ID, codersdk.UpdateWorkspaceDormancy{Dormant: false})
Expand DownExpand Up@@ -861,7 +861,6 @@ func TestWorkspaceAutobuild(t *testing.T) {
t.Parallel()

var (
ctx = testutil.Context(t, testutil.WaitMedium)
tickCh = make(chan time.Time)
statsCh = make(chan autobuild.Stats)
inactiveTTL = time.Minute
Expand DownExpand Up@@ -909,6 +908,8 @@ func TestWorkspaceAutobuild(t *testing.T) {
ws = coderdtest.MustWorkspace(t, client, ws.ID)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, ws.LatestBuild.ID)

ctx := testutil.Context(t, testutil.WaitMedium)

// Now that we've validated that the workspace is eligible for autostart
// lets cause it to become dormant.
_, err = client.UpdateTemplateMeta(ctx, template.ID, codersdk.UpdateTemplateMeta{
Expand DownExpand Up@@ -944,7 +945,6 @@ func TestWorkspaceAutobuild(t *testing.T) {
ticker = make(chan time.Time)
statCh = make(chan autobuild.Stats)
transitionTTL = time.Minute
ctx = testutil.Context(t, testutil.WaitMedium)
)

logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
Expand DownExpand Up@@ -984,6 +984,8 @@ func TestWorkspaceAutobuild(t *testing.T) {
})
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)

ctx := testutil.Context(t, testutil.WaitMedium)

// Try to delete the workspace. This simulates a "failed" autodelete.
build, err := templateAdmin.CreateWorkspaceBuild(ctx, ws.ID, codersdk.CreateWorkspaceBuildRequest{
Transition: codersdk.WorkspaceTransitionDelete,
Expand All@@ -994,6 +996,8 @@ func TestWorkspaceAutobuild(t *testing.T) {
build = coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, build.ID)
require.NotEmpty(t, build.Job.Error)

ctx = testutil.Context(t, testutil.WaitLong) // Reset the context to avoid timeouts.

// Update our workspace to be dormant so that it qualifies for auto-deletion.
err = templateAdmin.UpdateWorkspaceDormancy(ctx, ws.ID, codersdk.UpdateWorkspaceDormancy{
Dormant: true,
Expand DownExpand Up@@ -1030,7 +1034,6 @@ func TestWorkspaceAutobuild(t *testing.T) {
var (
tickCh = make(chan time.Time)
statsCh = make(chan autobuild.Stats)
ctx = testutil.Context(t, testutil.WaitMedium)
)

logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
Expand DownExpand Up@@ -1070,6 +1073,8 @@ func TestWorkspaceAutobuild(t *testing.T) {
})
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version2.ID)

ctx := testutil.Context(t, testutil.WaitMedium)

// Make sure to promote it.
err = client.UpdateActiveTemplateVersion(ctx, template.ID, codersdk.UpdateActiveTemplateVersion{
ID: version2.ID,
Expand All@@ -1089,6 +1094,8 @@ func TestWorkspaceAutobuild(t *testing.T) {
firstBuild := coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, started.LatestBuild.ID)
require.Equal(t, version1.ID, firstBuild.TemplateVersionID)

ctx = testutil.Context(t, testutil.WaitMedium) // Reset the context after workspace operations.

// Update the template to require the promoted version.
_, err = client.UpdateTemplateMeta(ctx, template.ID, codersdk.UpdateTemplateMeta{
RequireActiveVersion: true,
Expand DownExpand Up@@ -1538,9 +1545,6 @@ func TestWorkspaceTagsTerraform(t *testing.T) {
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
// This can take a while, so set a relatively long timeout.
ctx := testutil.Context(t, 2*testutil.WaitSuperLong)

client, owner := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
// We intentionally do not run a built-in provisioner daemon here.
Expand All@@ -1557,6 +1561,9 @@ func TestWorkspaceTagsTerraform(t *testing.T) {

_ = coderdenttest.NewExternalProvisionerDaemonTerraform(t, client, owner.OrganizationID, tc.provisionerTags)

// This can take a while, so set a relatively long timeout.
ctx := testutil.Context(t, 2*testutil.WaitSuperLong)

// Creating a template as a template admin must succeed
templateFiles := map[string]string{"main.tf": fmt.Sprintf(mainTfTemplate, tc.tfWorkspaceTags)}
tarBytes := testutil.CreateTar(t, templateFiles)
Expand DownExpand Up@@ -1595,13 +1602,11 @@ func downloadProviders(t *testing.T, providersTf string) string {
t.Helper()
// We firstly write a Terraform CLI config file to a temporary directory:
var (
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitLong)
tempDir = t.TempDir()
cacheDir = filepath.Join(tempDir, ".cache")
providersTfPath = filepath.Join(tempDir, "providers.tf")
cliConfigPath = filepath.Join(tempDir, "local.tfrc")
)
defer cancel()

// Write files to disk
require.NoError(t, os.MkdirAll(cacheDir, os.ModePerm|os.ModeDir))
Expand All@@ -1619,6 +1624,8 @@ func downloadProviders(t *testing.T, providersTf string) string {
err := os.WriteFile(cliConfigPath, []byte(fmt.Sprintf(cliConfigTemplate, cacheDir)), os.ModePerm) // nolint:gosec
require.NoError(t, err, "failed to write %s", cliConfigPath)

ctx := testutil.Context(t, testutil.WaitLong)

// Run terraform providers mirror to mirror required providers to cacheDir
cmd := exec.CommandContext(ctx, "terraform", "providers", "mirror", cacheDir)
cmd.Env = os.Environ() // without this terraform may complain about path
Expand DownExpand Up@@ -1702,7 +1709,6 @@ func TestWorkspacesFiltering(t *testing.T) {
t.Run("Dormant", func(t *testing.T) {
t.Parallel()

ctx := testutil.Context(t, testutil.WaitMedium)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
client, db, owner := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
Options: &coderdtest.Options{
Expand DownExpand Up@@ -1736,6 +1742,8 @@ func TestWorkspacesFiltering(t *testing.T) {
TemplateID: resp.Template.ID,
}).Do().Workspace

ctx := testutil.Context(t, testutil.WaitMedium)

err := templateAdminClient.UpdateWorkspaceDormancy(ctx, dormantWS1.ID, codersdk.UpdateWorkspaceDormancy{Dormant: true})
require.NoError(t, err)

Expand DownExpand Up@@ -2046,16 +2054,16 @@ func TestWorkspaceByOwnerAndName(t *testing.T) {
t.Run("No Matching Provisioner", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

client, db, userResponse := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureExternalProvisionerDaemons: 1,
},
},
})

ctx := testutil.Context(t, testutil.WaitLong)

userSubject, _, err := httpmw.UserRBACSubject(ctx, db, userResponse.UserID, rbac.ExpandableScope(rbac.ScopeAll))
require.NoError(t, err)
user, err := client.User(ctx, userSubject.ID)
Expand All@@ -2070,6 +2078,8 @@ func TestWorkspaceByOwnerAndName(t *testing.T) {
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, userResponse.OrganizationID, version.ID)

ctx = testutil.Context(t, testutil.WaitLong) // Reset the context to avoid timeouts.

// nolint:gocritic // unit testing
daemons, err := db.GetProvisionerDaemons(dbauthz.AsSystemRestricted(ctx))
require.NoError(t, err)
Expand DownExpand Up@@ -2121,16 +2131,16 @@ func TestWorkspaceByOwnerAndName(t *testing.T) {
t.Run("Unavailable Provisioner", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

client, db, userResponse := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureExternalProvisionerDaemons: 1,
},
},
})

ctx := testutil.Context(t, testutil.WaitLong)

userSubject, _, err := httpmw.UserRBACSubject(ctx, db, userResponse.UserID, rbac.ExpandableScope(rbac.ScopeAll))
require.NoError(t, err)
user, err := client.User(ctx, userSubject.ID)
Expand All@@ -2145,6 +2155,8 @@ func TestWorkspaceByOwnerAndName(t *testing.T) {
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, userResponse.OrganizationID, version.ID)

ctx = testutil.Context(t, testutil.WaitLong) // Reset the context to avoid timeouts.

// nolint:gocritic // unit testing
daemons, err := db.GetProvisionerDaemons(dbauthz.AsSystemRestricted(ctx))
require.NoError(t, err)
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp