1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/lib/controller/router"
16 "git.curoverse.com/arvados.git/lib/controller/rpc"
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
19 "git.curoverse.com/arvados.git/sdk/go/auth"
20 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
21 "git.curoverse.com/arvados.git/sdk/go/httpserver"
22 check "gopkg.in/check.v1"
25 // Gocheck boilerplate
26 func Test(t *testing.T) {
31 _ = check.Suite(&FederationSuite{})
32 _ = check.Suite(&CollectionListSuite{})
35 type FederationSuite struct {
36 cluster *arvados.Cluster
41 func (s *FederationSuite) SetUpTest(c *check.C) {
42 s.cluster = &arvados.Cluster{
44 RemoteClusters: map[string]arvados.RemoteCluster{
45 "aaaaa": arvados.RemoteCluster{
46 Host: os.Getenv("ARVADOS_API_HOST"),
50 arvadostest.SetServiceURL(&s.cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
51 s.cluster.TLS.Insecure = true
52 s.cluster.API.MaxItemsPerResponse = 3
54 ctx := context.Background()
55 ctx = ctxlog.Context(ctx, ctxlog.TestLogger(c))
56 ctx = auth.NewContext(ctx, &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
59 s.fed = New(s.cluster)
62 func (s *FederationSuite) addDirectRemote(c *check.C, id string, backend arvados.API) {
63 s.cluster.RemoteClusters[id] = arvados.RemoteCluster{
64 Host: "in-process.local",
66 s.fed.remotes[id] = backend
69 func (s *FederationSuite) addHTTPRemote(c *check.C, id string, backend arvados.API) {
70 srv := httpserver.Server{Addr: ":"}
71 srv.Handler = router.New(backend)
72 c.Check(srv.Start(), check.IsNil)
73 s.cluster.RemoteClusters[id] = arvados.RemoteCluster{
77 s.fed.remotes[id] = rpc.NewConn(id, &url.URL{Scheme: "http", Host: srv.Addr}, true, saltedTokenProvider(s.fed.local, id))
80 type collectionLister struct {
82 ItemsToReturn []arvados.Collection
86 func (cl *collectionLister) matchFilters(c arvados.Collection, filters []arvados.Filter) bool {
88 for _, f := range filters {
89 if f.Attr == "uuid" && f.Operator == "=" {
90 s, ok := f.Operand.(string)
91 if ok && s == c.UUID {
94 } else if f.Attr == "uuid" && f.Operator == "in" {
95 if operand, ok := f.Operand.([]string); ok {
96 for _, s := range operand {
101 } else if operand, ok := f.Operand.([]interface{}); ok {
102 for _, s := range operand {
103 if s, ok := s.(string); ok && s == c.UUID {
114 func (cl *collectionLister) CollectionList(ctx context.Context, options arvados.ListOptions) (resp arvados.CollectionList, _ error) {
115 cl.APIStub.CollectionList(ctx, options)
116 for _, c := range cl.ItemsToReturn {
117 if cl.MaxPageSize > 0 && len(resp.Items) >= cl.MaxPageSize {
120 if cl.matchFilters(c, options.Filters) {
121 resp.Items = append(resp.Items, c)
127 type CollectionListSuite struct {
129 ids []string // aaaaa, bbbbb, ccccc
130 uuids [][]string // [[aa-*, aa-*, aa-*], [bb-*, bb-*, ...], ...]
131 backends []*collectionLister
134 func (s *CollectionListSuite) SetUpTest(c *check.C) {
135 s.FederationSuite.SetUpTest(c)
140 for i, id := range []string{"aaaaa", "bbbbb", "ccccc"} {
141 cl := &collectionLister{}
142 s.ids = append(s.ids, id)
143 s.uuids = append(s.uuids, nil)
144 for j := 0; j < 5; j++ {
145 uuid := fmt.Sprintf("%s-4zz18-%s%010d", id, id, j)
146 s.uuids[i] = append(s.uuids[i], uuid)
147 cl.ItemsToReturn = append(cl.ItemsToReturn, arvados.Collection{
151 s.backends = append(s.backends, cl)
155 // call some backends directly via API
156 s.addDirectRemote(c, id, cl)
158 // call some backends through rpc->router->API
159 // to ensure nothing is lost in translation
160 s.addHTTPRemote(c, id, cl)
165 type listTrial struct {
170 filters []arvados.Filter
172 expectCalls []int // number of API calls to backends
176 func (s *CollectionListSuite) TestCollectionListOneLocal(c *check.C) {
180 filters: []arvados.Filter{{"uuid", "=", s.uuids[0][0]}},
181 expectUUIDs: []string{s.uuids[0][0]},
182 expectCalls: []int{1, 0, 0},
186 func (s *CollectionListSuite) TestCollectionListOneRemote(c *check.C) {
190 filters: []arvados.Filter{{"uuid", "=", s.uuids[1][0]}},
191 expectUUIDs: []string{s.uuids[1][0]},
192 expectCalls: []int{0, 1, 0},
196 func (s *CollectionListSuite) TestCollectionListOneLocalUsingInOperator(c *check.C) {
200 filters: []arvados.Filter{{"uuid", "in", []string{s.uuids[0][0]}}},
201 expectUUIDs: []string{s.uuids[0][0]},
202 expectCalls: []int{1, 0, 0},
206 func (s *CollectionListSuite) TestCollectionListOneRemoteUsingInOperator(c *check.C) {
210 filters: []arvados.Filter{{"uuid", "in", []string{s.uuids[1][1]}}},
211 expectUUIDs: []string{s.uuids[1][1]},
212 expectCalls: []int{0, 1, 0},
216 func (s *CollectionListSuite) TestCollectionListOneLocalOneRemote(c *check.C) {
220 filters: []arvados.Filter{{"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0]}}},
221 expectUUIDs: []string{s.uuids[0][0], s.uuids[1][0]},
222 expectCalls: []int{1, 1, 0},
226 func (s *CollectionListSuite) TestCollectionListTwoRemotes(c *check.C) {
230 filters: []arvados.Filter{{"uuid", "in", []string{s.uuids[2][0], s.uuids[1][0]}}},
231 expectUUIDs: []string{s.uuids[1][0], s.uuids[2][0]},
232 expectCalls: []int{0, 1, 1},
236 func (s *CollectionListSuite) TestCollectionListSatisfyAllFilters(c *check.C) {
237 s.cluster.API.MaxItemsPerResponse = 2
241 filters: []arvados.Filter{
242 {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][1], s.uuids[2][0], s.uuids[2][1], s.uuids[2][2]}},
243 {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][2], s.uuids[2][1]}},
245 expectUUIDs: []string{s.uuids[0][0], s.uuids[2][1]},
246 expectCalls: []int{1, 0, 1},
250 func (s *CollectionListSuite) TestCollectionListEmptySet(c *check.C) {
254 filters: []arvados.Filter{{"uuid", "in", []string{}}},
255 expectUUIDs: []string{},
256 expectCalls: []int{0, 0, 0},
260 func (s *CollectionListSuite) TestCollectionListUnmatchableUUID(c *check.C) {
264 filters: []arvados.Filter{
265 {"uuid", "in", []string{s.uuids[0][0], "abcdefg"}},
266 {"uuid", "in", []string{s.uuids[0][0], "bbbbb-4zz18-bogus"}},
267 {"uuid", "in", []string{s.uuids[0][0], "bogus-4zz18-bogus"}},
269 expectUUIDs: []string{s.uuids[0][0]},
270 expectCalls: []int{1, 0, 0},
274 func (s *CollectionListSuite) TestCollectionListMultiPage(c *check.C) {
275 for i := range s.backends {
276 s.uuids[i] = s.uuids[i][:3]
277 s.backends[i].ItemsToReturn = s.backends[i].ItemsToReturn[:3]
279 s.cluster.API.MaxItemsPerResponse = 9
280 for _, stub := range s.backends {
283 allUUIDs := append(append(append([]string(nil), s.uuids[0]...), s.uuids[1]...), s.uuids[2]...)
287 filters: []arvados.Filter{{"uuid", "in", append([]string(nil), allUUIDs...)}},
288 expectUUIDs: allUUIDs,
289 expectCalls: []int{2, 2, 2},
293 func (s *CollectionListSuite) TestCollectionListMultiSiteExtraFilters(c *check.C) {
294 // not [yet] supported
298 filters: []arvados.Filter{
299 {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0]}},
300 {"uuid", "is_a", "teapot"},
302 expectCalls: []int{0, 0, 0},
303 expectStatus: http.StatusBadRequest,
307 func (s *CollectionListSuite) TestCollectionListMultiSiteWithCount(c *check.C) {
308 for _, count := range []string{"", "exact"} {
312 filters: []arvados.Filter{
313 {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0]}},
314 {"uuid", "is_a", "teapot"},
316 expectCalls: []int{0, 0, 0},
317 expectStatus: http.StatusBadRequest,
322 func (s *CollectionListSuite) TestCollectionListMultiSiteWithLimit(c *check.C) {
323 for _, limit := range []int{0, 1, 2} {
327 filters: []arvados.Filter{
328 {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0]}},
329 {"uuid", "is_a", "teapot"},
331 expectCalls: []int{0, 0, 0},
332 expectStatus: http.StatusBadRequest,
337 func (s *CollectionListSuite) TestCollectionListMultiSiteWithOffset(c *check.C) {
342 filters: []arvados.Filter{
343 {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0]}},
344 {"uuid", "is_a", "teapot"},
346 expectCalls: []int{0, 0, 0},
347 expectStatus: http.StatusBadRequest,
351 func (s *CollectionListSuite) TestCollectionListMultiSiteWithOrder(c *check.C) {
355 order: []string{"uuid desc"},
356 filters: []arvados.Filter{
357 {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0]}},
358 {"uuid", "is_a", "teapot"},
360 expectCalls: []int{0, 0, 0},
361 expectStatus: http.StatusBadRequest,
365 func (s *CollectionListSuite) TestCollectionListInvalidFilters(c *check.C) {
369 filters: []arvados.Filter{
370 {"uuid", "in", "teapot"},
372 expectCalls: []int{0, 0, 0},
373 expectStatus: http.StatusBadRequest,
377 func (s *CollectionListSuite) TestCollectionListRemoteUnknown(c *check.C) {
381 filters: []arvados.Filter{
382 {"uuid", "in", []string{s.uuids[0][0], "bogus-4zz18-000001111122222"}},
384 expectStatus: http.StatusNotFound,
388 func (s *CollectionListSuite) TestCollectionListRemoteError(c *check.C) {
389 s.addDirectRemote(c, "bbbbb", &arvadostest.APIStub{})
393 filters: []arvados.Filter{
394 {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0]}},
396 expectStatus: http.StatusBadGateway,
400 func (s *CollectionListSuite) test(c *check.C, trial listTrial) {
401 resp, err := s.fed.CollectionList(s.ctx, arvados.ListOptions{
404 Offset: trial.offset,
406 Filters: trial.filters,
408 if trial.expectStatus != 0 {
409 c.Assert(err, check.NotNil)
410 err, _ := err.(interface{ HTTPStatus() int })
411 c.Assert(err, check.NotNil) // err must implement HTTPStatus()
412 c.Check(err.HTTPStatus(), check.Equals, trial.expectStatus)
413 c.Logf("returned error is %#v", err)
414 c.Logf("returned error string is %q", err)
416 c.Check(err, check.IsNil)
417 var expectItems []arvados.Collection
418 for _, uuid := range trial.expectUUIDs {
419 expectItems = append(expectItems, arvados.Collection{UUID: uuid})
421 c.Check(resp, check.DeepEquals, arvados.CollectionList{
426 for i, stub := range s.backends {
427 if i >= len(trial.expectCalls) {
430 calls := stub.Calls(nil)
431 c.Check(calls, check.HasLen, trial.expectCalls[i])
435 opts := calls[0].Options.(arvados.ListOptions)
436 c.Check(opts.Limit, check.Equals, -1)