|
| 1 | +package cli_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"fmt" |
| 6 | +"net/http" |
| 7 | +"net/http/httptest" |
| 8 | +"net/url" |
| 9 | +"strings" |
| 10 | +"testing" |
| 11 | + |
| 12 | +"github.com/google/uuid" |
| 13 | +"github.com/stretchr/testify/assert" |
| 14 | +"github.com/stretchr/testify/require" |
| 15 | + |
| 16 | +"github.com/coder/coder/v2/cli/clitest" |
| 17 | +"github.com/coder/coder/v2/cli/cliui" |
| 18 | +"github.com/coder/coder/v2/coderd/httpapi" |
| 19 | +"github.com/coder/coder/v2/codersdk" |
| 20 | +"github.com/coder/coder/v2/testutil" |
| 21 | +"github.com/coder/serpent" |
| 22 | +) |
| 23 | + |
| 24 | +funcTestTaskCreate(t*testing.T) { |
| 25 | +t.Parallel() |
| 26 | + |
| 27 | +var ( |
| 28 | +organizationID=uuid.New() |
| 29 | +templateID=uuid.New() |
| 30 | +templateVersionID=uuid.New() |
| 31 | +templateVersionPresetID=uuid.New() |
| 32 | +) |
| 33 | + |
| 34 | +templateAndVersionFoundHandler:=func(t*testing.T,ctx context.Context,templateName,templateVersionName,presetName,promptstring) http.HandlerFunc { |
| 35 | +t.Helper() |
| 36 | + |
| 37 | +returnfunc(w http.ResponseWriter,r*http.Request) { |
| 38 | +switchr.URL.Path { |
| 39 | +case"/api/v2/users/me/organizations": |
| 40 | +httpapi.Write(ctx,w,http.StatusOK, []codersdk.Organization{ |
| 41 | +{MinimalOrganization: codersdk.MinimalOrganization{ |
| 42 | +ID:organizationID, |
| 43 | +}}, |
| 44 | +}) |
| 45 | +casefmt.Sprintf("/api/v2/organizations/%s/templates/my-template/versions/my-template-version",organizationID): |
| 46 | +httpapi.Write(ctx,w,http.StatusOK, codersdk.TemplateVersion{ |
| 47 | +ID:templateVersionID, |
| 48 | +}) |
| 49 | +casefmt.Sprintf("/api/v2/organizations/%s/templates/my-template",organizationID): |
| 50 | +httpapi.Write(ctx,w,http.StatusOK, codersdk.Template{ |
| 51 | +ID:templateID, |
| 52 | +ActiveVersionID:templateVersionID, |
| 53 | +}) |
| 54 | +casefmt.Sprintf("/api/v2/templateversions/%s/presets",templateVersionID): |
| 55 | +httpapi.Write(ctx,w,http.StatusOK, []codersdk.Preset{ |
| 56 | +{ |
| 57 | +ID:templateVersionPresetID, |
| 58 | +Name:presetName, |
| 59 | +}, |
| 60 | +}) |
| 61 | +case"/api/experimental/tasks/me": |
| 62 | +varreq codersdk.CreateTaskRequest |
| 63 | +if!httpapi.Read(ctx,w,r,&req) { |
| 64 | +return |
| 65 | +} |
| 66 | + |
| 67 | +assert.Equal(t,prompt,req.Prompt,"prompt mismatch") |
| 68 | +assert.Equal(t,templateVersionID,req.TemplateVersionID,"template version mismatch") |
| 69 | + |
| 70 | +ifpresetName=="" { |
| 71 | +assert.Equal(t,uuid.Nil,req.TemplateVersionPresetID,"expected no template preset id") |
| 72 | +}else { |
| 73 | +assert.Equal(t,templateVersionPresetID,req.TemplateVersionPresetID,"template version preset id mismatch") |
| 74 | +} |
| 75 | + |
| 76 | +httpapi.Write(ctx,w,http.StatusCreated, codersdk.Workspace{ |
| 77 | +Name:"task-wild-goldfish-27", |
| 78 | +}) |
| 79 | +default: |
| 80 | +t.Errorf("unexpected path: %s",r.URL.Path) |
| 81 | +} |
| 82 | +} |
| 83 | +} |
| 84 | + |
| 85 | +tests:= []struct { |
| 86 | +args []string |
| 87 | +env []string |
| 88 | +expectErrorstring |
| 89 | +expectOutputstring |
| 90 | +handlerfunc(t*testing.T,ctx context.Context) http.HandlerFunc |
| 91 | +}{ |
| 92 | +{ |
| 93 | +args: []string{"my-template@my-template-version","--input","my custom prompt"}, |
| 94 | +expectOutput:fmt.Sprintf("The task %s has been created",cliui.Keyword("task-wild-goldfish-27")), |
| 95 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 96 | +returntemplateAndVersionFoundHandler(t,ctx,"my-template","my-template-version","","my custom prompt") |
| 97 | +}, |
| 98 | +}, |
| 99 | +{ |
| 100 | +args: []string{"my-template","--input","my custom prompt"}, |
| 101 | +env: []string{"CODER_TASK_TEMPLATE_VERSION=my-template-version"}, |
| 102 | +expectOutput:fmt.Sprintf("The task %s has been created",cliui.Keyword("task-wild-goldfish-27")), |
| 103 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 104 | +returntemplateAndVersionFoundHandler(t,ctx,"my-template","my-template-version","","my custom prompt") |
| 105 | +}, |
| 106 | +}, |
| 107 | +{ |
| 108 | +args: []string{"--input","my custom prompt"}, |
| 109 | +env: []string{"CODER_TASK_TEMPLATE_NAME=my-template","CODER_TASK_TEMPLATE_VERSION=my-template-version"}, |
| 110 | +expectOutput:fmt.Sprintf("The task %s has been created",cliui.Keyword("task-wild-goldfish-27")), |
| 111 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 112 | +returntemplateAndVersionFoundHandler(t,ctx,"my-template","my-template-version","","my custom prompt") |
| 113 | +}, |
| 114 | +}, |
| 115 | +{ |
| 116 | +env: []string{"CODER_TASK_TEMPLATE_NAME=my-template","CODER_TASK_TEMPLATE_VERSION=my-template-version","CODER_TASK_INPUT=my custom prompt"}, |
| 117 | +expectOutput:fmt.Sprintf("The task %s has been created",cliui.Keyword("task-wild-goldfish-27")), |
| 118 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 119 | +returntemplateAndVersionFoundHandler(t,ctx,"my-template","my-template-version","","my custom prompt") |
| 120 | +}, |
| 121 | +}, |
| 122 | +{ |
| 123 | +args: []string{"my-template","--input","my custom prompt"}, |
| 124 | +expectOutput:fmt.Sprintf("The task %s has been created",cliui.Keyword("task-wild-goldfish-27")), |
| 125 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 126 | +returntemplateAndVersionFoundHandler(t,ctx,"my-template","","","my custom prompt") |
| 127 | +}, |
| 128 | +}, |
| 129 | +{ |
| 130 | +args: []string{"my-template","--input","my custom prompt","--preset","my-preset"}, |
| 131 | +expectOutput:fmt.Sprintf("The task %s has been created",cliui.Keyword("task-wild-goldfish-27")), |
| 132 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 133 | +returntemplateAndVersionFoundHandler(t,ctx,"my-template","","my-preset","my custom prompt") |
| 134 | +}, |
| 135 | +}, |
| 136 | +{ |
| 137 | +args: []string{"my-template","--input","my custom prompt"}, |
| 138 | +env: []string{"CODER_TASK_PRESET_NAME=my-preset"}, |
| 139 | +expectOutput:fmt.Sprintf("The task %s has been created",cliui.Keyword("task-wild-goldfish-27")), |
| 140 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 141 | +returntemplateAndVersionFoundHandler(t,ctx,"my-template","","my-preset","my custom prompt") |
| 142 | +}, |
| 143 | +}, |
| 144 | +{ |
| 145 | +args: []string{"my-template","--input","my custom prompt","--preset","not-real-preset"}, |
| 146 | +expectError:`preset "not-real-preset" not found`, |
| 147 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 148 | +returntemplateAndVersionFoundHandler(t,ctx,"my-template","","my-preset","my custom prompt") |
| 149 | +}, |
| 150 | +}, |
| 151 | +{ |
| 152 | +args: []string{"my-template@not-real-template-version","--input","my custom prompt"}, |
| 153 | +expectError:httpapi.ResourceNotFoundResponse.Message, |
| 154 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 155 | +returnfunc(w http.ResponseWriter,r*http.Request) { |
| 156 | +switchr.URL.Path { |
| 157 | +case"/api/v2/users/me/organizations": |
| 158 | +httpapi.Write(ctx,w,http.StatusOK, []codersdk.Organization{ |
| 159 | +{MinimalOrganization: codersdk.MinimalOrganization{ |
| 160 | +ID:organizationID, |
| 161 | +}}, |
| 162 | +}) |
| 163 | +casefmt.Sprintf("/api/v2/organizations/%s/templates/my-template/versions/not-real-template-version",organizationID): |
| 164 | +httpapi.ResourceNotFound(w) |
| 165 | +default: |
| 166 | +t.Errorf("unexpected path: %s",r.URL.Path) |
| 167 | +} |
| 168 | +} |
| 169 | +}, |
| 170 | +}, |
| 171 | +{ |
| 172 | +args: []string{"not-real-template","--input","my custom prompt"}, |
| 173 | +expectError:httpapi.ResourceNotFoundResponse.Message, |
| 174 | +handler:func(t*testing.T,ctx context.Context) http.HandlerFunc { |
| 175 | +returnfunc(w http.ResponseWriter,r*http.Request) { |
| 176 | +switchr.URL.Path { |
| 177 | +case"/api/v2/users/me/organizations": |
| 178 | +httpapi.Write(ctx,w,http.StatusOK, []codersdk.Organization{ |
| 179 | +{MinimalOrganization: codersdk.MinimalOrganization{ |
| 180 | +ID:organizationID, |
| 181 | +}}, |
| 182 | +}) |
| 183 | +casefmt.Sprintf("/api/v2/organizations/%s/templates/not-real-template",organizationID): |
| 184 | +httpapi.ResourceNotFound(w) |
| 185 | +default: |
| 186 | +t.Errorf("unexpected path: %s",r.URL.Path) |
| 187 | +} |
| 188 | +} |
| 189 | +}, |
| 190 | +}, |
| 191 | +} |
| 192 | + |
| 193 | +for_,tt:=rangetests { |
| 194 | +t.Run(strings.Join(tt.args,","),func(t*testing.T) { |
| 195 | +t.Parallel() |
| 196 | + |
| 197 | +var ( |
| 198 | +ctx=testutil.Context(t,testutil.WaitShort) |
| 199 | +srv=httptest.NewServer(tt.handler(t,ctx)) |
| 200 | +client=new(codersdk.Client) |
| 201 | +args= []string{"exp","task","create"} |
| 202 | +sb strings.Builder |
| 203 | +errerror |
| 204 | +) |
| 205 | + |
| 206 | +t.Cleanup(srv.Close) |
| 207 | + |
| 208 | +client.URL,err=url.Parse(srv.URL) |
| 209 | +require.NoError(t,err) |
| 210 | + |
| 211 | +inv,root:=clitest.New(t,append(args,tt.args...)...) |
| 212 | +inv.Environ=serpent.ParseEnviron(tt.env,"") |
| 213 | +inv.Stdout=&sb |
| 214 | +inv.Stderr=&sb |
| 215 | +clitest.SetupConfig(t,client,root) |
| 216 | + |
| 217 | +err=inv.WithContext(ctx).Run() |
| 218 | +iftt.expectError=="" { |
| 219 | +assert.NoError(t,err) |
| 220 | +}else { |
| 221 | +assert.ErrorContains(t,err,tt.expectError) |
| 222 | +} |
| 223 | + |
| 224 | +assert.Contains(t,sb.String(),tt.expectOutput) |
| 225 | +}) |
| 226 | +} |
| 227 | +} |