15944: Merge branch 'master' into 15944-arvbox-publicdev-fix
[arvados.git] / lib / controller / federation_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package controller
6
7 import (
8         "bytes"
9         "context"
10         "encoding/json"
11         "fmt"
12         "io"
13         "io/ioutil"
14         "net/http"
15         "net/http/httptest"
16         "net/url"
17         "os"
18         "strings"
19         "time"
20
21         "git.arvados.org/arvados.git/sdk/go/arvados"
22         "git.arvados.org/arvados.git/sdk/go/arvadostest"
23         "git.arvados.org/arvados.git/sdk/go/ctxlog"
24         "git.arvados.org/arvados.git/sdk/go/httpserver"
25         "git.arvados.org/arvados.git/sdk/go/keepclient"
26         "github.com/sirupsen/logrus"
27         check "gopkg.in/check.v1"
28 )
29
30 // Gocheck boilerplate
31 var _ = check.Suite(&FederationSuite{})
32
33 type FederationSuite struct {
34         log logrus.FieldLogger
35         // testServer and testHandler are the controller being tested,
36         // "zhome".
37         testServer  *httpserver.Server
38         testHandler *Handler
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
44         // object.
45         remoteMock         *httpserver.Server
46         remoteMockRequests []http.Request
47 }
48
49 func (s *FederationSuite) SetUpTest(c *check.C) {
50         s.log = ctxlog.TestLogger(c)
51
52         s.remoteServer = newServerFromIntegrationTestEnv(c)
53         c.Assert(s.remoteServer.Start(), check.IsNil)
54
55         s.remoteMock = newServerFromIntegrationTestEnv(c)
56         s.remoteMock.Server.Handler = http.HandlerFunc(s.remoteMockHandler)
57         c.Assert(s.remoteMock.Start(), check.IsNil)
58
59         cluster := &arvados.Cluster{
60                 ClusterID:        "zhome",
61                 PostgreSQL:       integrationTestCluster().PostgreSQL,
62                 ForceLegacyAPI14: forceLegacyAPI14,
63         }
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)))
74
75         cluster.RemoteClusters = map[string]arvados.RemoteCluster{
76                 "zzzzz": {
77                         Host:   s.remoteServer.Addr,
78                         Proxy:  true,
79                         Scheme: "http",
80                 },
81                 "zmock": {
82                         Host:   s.remoteMock.Addr,
83                         Proxy:  true,
84                         Scheme: "http",
85                 },
86                 "*": {
87                         Scheme: "https",
88                 },
89         }
90
91         c.Assert(s.testServer.Start(), check.IsNil)
92
93         s.remoteMockRequests = nil
94 }
95
96 func (s *FederationSuite) remoteMockHandler(w http.ResponseWriter, req *http.Request) {
97         b := &bytes.Buffer{}
98         io.Copy(b, req.Body)
99         req.Body.Close()
100         req.Body = ioutil.NopCloser(b)
101         s.remoteMockRequests = append(s.remoteMockRequests, *req)
102         // Repond 200 with a valid JSON object
103         fmt.Fprint(w, "{}")
104 }
105
106 func (s *FederationSuite) TearDownTest(c *check.C) {
107         if s.remoteServer != nil {
108                 s.remoteServer.Close()
109         }
110         if s.testServer != nil {
111                 s.testServer.Close()
112         }
113 }
114
115 func (s *FederationSuite) testRequest(req *http.Request) *httptest.ResponseRecorder {
116         resp := httptest.NewRecorder()
117         s.testServer.Server.Handler.ServeHTTP(resp, req)
118         return resp
119 }
120
121 func (s *FederationSuite) TestLocalRequest(c *check.C) {
122         req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zhome-", 1), nil)
123         resp := s.testRequest(req).Result()
124         s.checkHandledLocally(c, resp)
125 }
126
127 func (s *FederationSuite) checkHandledLocally(c *check.C, resp *http.Response) {
128         // Our "home" controller can't handle local requests because
129         // it doesn't have its own stub/test Rails API, so we rely on
130         // "connection refused" to indicate the controller tried to
131         // proxy the request to its local Rails API.
132         c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway)
133         s.checkJSONErrorMatches(c, resp, `.*connection refused`)
134 }
135
136 func (s *FederationSuite) TestNoAuth(c *check.C) {
137         req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
138         resp := s.testRequest(req).Result()
139         c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
140         s.checkJSONErrorMatches(c, resp, `Not logged in.*`)
141 }
142
143 func (s *FederationSuite) TestBadAuth(c *check.C) {
144         req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
145         req.Header.Set("Authorization", "Bearer aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
146         resp := s.testRequest(req).Result()
147         c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
148         s.checkJSONErrorMatches(c, resp, `Not logged in.*`)
149 }
150
151 func (s *FederationSuite) TestNoAccess(c *check.C) {
152         req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
153         req.Header.Set("Authorization", "Bearer "+arvadostest.SpectatorToken)
154         resp := s.testRequest(req).Result()
155         c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
156         s.checkJSONErrorMatches(c, resp, `.*not found.*`)
157 }
158
159 func (s *FederationSuite) TestGetUnknownRemote(c *check.C) {
160         req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zz404-", 1), nil)
161         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
162         resp := s.testRequest(req).Result()
163         c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
164         s.checkJSONErrorMatches(c, resp, `.*no proxy available for cluster zz404`)
165 }
166
167 func (s *FederationSuite) TestRemoteError(c *check.C) {
168         rc := s.testHandler.Cluster.RemoteClusters["zzzzz"]
169         rc.Scheme = "https"
170         s.testHandler.Cluster.RemoteClusters["zzzzz"] = rc
171
172         req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
173         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
174         resp := s.testRequest(req).Result()
175         c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway)
176         s.checkJSONErrorMatches(c, resp, `.*HTTP response to HTTPS client`)
177 }
178
179 func (s *FederationSuite) TestGetRemoteWorkflow(c *check.C) {
180         req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
181         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
182         resp := s.testRequest(req).Result()
183         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
184         var wf arvados.Workflow
185         c.Check(json.NewDecoder(resp.Body).Decode(&wf), check.IsNil)
186         c.Check(wf.UUID, check.Equals, arvadostest.WorkflowWithDefinitionYAMLUUID)
187         c.Check(wf.OwnerUUID, check.Equals, arvadostest.ActiveUserUUID)
188 }
189
190 func (s *FederationSuite) TestOptionsMethod(c *check.C) {
191         req := httptest.NewRequest("OPTIONS", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
192         req.Header.Set("Origin", "https://example.com")
193         resp := s.testRequest(req).Result()
194         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
195         body, err := ioutil.ReadAll(resp.Body)
196         c.Check(err, check.IsNil)
197         c.Check(string(body), check.Equals, "")
198         c.Check(resp.Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
199         for _, hdr := range []string{"Authorization", "Content-Type"} {
200                 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
201         }
202         for _, method := range []string{"GET", "HEAD", "PUT", "POST", "DELETE"} {
203                 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
204         }
205 }
206
207 func (s *FederationSuite) TestRemoteWithTokenInQuery(c *check.C) {
208         req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1)+"?api_token="+arvadostest.ActiveToken, nil)
209         s.testRequest(req).Result()
210         c.Assert(s.remoteMockRequests, check.HasLen, 1)
211         pr := s.remoteMockRequests[0]
212         // Token is salted and moved from query to Authorization header.
213         c.Check(pr.URL.String(), check.Not(check.Matches), `.*api_token=.*`)
214         c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/7fd31b61f39c0e82a4155592163218272cedacdc")
215 }
216
217 func (s *FederationSuite) TestLocalTokenSalted(c *check.C) {
218         defer s.localServiceReturns404(c).Close()
219         for _, path := range []string{
220                 // During the transition to the strongly typed
221                 // controller implementation (#14287), workflows and
222                 // collections test different code paths.
223                 "/arvados/v1/workflows/" + strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1),
224                 "/arvados/v1/collections/" + strings.Replace(arvadostest.UserAgreementCollection, "zzzzz-", "zmock-", 1),
225         } {
226                 c.Log("testing path ", path)
227                 s.remoteMockRequests = nil
228                 req := httptest.NewRequest("GET", path, nil)
229                 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
230                 s.testRequest(req).Result()
231                 c.Assert(s.remoteMockRequests, check.HasLen, 1)
232                 pr := s.remoteMockRequests[0]
233                 // The salted token here has a "zzzzz-" UUID instead of a
234                 // "ztest-" UUID because ztest's local database has the
235                 // "zzzzz-" test fixtures. The "secret" part is HMAC(sha1,
236                 // arvadostest.ActiveToken, "zmock") = "7fd3...".
237                 c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/7fd31b61f39c0e82a4155592163218272cedacdc")
238         }
239 }
240
241 func (s *FederationSuite) TestRemoteTokenNotSalted(c *check.C) {
242         defer s.localServiceReturns404(c).Close()
243         // remoteToken can be any v1 token that doesn't appear in
244         // ztest's local db.
245         remoteToken := "abcdef00000000000000000000000000000000000000000000"
246
247         for _, path := range []string{
248                 // During the transition to the strongly typed
249                 // controller implementation (#14287), workflows and
250                 // collections test different code paths.
251                 "/arvados/v1/workflows/" + strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1),
252                 "/arvados/v1/collections/" + strings.Replace(arvadostest.UserAgreementCollection, "zzzzz-", "zmock-", 1),
253         } {
254                 c.Log("testing path ", path)
255                 s.remoteMockRequests = nil
256                 req := httptest.NewRequest("GET", path, nil)
257                 req.Header.Set("Authorization", "Bearer "+remoteToken)
258                 s.testRequest(req).Result()
259                 c.Assert(s.remoteMockRequests, check.HasLen, 1)
260                 pr := s.remoteMockRequests[0]
261                 c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer "+remoteToken)
262         }
263 }
264
265 func (s *FederationSuite) TestWorkflowCRUD(c *check.C) {
266         var wf arvados.Workflow
267         {
268                 req := httptest.NewRequest("POST", "/arvados/v1/workflows", strings.NewReader(url.Values{
269                         "workflow": {`{"description": "TestCRUD"}`},
270                 }.Encode()))
271                 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
272                 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
273                 rec := httptest.NewRecorder()
274                 s.remoteServer.Server.Handler.ServeHTTP(rec, req) // direct to remote -- can't proxy a create req because no uuid
275                 resp := rec.Result()
276                 s.checkResponseOK(c, resp)
277                 json.NewDecoder(resp.Body).Decode(&wf)
278
279                 defer func() {
280                         req := httptest.NewRequest("DELETE", "/arvados/v1/workflows/"+wf.UUID, nil)
281                         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
282                         s.remoteServer.Server.Handler.ServeHTTP(httptest.NewRecorder(), req)
283                 }()
284                 c.Check(wf.UUID, check.Not(check.Equals), "")
285
286                 c.Assert(wf.ModifiedAt, check.NotNil)
287                 c.Logf("wf.ModifiedAt: %v", wf.ModifiedAt)
288                 c.Check(time.Since(*wf.ModifiedAt) < time.Minute, check.Equals, true)
289         }
290         for _, method := range []string{"PATCH", "PUT", "POST"} {
291                 form := url.Values{
292                         "workflow": {`{"description": "Updated with ` + method + `"}`},
293                 }
294                 if method == "POST" {
295                         form["_method"] = []string{"PATCH"}
296                 }
297                 req := httptest.NewRequest(method, "/arvados/v1/workflows/"+wf.UUID, strings.NewReader(form.Encode()))
298                 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
299                 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
300                 resp := s.testRequest(req).Result()
301                 s.checkResponseOK(c, resp)
302                 err := json.NewDecoder(resp.Body).Decode(&wf)
303                 c.Check(err, check.IsNil)
304
305                 c.Check(wf.Description, check.Equals, "Updated with "+method)
306         }
307         {
308                 req := httptest.NewRequest("DELETE", "/arvados/v1/workflows/"+wf.UUID, nil)
309                 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
310                 resp := s.testRequest(req).Result()
311                 s.checkResponseOK(c, resp)
312                 err := json.NewDecoder(resp.Body).Decode(&wf)
313                 c.Check(err, check.IsNil)
314         }
315         {
316                 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+wf.UUID, nil)
317                 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
318                 resp := s.testRequest(req).Result()
319                 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
320         }
321 }
322
323 func (s *FederationSuite) checkResponseOK(c *check.C, resp *http.Response) {
324         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
325         if resp.StatusCode != http.StatusOK {
326                 body, err := ioutil.ReadAll(resp.Body)
327                 c.Logf("... response body = %q, %v\n", body, err)
328         }
329 }
330
331 func (s *FederationSuite) checkJSONErrorMatches(c *check.C, resp *http.Response, re string) {
332         var jresp httpserver.ErrorResponse
333         err := json.NewDecoder(resp.Body).Decode(&jresp)
334         c.Check(err, check.IsNil)
335         c.Assert(jresp.Errors, check.HasLen, 1)
336         c.Check(jresp.Errors[0], check.Matches, re)
337 }
338
339 func (s *FederationSuite) localServiceHandler(c *check.C, h http.Handler) *httpserver.Server {
340         srv := &httpserver.Server{
341                 Server: http.Server{
342                         Handler: h,
343                 },
344         }
345         c.Assert(srv.Start(), check.IsNil)
346         arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "http://"+srv.Addr)
347         return srv
348 }
349
350 func (s *FederationSuite) localServiceReturns404(c *check.C) *httpserver.Server {
351         return s.localServiceHandler(c, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
352                 if req.URL.Path == "/arvados/v1/api_client_authorizations/current" {
353                         if req.Header.Get("Authorization") == "Bearer "+arvadostest.ActiveToken {
354                                 json.NewEncoder(w).Encode(arvados.APIClientAuthorization{UUID: arvadostest.ActiveTokenUUID, APIToken: arvadostest.ActiveToken})
355                         } else {
356                                 w.WriteHeader(http.StatusUnauthorized)
357                         }
358                 } else {
359                         w.WriteHeader(404)
360                 }
361         }))
362 }
363
364 func (s *FederationSuite) TestGetLocalCollection(c *check.C) {
365         s.testHandler.Cluster.ClusterID = "zzzzz"
366         arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
367
368         // HTTP GET
369
370         req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementCollection, nil)
371         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
372         resp := s.testRequest(req).Result()
373
374         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
375         var col arvados.Collection
376         c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
377         c.Check(col.UUID, check.Equals, arvadostest.UserAgreementCollection)
378         c.Check(col.ManifestText, check.Matches,
379                 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+A[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
380 `)
381
382         // HTTP POST with _method=GET as a form parameter
383
384         req = httptest.NewRequest("POST", "/arvados/v1/collections/"+arvadostest.UserAgreementCollection, bytes.NewBufferString((url.Values{
385                 "_method": {"GET"},
386         }).Encode()))
387         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
388         req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
389         resp = s.testRequest(req).Result()
390
391         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
392         col = arvados.Collection{}
393         c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
394         c.Check(col.UUID, check.Equals, arvadostest.UserAgreementCollection)
395         c.Check(col.ManifestText, check.Matches,
396                 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+A[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
397 `)
398 }
399
400 func (s *FederationSuite) TestGetRemoteCollection(c *check.C) {
401         defer s.localServiceReturns404(c).Close()
402
403         req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementCollection, nil)
404         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
405         resp := s.testRequest(req).Result()
406         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
407         var col arvados.Collection
408         c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
409         c.Check(col.UUID, check.Equals, arvadostest.UserAgreementCollection)
410         c.Check(col.ManifestText, check.Matches,
411                 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+Rzzzzz-[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
412 `)
413 }
414
415 func (s *FederationSuite) TestGetRemoteCollectionError(c *check.C) {
416         defer s.localServiceReturns404(c).Close()
417
418         req := httptest.NewRequest("GET", "/arvados/v1/collections/zzzzz-4zz18-fakefakefakefak", nil)
419         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
420         resp := s.testRequest(req).Result()
421         c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
422 }
423
424 func (s *FederationSuite) TestSignedLocatorPattern(c *check.C) {
425         // Confirm the regular expression identifies other groups of hints correctly
426         c.Check(keepclient.SignedLocatorRe.FindStringSubmatch(`6a4ff0499484c6c79c95cd8c566bd25f+249025+B1+C2+A05227438989d04712ea9ca1c91b556cef01d5cc7@5ba5405b+D3+E4`),
427                 check.DeepEquals,
428                 []string{"6a4ff0499484c6c79c95cd8c566bd25f+249025+B1+C2+A05227438989d04712ea9ca1c91b556cef01d5cc7@5ba5405b+D3+E4",
429                         "6a4ff0499484c6c79c95cd8c566bd25f",
430                         "+249025",
431                         "+B1+C2", "+C2",
432                         "+A05227438989d04712ea9ca1c91b556cef01d5cc7@5ba5405b",
433                         "05227438989d04712ea9ca1c91b556cef01d5cc7", "5ba5405b",
434                         "+D3+E4", "+E4"})
435 }
436
437 func (s *FederationSuite) TestGetLocalCollectionByPDH(c *check.C) {
438         arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
439
440         req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementPDH, nil)
441         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
442         resp := s.testRequest(req).Result()
443
444         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
445         var col arvados.Collection
446         c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
447         c.Check(col.PortableDataHash, check.Equals, arvadostest.UserAgreementPDH)
448         c.Check(col.ManifestText, check.Matches,
449                 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+A[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
450 `)
451 }
452
453 func (s *FederationSuite) TestGetRemoteCollectionByPDH(c *check.C) {
454         defer s.localServiceReturns404(c).Close()
455
456         req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementPDH, nil)
457         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
458         resp := s.testRequest(req).Result()
459
460         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
461
462         var col arvados.Collection
463         c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
464         c.Check(col.PortableDataHash, check.Equals, arvadostest.UserAgreementPDH)
465         c.Check(col.ManifestText, check.Matches,
466                 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+Rzzzzz-[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
467 `)
468 }
469
470 func (s *FederationSuite) TestGetCollectionByPDHError(c *check.C) {
471         defer s.localServiceReturns404(c).Close()
472
473         // zmock's normal response (200 with an empty body) would
474         // change the outcome from 404 to 502
475         delete(s.testHandler.Cluster.RemoteClusters, "zmock")
476
477         req := httptest.NewRequest("GET", "/arvados/v1/collections/99999999999999999999999999999999+99", nil)
478         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
479
480         resp := s.testRequest(req).Result()
481         defer resp.Body.Close()
482
483         c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
484 }
485
486 func (s *FederationSuite) TestGetCollectionByPDHErrorBadHash(c *check.C) {
487         defer s.localServiceReturns404(c).Close()
488
489         // zmock's normal response (200 with an empty body) would
490         // change the outcome
491         delete(s.testHandler.Cluster.RemoteClusters, "zmock")
492
493         srv2 := &httpserver.Server{
494                 Server: http.Server{
495                         Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
496                                 w.WriteHeader(200)
497                                 // Return a collection where the hash
498                                 // of the manifest text doesn't match
499                                 // PDH that was requested.
500                                 var col arvados.Collection
501                                 col.PortableDataHash = "99999999999999999999999999999999+99"
502                                 col.ManifestText = `. 6a4ff0499484c6c79c95cd8c566bd25f\+249025 0:249025:GNU_General_Public_License,_version_3.pdf
503 `
504                                 enc := json.NewEncoder(w)
505                                 enc.Encode(col)
506                         }),
507                 },
508         }
509
510         c.Assert(srv2.Start(), check.IsNil)
511         defer srv2.Close()
512
513         // Direct zzzzz to service that returns a 200 result with a bogus manifest_text
514         s.testHandler.Cluster.RemoteClusters["zzzzz"] = arvados.RemoteCluster{
515                 Host:   srv2.Addr,
516                 Proxy:  true,
517                 Scheme: "http",
518         }
519
520         req := httptest.NewRequest("GET", "/arvados/v1/collections/99999999999999999999999999999999+99", nil)
521         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
522
523         resp := s.testRequest(req).Result()
524         defer resp.Body.Close()
525
526         c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway)
527 }
528
529 func (s *FederationSuite) TestSaltedTokenGetCollectionByPDH(c *check.C) {
530         arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
531
532         req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementPDH, nil)
533         req.Header.Set("Authorization", "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/282d7d172b6cfdce364c5ed12ddf7417b2d00065")
534         resp := s.testRequest(req).Result()
535
536         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
537         var col arvados.Collection
538         c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
539         c.Check(col.PortableDataHash, check.Equals, arvadostest.UserAgreementPDH)
540         c.Check(col.ManifestText, check.Matches,
541                 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+A[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
542 `)
543 }
544
545 func (s *FederationSuite) TestSaltedTokenGetCollectionByPDHError(c *check.C) {
546         arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
547
548         // zmock's normal response (200 with an empty body) would
549         // change the outcome
550         delete(s.testHandler.Cluster.RemoteClusters, "zmock")
551
552         req := httptest.NewRequest("GET", "/arvados/v1/collections/99999999999999999999999999999999+99", nil)
553         req.Header.Set("Authorization", "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/282d7d172b6cfdce364c5ed12ddf7417b2d00065")
554         resp := s.testRequest(req).Result()
555
556         c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
557 }
558
559 func (s *FederationSuite) TestGetRemoteContainerRequest(c *check.C) {
560         defer s.localServiceReturns404(c).Close()
561         req := httptest.NewRequest("GET", "/arvados/v1/container_requests/"+arvadostest.QueuedContainerRequestUUID, nil)
562         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
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, 1)
569 }
570
571 func (s *FederationSuite) TestUpdateRemoteContainerRequest(c *check.C) {
572         defer s.localServiceReturns404(c).Close()
573         setPri := func(pri int) {
574                 req := httptest.NewRequest("PATCH", "/arvados/v1/container_requests/"+arvadostest.QueuedContainerRequestUUID,
575                         strings.NewReader(fmt.Sprintf(`{"container_request": {"priority": %d}}`, pri)))
576                 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
577                 req.Header.Set("Content-type", "application/json")
578                 resp := s.testRequest(req).Result()
579                 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
580                 var cr arvados.ContainerRequest
581                 c.Check(json.NewDecoder(resp.Body).Decode(&cr), check.IsNil)
582                 c.Check(cr.UUID, check.Equals, arvadostest.QueuedContainerRequestUUID)
583                 c.Check(cr.Priority, check.Equals, pri)
584         }
585         setPri(696)
586         setPri(1) // Reset fixture so side effect doesn't break other tests.
587 }
588
589 func (s *FederationSuite) TestCreateContainerRequestBadToken(c *check.C) {
590         defer s.localServiceReturns404(c).Close()
591         // pass cluster_id via query parameter, this allows arvados-controller
592         // to avoid parsing the body
593         req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zzzzz",
594                 strings.NewReader(`{"container_request":{}}`))
595         req.Header.Set("Authorization", "Bearer abcdefg")
596         req.Header.Set("Content-type", "application/json")
597         resp := s.testRequest(req).Result()
598         c.Check(resp.StatusCode, check.Equals, http.StatusForbidden)
599         var e map[string][]string
600         c.Check(json.NewDecoder(resp.Body).Decode(&e), check.IsNil)
601         c.Check(e["errors"], check.DeepEquals, []string{"invalid API token"})
602 }
603
604 func (s *FederationSuite) TestCreateRemoteContainerRequest(c *check.C) {
605         defer s.localServiceReturns404(c).Close()
606         // pass cluster_id via query parameter, this allows arvados-controller
607         // to avoid parsing the body
608         req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zzzzz",
609                 strings.NewReader(`{
610   "container_request": {
611     "name": "hello world",
612     "state": "Uncommitted",
613     "output_path": "/",
614     "container_image": "123",
615     "command": ["abc"]
616   }
617 }
618 `))
619         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
620         req.Header.Set("Content-type", "application/json")
621         resp := s.testRequest(req).Result()
622         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
623         var cr arvados.ContainerRequest
624         c.Check(json.NewDecoder(resp.Body).Decode(&cr), check.IsNil)
625         c.Check(cr.Name, check.Equals, "hello world")
626         c.Check(strings.HasPrefix(cr.UUID, "zzzzz-"), check.Equals, true)
627 }
628
629 func (s *FederationSuite) TestCreateRemoteContainerRequestCheckRuntimeToken(c *check.C) {
630         // Send request to zmock and check that outgoing request has
631         // runtime_token set with a new random v2 token.
632
633         defer s.localServiceReturns404(c).Close()
634         // pass cluster_id via query parameter, this allows arvados-controller
635         // to avoid parsing the body
636         req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock",
637                 strings.NewReader(`{
638   "container_request": {
639     "name": "hello world",
640     "state": "Uncommitted",
641     "output_path": "/",
642     "container_image": "123",
643     "command": ["abc"]
644   }
645 }
646 `))
647         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveTokenV2)
648         req.Header.Set("Content-type", "application/json")
649
650         arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
651         s.testHandler.Cluster.ClusterID = "zzzzz"
652
653         resp := s.testRequest(req).Result()
654         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
655         var cr struct {
656                 arvados.ContainerRequest `json:"container_request"`
657         }
658         c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil)
659         c.Check(strings.HasPrefix(cr.ContainerRequest.RuntimeToken, "v2/zzzzz-gj3su-"), check.Equals, true)
660         c.Check(cr.ContainerRequest.RuntimeToken, check.Not(check.Equals), arvadostest.ActiveTokenV2)
661 }
662
663 func (s *FederationSuite) TestCreateRemoteContainerRequestCheckSetRuntimeToken(c *check.C) {
664         // Send request to zmock and check that outgoing request has
665         // runtime_token set with the explicitly provided token.
666
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",
671                 strings.NewReader(`{
672   "container_request": {
673     "name": "hello world",
674     "state": "Uncommitted",
675     "output_path": "/",
676     "container_image": "123",
677     "command": ["abc"],
678     "runtime_token": "xyz"
679   }
680 }
681 `))
682         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
683         req.Header.Set("Content-type", "application/json")
684         resp := s.testRequest(req).Result()
685         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
686         var cr struct {
687                 arvados.ContainerRequest `json:"container_request"`
688         }
689         c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil)
690         c.Check(cr.ContainerRequest.RuntimeToken, check.Equals, "xyz")
691 }
692
693 func (s *FederationSuite) TestCreateRemoteContainerRequestRuntimeTokenFromAuth(c *check.C) {
694         // Send request to zmock and check that outgoing request has
695         // runtime_token set using the Auth token because the user is remote.
696
697         defer s.localServiceReturns404(c).Close()
698         // pass cluster_id via query parameter, this allows arvados-controller
699         // to avoid parsing the body
700         req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock",
701                 strings.NewReader(`{
702   "container_request": {
703     "name": "hello world",
704     "state": "Uncommitted",
705     "output_path": "/",
706     "container_image": "123",
707     "command": ["abc"]
708   }
709 }
710 `))
711         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveTokenV2+"/zzzzz-dz642-parentcontainer")
712         req.Header.Set("Content-type", "application/json")
713         resp := s.testRequest(req).Result()
714         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
715         var cr struct {
716                 arvados.ContainerRequest `json:"container_request"`
717         }
718         c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil)
719         c.Check(cr.ContainerRequest.RuntimeToken, check.Equals, arvadostest.ActiveTokenV2)
720 }
721
722 func (s *FederationSuite) TestCreateRemoteContainerRequestError(c *check.C) {
723         defer s.localServiceReturns404(c).Close()
724         // pass cluster_id via query parameter, this allows arvados-controller
725         // to avoid parsing the body
726         req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zz404",
727                 strings.NewReader(`{
728   "container_request": {
729     "name": "hello world",
730     "state": "Uncommitted",
731     "output_path": "/",
732     "container_image": "123",
733     "command": ["abc"]
734   }
735 }
736 `))
737         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
738         req.Header.Set("Content-type", "application/json")
739         resp := s.testRequest(req).Result()
740         c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
741 }
742
743 func (s *FederationSuite) TestGetRemoteContainer(c *check.C) {
744         defer s.localServiceReturns404(c).Close()
745         req := httptest.NewRequest("GET", "/arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil)
746         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
747         resp := s.testRequest(req)
748         c.Check(resp.Code, check.Equals, http.StatusOK)
749         var cn arvados.Container
750         c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
751         c.Check(cn.UUID, check.Equals, arvadostest.QueuedContainerUUID)
752 }
753
754 func (s *FederationSuite) TestListRemoteContainer(c *check.C) {
755         defer s.localServiceReturns404(c).Close()
756         req := httptest.NewRequest("GET", "/arvados/v1/containers?count=none&filters="+
757                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v"]]]`, arvadostest.QueuedContainerUUID)), nil)
758         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
759         resp := s.testRequest(req).Result()
760         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
761         var cn arvados.ContainerList
762         c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
763         c.Assert(cn.Items, check.HasLen, 1)
764         c.Check(cn.Items[0].UUID, check.Equals, arvadostest.QueuedContainerUUID)
765 }
766
767 func (s *FederationSuite) TestListMultiRemoteContainers(c *check.C) {
768         defer s.localServiceHandler(c, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
769                 bd, _ := ioutil.ReadAll(req.Body)
770                 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`)
771                 w.WriteHeader(200)
772                 w.Write([]byte(`{"kind": "arvados#containerList", "items": [{"uuid": "zhome-xvhdp-cr5queuedcontnr", "command": ["abc"]}]}`))
773         })).Close()
774         req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&select=%s",
775                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
776                         arvadostest.QueuedContainerUUID)),
777                 url.QueryEscape(`["uuid", "command"]`)),
778                 nil)
779         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
780         resp := s.testRequest(req).Result()
781         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
782         var cn arvados.ContainerList
783         c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
784         c.Check(cn.Items, check.HasLen, 2)
785         mp := make(map[string]arvados.Container)
786         for _, cr := range cn.Items {
787                 mp[cr.UUID] = cr
788         }
789         c.Check(mp[arvadostest.QueuedContainerUUID].Command, check.DeepEquals, []string{"echo", "hello"})
790         c.Check(mp[arvadostest.QueuedContainerUUID].ContainerImage, check.Equals, "")
791         c.Check(mp["zhome-xvhdp-cr5queuedcontnr"].Command, check.DeepEquals, []string{"abc"})
792         c.Check(mp["zhome-xvhdp-cr5queuedcontnr"].ContainerImage, check.Equals, "")
793 }
794
795 func (s *FederationSuite) TestListMultiRemoteContainerError(c *check.C) {
796         defer s.localServiceReturns404(c).Close()
797         req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&select=%s",
798                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
799                         arvadostest.QueuedContainerUUID)),
800                 url.QueryEscape(`["uuid", "command"]`)),
801                 nil)
802         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
803         resp := s.testRequest(req).Result()
804         c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway)
805         s.checkJSONErrorMatches(c, resp, `error fetching from zhome \(404 Not Found\): EOF`)
806 }
807
808 func (s *FederationSuite) TestListMultiRemoteContainersPaged(c *check.C) {
809
810         callCount := 0
811         defer s.localServiceHandler(c, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
812                 bd, _ := ioutil.ReadAll(req.Body)
813                 if callCount == 0 {
814                         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`)
815                         w.WriteHeader(200)
816                         w.Write([]byte(`{"kind": "arvados#containerList", "items": [{"uuid": "zhome-xvhdp-cr5queuedcontnr", "command": ["abc"]}]}`))
817                 } else if callCount == 1 {
818                         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`)
819                         w.WriteHeader(200)
820                         w.Write([]byte(`{"kind": "arvados#containerList", "items": [{"uuid": "zhome-xvhdp-cr6queuedcontnr", "command": ["efg"]}]}`))
821                 }
822                 callCount += 1
823         })).Close()
824         req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s",
825                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr", "zhome-xvhdp-cr6queuedcontnr"]]]`,
826                         arvadostest.QueuedContainerUUID))),
827                 nil)
828         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
829         resp := s.testRequest(req).Result()
830         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
831         c.Check(callCount, check.Equals, 2)
832         var cn arvados.ContainerList
833         c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
834         c.Check(cn.Items, check.HasLen, 3)
835         mp := make(map[string]arvados.Container)
836         for _, cr := range cn.Items {
837                 mp[cr.UUID] = cr
838         }
839         c.Check(mp[arvadostest.QueuedContainerUUID].Command, check.DeepEquals, []string{"echo", "hello"})
840         c.Check(mp["zhome-xvhdp-cr5queuedcontnr"].Command, check.DeepEquals, []string{"abc"})
841         c.Check(mp["zhome-xvhdp-cr6queuedcontnr"].Command, check.DeepEquals, []string{"efg"})
842 }
843
844 func (s *FederationSuite) TestListMultiRemoteContainersMissing(c *check.C) {
845
846         callCount := 0
847         defer s.localServiceHandler(c, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
848                 bd, _ := ioutil.ReadAll(req.Body)
849                 if callCount == 0 {
850                         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`)
851                         w.WriteHeader(200)
852                         w.Write([]byte(`{"kind": "arvados#containerList", "items": [{"uuid": "zhome-xvhdp-cr6queuedcontnr", "command": ["efg"]}]}`))
853                 } else if callCount == 1 {
854                         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`)
855                         w.WriteHeader(200)
856                         w.Write([]byte(`{"kind": "arvados#containerList", "items": []}`))
857                 }
858                 callCount += 1
859         })).Close()
860         req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s",
861                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr", "zhome-xvhdp-cr6queuedcontnr"]]]`,
862                         arvadostest.QueuedContainerUUID))),
863                 nil)
864         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
865         resp := s.testRequest(req).Result()
866         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
867         c.Check(callCount, check.Equals, 2)
868         var cn arvados.ContainerList
869         c.Check(json.NewDecoder(resp.Body).Decode(&cn), check.IsNil)
870         c.Check(cn.Items, check.HasLen, 2)
871         mp := make(map[string]arvados.Container)
872         for _, cr := range cn.Items {
873                 mp[cr.UUID] = cr
874         }
875         c.Check(mp[arvadostest.QueuedContainerUUID].Command, check.DeepEquals, []string{"echo", "hello"})
876         c.Check(mp["zhome-xvhdp-cr6queuedcontnr"].Command, check.DeepEquals, []string{"efg"})
877 }
878
879 func (s *FederationSuite) TestListMultiRemoteContainerPageSizeError(c *check.C) {
880         s.testHandler.Cluster.API.MaxItemsPerResponse = 1
881         req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s",
882                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
883                         arvadostest.QueuedContainerUUID))),
884                 nil)
885         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
886         resp := s.testRequest(req).Result()
887         c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
888         s.checkJSONErrorMatches(c, resp, `Federated multi-object request for 2 objects which is more than max page size 1.`)
889 }
890
891 func (s *FederationSuite) TestListMultiRemoteContainerLimitError(c *check.C) {
892         req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&limit=1",
893                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
894                         arvadostest.QueuedContainerUUID))),
895                 nil)
896         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
897         resp := s.testRequest(req).Result()
898         c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
899         s.checkJSONErrorMatches(c, resp, `Federated multi-object may not provide 'limit', 'offset' or 'order'.`)
900 }
901
902 func (s *FederationSuite) TestListMultiRemoteContainerOffsetError(c *check.C) {
903         req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&offset=1",
904                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
905                         arvadostest.QueuedContainerUUID))),
906                 nil)
907         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
908         resp := s.testRequest(req).Result()
909         c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
910         s.checkJSONErrorMatches(c, resp, `Federated multi-object may not provide 'limit', 'offset' or 'order'.`)
911 }
912
913 func (s *FederationSuite) TestListMultiRemoteContainerOrderError(c *check.C) {
914         req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&order=uuid",
915                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
916                         arvadostest.QueuedContainerUUID))),
917                 nil)
918         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
919         resp := s.testRequest(req).Result()
920         c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
921         s.checkJSONErrorMatches(c, resp, `Federated multi-object may not provide 'limit', 'offset' or 'order'.`)
922 }
923
924 func (s *FederationSuite) TestListMultiRemoteContainerSelectError(c *check.C) {
925         req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s&select=%s",
926                 url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr"]]]`,
927                         arvadostest.QueuedContainerUUID)),
928                 url.QueryEscape(`["command"]`)),
929                 nil)
930         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
931         resp := s.testRequest(req).Result()
932         c.Check(resp.StatusCode, check.Equals, http.StatusBadRequest)
933         s.checkJSONErrorMatches(c, resp, `Federated multi-object request must include 'uuid' in 'select'`)
934 }