1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 "git.curoverse.com/arvados.git/sdk/go/arvados"
22 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
23 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
24 "git.curoverse.com/arvados.git/sdk/go/httpserver"
25 "git.curoverse.com/arvados.git/sdk/go/keepclient"
26 "github.com/sirupsen/logrus"
27 check "gopkg.in/check.v1"
30 // Gocheck boilerplate
31 var _ = check.Suite(&FederationSuite{})
33 type FederationSuite struct {
34 log logrus.FieldLogger
35 // testServer and testHandler are the controller being tested,
37 testServer *httpserver.Server
39 // remoteServer ("zzzzz") forwards requests to the Rails API
40 // provided by the integration test environment.
41 remoteServer *httpserver.Server
42 // remoteMock ("zmock") appends each incoming request to
43 // remoteMockRequests, and returns 200 with an empty JSON
45 remoteMock *httpserver.Server
46 remoteMockRequests []http.Request
49 func (s *FederationSuite) SetUpTest(c *check.C) {
50 s.log = ctxlog.TestLogger(c)
52 s.remoteServer = newServerFromIntegrationTestEnv(c)
53 c.Assert(s.remoteServer.Start(), check.IsNil)
55 s.remoteMock = newServerFromIntegrationTestEnv(c)
56 s.remoteMock.Server.Handler = http.HandlerFunc(s.remoteMockHandler)
57 c.Assert(s.remoteMock.Start(), check.IsNil)
59 cluster := &arvados.Cluster{
61 PostgreSQL: integrationTestCluster().PostgreSQL,
62 EnableBetaController14287: enableBetaController14287,
64 cluster.TLS.Insecure = true
65 cluster.API.MaxItemsPerResponse = 1000
66 cluster.API.MaxRequestAmplification = 4
67 arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "http://localhost:1/")
68 arvadostest.SetServiceURL(&cluster.Services.Controller, "http://localhost:/")
69 s.testHandler = &Handler{Cluster: cluster}
70 s.testServer = newServerFromIntegrationTestEnv(c)
71 s.testServer.Server.Handler = httpserver.HandlerWithContext(
72 ctxlog.Context(context.Background(), s.log),
73 httpserver.AddRequestIDs(httpserver.LogRequests(s.testHandler)))
75 cluster.RemoteClusters = map[string]arvados.RemoteCluster{
77 Host: s.remoteServer.Addr,
82 Host: s.remoteMock.Addr,
88 c.Assert(s.testServer.Start(), check.IsNil)
90 s.remoteMockRequests = nil
93 func (s *FederationSuite) remoteMockHandler(w http.ResponseWriter, req *http.Request) {
97 req.Body = ioutil.NopCloser(b)
98 s.remoteMockRequests = append(s.remoteMockRequests, *req)
99 // Repond 200 with a valid JSON object
103 func (s *FederationSuite) TearDownTest(c *check.C) {
104 if s.remoteServer != nil {
105 s.remoteServer.Close()
107 if s.testServer != nil {
112 func (s *FederationSuite) testRequest(req *http.Request) *httptest.ResponseRecorder {
113 resp := httptest.NewRecorder()
114 s.testServer.Server.Handler.ServeHTTP(resp, req)
118 func (s *FederationSuite) TestLocalRequest(c *check.C) {
119 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zhome-", 1), nil)
120 resp := s.testRequest(req).Result()
121 s.checkHandledLocally(c, resp)
124 func (s *FederationSuite) checkHandledLocally(c *check.C, resp *http.Response) {
125 // Our "home" controller can't handle local requests because
126 // it doesn't have its own stub/test Rails API, so we rely on
127 // "connection refused" to indicate the controller tried to
128 // proxy the request to its local Rails API.
129 c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway)
130 s.checkJSONErrorMatches(c, resp, `.*connection refused`)
133 func (s *FederationSuite) TestNoAuth(c *check.C) {
134 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
135 resp := s.testRequest(req).Result()
136 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
137 s.checkJSONErrorMatches(c, resp, `Not logged in.*`)
140 func (s *FederationSuite) TestBadAuth(c *check.C) {
141 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
142 req.Header.Set("Authorization", "Bearer aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
143 resp := s.testRequest(req).Result()
144 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
145 s.checkJSONErrorMatches(c, resp, `Not logged in.*`)
148 func (s *FederationSuite) TestNoAccess(c *check.C) {
149 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
150 req.Header.Set("Authorization", "Bearer "+arvadostest.SpectatorToken)
151 resp := s.testRequest(req).Result()
152 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
153 s.checkJSONErrorMatches(c, resp, `.*not found.*`)
156 func (s *FederationSuite) TestGetUnknownRemote(c *check.C) {
157 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zz404-", 1), nil)
158 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
159 resp := s.testRequest(req).Result()
160 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
161 s.checkJSONErrorMatches(c, resp, `.*no proxy available for cluster zz404`)
164 func (s *FederationSuite) TestRemoteError(c *check.C) {
165 rc := s.testHandler.Cluster.RemoteClusters["zzzzz"]
167 s.testHandler.Cluster.RemoteClusters["zzzzz"] = rc
169 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
170 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
171 resp := s.testRequest(req).Result()
172 c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway)
173 s.checkJSONErrorMatches(c, resp, `.*HTTP response to HTTPS client`)
176 func (s *FederationSuite) TestGetRemoteWorkflow(c *check.C) {
177 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
178 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
179 resp := s.testRequest(req).Result()
180 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
181 var wf arvados.Workflow
182 c.Check(json.NewDecoder(resp.Body).Decode(&wf), check.IsNil)
183 c.Check(wf.UUID, check.Equals, arvadostest.WorkflowWithDefinitionYAMLUUID)
184 c.Check(wf.OwnerUUID, check.Equals, arvadostest.ActiveUserUUID)
187 func (s *FederationSuite) TestOptionsMethod(c *check.C) {
188 req := httptest.NewRequest("OPTIONS", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
189 req.Header.Set("Origin", "https://example.com")
190 resp := s.testRequest(req).Result()
191 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
192 body, err := ioutil.ReadAll(resp.Body)
193 c.Check(err, check.IsNil)
194 c.Check(string(body), check.Equals, "")
195 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
196 for _, hdr := range []string{"Authorization", "Content-Type"} {
197 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
199 for _, method := range []string{"GET", "HEAD", "PUT", "POST", "DELETE"} {
200 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
204 func (s *FederationSuite) TestRemoteWithTokenInQuery(c *check.C) {
205 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1)+"?api_token="+arvadostest.ActiveToken, nil)
206 s.testRequest(req).Result()
207 c.Assert(s.remoteMockRequests, check.HasLen, 1)
208 pr := s.remoteMockRequests[0]
209 // Token is salted and moved from query to Authorization header.
210 c.Check(pr.URL.String(), check.Not(check.Matches), `.*api_token=.*`)
211 c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/7fd31b61f39c0e82a4155592163218272cedacdc")
214 func (s *FederationSuite) TestLocalTokenSalted(c *check.C) {
215 defer s.localServiceReturns404(c).Close()
216 for _, path := range []string{
217 // During the transition to the strongly typed
218 // controller implementation (#14287), workflows and
219 // collections test different code paths.
220 "/arvados/v1/workflows/" + strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1),
221 "/arvados/v1/collections/" + strings.Replace(arvadostest.UserAgreementCollection, "zzzzz-", "zmock-", 1),
223 c.Log("testing path ", path)
224 s.remoteMockRequests = nil
225 req := httptest.NewRequest("GET", path, nil)
226 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
227 s.testRequest(req).Result()
228 c.Assert(s.remoteMockRequests, check.HasLen, 1)
229 pr := s.remoteMockRequests[0]
230 // The salted token here has a "zzzzz-" UUID instead of a
231 // "ztest-" UUID because ztest's local database has the
232 // "zzzzz-" test fixtures. The "secret" part is HMAC(sha1,
233 // arvadostest.ActiveToken, "zmock") = "7fd3...".
234 c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/7fd31b61f39c0e82a4155592163218272cedacdc")
238 func (s *FederationSuite) TestRemoteTokenNotSalted(c *check.C) {
239 defer s.localServiceReturns404(c).Close()
240 // remoteToken can be any v1 token that doesn't appear in
242 remoteToken := "abcdef00000000000000000000000000000000000000000000"
244 for _, path := range []string{
245 // During the transition to the strongly typed
246 // controller implementation (#14287), workflows and
247 // collections test different code paths.
248 "/arvados/v1/workflows/" + strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1),
249 "/arvados/v1/collections/" + strings.Replace(arvadostest.UserAgreementCollection, "zzzzz-", "zmock-", 1),
251 c.Log("testing path ", path)
252 s.remoteMockRequests = nil
253 req := httptest.NewRequest("GET", path, nil)
254 req.Header.Set("Authorization", "Bearer "+remoteToken)
255 s.testRequest(req).Result()
256 c.Assert(s.remoteMockRequests, check.HasLen, 1)
257 pr := s.remoteMockRequests[0]
258 c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer "+remoteToken)
262 func (s *FederationSuite) TestWorkflowCRUD(c *check.C) {
263 var wf arvados.Workflow
265 req := httptest.NewRequest("POST", "/arvados/v1/workflows", strings.NewReader(url.Values{
266 "workflow": {`{"description": "TestCRUD"}`},
268 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
269 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
270 rec := httptest.NewRecorder()
271 s.remoteServer.Server.Handler.ServeHTTP(rec, req) // direct to remote -- can't proxy a create req because no uuid
273 s.checkResponseOK(c, resp)
274 json.NewDecoder(resp.Body).Decode(&wf)
277 req := httptest.NewRequest("DELETE", "/arvados/v1/workflows/"+wf.UUID, nil)
278 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
279 s.remoteServer.Server.Handler.ServeHTTP(httptest.NewRecorder(), req)
281 c.Check(wf.UUID, check.Not(check.Equals), "")
283 c.Assert(wf.ModifiedAt, check.NotNil)
284 c.Logf("wf.ModifiedAt: %v", wf.ModifiedAt)
285 c.Check(time.Since(*wf.ModifiedAt) < time.Minute, check.Equals, true)
287 for _, method := range []string{"PATCH", "PUT", "POST"} {
289 "workflow": {`{"description": "Updated with ` + method + `"}`},
291 if method == "POST" {
292 form["_method"] = []string{"PATCH"}
294 req := httptest.NewRequest(method, "/arvados/v1/workflows/"+wf.UUID, strings.NewReader(form.Encode()))
295 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
296 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
297 resp := s.testRequest(req).Result()
298 s.checkResponseOK(c, resp)
299 err := json.NewDecoder(resp.Body).Decode(&wf)
300 c.Check(err, check.IsNil)
302 c.Check(wf.Description, check.Equals, "Updated with "+method)
305 req := httptest.NewRequest("DELETE", "/arvados/v1/workflows/"+wf.UUID, nil)
306 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
307 resp := s.testRequest(req).Result()
308 s.checkResponseOK(c, resp)
309 err := json.NewDecoder(resp.Body).Decode(&wf)
310 c.Check(err, check.IsNil)
313 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+wf.UUID, nil)
314 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
315 resp := s.testRequest(req).Result()
316 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
320 func (s *FederationSuite) checkResponseOK(c *check.C, resp *http.Response) {
321 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
322 if resp.StatusCode != http.StatusOK {
323 body, err := ioutil.ReadAll(resp.Body)
324 c.Logf("... response body = %q, %v\n", body, err)
328 func (s *FederationSuite) checkJSONErrorMatches(c *check.C, resp *http.Response, re string) {
329 var jresp httpserver.ErrorResponse
330 err := json.NewDecoder(resp.Body).Decode(&jresp)
331 c.Check(err, check.IsNil)
332 c.Assert(jresp.Errors, check.HasLen, 1)
333 c.Check(jresp.Errors[0], check.Matches, re)
336 func (s *FederationSuite) localServiceHandler(c *check.C, h http.Handler) *httpserver.Server {
337 srv := &httpserver.Server{
342 c.Assert(srv.Start(), check.IsNil)
343 arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "http://"+srv.Addr)
347 func (s *FederationSuite) localServiceReturns404(c *check.C) *httpserver.Server {
348 return s.localServiceHandler(c, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
349 if req.URL.Path == "/arvados/v1/api_client_authorizations/current" {
350 if req.Header.Get("Authorization") == "Bearer "+arvadostest.ActiveToken {
351 json.NewEncoder(w).Encode(arvados.APIClientAuthorization{UUID: arvadostest.ActiveTokenUUID, APIToken: arvadostest.ActiveToken})
353 w.WriteHeader(http.StatusUnauthorized)
361 func (s *FederationSuite) TestGetLocalCollection(c *check.C) {
362 s.testHandler.Cluster.ClusterID = "zzzzz"
363 arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
367 req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementCollection, nil)
368 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
369 resp := s.testRequest(req).Result()
371 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
372 var col arvados.Collection
373 c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
374 c.Check(col.UUID, check.Equals, arvadostest.UserAgreementCollection)
375 c.Check(col.ManifestText, check.Matches,
376 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+A[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
379 // HTTP POST with _method=GET as a form parameter
381 req = httptest.NewRequest("POST", "/arvados/v1/collections/"+arvadostest.UserAgreementCollection, bytes.NewBufferString((url.Values{
384 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
385 req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
386 resp = s.testRequest(req).Result()
388 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
389 col = arvados.Collection{}
390 c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
391 c.Check(col.UUID, check.Equals, arvadostest.UserAgreementCollection)
392 c.Check(col.ManifestText, check.Matches,
393 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+A[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
397 func (s *FederationSuite) TestGetRemoteCollection(c *check.C) {
398 defer s.localServiceReturns404(c).Close()
400 req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementCollection, nil)
401 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
402 resp := s.testRequest(req).Result()
403 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
404 var col arvados.Collection
405 c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
406 c.Check(col.UUID, check.Equals, arvadostest.UserAgreementCollection)
407 c.Check(col.ManifestText, check.Matches,
408 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+Rzzzzz-[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
412 func (s *FederationSuite) TestGetRemoteCollectionError(c *check.C) {
413 defer s.localServiceReturns404(c).Close()
415 req := httptest.NewRequest("GET", "/arvados/v1/collections/zzzzz-4zz18-fakefakefakefak", nil)
416 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
417 resp := s.testRequest(req).Result()
418 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
421 func (s *FederationSuite) TestSignedLocatorPattern(c *check.C) {
422 // Confirm the regular expression identifies other groups of hints correctly
423 c.Check(keepclient.SignedLocatorRe.FindStringSubmatch(`6a4ff0499484c6c79c95cd8c566bd25f+249025+B1+C2+A05227438989d04712ea9ca1c91b556cef01d5cc7@5ba5405b+D3+E4`),
425 []string{"6a4ff0499484c6c79c95cd8c566bd25f+249025+B1+C2+A05227438989d04712ea9ca1c91b556cef01d5cc7@5ba5405b+D3+E4",
426 "6a4ff0499484c6c79c95cd8c566bd25f",
429 "+A05227438989d04712ea9ca1c91b556cef01d5cc7@5ba5405b",
430 "05227438989d04712ea9ca1c91b556cef01d5cc7", "5ba5405b",
434 func (s *FederationSuite) TestGetLocalCollectionByPDH(c *check.C) {
435 arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
437 req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementPDH, nil)
438 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
439 resp := s.testRequest(req).Result()
441 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
442 var col arvados.Collection
443 c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
444 c.Check(col.PortableDataHash, check.Equals, arvadostest.UserAgreementPDH)
445 c.Check(col.ManifestText, check.Matches,
446 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+A[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
450 func (s *FederationSuite) TestGetRemoteCollectionByPDH(c *check.C) {
451 defer s.localServiceReturns404(c).Close()
453 req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementPDH, nil)
454 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
455 resp := s.testRequest(req).Result()
457 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
459 var col arvados.Collection
460 c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
461 c.Check(col.PortableDataHash, check.Equals, arvadostest.UserAgreementPDH)
462 c.Check(col.ManifestText, check.Matches,
463 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+Rzzzzz-[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
467 func (s *FederationSuite) TestGetCollectionByPDHError(c *check.C) {
468 defer s.localServiceReturns404(c).Close()
470 req := httptest.NewRequest("GET", "/arvados/v1/collections/99999999999999999999999999999999+99", nil)
471 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
473 resp := s.testRequest(req).Result()
474 defer resp.Body.Close()
476 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
479 func (s *FederationSuite) TestGetCollectionByPDHErrorBadHash(c *check.C) {
480 defer s.localServiceReturns404(c).Close()
482 srv2 := &httpserver.Server{
484 Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
486 // Return a collection where the hash
487 // of the manifest text doesn't match
488 // PDH that was requested.
489 var col arvados.Collection
490 col.PortableDataHash = "99999999999999999999999999999999+99"
491 col.ManifestText = `. 6a4ff0499484c6c79c95cd8c566bd25f\+249025 0:249025:GNU_General_Public_License,_version_3.pdf
493 enc := json.NewEncoder(w)
499 c.Assert(srv2.Start(), check.IsNil)
502 // Direct zzzzz to service that returns a 200 result with a bogus manifest_text
503 s.testHandler.Cluster.RemoteClusters["zzzzz"] = arvados.RemoteCluster{
509 req := httptest.NewRequest("GET", "/arvados/v1/collections/99999999999999999999999999999999+99", nil)
510 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
512 resp := s.testRequest(req).Result()
513 defer resp.Body.Close()
515 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
518 func (s *FederationSuite) TestSaltedTokenGetCollectionByPDH(c *check.C) {
519 arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
521 req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementPDH, nil)
522 req.Header.Set("Authorization", "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/282d7d172b6cfdce364c5ed12ddf7417b2d00065")
523 resp := s.testRequest(req).Result()
525 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
526 var col arvados.Collection
527 c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
528 c.Check(col.PortableDataHash, check.Equals, arvadostest.UserAgreementPDH)
529 c.Check(col.ManifestText, check.Matches,
530 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+A[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
534 func (s *FederationSuite) TestSaltedTokenGetCollectionByPDHError(c *check.C) {
535 arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
537 req := httptest.NewRequest("GET", "/arvados/v1/collections/99999999999999999999999999999999+99", nil)
538 req.Header.Set("Authorization", "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/282d7d172b6cfdce364c5ed12ddf7417b2d00065")
539 resp := s.testRequest(req).Result()
541 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
544 func (s *FederationSuite) TestGetRemoteContainerRequest(c *check.C) {
545 defer s.localServiceReturns404(c).Close()
546 req := httptest.NewRequest("GET", "/arvados/v1/container_requests/"+arvadostest.QueuedContainerRequestUUID, nil)
547 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
548 resp := s.testRequest(req).Result()
549 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
550 var cr arvados.ContainerRequest
551 c.Check(json.NewDecoder(resp.Body).Decode(&cr), check.IsNil)
552 c.Check(cr.UUID, check.Equals, arvadostest.QueuedContainerRequestUUID)
553 c.Check(cr.Priority, check.Equals, 1)
556 func (s *FederationSuite) TestUpdateRemoteContainerRequest(c *check.C) {
557 defer s.localServiceReturns404(c).Close()
558 setPri := func(pri int) {
559 req := httptest.NewRequest("PATCH", "/arvados/v1/container_requests/"+arvadostest.QueuedContainerRequestUUID,
560 strings.NewReader(fmt.Sprintf(`{"container_request": {"priority": %d}}`, pri)))
561 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
562 req.Header.Set("Content-type", "application/json")
563 resp := s.testRequest(req).Result()
564 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
565 var cr arvados.ContainerRequest
566 c.Check(json.NewDecoder(resp.Body).Decode(&cr), check.IsNil)
567 c.Check(cr.UUID, check.Equals, arvadostest.QueuedContainerRequestUUID)
568 c.Check(cr.Priority, check.Equals, pri)
571 setPri(1) // Reset fixture so side effect doesn't break other tests.
574 func (s *FederationSuite) TestCreateRemoteContainerRequest(c *check.C) {
575 defer s.localServiceReturns404(c).Close()
576 // pass cluster_id via query parameter, this allows arvados-controller
577 // to avoid parsing the body
578 req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zzzzz",
580 "container_request": {
581 "name": "hello world",
582 "state": "Uncommitted",
584 "container_image": "123",
589 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
590 req.Header.Set("Content-type", "application/json")
591 resp := s.testRequest(req).Result()
592 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
593 var cr arvados.ContainerRequest
594 c.Check(json.NewDecoder(resp.Body).Decode(&cr), check.IsNil)
595 c.Check(cr.Name, check.Equals, "hello world")
596 c.Check(strings.HasPrefix(cr.UUID, "zzzzz-"), check.Equals, true)
599 func (s *FederationSuite) TestCreateRemoteContainerRequestCheckRuntimeToken(c *check.C) {
600 // Send request to zmock and check that outgoing request has
601 // runtime_token set with a new random v2 token.
603 defer s.localServiceReturns404(c).Close()
604 // pass cluster_id via query parameter, this allows arvados-controller
605 // to avoid parsing the body
606 req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock",
608 "container_request": {
609 "name": "hello world",
610 "state": "Uncommitted",
612 "container_image": "123",
617 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveTokenV2)
618 req.Header.Set("Content-type", "application/json")
620 arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
621 s.testHandler.Cluster.ClusterID = "zzzzz"
623 resp := s.testRequest(req).Result()
624 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
626 arvados.ContainerRequest `json:"container_request"`
628 c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil)
629 c.Check(strings.HasPrefix(cr.ContainerRequest.RuntimeToken, "v2/zzzzz-gj3su-"), check.Equals, true)
630 c.Check(cr.ContainerRequest.RuntimeToken, check.Not(check.Equals), arvadostest.ActiveTokenV2)
633 func (s *FederationSuite) TestCreateRemoteContainerRequestCheckSetRuntimeToken(c *check.C) {
634 // Send request to zmock and check that outgoing request has
635 // runtime_token set with the explicitly provided token.
637 defer s.localServiceReturns404(c).Close()
638 // pass cluster_id via query parameter, this allows arvados-controller
639 // to avoid parsing the body
640 req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock",
642 "container_request": {
643 "name": "hello world",
644 "state": "Uncommitted",
646 "container_image": "123",
648 "runtime_token": "xyz"
652 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
653 req.Header.Set("Content-type", "application/json")
654 resp := s.testRequest(req).Result()
655 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
657 arvados.ContainerRequest `json:"container_request"`
659 c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil)
660 c.Check(cr.ContainerRequest.RuntimeToken, check.Equals, "xyz")
663 func (s *FederationSuite) TestCreateRemoteContainerRequestRuntimeTokenFromAuth(c *check.C) {
664 // Send request to zmock and check that outgoing request has
665 // runtime_token set using the Auth token because the user is remote.
667 defer s.localServiceReturns404(c).Close()
668 // pass cluster_id via query parameter, this allows arvados-controller
669 // to avoid parsing the body
670 req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock",
672 "container_request": {
673 "name": "hello world",
674 "state": "Uncommitted",
676 "container_image": "123",
681 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveTokenV2+"/zzzzz-dz642-parentcontainer")
682 req.Header.Set("Content-type", "application/json")
683 resp := s.testRequest(req).Result()
684 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
686 arvados.ContainerRequest `json:"container_request"`
688 c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil)
689 c.Check(cr.ContainerRequest.RuntimeToken, check.Equals, arvadostest.ActiveTokenV2)
692 func (s *FederationSuite) TestCreateRemoteContainerRequestError(c *check.C) {
693 defer s.localServiceReturns404(c).Close()
694 // pass cluster_id via query parameter, this allows arvados-controller
695 // to avoid parsing the body
696 req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zz404",
698 "container_request": {
699 "name": "hello world",
700 "state": "Uncommitted",
702 "container_image": "123",
707 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
708 req.Header.Set("Content-type", "application/json")
709 resp := s.testRequest(req).Result()
710 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
713 func (s *FederationSuite) TestGetRemoteContainer(c *check.C) {
714 defer s.localServiceReturns404(c).Close()
715 req := httptest.NewRequest("GET", "/arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil)
716 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
717 resp := s.testRequest(req)
718 c.Check(resp.Code, check.Equals, http.StatusOK)
719 var cn arvados.Container
720 c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
721 c.Check(cn.UUID, check.Equals, arvadostest.QueuedContainerUUID)
724 func (s *FederationSuite) TestListRemoteContainer(c *check.C) {
725 defer s.localServiceReturns404(c).Close()
726 req := httptest.NewRequest("GET", "/arvados/v1/containers?count=none&filters="+
727 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v"]]]`, arvadostest.QueuedContainerUUID)), nil)
728 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
729 resp := s.testRequest(req).Result()
730 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
731 var cn arvados.ContainerList
732 c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
733 c.Assert(cn.Items, check.HasLen, 1)
734 c.Check(cn.Items[0].UUID, check.Equals, arvadostest.QueuedContainerUUID)
737 func (s *FederationSuite) TestListMultiRemoteContainers(c *check.C) {
738 defer s.localServiceHandler(c, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
739 bd, _ := ioutil.ReadAll(req.Body)
740 c.Check(string(bd), check.Equals, `_method=GET&count=none&filters=%5B%5B%22uuid%22%2C+%22in%22%2C+%5B%22zhome-xvhdp-cr5queuedcontnr%22%5D%5D%5D&select=%5B%22uuid%22%2C+%22command%22%5D`)
742 w.Write([]byte(`{"kind": "arvados#containerList", "items": [{"uuid": "zhome-xvhdp-cr5queuedcontnr", "command": ["abc"]}]}`))
744 req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&select=%s",
745 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
746 arvadostest.QueuedContainerUUID)),
747 url.QueryEscape(`["uuid", "command"]`)),
749 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
750 resp := s.testRequest(req).Result()
751 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
752 var cn arvados.ContainerList
753 c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
754 c.Check(cn.Items, check.HasLen, 2)
755 mp := make(map[string]arvados.Container)
756 for _, cr := range cn.Items {
759 c.Check(mp[arvadostest.QueuedContainerUUID].Command, check.DeepEquals, []string{"echo", "hello"})
760 c.Check(mp[arvadostest.QueuedContainerUUID].ContainerImage, check.Equals, "")
761 c.Check(mp["zhome-xvhdp-cr5queuedcontnr"].Command, check.DeepEquals, []string{"abc"})
762 c.Check(mp["zhome-xvhdp-cr5queuedcontnr"].ContainerImage, check.Equals, "")
765 func (s *FederationSuite) TestListMultiRemoteContainerError(c *check.C) {
766 defer s.localServiceReturns404(c).Close()
767 req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&select=%s",
768 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
769 arvadostest.QueuedContainerUUID)),
770 url.QueryEscape(`["uuid", "command"]`)),
772 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
773 resp := s.testRequest(req).Result()
774 c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway)
775 s.checkJSONErrorMatches(c, resp, `error fetching from zhome \(404 Not Found\): EOF`)
778 func (s *FederationSuite) TestListMultiRemoteContainersPaged(c *check.C) {
781 defer s.localServiceHandler(c, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
782 bd, _ := ioutil.ReadAll(req.Body)
784 c.Check(string(bd), check.Equals, `_method=GET&count=none&filters=%5B%5B%22uuid%22%2C+%22in%22%2C+%5B%22zhome-xvhdp-cr5queuedcontnr%22%2C%22zhome-xvhdp-cr6queuedcontnr%22%5D%5D%5D`)
786 w.Write([]byte(`{"kind": "arvados#containerList", "items": [{"uuid": "zhome-xvhdp-cr5queuedcontnr", "command": ["abc"]}]}`))
787 } else if callCount == 1 {
788 c.Check(string(bd), check.Equals, `_method=GET&count=none&filters=%5B%5B%22uuid%22%2C+%22in%22%2C+%5B%22zhome-xvhdp-cr6queuedcontnr%22%5D%5D%5D`)
790 w.Write([]byte(`{"kind": "arvados#containerList", "items": [{"uuid": "zhome-xvhdp-cr6queuedcontnr", "command": ["efg"]}]}`))
794 req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s",
795 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr", "zhome-xvhdp-cr6queuedcontnr"]]]`,
796 arvadostest.QueuedContainerUUID))),
798 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
799 resp := s.testRequest(req).Result()
800 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
801 c.Check(callCount, check.Equals, 2)
802 var cn arvados.ContainerList
803 c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
804 c.Check(cn.Items, check.HasLen, 3)
805 mp := make(map[string]arvados.Container)
806 for _, cr := range cn.Items {
809 c.Check(mp[arvadostest.QueuedContainerUUID].Command, check.DeepEquals, []string{"echo", "hello"})
810 c.Check(mp["zhome-xvhdp-cr5queuedcontnr"].Command, check.DeepEquals, []string{"abc"})
811 c.Check(mp["zhome-xvhdp-cr6queuedcontnr"].Command, check.DeepEquals, []string{"efg"})
814 func (s *FederationSuite) TestListMultiRemoteContainersMissing(c *check.C) {
817 defer s.localServiceHandler(c, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
818 bd, _ := ioutil.ReadAll(req.Body)
820 c.Check(string(bd), check.Equals, `_method=GET&count=none&filters=%5B%5B%22uuid%22%2C+%22in%22%2C+%5B%22zhome-xvhdp-cr5queuedcontnr%22%2C%22zhome-xvhdp-cr6queuedcontnr%22%5D%5D%5D`)
822 w.Write([]byte(`{"kind": "arvados#containerList", "items": [{"uuid": "zhome-xvhdp-cr6queuedcontnr", "command": ["efg"]}]}`))
823 } else if callCount == 1 {
824 c.Check(string(bd), check.Equals, `_method=GET&count=none&filters=%5B%5B%22uuid%22%2C+%22in%22%2C+%5B%22zhome-xvhdp-cr5queuedcontnr%22%5D%5D%5D`)
826 w.Write([]byte(`{"kind": "arvados#containerList", "items": []}`))
830 req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s",
831 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr", "zhome-xvhdp-cr6queuedcontnr"]]]`,
832 arvadostest.QueuedContainerUUID))),
834 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
835 resp := s.testRequest(req).Result()
836 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
837 c.Check(callCount, check.Equals, 2)
838 var cn arvados.ContainerList
839 c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
840 c.Check(cn.Items, check.HasLen, 2)
841 mp := make(map[string]arvados.Container)
842 for _, cr := range cn.Items {
845 c.Check(mp[arvadostest.QueuedContainerUUID].Command, check.DeepEquals, []string{"echo", "hello"})
846 c.Check(mp["zhome-xvhdp-cr6queuedcontnr"].Command, check.DeepEquals, []string{"efg"})
849 func (s *FederationSuite) TestListMultiRemoteContainerPageSizeError(c *check.C) {
850 s.testHandler.Cluster.API.MaxItemsPerResponse = 1
851 req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s",
852 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
853 arvadostest.QueuedContainerUUID))),
855 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
856 resp := s.testRequest(req).Result()
857 c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
858 s.checkJSONErrorMatches(c, resp, `Federated multi-object request for 2 objects which is more than max page size 1.`)
861 func (s *FederationSuite) TestListMultiRemoteContainerLimitError(c *check.C) {
862 req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&limit=1",
863 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
864 arvadostest.QueuedContainerUUID))),
866 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
867 resp := s.testRequest(req).Result()
868 c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
869 s.checkJSONErrorMatches(c, resp, `Federated multi-object may not provide 'limit', 'offset' or 'order'.`)
872 func (s *FederationSuite) TestListMultiRemoteContainerOffsetError(c *check.C) {
873 req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&offset=1",
874 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
875 arvadostest.QueuedContainerUUID))),
877 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
878 resp := s.testRequest(req).Result()
879 c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
880 s.checkJSONErrorMatches(c, resp, `Federated multi-object may not provide 'limit', 'offset' or 'order'.`)
883 func (s *FederationSuite) TestListMultiRemoteContainerOrderError(c *check.C) {
884 req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&order=uuid",
885 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
886 arvadostest.QueuedContainerUUID))),
888 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
889 resp := s.testRequest(req).Result()
890 c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
891 s.checkJSONErrorMatches(c, resp, `Federated multi-object may not provide 'limit', 'offset' or 'order'.`)
894 func (s *FederationSuite) TestListMultiRemoteContainerSelectError(c *check.C) {
895 req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&select=%s",
896 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
897 arvadostest.QueuedContainerUUID)),
898 url.QueryEscape(`["command"]`)),
900 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
901 resp := s.testRequest(req).Result()
902 c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
903 s.checkJSONErrorMatches(c, resp, `Federated multi-object request must include 'uuid' in 'select'`)