19146: Remove unneeded special case checks, explain the needed one.
[arvados.git] / lib / controller / federation / list_test.go
index a9c4f588f12a38b017d1c89b2995263a8c12d3d6..23671b7fbdd068e042e39a0291b250ae35a70f1f 100644 (file)
@@ -8,10 +8,11 @@ import (
        "context"
        "fmt"
        "net/http"
+       "reflect"
        "sort"
 
-       "git.curoverse.com/arvados.git/sdk/go/arvados"
-       "git.curoverse.com/arvados.git/sdk/go/arvadostest"
+       "git.arvados.org/arvados.git/sdk/go/arvados"
+       "git.arvados.org/arvados.git/sdk/go/arvadostest"
        check "gopkg.in/check.v1"
 )
 
@@ -57,10 +58,17 @@ func (cl *collectionLister) CollectionList(ctx context.Context, options arvados.
                if cl.MaxPageSize > 0 && len(resp.Items) >= cl.MaxPageSize {
                        break
                }
-               if options.Limit >= 0 && len(resp.Items) >= options.Limit {
+               if options.Limit >= 0 && int64(len(resp.Items)) >= options.Limit {
                        break
                }
                if cl.matchFilters(c, options.Filters) {
+                       if reflect.DeepEqual(options.Select, []string{"uuid", "name"}) {
+                               c = arvados.Collection{UUID: c.UUID, Name: c.Name}
+                       } else if reflect.DeepEqual(options.Select, []string{"name"}) {
+                               c = arvados.Collection{Name: c.Name}
+                       } else if len(options.Select) > 0 {
+                               panic(fmt.Sprintf("not implemented: options=%#v", options))
+                       }
                        resp.Items = append(resp.Items, c)
                }
        }
@@ -107,10 +115,11 @@ func (s *CollectionListSuite) SetUpTest(c *check.C) {
 
 type listTrial struct {
        count        string
-       limit        int
-       offset       int
+       limit        int64
+       offset       int64
        order        []string
        filters      []arvados.Filter
+       selectfields []string
        expectUUIDs  []string
        expectCalls  []int // number of API calls to backends
        expectStatus int
@@ -145,6 +154,17 @@ func (s *CollectionListSuite) TestCollectionListOneRemote(c *check.C) {
        })
 }
 
+func (s *CollectionListSuite) TestCollectionListOneLocalDeselectingUUID(c *check.C) {
+       s.test(c, listTrial{
+               count:        "none",
+               limit:        -1,
+               filters:      []arvados.Filter{{"uuid", "=", s.uuids[0][0]}},
+               selectfields: []string{"name"},
+               expectUUIDs:  []string{""}, // select=name is honored
+               expectCalls:  []int{1, 0, 0},
+       })
+}
+
 func (s *CollectionListSuite) TestCollectionListOneLocalUsingInOperator(c *check.C) {
        s.test(c, listTrial{
                count:       "none",
@@ -165,6 +185,17 @@ func (s *CollectionListSuite) TestCollectionListOneRemoteUsingInOperator(c *chec
        })
 }
 
+func (s *CollectionListSuite) TestCollectionListOneRemoteDeselectingUUID(c *check.C) {
+       s.test(c, listTrial{
+               count:        "none",
+               limit:        -1,
+               filters:      []arvados.Filter{{"uuid", "=", s.uuids[1][0]}},
+               selectfields: []string{"name"},
+               expectUUIDs:  []string{s.uuids[1][0]}, // uuid is returned, despite not being selected
+               expectCalls:  []int{0, 1, 0},
+       })
+}
+
 func (s *CollectionListSuite) TestCollectionListOneLocalOneRemote(c *check.C) {
        s.test(c, listTrial{
                count:       "none",
@@ -175,6 +206,17 @@ func (s *CollectionListSuite) TestCollectionListOneLocalOneRemote(c *check.C) {
        })
 }
 
+func (s *CollectionListSuite) TestCollectionListOneLocalOneRemoteDeselectingUUID(c *check.C) {
+       s.test(c, listTrial{
+               count:        "none",
+               limit:        -1,
+               filters:      []arvados.Filter{{"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0]}}},
+               selectfields: []string{"name"},
+               expectUUIDs:  []string{s.uuids[0][0], s.uuids[1][0]}, // uuid is returned, despite not being selected
+               expectCalls:  []int{1, 1, 0},
+       })
+}
+
 func (s *CollectionListSuite) TestCollectionListTwoRemotes(c *check.C) {
        s.test(c, listTrial{
                count:       "none",
@@ -258,6 +300,7 @@ func (s *CollectionListSuite) TestCollectionListMultiSiteExtraFilters(c *check.C
 
 func (s *CollectionListSuite) TestCollectionListMultiSiteWithCount(c *check.C) {
        for _, count := range []string{"", "exact"} {
+               s.SetUpTest(c) // Reset backends / call counters
                s.test(c, listTrial{
                        count: count,
                        limit: -1,
@@ -272,12 +315,13 @@ func (s *CollectionListSuite) TestCollectionListMultiSiteWithCount(c *check.C) {
 }
 
 func (s *CollectionListSuite) TestCollectionListMultiSiteWithLimit(c *check.C) {
-       for _, limit := range []int{0, 1, 2} {
+       for _, limit := range []int64{0, 1, 2} {
+               s.SetUpTest(c) // Reset backends / call counters
                s.test(c, listTrial{
                        count: "none",
                        limit: limit,
                        filters: []arvados.Filter{
-                               {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0]}},
+                               {"uuid", "in", []string{s.uuids[0][0], s.uuids[1][0], s.uuids[2][0]}},
                                {"uuid", "is_a", "teapot"},
                        },
                        expectCalls:  []int{0, 0, 0},
@@ -286,6 +330,22 @@ func (s *CollectionListSuite) TestCollectionListMultiSiteWithLimit(c *check.C) {
        }
 }
 
+func (s *CollectionListSuite) TestCollectionListMultiSiteWithHighLimit(c *check.C) {
+       uuids := []string{s.uuids[0][0], s.uuids[1][0], s.uuids[2][0]}
+       for _, limit := range []int64{3, 4, 1234567890} {
+               s.SetUpTest(c) // Reset backends / call counters
+               s.test(c, listTrial{
+                       count: "none",
+                       limit: limit,
+                       filters: []arvados.Filter{
+                               {"uuid", "in", uuids},
+                       },
+                       expectUUIDs: uuids,
+                       expectCalls: []int{1, 1, 1},
+               })
+       }
+}
+
 func (s *CollectionListSuite) TestCollectionListMultiSiteWithOffset(c *check.C) {
        s.test(c, listTrial{
                count:  "none",
@@ -338,7 +398,7 @@ func (s *CollectionListSuite) TestCollectionListRemoteUnknown(c *check.C) {
 }
 
 func (s *CollectionListSuite) TestCollectionListRemoteError(c *check.C) {
-       s.addDirectRemote(c, "bbbbb", &arvadostest.APIStub{})
+       s.addDirectRemote(c, "bbbbb", &arvadostest.APIStub{Error: fmt.Errorf("stub backend error")})
        s.test(c, listTrial{
                count: "none",
                limit: -1,
@@ -356,11 +416,12 @@ func (s *CollectionListSuite) test(c *check.C, trial listTrial) {
                Offset:  trial.offset,
                Order:   trial.order,
                Filters: trial.filters,
+               Select:  trial.selectfields,
        })
        if trial.expectStatus != 0 {
                c.Assert(err, check.NotNil)
-               err, _ := err.(interface{ HTTPStatus() int })
-               c.Assert(err, check.NotNil) // err must implement HTTPStatus()
+               err, ok := err.(interface{ HTTPStatus() int })
+               c.Assert(ok, check.Equals, true) // err must implement interface{ HTTPStatus() int }
                c.Check(err.HTTPStatus(), check.Equals, trial.expectStatus)
                c.Logf("returned error is %#v", err)
                c.Logf("returned error string is %q", err)