From ba77e74552b1216fb39231d39a74d48579c45563 Mon Sep 17 00:00:00 2001 From: ___ Date: Wed, 10 May 2023 19:43:14 +0000 Subject: [PATCH 1/5] feat: Add arg to allow non-active template version pushes So that we can push template updates for testing without impacting normal users of the template. --- cli/templatepush.go | 19 ++++++++++++----- cli/templatepush_test.go | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 2862ed0d21824..5e1be7a4c6e13 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -116,6 +116,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd { alwaysPrompt bool provisionerTags []string uploadFlags templateUploadFlags + makeActive bool ) client := new(codersdk.Client) cmd := &clibase.Cmd{ @@ -174,11 +175,13 @@ func (r *RootCmd) templatePush() *clibase.Cmd { return xerrors.Errorf("job failed: %s", job.Job.Status) } - err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{ - ID: job.ID, - }) - if err != nil { - return err + if makeActive { + err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{ + ID: job.ID, + }) + if err != nil { + return err + } } _, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp))) @@ -233,6 +236,12 @@ func (r *RootCmd) templatePush() *clibase.Cmd { Description: "Always prompt all parameters. Does not pull parameter values from active template version.", Value: clibase.BoolOf(&alwaysPrompt), }, + { + Flag: "make-active", + Description: "Whether the new template will be marked active.", + Default: "true", + Value: clibase.BoolOf(&makeActive), + }, cliui.SkipPromptOption(), uploadFlags.option(), } diff --git a/cli/templatepush_test.go b/cli/templatepush_test.go index 6bf8d6af97924..ea3fda59da3e7 100644 --- a/cli/templatepush_test.go +++ b/cli/templatepush_test.go @@ -156,6 +156,52 @@ func TestTemplatePush(t *testing.T) { require.Equal(t, "example", templateVersions[1].Name) }) + t.Run("PushInactiveTemplateVersion", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: 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, + ProvisionApply: echo.ProvisionComplete, + }) + inv, root := clitest.New(t, "templates", "push", template.Name, "--make-active", "false", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example") + clitest.SetupConfig(t, client, root) + pty := ptytest.New(t).Attach(inv) + + execDone := make(chan error) + go func() { + execDone <- inv.Run() + }() + + 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 didn't change. + templateVersions, err := client.TemplateVersionsByTemplate(context.Background(), codersdk.TemplateVersionsByTemplateRequest{ + TemplateID: template.ID, + }) + require.NoError(t, err) + assert.Len(t, templateVersions, 2) + assert.Equal(t, template.ActiveVersionID, templateVersions[0].ID) + require.NotEqual(t, "example", templateVersions[0].Name) + }) + t.Run("UseWorkingDir", func(t *testing.T) { t.Parallel() From 7e023fb5cc3208c588e5e22b0eb4a2491adac66a Mon Sep 17 00:00:00 2001 From: ___ Date: Fri, 12 May 2023 17:07:57 +0000 Subject: [PATCH 2/5] Update --- cli/templatepush.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/templatepush.go b/cli/templatepush.go index 5e1be7a4c6e13..d8409d6759758 100644 --- a/cli/templatepush.go +++ b/cli/templatepush.go @@ -116,7 +116,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd { alwaysPrompt bool provisionerTags []string uploadFlags templateUploadFlags - makeActive bool + activate bool ) client := new(codersdk.Client) cmd := &clibase.Cmd{ @@ -175,7 +175,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd { return xerrors.Errorf("job failed: %s", job.Job.Status) } - if makeActive { + if activate { err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{ ID: job.ID, }) @@ -237,10 +237,10 @@ func (r *RootCmd) templatePush() *clibase.Cmd { Value: clibase.BoolOf(&alwaysPrompt), }, { - Flag: "make-active", + Flag: "activate", Description: "Whether the new template will be marked active.", Default: "true", - Value: clibase.BoolOf(&makeActive), + Value: clibase.BoolOf(&activate), }, cliui.SkipPromptOption(), uploadFlags.option(), From 2ce59d9f63b6b1a6065455d161fe9ae904ae54cc Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Tue, 16 May 2023 16:11:01 +0000 Subject: [PATCH 3/5] make gen update-golden-files --- cli/testdata/coder_templates_push_--help.golden | 3 +++ docs/cli/templates_push.md | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/cli/testdata/coder_templates_push_--help.golden b/cli/testdata/coder_templates_push_--help.golden index 0b82f622fdc58..447a354051a03 100644 --- a/cli/testdata/coder_templates_push_--help.golden +++ b/cli/testdata/coder_templates_push_--help.golden @@ -3,6 +3,9 @@ Usage: coder templates push [flags] [template] Push a new template version from the current directory or as specified by flag Options + --activate bool (default: true) + Whether the new template will be marked active. + --always-prompt bool Always prompt all parameters. Does not pull parameter values from active template version. diff --git a/docs/cli/templates_push.md b/docs/cli/templates_push.md index 9a83566f6ad13..3729fe1e5583d 100644 --- a/docs/cli/templates_push.md +++ b/docs/cli/templates_push.md @@ -12,6 +12,15 @@ coder templates push [flags] [template] ## Options +### --activate + +| | | +| ------- | ----------------- | +| Type | bool | +| Default | true | + +Whether the new template will be marked active. + ### --always-prompt | | | From cd158c51f852b07d9a236099d5669ac8fd41a72a Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Wed, 17 May 2023 08:25:02 +0300 Subject: [PATCH 4/5] fix(test): update flag name in test --- cli/templatepush_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/templatepush_test.go b/cli/templatepush_test.go index ea3fda59da3e7..a64c09bd661a8 100644 --- a/cli/templatepush_test.go +++ b/cli/templatepush_test.go @@ -170,7 +170,7 @@ func TestTemplatePush(t *testing.T) { Parse: echo.ParseComplete, ProvisionApply: echo.ProvisionComplete, }) - inv, root := clitest.New(t, "templates", "push", template.Name, "--make-active", "false", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example") + inv, root := clitest.New(t, "templates", "push", template.Name, "--activate", "false", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example") clitest.SetupConfig(t, client, root) pty := ptytest.New(t).Attach(inv) From eb30242d8d6530017ec179faa425f6650bb48a0b Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Sat, 3 Jun 2023 21:49:32 +0000 Subject: [PATCH 5/5] Fix tests --- cli/templatepush_test.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/cli/templatepush_test.go b/cli/templatepush_test.go index 5aba79fef2d28..82b0fd51980d1 100644 --- a/cli/templatepush_test.go +++ b/cli/templatepush_test.go @@ -83,14 +83,10 @@ func TestTemplatePush(t *testing.T) { Parse: echo.ParseComplete, ProvisionApply: echo.ProvisionComplete, }) - inv, root := clitest.New(t, "templates", "push", template.Name, "--activate", "false", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example") + inv, root := clitest.New(t, "templates", "push", template.Name, "--activate=false", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example") clitest.SetupConfig(t, client, root) pty := ptytest.New(t).Attach(inv) - - execDone := make(chan error) - go func() { - execDone <- inv.Run() - }() + w := clitest.StartWithWaiter(t, inv) matches := []struct { match string @@ -103,7 +99,7 @@ func TestTemplatePush(t *testing.T) { pty.WriteLine(m.write) } - require.NoError(t, <-execDone) + w.RequireSuccess() // Assert that the template version didn't change. templateVersions, err := client.TemplateVersionsByTemplate(context.Background(), codersdk.TemplateVersionsByTemplateRequest{