|
| 1 | +package cli_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"fmt" |
| 6 | +"net/http" |
| 7 | +"net/http/httptest" |
| 8 | +"strings" |
| 9 | +"testing" |
| 10 | + |
| 11 | +"github.com/google/uuid" |
| 12 | +"github.com/stretchr/testify/assert" |
| 13 | + |
| 14 | +"github.com/coder/coder/v2/cli/clitest" |
| 15 | +"github.com/coder/coder/v2/coderd/httpapi" |
| 16 | +"github.com/coder/coder/v2/codersdk" |
| 17 | +"github.com/coder/coder/v2/testutil" |
| 18 | +) |
| 19 | + |
| 20 | +funcTest_TaskSend(t*testing.T) { |
| 21 | +t.Parallel() |
| 22 | + |
| 23 | +var ( |
| 24 | +taskName="task-workspace" |
| 25 | +taskID=uuid.MustParse("11111111-1111-1111-1111-111111111111") |
| 26 | +) |
| 27 | + |
| 28 | +tests:= []struct { |
| 29 | +args []string |
| 30 | +stdinstring |
| 31 | +expectErrorstring |
| 32 | +handlerfunc(t*testing.T,ctx context.Context) http.HandlerFunc |
| 33 | +}{ |
| 34 | +{ |
| 35 | +args: []string{taskName,"carry on with the task"}, |
| 36 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 37 | +returnfunc(w http.ResponseWriter,r*http.Request) { |
| 38 | +switchr.URL.Path { |
| 39 | +casefmt.Sprintf("/api/v2/users/me/workspace/%s",taskName): |
| 40 | +httpapi.Write(ctx,w,http.StatusOK, codersdk.Workspace{ |
| 41 | +ID:taskID, |
| 42 | +}) |
| 43 | +casefmt.Sprintf("/api/experimental/tasks/me/%s/send",taskID.String()): |
| 44 | +varreq codersdk.TaskSendRequest |
| 45 | +if!httpapi.Read(ctx,w,r,&req) { |
| 46 | +return |
| 47 | +} |
| 48 | + |
| 49 | +assert.Equal(t,"carry on with the task",req.Input) |
| 50 | + |
| 51 | +httpapi.Write(ctx,w,http.StatusNoContent,nil) |
| 52 | +default: |
| 53 | +t.Errorf("unexpected path: %s",r.URL.Path) |
| 54 | +} |
| 55 | +} |
| 56 | +}, |
| 57 | +}, |
| 58 | +{ |
| 59 | +args: []string{taskID.String(),"carry on with the task"}, |
| 60 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 61 | +returnfunc(w http.ResponseWriter,r*http.Request) { |
| 62 | +switchr.URL.Path { |
| 63 | +casefmt.Sprintf("/api/experimental/tasks/me/%s/send",taskID.String()): |
| 64 | +varreq codersdk.TaskSendRequest |
| 65 | +if!httpapi.Read(ctx,w,r,&req) { |
| 66 | +return |
| 67 | +} |
| 68 | + |
| 69 | +assert.Equal(t,"carry on with the task",req.Input) |
| 70 | + |
| 71 | +httpapi.Write(ctx,w,http.StatusNoContent,nil) |
| 72 | +default: |
| 73 | +t.Errorf("unexpected path: %s",r.URL.Path) |
| 74 | +} |
| 75 | +} |
| 76 | +}, |
| 77 | +}, |
| 78 | +{ |
| 79 | +args: []string{taskName,"--stdin"}, |
| 80 | +stdin:"carry on with the task", |
| 81 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 82 | +returnfunc(w http.ResponseWriter,r*http.Request) { |
| 83 | +switchr.URL.Path { |
| 84 | +casefmt.Sprintf("/api/v2/users/me/workspace/%s",taskName): |
| 85 | +httpapi.Write(ctx,w,http.StatusOK, codersdk.Workspace{ |
| 86 | +ID:taskID, |
| 87 | +}) |
| 88 | +casefmt.Sprintf("/api/experimental/tasks/me/%s/send",taskID.String()): |
| 89 | +varreq codersdk.TaskSendRequest |
| 90 | +if!httpapi.Read(ctx,w,r,&req) { |
| 91 | +return |
| 92 | +} |
| 93 | + |
| 94 | +assert.Equal(t,"carry on with the task",req.Input) |
| 95 | + |
| 96 | +httpapi.Write(ctx,w,http.StatusNoContent,nil) |
| 97 | +default: |
| 98 | +t.Errorf("unexpected path: %s",r.URL.Path) |
| 99 | +} |
| 100 | +} |
| 101 | +}, |
| 102 | +}, |
| 103 | +{ |
| 104 | +args: []string{"doesnotexist","some task input"}, |
| 105 | +expectError:httpapi.ResourceNotFoundResponse.Message, |
| 106 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 107 | +returnfunc(w http.ResponseWriter,r*http.Request) { |
| 108 | +switchr.URL.Path { |
| 109 | +case"/api/v2/users/me/workspace/doesnotexist": |
| 110 | +httpapi.ResourceNotFound(w) |
| 111 | +default: |
| 112 | +t.Errorf("unexpected path: %s",r.URL.Path) |
| 113 | +} |
| 114 | +} |
| 115 | +}, |
| 116 | +}, |
| 117 | +{ |
| 118 | +args: []string{uuid.Nil.String(),"some task input"}, |
| 119 | +expectError:httpapi.ResourceNotFoundResponse.Message, |
| 120 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 121 | +returnfunc(w http.ResponseWriter,r*http.Request) { |
| 122 | +switchr.URL.Path { |
| 123 | +casefmt.Sprintf("/api/experimental/tasks/me/%s/send",uuid.Nil.String()): |
| 124 | +httpapi.ResourceNotFound(w) |
| 125 | +default: |
| 126 | +t.Errorf("unexpected path: %s",r.URL.Path) |
| 127 | +} |
| 128 | +} |
| 129 | +}, |
| 130 | +}, |
| 131 | +{ |
| 132 | +args: []string{uuid.Nil.String(),"some task input"}, |
| 133 | +expectError:assert.AnError.Error(), |
| 134 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 135 | +returnfunc(w http.ResponseWriter,r*http.Request) { |
| 136 | +switchr.URL.Path { |
| 137 | +casefmt.Sprintf("/api/experimental/tasks/me/%s/send",uuid.Nil.String()): |
| 138 | +httpapi.InternalServerError(w,assert.AnError) |
| 139 | +default: |
| 140 | +t.Errorf("unexpected path: %s",r.URL.Path) |
| 141 | +} |
| 142 | +} |
| 143 | +}, |
| 144 | +}, |
| 145 | +} |
| 146 | + |
| 147 | +for_,tt:=rangetests { |
| 148 | +t.Run(strings.Join(tt.args,","),func(t*testing.T) { |
| 149 | +t.Parallel() |
| 150 | + |
| 151 | +var ( |
| 152 | +ctx=testutil.Context(t,testutil.WaitShort) |
| 153 | +srv=httptest.NewServer(tt.handler(t,ctx)) |
| 154 | +client=codersdk.New(testutil.MustURL(t,srv.URL)) |
| 155 | +args= []string{"exp","task","send"} |
| 156 | +errerror |
| 157 | +) |
| 158 | + |
| 159 | +t.Cleanup(srv.Close) |
| 160 | + |
| 161 | +inv,root:=clitest.New(t,append(args,tt.args...)...) |
| 162 | +inv.Stdin=strings.NewReader(tt.stdin) |
| 163 | +clitest.SetupConfig(t,client,root) |
| 164 | + |
| 165 | +err=inv.WithContext(ctx).Run() |
| 166 | +iftt.expectError=="" { |
| 167 | +assert.NoError(t,err) |
| 168 | +}else { |
| 169 | +assert.ErrorContains(t,err,tt.expectError) |
| 170 | +} |
| 171 | +}) |
| 172 | +} |
| 173 | +} |