Modified pull list code to change json output to match the specification at https...
authormishaz <misha@curoverse.com>
Tue, 19 May 2015 19:10:35 +0000 (19:10 +0000)
committermishaz <misha@curoverse.com>
Tue, 19 May 2015 19:10:35 +0000 (19:10 +0000)
Changed PullList type
Renamed PullListEntry to PullRequest to match the type defined in services/keepstore/handlers.go
Renamed EntriesByDigest sorter to PullListByDigest

Modified TestPullListPrintsJSONCorrectly to update expected json format.

Lots of changes in unittests to accomodate renames.
Reformatted test to improve readability.

services/datamanager/summary/pull_list.go
services/datamanager/summary/pull_list_test.go

index 6273bba43286e29a91dcb6217a2e12d1683e33a9..64a83a528eb2774620af0cdbe052afb4acdee1d5 100644 (file)
@@ -24,23 +24,21 @@ func (l Locator) MarshalJSON() ([]byte, error) {
 }
 
 // One entry in the Pull List
-type PullListEntry struct {
+type PullRequest struct {
        Locator Locator  `json:"locator"`
        Servers []string `json:"servers"`
 }
 
 // The Pull List for a particular server
-type PullList struct {
-       Entries []PullListEntry `json:"blocks"`
-}
+type PullList []PullRequest
 
-// EntriesByDigest implements sort.Interface for []PullListEntry
-// based on the Digest.
-type EntriesByDigest []PullListEntry
+// PullListByDigest implements sort.Interface for PullList based on
+// the Digest.
+type PullListByDigest PullList
 
-func (a EntriesByDigest) Len() int      { return len(a) }
-func (a EntriesByDigest) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
-func (a EntriesByDigest) Less(i, j int) bool {
+func (a PullListByDigest) Len() int      { return len(a) }
+func (a PullListByDigest) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+func (a PullListByDigest) Less(i, j int) bool {
        di, dj := a[i].Locator.Digest, a[j].Locator.Digest
        return di.H < dj.H || (di.H == dj.H && di.L < dj.L)
 }
@@ -138,11 +136,11 @@ func BuildPullLists(lps map[Locator]PullServers) (spl map[string]PullList) {
                for _, destination := range pullServers.To {
                        pullList, pullListExists := spl[destination]
                        if !pullListExists {
-                               pullList = PullList{Entries: []PullListEntry{}}
+                               pullList = PullList{}
                                spl[destination] = pullList
                        }
-                       pullList.Entries = append(pullList.Entries,
-                               PullListEntry{Locator: locator, Servers: pullServers.From})
+                       pullList = append(pullList,
+                               PullRequest{Locator: locator, Servers: pullServers.From})
                        spl[destination] = pullList
                }
        }
index dd73bf5349b92649984f84387867a4c0b74a595e..5638dc40f13c7ad034352d29646eb3edfeba900d 100644 (file)
@@ -28,16 +28,16 @@ func stringSet(slice ...string) (m map[string]struct{}) {
 }
 
 func (s *MySuite) TestPullListPrintsJSONCorrectly(c *C) {
-       pl := PullList{Entries: []PullListEntry{PullListEntry{
+       pl := PullList{PullRequest{
                Locator: Locator{Digest: blockdigest.MakeTestBlockDigest(0xBadBeef)},
                Servers: []string{"keep0.qr1hi.arvadosapi.com:25107",
-                       "keep1.qr1hi.arvadosapi.com:25108"}}}}
+                       "keep1.qr1hi.arvadosapi.com:25108"}}}
 
        b, err := json.Marshal(pl)
        c.Assert(err, IsNil)
-       expectedOutput := `{"blocks":[{"locator":"0000000000000000000000000badbeef",` +
+       expectedOutput := `[{"locator":"0000000000000000000000000badbeef",` +
                `"servers":["keep0.qr1hi.arvadosapi.com:25107",` +
-               `"keep1.qr1hi.arvadosapi.com:25108"]}]}`
+               `"keep1.qr1hi.arvadosapi.com:25108"]}]`
        c.Check(string(b), Equals, expectedOutput)
 }
 
@@ -113,18 +113,19 @@ func (c *pullListMapEqualsChecker) Check(params []interface{}, names []string) (
        }
 
        for _, v := range obtained {
-               sort.Sort(EntriesByDigest(v.Entries))
+               sort.Sort(PullListByDigest(v))
        }
        for _, v := range expected {
-               sort.Sort(EntriesByDigest(v.Entries))
+               sort.Sort(PullListByDigest(v))
        }
 
        return DeepEquals.Check(params, names)
 }
 
-var PullListMapEquals Checker = &pullListMapEqualsChecker{
-       &CheckerInfo{Name: "PullListMapEquals", Params: []string{"obtained", "expected"}},
-}
+var PullListMapEquals Checker = &pullListMapEqualsChecker{&CheckerInfo{
+       Name:   "PullListMapEquals",
+       Params: []string{"obtained", "expected"},
+}}
 
 func (s *MySuite) TestBuildPullLists(c *C) {
        c.Check(
@@ -149,29 +150,28 @@ func (s *MySuite) TestBuildPullLists(c *C) {
                BuildPullLists(map[Locator]PullServers{
                        locator1: PullServers{To: []string{"t1"}, From: []string{"f1", "f2"}}}),
                PullListMapEquals,
-               map[string]PullList{"t1": PullList{Entries: []PullListEntry{PullListEntry{
-                       Locator: locator1,
-                       Servers: []string{"f1", "f2"}}}}})
+               map[string]PullList{"t1": PullList{
+                       PullRequest{Locator: locator1, Servers: []string{"f1", "f2"}}}})
 
        c.Check(
                BuildPullLists(map[Locator]PullServers{
                        locator1: PullServers{To: []string{"t1"}, From: []string{}}}),
                PullListMapEquals,
-               map[string]PullList{"t1": PullList{Entries: []PullListEntry{PullListEntry{
-                       Locator: locator1,
-                       Servers: []string{}}}}})
+               map[string]PullList{"t1": PullList{
+                       PullRequest{Locator: locator1, Servers: []string{}}}})
 
        c.Check(
                BuildPullLists(map[Locator]PullServers{
-                       locator1: PullServers{To: []string{"t1", "t2"}, From: []string{"f1", "f2"}}}),
+                       locator1: PullServers{
+                               To:   []string{"t1", "t2"},
+                               From: []string{"f1", "f2"},
+                       }}),
                PullListMapEquals,
                map[string]PullList{
-                       "t1": PullList{Entries: []PullListEntry{PullListEntry{
-                               Locator: locator1,
-                               Servers: []string{"f1", "f2"}}}},
-                       "t2": PullList{Entries: []PullListEntry{PullListEntry{
-                               Locator: locator1,
-                               Servers: []string{"f1", "f2"}}}},
+                       "t1": PullList{
+                               PullRequest{Locator: locator1, Servers: []string{"f1", "f2"}}},
+                       "t2": PullList{
+                               PullRequest{Locator: locator1, Servers: []string{"f1", "f2"}}},
                })
 
        locator2 := Locator{Digest: blockdigest.MakeTestBlockDigest(0xCabbed)}
@@ -181,81 +181,69 @@ func (s *MySuite) TestBuildPullLists(c *C) {
                        locator2: PullServers{To: []string{"t2"}, From: []string{"f3", "f4"}}}),
                PullListMapEquals,
                map[string]PullList{
-                       "t1": PullList{Entries: []PullListEntry{PullListEntry{
-                               Locator: locator1,
-                               Servers: []string{"f1", "f2"}}}},
-                       "t2": PullList{Entries: []PullListEntry{PullListEntry{
-                               Locator: locator2,
-                               Servers: []string{"f3", "f4"}}}},
+                       "t1": PullList{
+                               PullRequest{Locator: locator1, Servers: []string{"f1", "f2"}}},
+                       "t2": PullList{
+                               PullRequest{Locator: locator2, Servers: []string{"f3", "f4"}}},
                })
 
        c.Check(
                BuildPullLists(map[Locator]PullServers{
-                       locator1: PullServers{To: []string{"t1"}, From: []string{"f1", "f2"}},
-                       locator2: PullServers{To: []string{"t2", "t1"}, From: []string{"f3", "f4"}}}),
+                       locator1: PullServers{
+                               To:   []string{"t1"},
+                               From: []string{"f1", "f2"}},
+                       locator2: PullServers{
+                               To:   []string{"t2", "t1"},
+                               From: []string{"f3", "f4"}},
+               }),
                PullListMapEquals,
                map[string]PullList{
-                       "t1": PullList{Entries: []PullListEntry{
-                               PullListEntry{
-                                       Locator: locator1,
-                                       Servers: []string{"f1", "f2"}},
-                               PullListEntry{
-                                       Locator: locator2,
-                                       Servers: []string{"f3", "f4"}}}},
-                       "t2": PullList{Entries: []PullListEntry{PullListEntry{
-                               Locator: locator2,
-                               Servers: []string{"f3", "f4"}}}},
+                       "t1": PullList{
+                               PullRequest{Locator: locator1, Servers: []string{"f1", "f2"}},
+                               PullRequest{Locator: locator2, Servers: []string{"f3", "f4"}},
+                       },
+                       "t2": PullList{
+                               PullRequest{Locator: locator2, Servers: []string{"f3", "f4"}},
+                       },
                })
 
        locator3 := Locator{Digest: blockdigest.MakeTestBlockDigest(0xDeadBeef)}
        locator4 := Locator{Digest: blockdigest.MakeTestBlockDigest(0xFedBeef)}
        c.Check(
                BuildPullLists(map[Locator]PullServers{
-                       locator1: PullServers{To: []string{"t1"}, From: []string{"f1", "f2"}},
-                       locator2: PullServers{To: []string{"t2", "t1"}, From: []string{"f3", "f4"}},
-                       locator3: PullServers{To: []string{"t3", "t2", "t1"}, From: []string{"f4", "f5"}},
-                       locator4: PullServers{To: []string{"t4", "t3", "t2", "t1"}, From: []string{"f1", "f5"}},
+                       locator1: PullServers{
+                               To:   []string{"t1"},
+                               From: []string{"f1", "f2"}},
+                       locator2: PullServers{
+                               To:   []string{"t2", "t1"},
+                               From: []string{"f3", "f4"}},
+                       locator3: PullServers{
+                               To:   []string{"t3", "t2", "t1"},
+                               From: []string{"f4", "f5"}},
+                       locator4: PullServers{
+                               To:   []string{"t4", "t3", "t2", "t1"},
+                               From: []string{"f1", "f5"}},
                }),
                PullListMapEquals,
                map[string]PullList{
-                       "t1": PullList{Entries: []PullListEntry{
-                               PullListEntry{
-                                       Locator: locator1,
-                                       Servers: []string{"f1", "f2"}},
-                               PullListEntry{
-                                       Locator: locator2,
-                                       Servers: []string{"f3", "f4"}},
-                               PullListEntry{
-                                       Locator: locator3,
-                                       Servers: []string{"f4", "f5"}},
-                               PullListEntry{
-                                       Locator: locator4,
-                                       Servers: []string{"f1", "f5"}},
-                       }},
-                       "t2": PullList{Entries: []PullListEntry{
-                               PullListEntry{
-                                       Locator: locator2,
-                                       Servers: []string{"f3", "f4"}},
-                               PullListEntry{
-                                       Locator: locator3,
-                                       Servers: []string{"f4", "f5"}},
-                               PullListEntry{
-                                       Locator: locator4,
-                                       Servers: []string{"f1", "f5"}},
-                       }},
-                       "t3": PullList{Entries: []PullListEntry{
-                               PullListEntry{
-                                       Locator: locator3,
-                                       Servers: []string{"f4", "f5"}},
-                               PullListEntry{
-                                       Locator: locator4,
-                                       Servers: []string{"f1", "f5"}},
-                       }},
-                       "t4": PullList{Entries: []PullListEntry{
-                               PullListEntry{
-                                       Locator: locator4,
-                                       Servers: []string{"f1", "f5"}},
-                       }},
+                       "t1": PullList{
+                               PullRequest{Locator: locator1, Servers: []string{"f1", "f2"}},
+                               PullRequest{Locator: locator2, Servers: []string{"f3", "f4"}},
+                               PullRequest{Locator: locator3, Servers: []string{"f4", "f5"}},
+                               PullRequest{Locator: locator4, Servers: []string{"f1", "f5"}},
+                       },
+                       "t2": PullList{
+                               PullRequest{Locator: locator2, Servers: []string{"f3", "f4"}},
+                               PullRequest{Locator: locator3, Servers: []string{"f4", "f5"}},
+                               PullRequest{Locator: locator4, Servers: []string{"f1", "f5"}},
+                       },
+                       "t3": PullList{
+                               PullRequest{Locator: locator3, Servers: []string{"f4", "f5"}},
+                               PullRequest{Locator: locator4, Servers: []string{"f1", "f5"}},
+                       },
+                       "t4": PullList{
+                               PullRequest{Locator: locator4, Servers: []string{"f1", "f5"}},
+                       },
                })
 }