mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
chore: reduce execution time of TestProvisionerJobs (#19475)
Note: this commit was partially authored by AI. - Replaces coderdtest.CreateTemplate/TemplateVersion() with direct dbgen calls. We do not need a fully functional template for these tests. - Removes provisioner daemon creation/cleanup. We do not need a running provisioner daemon here; this functionality is tested elsewhere. - Simplifies provisioner job creation test helpers. This reduces the test runtime by over 50%: Old: ``` time go test -count=100 ./cli -test.run=TestProvisionerJobs ok github.com/coder/coder/v2/cli 50.149s ``` New: ``` time go test -count=100 ./cli -test.run=TestProvisionerJobs ok github.com/coder/coder/v2/cli 21.898 ```
This commit is contained in:
+52
-59
@@ -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"
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user