Skip to content

feat: Allow inheriting parameters from previous template_versions when updating a template #2397

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

Merged
merged 18 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Unit test param prompting
  • Loading branch information
Emyrk committed Jun 17, 2022
commit c43aedcbd05bd06bd9c15be668e579b3f2767957
1 change: 0 additions & 1 deletion cli/parameterslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func parameterList() *cobra.Command {
return xerrors.Errorf("get workspace template: %w", err)
}
scopeID = template.ID

case codersdk.ParameterImportJob, "template_version":
scope = string(codersdk.ParameterImportJob)
scopeID, err = uuid.Parse(name)
Expand Down
180 changes: 142 additions & 38 deletions cli/templateupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -17,47 +18,150 @@ import (

func TestTemplateUpdate(t *testing.T) {
t.Parallel()
// NewParameter will:
// 1. Create a template version with 0 params
// 2. Create a new version with 1 param
// 2a. Expects 1 param prompt, fills in value
// 3. Assert 1 param value in new version
// 4. Creates a new version with same param
// 4a. Expects 0 prompts as the param value is carried over
// 5. Assert 1 param value in new version
// 6. Creates a new version with 0 params
// 7. Asset 0 params in new version
t.Run("NewParameter", func(t *testing.T) {
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user := coderdtest.CreateFirstUser(t, client)
// Create initial template version to update
lastActiveVersion := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, lastActiveVersion.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, lastActiveVersion.ID)

client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
// Create new template version with a new parameter
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
Parse: createTestParseResponse(),
Provision: echo.ProvisionComplete,
})
cmd, root := clitest.New(t, "templates", "update", template.Name, "-y", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho))
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())

// Test the cli command.
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
Parse: echo.ParseComplete,
Provision: echo.ProvisionComplete,
execDone := make(chan error)
go func() {
execDone <- cmd.Execute()
}()

matches := []struct {
match string
write string
}{
// Expect to be prompted for the new param
{match: "Enter a value:", write: "peter-pan"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
pty.WriteLine(m.write)
}

require.NoError(t, <-execDone)

// Assert template version changed and we have the new param
latestTV, latestParams := latestTemplateVersion(t, client, template.ID)
assert.NotEqual(t, lastActiveVersion.ID, latestTV.ID)
require.Len(t, latestParams, 1, "expect 1 param")
lastActiveVersion = latestTV

// Second update of the same source requires no prompt since the params
// are carried over.
cmd, root = clitest.New(t, "templates", "update", template.Name, "-y", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho))
clitest.SetupConfig(t, client, root)
go func() {
execDone <- cmd.Execute()
}()
require.NoError(t, <-execDone)

// Assert template version changed and we have the carried over param
latestTV, latestParams = latestTemplateVersion(t, client, template.ID)
assert.NotEqual(t, lastActiveVersion.ID, latestTV.ID)
require.Len(t, latestParams, 1, "expect 1 param")
lastActiveVersion = latestTV

// Remove the param
source = clitest.CreateTemplateVersionSource(t, &echo.Responses{
Parse: echo.ParseComplete,
Provision: echo.ProvisionComplete,
})

cmd, root = clitest.New(t, "templates", "update", template.Name, "-y", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho))
clitest.SetupConfig(t, client, root)
go func() {
execDone <- cmd.Execute()
}()
require.NoError(t, <-execDone)
// Assert template version changed and the param was removed
latestTV, latestParams = latestTemplateVersion(t, client, template.ID)
assert.NotEqual(t, lastActiveVersion.ID, latestTV.ID)
require.Len(t, latestParams, 0, "expect 0 param")
lastActiveVersion = latestTV
})
cmd, root := clitest.New(t, "templates", "update", template.Name, "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho))
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())

execDone := make(chan error)
go func() {
execDone <- cmd.Execute()
}()

matches := []struct {
match string
write string
}{
{match: "Upload", write: "yes"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
pty.WriteLine(m.write)
}

require.NoError(t, <-execDone)

// Assert that the template version changed.
templateVersions, err := client.TemplateVersionsByTemplate(context.Background(), codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,

t.Run("OK", func(t *testing.T) {
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

// Test the cli command.
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
Parse: echo.ParseComplete,
Provision: echo.ProvisionComplete,
})
cmd, root := clitest.New(t, "templates", "update", template.Name, "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho))
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())

execDone := make(chan error)
go func() {
execDone <- cmd.Execute()
}()

matches := []struct {
match string
write string
}{
{match: "Upload", write: "yes"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
pty.WriteLine(m.write)
}

require.NoError(t, <-execDone)

// Assert that the template version changed.
templateVersions, err := client.TemplateVersionsByTemplate(context.Background(), codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,
})
require.NoError(t, err)
assert.Len(t, templateVersions, 2)
assert.NotEqual(t, template.ActiveVersionID, templateVersions[1].ID)
})
}

func latestTemplateVersion(t *testing.T, client *codersdk.Client, templateID uuid.UUID) (codersdk.TemplateVersion, []codersdk.Parameter) {
t.Helper()

ctx := context.Background()
newTemplate, err := client.Template(ctx, templateID)
require.NoError(t, err)
tv, err := client.TemplateVersion(ctx, newTemplate.ActiveVersionID)
require.NoError(t, err)
assert.Len(t, templateVersions, 2)
assert.NotEqual(t, template.ActiveVersionID, templateVersions[1].ID)
params, err := client.Parameters(ctx, codersdk.ParameterImportJob, tv.Job.ID)
require.NoError(t, err)

return tv, params
}
20 changes: 7 additions & 13 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sync"
"time"

"github.com/coder/coder/coderd/util/slice"

"github.com/google/uuid"
"golang.org/x/exp/slices"

Expand Down Expand Up @@ -66,6 +68,8 @@ type fakeQuerier struct {
workspaceBuilds []database.WorkspaceBuild
workspaceApps []database.WorkspaceApp
workspaces []database.Workspace

deploymentID string
}

// InTx doesn't rollback data properly for in-memory yet.
Expand Down Expand Up @@ -762,18 +766,18 @@ func (q *fakeQuerier) ParameterValues(_ context.Context, arg database.ParameterV
parameterValues := make([]database.ParameterValue, 0)
for _, parameterValue := range q.parameterValues {
if len(arg.Scopes) > 0 {
if !contains(arg.Scopes, parameterValue.Scope) {
if !slice.Contains(arg.Scopes, parameterValue.Scope) {
continue
}
}
if len(arg.ScopeIds) > 0 {
if !contains(arg.ScopeIds, parameterValue.ScopeID) {
if !slice.Contains(arg.ScopeIds, parameterValue.ScopeID) {
continue
}
}

if len(arg.Ids) > 0 {
if !contains(arg.Ids, parameterValue.ID) {
if !slice.Contains(arg.Ids, parameterValue.ID) {
continue
}
}
Expand Down Expand Up @@ -2115,13 +2119,3 @@ func (q *fakeQuerier) GetDeploymentID(_ context.Context) (string, error) {

return q.deploymentID, nil
}


func contains[T comparable](haystack []T, needle T) bool {
for _, hay := range haystack {
if needle == hay {
return true
}
}
return false
}
10 changes: 10 additions & 0 deletions coderd/util/slice/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package slice

func Contains[T comparable](haystack []T, needle T) bool {
for _, hay := range haystack {
if needle == hay {
return true
}
}
return false
}
30 changes: 30 additions & 0 deletions coderd/util/slice/slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package slice_test

import (
"github.com/coder/coder/coderd/util/slice"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"testing"
)

func TestContains(t *testing.T) {
testContains(t, []int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}, []int{0, 6, -1, -2, 100})
testContains(t, []string{"hello", "world", "foo", "bar", "baz"}, []string{"hello", "world", "baz"}, []string{"not", "words", "in", "set"})
testContains(t,
[]uuid.UUID{uuid.New(), uuid.MustParse("c7c6686d-a93c-4df2-bef9-5f837e9a33d5"), uuid.MustParse("8f3b3e0b-2c3f-46a5-a365-fd5b62bd8818")},
[]uuid.UUID{uuid.MustParse("c7c6686d-a93c-4df2-bef9-5f837e9a33d5")},
[]uuid.UUID{uuid.MustParse("1d00e27d-8de6-46f8-80d5-1da0ca83874a")},
)
}

func testContains[T comparable](t *testing.T, set []T, in []T, out []T) {
for _, e := range set {
require.True(t, slice.Contains(set, e), "elements in set should be in the set")
}
for _, e := range in {
require.True(t, slice.Contains(set, e), "expect element in set")
}
for _, e := range out {
require.False(t, slice.Contains(set, e), "expect element in set")
}
}