mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: evaluate provisioner tags (#13333)
This commit is contained in:
@@ -60,6 +60,7 @@ func TestBuilder_NoOptions(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(nil),
|
||||
withParameterSchemas(inactiveJobID, nil),
|
||||
withWorkspaceTags(inactiveVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {
|
||||
@@ -112,6 +113,7 @@ func TestBuilder_Initiator(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(nil),
|
||||
withParameterSchemas(inactiveJobID, nil),
|
||||
withWorkspaceTags(inactiveVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {
|
||||
@@ -154,6 +156,7 @@ func TestBuilder_Baggage(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(nil),
|
||||
withParameterSchemas(inactiveJobID, nil),
|
||||
withWorkspaceTags(inactiveVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {
|
||||
@@ -188,9 +191,10 @@ func TestBuilder_Reason(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(nil),
|
||||
withParameterSchemas(inactiveJobID, nil),
|
||||
withWorkspaceTags(inactiveVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {
|
||||
expectProvisionerJob(func(_ database.InsertProvisionerJobParams) {
|
||||
}),
|
||||
withInTx,
|
||||
expectBuild(func(bld database.InsertWorkspaceBuildParams) {
|
||||
@@ -221,6 +225,7 @@ func TestBuilder_ActiveVersion(t *testing.T) {
|
||||
withActiveVersion(nil),
|
||||
withLastBuildNotFound,
|
||||
withParameterSchemas(activeJobID, nil),
|
||||
withWorkspaceTags(activeVersionID, nil),
|
||||
// previous rich parameters are not queried because there is no previous build.
|
||||
|
||||
// Outputs
|
||||
@@ -246,6 +251,102 @@ func TestBuilder_ActiveVersion(t *testing.T) {
|
||||
req.NoError(err)
|
||||
}
|
||||
|
||||
func TestWorkspaceBuildWithTags(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
asrt := assert.New(t)
|
||||
req := require.New(t)
|
||||
|
||||
workspaceTags := []database.TemplateVersionWorkspaceTag{
|
||||
{
|
||||
Key: "fruits_tag",
|
||||
Value: "data.coder_parameter.number_of_apples.value + data.coder_parameter.number_of_oranges.value",
|
||||
},
|
||||
{
|
||||
Key: "cluster_tag",
|
||||
Value: `"best_developers"`,
|
||||
},
|
||||
{
|
||||
Key: "project_tag",
|
||||
Value: `"${data.coder_parameter.project.value}+12345"`,
|
||||
},
|
||||
{
|
||||
Key: "team_tag",
|
||||
Value: `data.coder_parameter.team.value`,
|
||||
},
|
||||
{
|
||||
Key: "yes_or_no",
|
||||
Value: `data.coder_parameter.is_debug_build.value`,
|
||||
},
|
||||
{
|
||||
Key: "actually_no",
|
||||
Value: `!data.coder_parameter.is_debug_build.value`,
|
||||
},
|
||||
{
|
||||
Key: "is_debug_build",
|
||||
Value: `data.coder_parameter.is_debug_build.value == "true" ? "in-debug-mode" : "no-debug"`,
|
||||
},
|
||||
}
|
||||
|
||||
richParameters := []database.TemplateVersionParameter{
|
||||
// Parameters can be mutable although it is discouraged as the workspace can be moved between provisioner nodes.
|
||||
{Name: "project", Description: "This is first parameter", Mutable: true, Options: json.RawMessage("[]")},
|
||||
{Name: "team", Description: "This is second parameter", Mutable: true, DefaultValue: "godzilla", Options: json.RawMessage("[]")},
|
||||
{Name: "is_debug_build", Type: "bool", Description: "This is third parameter", Mutable: false, DefaultValue: "false", Options: json.RawMessage("[]")},
|
||||
{Name: "number_of_apples", Type: "number", Description: "This is fourth parameter", Mutable: false, DefaultValue: "4", Options: json.RawMessage("[]")},
|
||||
{Name: "number_of_oranges", Type: "number", Description: "This is fifth parameter", Mutable: false, DefaultValue: "6", Options: json.RawMessage("[]")},
|
||||
}
|
||||
|
||||
buildParameters := []codersdk.WorkspaceBuildParameter{
|
||||
{Name: "project", Value: "foobar-foobaz"},
|
||||
{Name: "is_debug_build", Value: "true"},
|
||||
// Parameters "team", "number_of_apples", "number_of_oranges" are skipped, so default value is selected
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
mDB := expectDB(t,
|
||||
// Inputs
|
||||
withTemplate,
|
||||
withInactiveVersion(richParameters),
|
||||
withLastBuildFound,
|
||||
withRichParameters(nil),
|
||||
withParameterSchemas(inactiveJobID, nil),
|
||||
withWorkspaceTags(inactiveVersionID, workspaceTags),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {
|
||||
asrt.Len(job.Tags, 10)
|
||||
|
||||
expected := database.StringMap{
|
||||
"actually_no": "false",
|
||||
"cluster_tag": "best_developers",
|
||||
"fruits_tag": "10",
|
||||
"is_debug_build": "in-debug-mode",
|
||||
"project_tag": "foobar-foobaz+12345",
|
||||
"team_tag": "godzilla",
|
||||
"yes_or_no": "true",
|
||||
|
||||
"scope": "user",
|
||||
"version": "inactive",
|
||||
"owner": userID.String(),
|
||||
}
|
||||
asrt.Equal(job.Tags, expected)
|
||||
}),
|
||||
withInTx,
|
||||
expectBuild(func(_ database.InsertWorkspaceBuildParams) {}),
|
||||
expectBuildParameters(func(_ database.InsertWorkspaceBuildParametersParams) {
|
||||
}),
|
||||
withBuild,
|
||||
)
|
||||
|
||||
ws := database.Workspace{ID: workspaceID, TemplateID: templateID, OwnerID: userID}
|
||||
uut := wsbuilder.New(ws, database.WorkspaceTransitionStart).RichParameterValues(buildParameters)
|
||||
_, _, err := uut.Build(ctx, mDB, nil, audit.WorkspaceBuildBaggage{})
|
||||
req.NoError(err)
|
||||
}
|
||||
|
||||
func TestWorkspaceBuildWithRichParameters(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -302,6 +403,7 @@ func TestWorkspaceBuildWithRichParameters(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(initialBuildParameters),
|
||||
withParameterSchemas(inactiveJobID, nil),
|
||||
withWorkspaceTags(inactiveVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {}),
|
||||
@@ -345,6 +447,7 @@ func TestWorkspaceBuildWithRichParameters(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(initialBuildParameters),
|
||||
withParameterSchemas(inactiveJobID, nil),
|
||||
withWorkspaceTags(inactiveVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {}),
|
||||
@@ -394,11 +497,7 @@ func TestWorkspaceBuildWithRichParameters(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(nil),
|
||||
withParameterSchemas(inactiveJobID, schemas),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {}),
|
||||
withInTx,
|
||||
expectBuild(func(bld database.InsertWorkspaceBuildParams) {}),
|
||||
withWorkspaceTags(inactiveVersionID, nil),
|
||||
)
|
||||
|
||||
ws := database.Workspace{ID: workspaceID, TemplateID: templateID, OwnerID: userID}
|
||||
@@ -429,13 +528,10 @@ func TestWorkspaceBuildWithRichParameters(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(initialBuildParameters),
|
||||
withParameterSchemas(inactiveJobID, nil),
|
||||
withWorkspaceTags(inactiveVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {}),
|
||||
withInTx,
|
||||
expectBuild(func(bld database.InsertWorkspaceBuildParams) {}),
|
||||
// no build parameters, since we hit an error validating.
|
||||
// expectBuildParameters(func(params database.InsertWorkspaceBuildParametersParams) {}),
|
||||
// no transaction, since we failed fast while validation build parameters
|
||||
)
|
||||
|
||||
ws := database.Workspace{ID: workspaceID, TemplateID: templateID, OwnerID: userID}
|
||||
@@ -482,6 +578,7 @@ func TestWorkspaceBuildWithRichParameters(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(initialBuildParameters),
|
||||
withParameterSchemas(activeJobID, nil),
|
||||
withWorkspaceTags(activeVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {}),
|
||||
@@ -542,6 +639,7 @@ func TestWorkspaceBuildWithRichParameters(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(initialBuildParameters),
|
||||
withParameterSchemas(activeJobID, nil),
|
||||
withWorkspaceTags(activeVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {}),
|
||||
@@ -600,6 +698,7 @@ func TestWorkspaceBuildWithRichParameters(t *testing.T) {
|
||||
withLastBuildFound,
|
||||
withRichParameters(initialBuildParameters),
|
||||
withParameterSchemas(activeJobID, nil),
|
||||
withWorkspaceTags(activeVersionID, nil),
|
||||
|
||||
// Outputs
|
||||
expectProvisionerJob(func(job database.InsertProvisionerJobParams) {}),
|
||||
@@ -813,6 +912,18 @@ func withRichParameters(params []database.WorkspaceBuildParameter) func(mTx *dbm
|
||||
}
|
||||
}
|
||||
|
||||
func withWorkspaceTags(versionID uuid.UUID, tags []database.TemplateVersionWorkspaceTag) func(mTx *dbmock.MockStore) {
|
||||
return func(mTx *dbmock.MockStore) {
|
||||
c := mTx.EXPECT().GetTemplateVersionWorkspaceTags(gomock.Any(), versionID).
|
||||
Times(1)
|
||||
if len(tags) > 0 {
|
||||
c.Return(tags, nil)
|
||||
} else {
|
||||
c.Return(nil, sql.ErrNoRows)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Since there is expected to be only one each of job, build, and build-parameters inserted, instead
|
||||
// of building matchers, we match any call and then assert its parameters. This will feel
|
||||
// more familiar to the way we write other tests.
|
||||
|
||||
Reference in New Issue
Block a user