- Notifications
You must be signed in to change notification settings - Fork924
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
9d3ea6f
d46f156
dba9fce
f312292
2eea932
459ce5c
92a4b99
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -4464,7 +4464,8 @@ func (q *FakeQuerier) GetProvisionerDaemons(_ context.Context) ([]database.Provi | ||
defer q.mutex.RUnlock() | ||
if len(q.provisionerDaemons) == 0 { | ||
// Returning err=nil here for consistency with real querier | ||
return []database.ProvisionerDaemon{}, nil | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
// copy the data so that the caller can't manipulate any data inside dbmem | ||
// after returning | ||
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() | ||
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 | ||
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()) | ||
}) | ||
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()) | ||
}) | ||
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()) | ||
}) | ||
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, | ||
})) | ||
}) | ||
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, | ||
})) | ||
}) | ||
}) | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.