-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(cli): add coder task pause command
#22012
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
de4d5f0
feat(cli): add `coder task pause` command
DanielleMaywood e094488
chore: add generated docs for task pause command
DanielleMaywood 75cc5b0
test: replace unit tests with integration tests for task pause
DanielleMaywood a742b6e
style: move ctx declaration to just before first use
DanielleMaywood 4150cd5
fix: address review comments on task pause command
DanielleMaywood 128548f
chore: format generated docs
DanielleMaywood f21f552
test: add cross-user task pause test and CLI example
DanielleMaywood a6796dd
chore: respond to feedback
DanielleMaywood 4ae8df3
chore: add an already paused task test
DanielleMaywood 6a2918c
chore: return error when `WorkspaceBuild == nil`
DanielleMaywood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "golang.org/x/xerrors" | ||
|
|
||
| "github.com/coder/coder/v2/cli/cliui" | ||
| "github.com/coder/coder/v2/codersdk" | ||
| "github.com/coder/pretty" | ||
| "github.com/coder/serpent" | ||
| ) | ||
|
|
||
| func (r *RootCmd) taskPause() *serpent.Command { | ||
| cmd := &serpent.Command{ | ||
| Use: "pause <task>", | ||
| Short: "Pause a task", | ||
| Long: FormatExamples( | ||
| Example{ | ||
| Description: "Pause a task by name", | ||
| Command: "coder task pause my-task", | ||
| }, | ||
| Example{ | ||
| Description: "Pause another user's task", | ||
| Command: "coder task pause alice/my-task", | ||
| }, | ||
| Example{ | ||
| Description: "Pause a task without confirmation", | ||
| Command: "coder task pause my-task --yes", | ||
| }, | ||
| ), | ||
| Middleware: serpent.Chain( | ||
| serpent.RequireNArgs(1), | ||
| ), | ||
| Options: serpent.OptionSet{ | ||
| cliui.SkipPromptOption(), | ||
| }, | ||
| Handler: func(inv *serpent.Invocation) error { | ||
| ctx := inv.Context() | ||
| client, err := r.InitClient(inv) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| task, err := client.TaskByIdentifier(ctx, inv.Args[0]) | ||
| if err != nil { | ||
| return xerrors.Errorf("resolve task %q: %w", inv.Args[0], err) | ||
| } | ||
|
|
||
| display := fmt.Sprintf("%s/%s", task.OwnerName, task.Name) | ||
|
|
||
| if task.Status == codersdk.TaskStatusPaused { | ||
| return xerrors.Errorf("task %q is already paused", display) | ||
| } | ||
|
|
||
| _, err = cliui.Prompt(inv, cliui.PromptOptions{ | ||
| Text: fmt.Sprintf("Pause task %s?", pretty.Sprint(cliui.DefaultStyles.Code, display)), | ||
| IsConfirm: true, | ||
| Default: cliui.ConfirmNo, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resp, err := client.PauseTask(ctx, task.OwnerName, task.ID) | ||
| if err != nil { | ||
| return xerrors.Errorf("pause task %q: %w", display, err) | ||
| } | ||
|
|
||
| if resp.WorkspaceBuild == nil { | ||
| return xerrors.Errorf("pause task %q: no workspace build returned", display) | ||
| } | ||
|
|
||
| err = cliui.WorkspaceBuild(ctx, inv.Stdout, client, resp.WorkspaceBuild.ID) | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return xerrors.Errorf("watch pause build for task %q: %w", display, err) | ||
| } | ||
|
|
||
| _, _ = fmt.Fprintf( | ||
| inv.Stdout, | ||
| "\nThe %s task has been paused at %s!\n", | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cliui.Keyword(task.Name), | ||
| cliui.Timestamp(time.Now()), | ||
| ) | ||
| return nil | ||
| }, | ||
| } | ||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package cli_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/coder/coder/v2/cli/clitest" | ||
| "github.com/coder/coder/v2/coderd/coderdtest" | ||
| "github.com/coder/coder/v2/codersdk" | ||
| "github.com/coder/coder/v2/pty/ptytest" | ||
| "github.com/coder/coder/v2/testutil" | ||
| ) | ||
|
|
||
| func TestExpTaskPause(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("WithYesFlag", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Given: A running task | ||
| setupCtx := testutil.Context(t, testutil.WaitLong) | ||
| _, userClient, task := setupCLITaskTest(setupCtx, t, nil) | ||
|
|
||
| // When: We attempt to pause the task | ||
| inv, root := clitest.New(t, "task", "pause", task.Name, "--yes") | ||
| output := clitest.Capture(inv) | ||
| clitest.SetupConfig(t, userClient, root) | ||
|
|
||
| // Then: Expect the task to be paused | ||
| ctx := testutil.Context(t, testutil.WaitMedium) | ||
| err := inv.WithContext(ctx).Run() | ||
| require.NoError(t, err) | ||
| require.Contains(t, output.Stdout(), "has been paused") | ||
|
|
||
| updated, err := userClient.TaskByIdentifier(ctx, task.Name) | ||
| require.NoError(t, err) | ||
| require.Equal(t, codersdk.TaskStatusPaused, updated.Status) | ||
| }) | ||
|
|
||
| // OtherUserTask verifies that an admin can pause a task owned by | ||
| // another user using the "owner/name" identifier format. | ||
| t.Run("OtherUserTask", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Given: A different user's running task | ||
| setupCtx := testutil.Context(t, testutil.WaitLong) | ||
| adminClient, _, task := setupCLITaskTest(setupCtx, t, nil) | ||
|
|
||
| // When: We attempt to pause their task | ||
| identifier := fmt.Sprintf("%s/%s", task.OwnerName, task.Name) | ||
| inv, root := clitest.New(t, "task", "pause", identifier, "--yes") | ||
| output := clitest.Capture(inv) | ||
| clitest.SetupConfig(t, adminClient, root) | ||
|
|
||
| // Then: We expect the task to be paused | ||
| ctx := testutil.Context(t, testutil.WaitMedium) | ||
| err := inv.WithContext(ctx).Run() | ||
| require.NoError(t, err) | ||
| require.Contains(t, output.Stdout(), "has been paused") | ||
|
|
||
| updated, err := adminClient.TaskByIdentifier(ctx, identifier) | ||
| require.NoError(t, err) | ||
| require.Equal(t, codersdk.TaskStatusPaused, updated.Status) | ||
| }) | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| t.Run("PromptConfirm", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Given: A running task | ||
| setupCtx := testutil.Context(t, testutil.WaitLong) | ||
| _, userClient, task := setupCLITaskTest(setupCtx, t, nil) | ||
|
|
||
| // When: We attempt to pause the task | ||
| inv, root := clitest.New(t, "task", "pause", task.Name) | ||
| clitest.SetupConfig(t, userClient, root) | ||
|
|
||
| // And: We confirm we want to pause the task | ||
| ctx := testutil.Context(t, testutil.WaitMedium) | ||
| inv = inv.WithContext(ctx) | ||
| pty := ptytest.New(t).Attach(inv) | ||
| w := clitest.StartWithWaiter(t, inv) | ||
| pty.ExpectMatchContext(ctx, "Pause task") | ||
| pty.WriteLine("yes") | ||
|
|
||
| // Then: We expect the task to be paused | ||
| pty.ExpectMatchContext(ctx, "has been paused") | ||
| require.NoError(t, w.Wait()) | ||
|
|
||
| updated, err := userClient.TaskByIdentifier(ctx, task.Name) | ||
| require.NoError(t, err) | ||
| require.Equal(t, codersdk.TaskStatusPaused, updated.Status) | ||
| }) | ||
|
|
||
| t.Run("PromptDecline", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Given: A running task | ||
| setupCtx := testutil.Context(t, testutil.WaitLong) | ||
| _, userClient, task := setupCLITaskTest(setupCtx, t, nil) | ||
|
|
||
| // When: We attempt to pause the task | ||
| inv, root := clitest.New(t, "task", "pause", task.Name) | ||
| clitest.SetupConfig(t, userClient, root) | ||
|
|
||
| // But: We say no at the confirmation screen | ||
| ctx := testutil.Context(t, testutil.WaitMedium) | ||
| inv = inv.WithContext(ctx) | ||
| pty := ptytest.New(t).Attach(inv) | ||
| w := clitest.StartWithWaiter(t, inv) | ||
| pty.ExpectMatchContext(ctx, "Pause task") | ||
| pty.WriteLine("no") | ||
| require.Error(t, w.Wait()) | ||
|
|
||
| // Then: We expect the task to not be paused | ||
| updated, err := userClient.TaskByIdentifier(ctx, task.Name) | ||
| require.NoError(t, err) | ||
| require.NotEqual(t, codersdk.TaskStatusPaused, updated.Status) | ||
| }) | ||
|
|
||
| t.Run("TaskAlreadyPaused", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Given: A running task | ||
| setupCtx := testutil.Context(t, testutil.WaitLong) | ||
| _, userClient, task := setupCLITaskTest(setupCtx, t, nil) | ||
|
|
||
| // And: We paused the running task | ||
| ctx := testutil.Context(t, testutil.WaitMedium) | ||
| resp, err := userClient.PauseTask(ctx, task.OwnerName, task.ID) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, resp.WorkspaceBuild) | ||
| coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, resp.WorkspaceBuild.ID) | ||
|
|
||
| // When: We attempt to pause the task again | ||
| inv, root := clitest.New(t, "task", "pause", task.Name, "--yes") | ||
| clitest.SetupConfig(t, userClient, root) | ||
|
|
||
| // Then: We expect to get an error that the task is already paused | ||
| err = inv.WithContext(ctx).Run() | ||
| require.ErrorContains(t, err, "is already paused") | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint already fetches the task based on the name/id provided in its middleware. Do we need to fetch it here in the CLI? We're incurring an additional round trip through HTTP and SQL.
Looks like we mostly fetch it to determine its owner for display purposes and then to pass back into the endpoint.
Would it be better to require the user to provide the owner name/id if they're trying to delete a task for a different user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do what you're suggesting although it'd require updating the client helper
PauseTaskdefined incodersdk/aitasks.gofromuser string, id uuid.UUIDtouser, idOrName string