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