Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
111 changes: 52 additions & 59 deletions cli/provisionerjobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"
"time"

"github.com/aws/smithy-go/ptr"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisionersdk"
"github.com/coder/coder/v2/testutil"
)

Expand All @@ -36,67 +36,43 @@ func TestProvisionerJobs(t *testing.T) {
templateAdminClient, templateAdmin := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.ScopedRoleOrgTemplateAdmin(owner.OrganizationID))
memberClient, member := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)

// Create initial resources with a running provisioner.
firstProvisioner := coderdtest.NewTaggedProvisionerDaemon(t, coderdAPI, "default-provisioner", map[string]string{"owner": "", "scope": "organization"})
t.Cleanup(func() { _ = firstProvisioner.Close() })
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, completeWithAgent())
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID, func(req *codersdk.CreateTemplateRequest) {
req.AllowUserCancelWorkspaceJobs = ptr.Bool(true)
// These CLI tests are related to provisioner job CRUD operations and as such
// do not require the overhead of starting a provisioner. Other provisioner job
// functionalities (acquisition etc.) are tested elsewhere.
template := dbgen.Template(t, db, database.Template{
OrganizationID: owner.OrganizationID,
CreatedBy: owner.UserID,
AllowUserCancelWorkspaceJobs: true,
})
version := dbgen.TemplateVersion(t, db, database.TemplateVersion{
OrganizationID: owner.OrganizationID,
CreatedBy: owner.UserID,
TemplateID: uuid.NullUUID{UUID: template.ID, Valid: true},
})

// Stop the provisioner so it doesn't grab any more jobs.
firstProvisioner.Close()

t.Run("Cancel", func(t *testing.T) {
t.Parallel()

// Set up test helpers.
type jobInput struct {
WorkspaceBuildID string `json:"workspace_build_id,omitempty"`
TemplateVersionID string `json:"template_version_id,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
}
prepareJob := func(t *testing.T, input jobInput) database.ProvisionerJob {
// Test helper to create a provisioner job of a given type with a given input.
prepareJob := func(t *testing.T, jobType database.ProvisionerJobType, input json.RawMessage) database.ProvisionerJob {
t.Helper()

inputBytes, err := json.Marshal(input)
require.NoError(t, err)

var typ database.ProvisionerJobType
switch {
case input.WorkspaceBuildID != "":
typ = database.ProvisionerJobTypeWorkspaceBuild
case input.TemplateVersionID != "":
if input.DryRun {
typ = database.ProvisionerJobTypeTemplateVersionDryRun
} else {
typ = database.ProvisionerJobTypeTemplateVersionImport
}
default:
t.Fatal("invalid input")
}

var (
tags = database.StringMap{"owner": "", "scope": "organization", "foo": uuid.New().String()}
_ = dbgen.ProvisionerDaemon(t, db, database.ProvisionerDaemon{Tags: tags})
job = dbgen.ProvisionerJob(t, db, coderdAPI.Pubsub, database.ProvisionerJob{
InitiatorID: member.ID,
Input: json.RawMessage(inputBytes),
Type: typ,
Tags: tags,
StartedAt: sql.NullTime{Time: coderdAPI.Clock.Now().Add(-time.Minute), Valid: true},
})
)
return job
return dbgen.ProvisionerJob(t, db, coderdAPI.Pubsub, database.ProvisionerJob{
InitiatorID: member.ID,
Input: input,
Type: jobType,
StartedAt: sql.NullTime{Time: coderdAPI.Clock.Now().Add(-time.Minute), Valid: true},
Tags: database.StringMap{provisionersdk.TagOwner: "", provisionersdk.TagScope: provisionersdk.ScopeOrganization, "foo": uuid.NewString()},
})
}

// Test helper to create a workspace build job with a predefined input.
prepareWorkspaceBuildJob := func(t *testing.T) database.ProvisionerJob {
t.Helper()
var (
wbID = uuid.New()
job = prepareJob(t, jobInput{WorkspaceBuildID: wbID.String()})
w = dbgen.Workspace(t, db, database.WorkspaceTable{
wbID = uuid.New()
input, _ = json.Marshal(map[string]string{"workspace_build_id": wbID.String()})
job = prepareJob(t, database.ProvisionerJobTypeWorkspaceBuild, input)
w = dbgen.Workspace(t, db, database.WorkspaceTable{
OrganizationID: owner.OrganizationID,
OwnerID: member.ID,
TemplateID: template.ID,
Expand All @@ -112,12 +88,14 @@ func TestProvisionerJobs(t *testing.T) {
return job
}

prepareTemplateVersionImportJobBuilder := func(t *testing.T, dryRun bool) database.ProvisionerJob {
// Test helper to create a template version import job with a predefined input.
prepareTemplateVersionImportJob := func(t *testing.T) database.ProvisionerJob {
t.Helper()
var (
tvID = uuid.New()
job = prepareJob(t, jobInput{TemplateVersionID: tvID.String(), DryRun: dryRun})
_ = dbgen.TemplateVersion(t, db, database.TemplateVersion{
tvID = uuid.New()
input, _ = json.Marshal(map[string]string{"template_version_id": tvID.String()})
job = prepareJob(t, database.ProvisionerJobTypeTemplateVersionImport, input)
_ = dbgen.TemplateVersion(t, db, database.TemplateVersion{
OrganizationID: owner.OrganizationID,
CreatedBy: templateAdmin.ID,
ID: tvID,
Expand All @@ -127,11 +105,26 @@ func TestProvisionerJobs(t *testing.T) {
)
return job
}
prepareTemplateVersionImportJob := func(t *testing.T) database.ProvisionerJob {
return prepareTemplateVersionImportJobBuilder(t, false)
}

// Test helper to create a template version import dry run job with a predefined input.
prepareTemplateVersionImportJobDryRun := func(t *testing.T) database.ProvisionerJob {
return prepareTemplateVersionImportJobBuilder(t, true)
t.Helper()
var (
tvID = uuid.New()
input, _ = json.Marshal(map[string]interface{}{
"template_version_id": tvID.String(),
"dry_run": true,
})
job = prepareJob(t, database.ProvisionerJobTypeTemplateVersionDryRun, input)
_ = dbgen.TemplateVersion(t, db, database.TemplateVersion{
OrganizationID: owner.OrganizationID,
CreatedBy: templateAdmin.ID,
ID: tvID,
TemplateID: uuid.NullUUID{UUID: template.ID, Valid: true},
JobID: job.ID,
})
)
return job
}

// Run the cancellation test suite.
Expand Down
Loading
Loading