| 175 | } |
| 176 | |
| 177 | func TestWorkspaceContextAPI(t *testing.T) { |
| 178 | t.Parallel() |
| 179 | srv := testServer(t) |
| 180 | |
| 181 | rr := doRequest(srv, "POST", "/api/v1/workspaces", map[string]interface{}{ |
| 182 | "name": "Contextual", |
| 183 | "context": map[string]interface{}{ |
| 184 | "repositories": []map[string]string{ |
| 185 | {"name": "docapp", "role": "primary", "path": ".", "repo": "PerpetualSoftware/pad"}, |
| 186 | }, |
| 187 | "commands": map[string]string{ |
| 188 | "build": "make install", |
| 189 | "test": "go test ./...", |
| 190 | }, |
| 191 | "deployment": map[string]string{ |
| 192 | "mode": "local", |
| 193 | "base_url": "http://127.0.0.1:7777", |
| 194 | }, |
| 195 | }, |
| 196 | }) |
| 197 | if rr.Code != http.StatusCreated { |
| 198 | t.Fatalf("create workspace with context: expected 201, got %d: %s", rr.Code, rr.Body.String()) |
| 199 | } |
| 200 | |
| 201 | var ws models.Workspace |
| 202 | parseJSON(t, rr, &ws) |
| 203 | if ws.Context == nil || ws.Context.Commands == nil { |
| 204 | t.Fatalf("expected workspace context in create response, got %#v", ws.Context) |
| 205 | } |
| 206 | if ws.Context.Commands.Build != "make install" { |
| 207 | t.Fatalf("expected build command in context, got %#v", ws.Context.Commands) |
| 208 | } |
| 209 | |
| 210 | rr = doRequest(srv, "PATCH", "/api/v1/workspaces/contextual", map[string]interface{}{ |
| 211 | "context": map[string]interface{}{ |
| 212 | "assumptions": []string{"pad-web lives at ../pad-web"}, |
| 213 | }, |
| 214 | }) |
| 215 | if rr.Code != http.StatusOK { |
| 216 | t.Fatalf("update workspace context: expected 200, got %d: %s", rr.Code, rr.Body.String()) |
| 217 | } |
| 218 | |
| 219 | var updated models.Workspace |
| 220 | parseJSON(t, rr, &updated) |
| 221 | if updated.Context == nil { |
| 222 | t.Fatal("expected context after update") |
| 223 | } |
| 224 | if len(updated.Context.Assumptions) != 1 { |
| 225 | t.Fatalf("expected assumptions after update, got %#v", updated.Context.Assumptions) |
| 226 | } |
| 227 | if updated.Context.Commands != nil { |
| 228 | t.Fatalf("expected context replacement semantics on update, got %#v", updated.Context.Commands) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func createWSForTest(t *testing.T, srv *Server) string { |
| 233 | t.Helper() |