|
| 1 | +package cli_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "golang.org/x/xerrors" |
| 12 | + |
| 13 | + "github.com/coder/coder/v2/cli/clitest" |
| 14 | + "github.com/coder/coder/v2/coderd/httpapi" |
| 15 | + "github.com/coder/coder/v2/codersdk" |
| 16 | + "github.com/coder/coder/v2/pty/ptytest" |
| 17 | + "github.com/coder/coder/v2/testutil" |
| 18 | +) |
| 19 | + |
| 20 | +func TestExpTaskPause(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + type testCase struct { |
| 24 | + name string |
| 25 | + args []string |
| 26 | + promptYes bool |
| 27 | + promptNo bool |
| 28 | + wantErr string |
| 29 | + wantPausedMsg bool |
| 30 | + buildHandler func() http.HandlerFunc |
| 31 | + } |
| 32 | + |
| 33 | + const ( |
| 34 | + id1 = "11111111-1111-1111-1111-111111111111" |
| 35 | + id2 = "22222222-2222-2222-2222-222222222222" |
| 36 | + id3 = "33333333-3333-3333-3333-333333333333" |
| 37 | + ) |
| 38 | + |
| 39 | + cases := []testCase{ |
| 40 | + { |
| 41 | + name: "OK_ByName", |
| 42 | + args: []string{"my-task"}, |
| 43 | + promptYes: true, |
| 44 | + buildHandler: func() http.HandlerFunc { |
| 45 | + taskID := uuid.MustParse(id1) |
| 46 | + return func(w http.ResponseWriter, r *http.Request) { |
| 47 | + switch { |
| 48 | + case r.Method == http.MethodGet && r.URL.Path == "/api/v2/tasks/me/my-task": |
| 49 | + httpapi.Write(r.Context(), w, http.StatusOK, codersdk.Task{ |
| 50 | + ID: taskID, |
| 51 | + Name: "my-task", |
| 52 | + OwnerName: "me", |
| 53 | + }) |
| 54 | + case r.Method == http.MethodPost && r.URL.Path == "/api/experimental/tasks/me/"+id1+"/pause": |
| 55 | + httpapi.Write(r.Context(), w, http.StatusAccepted, codersdk.PauseTaskResponse{}) |
| 56 | + default: |
| 57 | + httpapi.InternalServerError(w, xerrors.New("unwanted path: "+r.Method+" "+r.URL.Path)) |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + wantPausedMsg: true, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "OK_ByUUID", |
| 65 | + args: []string{id2}, |
| 66 | + promptYes: true, |
| 67 | + buildHandler: func() http.HandlerFunc { |
| 68 | + return func(w http.ResponseWriter, r *http.Request) { |
| 69 | + switch { |
| 70 | + case r.Method == http.MethodGet && r.URL.Path == "/api/v2/tasks/me/"+id2: |
| 71 | + httpapi.Write(r.Context(), w, http.StatusOK, codersdk.Task{ |
| 72 | + ID: uuid.MustParse(id2), |
| 73 | + OwnerName: "me", |
| 74 | + Name: "uuid-task", |
| 75 | + }) |
| 76 | + case r.Method == http.MethodPost && r.URL.Path == "/api/experimental/tasks/me/"+id2+"/pause": |
| 77 | + httpapi.Write(r.Context(), w, http.StatusAccepted, codersdk.PauseTaskResponse{}) |
| 78 | + default: |
| 79 | + httpapi.InternalServerError(w, xerrors.New("unwanted path: "+r.Method+" "+r.URL.Path)) |
| 80 | + } |
| 81 | + } |
| 82 | + }, |
| 83 | + wantPausedMsg: true, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "YesFlag", |
| 87 | + args: []string{"--yes", "my-task"}, |
| 88 | + buildHandler: func() http.HandlerFunc { |
| 89 | + taskID := uuid.MustParse(id3) |
| 90 | + return func(w http.ResponseWriter, r *http.Request) { |
| 91 | + switch { |
| 92 | + case r.Method == http.MethodGet && r.URL.Path == "/api/v2/tasks/me/my-task": |
| 93 | + httpapi.Write(r.Context(), w, http.StatusOK, codersdk.Task{ |
| 94 | + ID: taskID, |
| 95 | + Name: "my-task", |
| 96 | + OwnerName: "me", |
| 97 | + }) |
| 98 | + case r.Method == http.MethodPost && r.URL.Path == "/api/experimental/tasks/me/"+id3+"/pause": |
| 99 | + httpapi.Write(r.Context(), w, http.StatusAccepted, codersdk.PauseTaskResponse{}) |
| 100 | + default: |
| 101 | + httpapi.InternalServerError(w, xerrors.New("unwanted path: "+r.Method+" "+r.URL.Path)) |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + wantPausedMsg: true, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "ResolveError", |
| 109 | + args: []string{"doesnotexist"}, |
| 110 | + wantErr: "resolve task", |
| 111 | + buildHandler: func() http.HandlerFunc { |
| 112 | + return func(w http.ResponseWriter, r *http.Request) { |
| 113 | + switch { |
| 114 | + case r.Method == http.MethodGet && r.URL.Path == "/api/v2/tasks/me/doesnotexist": |
| 115 | + httpapi.Write(r.Context(), w, http.StatusNotFound, codersdk.Response{ |
| 116 | + Message: "Task not found.", |
| 117 | + }) |
| 118 | + default: |
| 119 | + httpapi.InternalServerError(w, xerrors.New("unwanted path: "+r.Method+" "+r.URL.Path)) |
| 120 | + } |
| 121 | + } |
| 122 | + }, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "PauseError", |
| 126 | + args: []string{"bad-task"}, |
| 127 | + promptYes: true, |
| 128 | + wantErr: "pause task", |
| 129 | + buildHandler: func() http.HandlerFunc { |
| 130 | + taskID := uuid.MustParse(id1) |
| 131 | + return func(w http.ResponseWriter, r *http.Request) { |
| 132 | + switch { |
| 133 | + case r.Method == http.MethodGet && r.URL.Path == "/api/v2/tasks/me/bad-task": |
| 134 | + httpapi.Write(r.Context(), w, http.StatusOK, codersdk.Task{ |
| 135 | + ID: taskID, |
| 136 | + Name: "bad-task", |
| 137 | + OwnerName: "me", |
| 138 | + }) |
| 139 | + case r.Method == http.MethodPost && r.URL.Path == "/api/experimental/tasks/me/"+id1+"/pause": |
| 140 | + httpapi.Write(r.Context(), w, http.StatusInternalServerError, codersdk.Response{ |
| 141 | + Message: "Internal error.", |
| 142 | + Detail: "boom", |
| 143 | + }) |
| 144 | + default: |
| 145 | + httpapi.InternalServerError(w, xerrors.New("unwanted path: "+r.Method+" "+r.URL.Path)) |
| 146 | + } |
| 147 | + } |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "PromptDeclined", |
| 152 | + args: []string{"my-task"}, |
| 153 | + promptNo: true, |
| 154 | + wantErr: "canceled", |
| 155 | + buildHandler: func() http.HandlerFunc { |
| 156 | + taskID := uuid.MustParse(id1) |
| 157 | + return func(w http.ResponseWriter, r *http.Request) { |
| 158 | + switch { |
| 159 | + case r.Method == http.MethodGet && r.URL.Path == "/api/v2/tasks/me/my-task": |
| 160 | + httpapi.Write(r.Context(), w, http.StatusOK, codersdk.Task{ |
| 161 | + ID: taskID, |
| 162 | + Name: "my-task", |
| 163 | + OwnerName: "me", |
| 164 | + }) |
| 165 | + default: |
| 166 | + httpapi.InternalServerError(w, xerrors.New("unwanted path: "+r.Method+" "+r.URL.Path)) |
| 167 | + } |
| 168 | + } |
| 169 | + }, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + for _, tc := range cases { |
| 174 | + t.Run(tc.name, func(t *testing.T) { |
| 175 | + t.Parallel() |
| 176 | + |
| 177 | + ctx := testutil.Context(t, testutil.WaitMedium) |
| 178 | + |
| 179 | + srv := httptest.NewServer(tc.buildHandler()) |
| 180 | + t.Cleanup(srv.Close) |
| 181 | + |
| 182 | + client := codersdk.New(testutil.MustURL(t, srv.URL)) |
| 183 | + |
| 184 | + args := append([]string{"task", "pause"}, tc.args...) |
| 185 | + inv, root := clitest.New(t, args...) |
| 186 | + inv = inv.WithContext(ctx) |
| 187 | + clitest.SetupConfig(t, client, root) |
| 188 | + |
| 189 | + var runErr error |
| 190 | + var outBuf bytes.Buffer |
| 191 | + if tc.promptYes || tc.promptNo { |
| 192 | + pty := ptytest.New(t).Attach(inv) |
| 193 | + w := clitest.StartWithWaiter(t, inv) |
| 194 | + pty.ExpectMatchContext(ctx, "Pause task") |
| 195 | + if tc.promptYes { |
| 196 | + pty.WriteLine("yes") |
| 197 | + } else { |
| 198 | + pty.WriteLine("no") |
| 199 | + } |
| 200 | + if tc.wantPausedMsg { |
| 201 | + pty.ExpectMatchContext(ctx, "has been paused") |
| 202 | + } |
| 203 | + runErr = w.Wait() |
| 204 | + } else { |
| 205 | + inv.Stdout = &outBuf |
| 206 | + inv.Stderr = &outBuf |
| 207 | + runErr = inv.Run() |
| 208 | + } |
| 209 | + |
| 210 | + if tc.wantErr != "" { |
| 211 | + require.ErrorContains(t, runErr, tc.wantErr) |
| 212 | + } else { |
| 213 | + require.NoError(t, runErr) |
| 214 | + } |
| 215 | + |
| 216 | + // For non-PTY tests, verify output from the buffer. |
| 217 | + if tc.wantPausedMsg && !tc.promptYes && !tc.promptNo { |
| 218 | + output := outBuf.String() |
| 219 | + require.Contains(t, output, "has been paused") |
| 220 | + } |
| 221 | + }) |
| 222 | + } |
| 223 | +} |
0 commit comments