-
Notifications
You must be signed in to change notification settings - Fork 920
fix: wsbuilder: complete job and mark workspace deleted if no provisioners available #18465
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johnstcn
wants to merge
7
commits into
main
Choose a base branch
from
cj/delete-orphan-fake-complete-job
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+494
−86
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9d3ea6f
fix: wsbuilder: complete job and mark workspace deleted if no provisi…
johnstcn d46f156
check daemon last seen at
johnstcn dba9fce
allow orphaning if last build was cancelled
johnstcn f312292
re-fetch provisioner job before returning
johnstcn 2eea932
ensure that audit logs are created
johnstcn 459ce5c
write audit log
johnstcn 92a4b99
nits
johnstcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package coderd_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"database/sql" | ||
"errors" | ||
|
@@ -25,6 +26,7 @@ import ( | |
"github.com/coder/coder/v2/coderd/coderdtest/oidctest" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||
"github.com/coder/coder/v2/coderd/database/dbfake" | ||
"github.com/coder/coder/v2/coderd/database/dbgen" | ||
"github.com/coder/coder/v2/coderd/database/dbtestutil" | ||
"github.com/coder/coder/v2/coderd/database/dbtime" | ||
|
@@ -371,42 +373,174 @@ func TestWorkspaceBuildsProvisionerState(t *testing.T) { | |
|
||
t.Run("Orphan", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) | ||
first := coderdtest.CreateFirstUser(t, client) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
Comment on lines
-374
to
-378
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. review: refactored to use dbfake instead |
||
|
||
version := coderdtest.CreateTemplateVersion(t, client, first.OrganizationID, nil) | ||
template := coderdtest.CreateTemplate(t, client, first.OrganizationID, version.ID) | ||
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) | ||
t.Run("WithoutDelete", func(t *testing.T) { | ||
t.Parallel() | ||
client, store := coderdtest.NewWithDatabase(t, nil) | ||
first := coderdtest.CreateFirstUser(t, client) | ||
templateAdmin, templateAdminUser := coderdtest.CreateAnotherUser(t, client, first.OrganizationID, rbac.RoleTemplateAdmin()) | ||
|
||
r := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{ | ||
OwnerID: templateAdminUser.ID, | ||
OrganizationID: first.OrganizationID, | ||
}).Do() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
|
||
// Trying to orphan without delete transition fails. | ||
_, err := templateAdmin.CreateWorkspaceBuild(ctx, r.Workspace.ID, codersdk.CreateWorkspaceBuildRequest{ | ||
TemplateVersionID: r.TemplateVersion.ID, | ||
Transition: codersdk.WorkspaceTransitionStart, | ||
Orphan: true, | ||
}) | ||
require.Error(t, err, "Orphan is only permitted when deleting a workspace.") | ||
cerr := coderdtest.SDKError(t, err) | ||
require.Equal(t, http.StatusBadRequest, cerr.StatusCode()) | ||
}) | ||
|
||
workspace := coderdtest.CreateWorkspace(t, client, template.ID) | ||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID) | ||
t.Run("WithState", func(t *testing.T) { | ||
t.Parallel() | ||
client, store := coderdtest.NewWithDatabase(t, nil) | ||
first := coderdtest.CreateFirstUser(t, client) | ||
templateAdmin, templateAdminUser := coderdtest.CreateAnotherUser(t, client, first.OrganizationID, rbac.RoleTemplateAdmin()) | ||
|
||
r := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{ | ||
OwnerID: templateAdminUser.ID, | ||
OrganizationID: first.OrganizationID, | ||
}).Do() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
|
||
// Providing both state and orphan fails. | ||
_, err := templateAdmin.CreateWorkspaceBuild(ctx, r.Workspace.ID, codersdk.CreateWorkspaceBuildRequest{ | ||
TemplateVersionID: r.TemplateVersion.ID, | ||
Transition: codersdk.WorkspaceTransitionDelete, | ||
ProvisionerState: []byte(" "), | ||
Orphan: true, | ||
}) | ||
require.Error(t, err) | ||
cerr := coderdtest.SDKError(t, err) | ||
require.Equal(t, http.StatusBadRequest, cerr.StatusCode()) | ||
}) | ||
|
||
// Providing both state and orphan fails. | ||
_, err := client.CreateWorkspaceBuild(ctx, workspace.ID, codersdk.CreateWorkspaceBuildRequest{ | ||
TemplateVersionID: workspace.LatestBuild.TemplateVersionID, | ||
Transition: codersdk.WorkspaceTransitionDelete, | ||
ProvisionerState: []byte(" "), | ||
Orphan: true, | ||
t.Run("NoPermission", func(t *testing.T) { | ||
t.Parallel() | ||
client, store := coderdtest.NewWithDatabase(t, nil) | ||
first := coderdtest.CreateFirstUser(t, client) | ||
member, memberUser := coderdtest.CreateAnotherUser(t, client, first.OrganizationID) | ||
|
||
r := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{ | ||
OwnerID: memberUser.ID, | ||
OrganizationID: first.OrganizationID, | ||
}).Do() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
|
||
// Trying to orphan without being a template admin fails. | ||
_, err := member.CreateWorkspaceBuild(ctx, r.Workspace.ID, codersdk.CreateWorkspaceBuildRequest{ | ||
TemplateVersionID: r.TemplateVersion.ID, | ||
Transition: codersdk.WorkspaceTransitionDelete, | ||
Orphan: true, | ||
}) | ||
require.Error(t, err) | ||
cerr := coderdtest.SDKError(t, err) | ||
require.Equal(t, http.StatusForbidden, cerr.StatusCode()) | ||
}) | ||
require.Error(t, err) | ||
cerr := coderdtest.SDKError(t, err) | ||
require.Equal(t, http.StatusBadRequest, cerr.StatusCode()) | ||
|
||
// Regular orphan operation succeeds. | ||
build, err := client.CreateWorkspaceBuild(ctx, workspace.ID, codersdk.CreateWorkspaceBuildRequest{ | ||
TemplateVersionID: workspace.LatestBuild.TemplateVersionID, | ||
Transition: codersdk.WorkspaceTransitionDelete, | ||
Orphan: true, | ||
t.Run("OK", func(t *testing.T) { | ||
// Include a provisioner so that we can test that provisionerdserver | ||
// performs deletion. | ||
auditor := audit.NewMock() | ||
client, store := coderdtest.NewWithDatabase(t, &coderdtest.Options{IncludeProvisionerDaemon: true, Auditor: auditor}) | ||
first := coderdtest.CreateFirstUser(t, client) | ||
templateAdmin, templateAdminUser := coderdtest.CreateAnotherUser(t, client, first.OrganizationID, rbac.RoleTemplateAdmin()) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
// This is a valid zip file. Without this the job will fail to complete. | ||
// TODO: add this to dbfake by default. | ||
zipBytes := make([]byte, 22) | ||
zipBytes[0] = 80 | ||
zipBytes[1] = 75 | ||
zipBytes[2] = 0o5 | ||
zipBytes[3] = 0o6 | ||
uploadRes, err := client.Upload(ctx, codersdk.ContentTypeZip, bytes.NewReader(zipBytes)) | ||
require.NoError(t, err) | ||
|
||
tv := dbfake.TemplateVersion(t, store). | ||
FileID(uploadRes.ID). | ||
Seed(database.TemplateVersion{ | ||
OrganizationID: first.OrganizationID, | ||
CreatedBy: templateAdminUser.ID, | ||
}). | ||
Do() | ||
|
||
r := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{ | ||
OwnerID: templateAdminUser.ID, | ||
OrganizationID: first.OrganizationID, | ||
TemplateID: tv.Template.ID, | ||
}).Do() | ||
|
||
auditor.ResetLogs() | ||
// Regular orphan operation succeeds. | ||
build, err := templateAdmin.CreateWorkspaceBuild(ctx, r.Workspace.ID, codersdk.CreateWorkspaceBuildRequest{ | ||
TemplateVersionID: r.TemplateVersion.ID, | ||
Transition: codersdk.WorkspaceTransitionDelete, | ||
Orphan: true, | ||
}) | ||
require.NoError(t, err) | ||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, build.ID) | ||
|
||
// Validate that the deletion was audited. | ||
require.True(t, auditor.Contains(t, database.AuditLog{ | ||
ResourceID: build.ID, | ||
Action: database.AuditActionDelete, | ||
})) | ||
}) | ||
require.NoError(t, err) | ||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, build.ID) | ||
|
||
_, err = client.Workspace(ctx, workspace.ID) | ||
require.Error(t, err) | ||
require.Equal(t, http.StatusGone, coderdtest.SDKError(t, err).StatusCode()) | ||
Comment on lines
-407
to
-409
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. review: moved this check to the relevant CLI test |
||
t.Run("NoProvisioners", func(t *testing.T) { | ||
t.Parallel() | ||
auditor := audit.NewMock() | ||
client, store := coderdtest.NewWithDatabase(t, &coderdtest.Options{Auditor: auditor}) | ||
first := coderdtest.CreateFirstUser(t, client) | ||
templateAdmin, templateAdminUser := coderdtest.CreateAnotherUser(t, client, first.OrganizationID, rbac.RoleTemplateAdmin()) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
r := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{ | ||
OwnerID: templateAdminUser.ID, | ||
OrganizationID: first.OrganizationID, | ||
}).Do() | ||
|
||
// nolint:gocritic // For testing | ||
daemons, err := store.GetProvisionerDaemons(dbauthz.AsSystemReadProvisionerDaemons(ctx)) | ||
require.NoError(t, err) | ||
require.Empty(t, daemons, "Provisioner daemons should be empty for this test") | ||
|
||
// Orphan deletion still succeeds despite no provisioners being available. | ||
build, err := templateAdmin.CreateWorkspaceBuild(ctx, r.Workspace.ID, codersdk.CreateWorkspaceBuildRequest{ | ||
TemplateVersionID: r.TemplateVersion.ID, | ||
Transition: codersdk.WorkspaceTransitionDelete, | ||
Orphan: true, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, codersdk.WorkspaceTransitionDelete, build.Transition) | ||
require.Equal(t, codersdk.ProvisionerJobFailed, build.Job.Status) | ||
require.Contains(t, build.Job.Error, "No provisioners were available to handle the request") | ||
|
||
ws, err := client.Workspace(ctx, r.Workspace.ID) | ||
require.Empty(t, ws) | ||
require.Equal(t, http.StatusGone, coderdtest.SDKError(t, err).StatusCode()) | ||
|
||
// Validate that the deletion was audited. | ||
require.True(t, auditor.Contains(t, database.AuditLog{ | ||
ResourceID: build.ID, | ||
Action: database.AuditActionDelete, | ||
})) | ||
}) | ||
}) | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.