More reformatting of unittests to improve readability.
[arvados.git] / services / datamanager / summary / pull_list_test.go
1 package summary
2
3 import (
4         "encoding/json"
5         "git.curoverse.com/arvados.git/sdk/go/blockdigest"
6         . "gopkg.in/check.v1"
7         "sort"
8         "testing"
9 )
10
11 // Gocheck boilerplate
12 func Test(t *testing.T) {
13         TestingT(t)
14 }
15
16 type MySuite struct{}
17
18 var _ = Suite(&MySuite{})
19
20 // Helper method to declare string sets more succinctly
21 // Could be placed somewhere more general.
22 func stringSet(slice ...string) (m map[string]struct{}) {
23         m = map[string]struct{}{}
24         for _, s := range slice {
25                 m[s] = struct{}{}
26         }
27         return
28 }
29
30 func (s *MySuite) TestPullListPrintsJSONCorrectly(c *C) {
31         pl := PullList{PullRequest{
32                 Locator: Locator{Digest: blockdigest.MakeTestBlockDigest(0xBadBeef)},
33                 Servers: []string{"keep0.qr1hi.arvadosapi.com:25107",
34                         "keep1.qr1hi.arvadosapi.com:25108"}}}
35
36         b, err := json.Marshal(pl)
37         c.Assert(err, IsNil)
38         expectedOutput := `[{"locator":"0000000000000000000000000badbeef",` +
39                 `"servers":["keep0.qr1hi.arvadosapi.com:25107",` +
40                 `"keep1.qr1hi.arvadosapi.com:25108"]}]`
41         c.Check(string(b), Equals, expectedOutput)
42 }
43
44 func (s *MySuite) TestCreatePullServers(c *C) {
45         var cs CanonicalString
46         c.Check(
47                 CreatePullServers(cs,
48                         stringSet(),
49                         []string{},
50                         5),
51                 DeepEquals,
52                 PullServers{To: []string{}, From: []string{}})
53
54         c.Check(
55                 CreatePullServers(cs,
56                         stringSet("keep0:25107", "keep1:25108"),
57                         []string{},
58                         5),
59                 DeepEquals,
60                 PullServers{To: []string{}, From: []string{}})
61
62         c.Check(
63                 CreatePullServers(cs,
64                         stringSet("keep0:25107", "keep1:25108"),
65                         []string{"keep0:25107"},
66                         5),
67                 DeepEquals,
68                 PullServers{To: []string{}, From: []string{"keep0:25107"}})
69
70         c.Check(
71                 CreatePullServers(cs,
72                         stringSet("keep0:25107", "keep1:25108"),
73                         []string{"keep3:25110", "keep2:25109", "keep1:25108", "keep0:25107"},
74                         5),
75                 DeepEquals,
76                 PullServers{To: []string{"keep3:25110", "keep2:25109"},
77                         From: []string{"keep1:25108", "keep0:25107"}})
78
79         c.Check(
80                 CreatePullServers(cs,
81                         stringSet("keep0:25107", "keep1:25108"),
82                         []string{"keep3:25110", "keep2:25109", "keep1:25108", "keep0:25107"},
83                         1),
84                 DeepEquals,
85                 PullServers{To: []string{"keep3:25110"},
86                         From: []string{"keep1:25108", "keep0:25107"}})
87
88         c.Check(
89                 CreatePullServers(cs,
90                         stringSet("keep0:25107", "keep1:25108"),
91                         []string{"keep3:25110", "keep2:25109", "keep1:25108", "keep0:25107"},
92                         0),
93                 DeepEquals,
94                 PullServers{To: []string{},
95                         From: []string{"keep1:25108", "keep0:25107"}})
96 }
97
98 // Checks whether two pull list maps are equal. Since pull lists are
99 // ordered arbitrarily, we need to sort them by digest before
100 // comparing them for deep equality.
101 type pullListMapEqualsChecker struct {
102         *CheckerInfo
103 }
104
105 func (c *pullListMapEqualsChecker) Check(params []interface{}, names []string) (result bool, error string) {
106         obtained, ok := params[0].(map[string]PullList)
107         if !ok {
108                 return false, "First parameter is not a PullList map"
109         }
110         expected, ok := params[1].(map[string]PullList)
111         if !ok {
112                 return false, "Second parameter is not a PullList map"
113         }
114
115         for _, v := range obtained {
116                 sort.Sort(PullListByDigest(v))
117         }
118         for _, v := range expected {
119                 sort.Sort(PullListByDigest(v))
120         }
121
122         return DeepEquals.Check(params, names)
123 }
124
125 var PullListMapEquals Checker = &pullListMapEqualsChecker{&CheckerInfo{
126         Name:   "PullListMapEquals",
127         Params: []string{"obtained", "expected"},
128 }}
129
130 func (s *MySuite) TestBuildPullLists(c *C) {
131         c.Check(
132                 BuildPullLists(map[Locator]PullServers{}),
133                 PullListMapEquals,
134                 map[string]PullList{})
135
136         locator1 := Locator{Digest: blockdigest.MakeTestBlockDigest(0xBadBeef)}
137         c.Check(
138                 BuildPullLists(map[Locator]PullServers{
139                         locator1: PullServers{To: []string{}, From: []string{}}}),
140                 PullListMapEquals,
141                 map[string]PullList{})
142
143         c.Check(
144                 BuildPullLists(map[Locator]PullServers{
145                         locator1: PullServers{To: []string{}, From: []string{"f1", "f2"}}}),
146                 PullListMapEquals,
147                 map[string]PullList{})
148
149         c.Check(
150                 BuildPullLists(map[Locator]PullServers{
151                         locator1: PullServers{To: []string{"t1"}, From: []string{"f1", "f2"}}}),
152                 PullListMapEquals,
153                 map[string]PullList{
154                         "t1": PullList{PullRequest{locator1, []string{"f1", "f2"}}}})
155
156         c.Check(
157                 BuildPullLists(map[Locator]PullServers{
158                         locator1: PullServers{To: []string{"t1"}, From: []string{}}}),
159                 PullListMapEquals,
160                 map[string]PullList{"t1": PullList{
161                         PullRequest{locator1, []string{}}}})
162
163         c.Check(
164                 BuildPullLists(map[Locator]PullServers{
165                         locator1: PullServers{
166                                 To:   []string{"t1", "t2"},
167                                 From: []string{"f1", "f2"},
168                         }}),
169                 PullListMapEquals,
170                 map[string]PullList{
171                         "t1": PullList{PullRequest{locator1, []string{"f1", "f2"}}},
172                         "t2": PullList{PullRequest{locator1, []string{"f1", "f2"}}},
173                 })
174
175         locator2 := Locator{Digest: blockdigest.MakeTestBlockDigest(0xCabbed)}
176         c.Check(
177                 BuildPullLists(map[Locator]PullServers{
178                         locator1: PullServers{To: []string{"t1"}, From: []string{"f1", "f2"}},
179                         locator2: PullServers{To: []string{"t2"}, From: []string{"f3", "f4"}}}),
180                 PullListMapEquals,
181                 map[string]PullList{
182                         "t1": PullList{PullRequest{locator1, []string{"f1", "f2"}}},
183                         "t2": PullList{PullRequest{locator2, []string{"f3", "f4"}}},
184                 })
185
186         c.Check(
187                 BuildPullLists(map[Locator]PullServers{
188                         locator1: PullServers{
189                                 To:   []string{"t1"},
190                                 From: []string{"f1", "f2"}},
191                         locator2: PullServers{
192                                 To:   []string{"t2", "t1"},
193                                 From: []string{"f3", "f4"}},
194                 }),
195                 PullListMapEquals,
196                 map[string]PullList{
197                         "t1": PullList{
198                                 PullRequest{locator1, []string{"f1", "f2"}},
199                                 PullRequest{locator2, []string{"f3", "f4"}},
200                         },
201                         "t2": PullList{
202                                 PullRequest{locator2, []string{"f3", "f4"}},
203                         },
204                 })
205
206         locator3 := Locator{Digest: blockdigest.MakeTestBlockDigest(0xDeadBeef)}
207         locator4 := Locator{Digest: blockdigest.MakeTestBlockDigest(0xFedBeef)}
208         c.Check(
209                 BuildPullLists(map[Locator]PullServers{
210                         locator1: PullServers{
211                                 To:   []string{"t1"},
212                                 From: []string{"f1", "f2"}},
213                         locator2: PullServers{
214                                 To:   []string{"t2", "t1"},
215                                 From: []string{"f3", "f4"}},
216                         locator3: PullServers{
217                                 To:   []string{"t3", "t2", "t1"},
218                                 From: []string{"f4", "f5"}},
219                         locator4: PullServers{
220                                 To:   []string{"t4", "t3", "t2", "t1"},
221                                 From: []string{"f1", "f5"}},
222                 }),
223                 PullListMapEquals,
224                 map[string]PullList{
225                         "t1": PullList{
226                                 PullRequest{locator1, []string{"f1", "f2"}},
227                                 PullRequest{locator2, []string{"f3", "f4"}},
228                                 PullRequest{locator3, []string{"f4", "f5"}},
229                                 PullRequest{locator4, []string{"f1", "f5"}},
230                         },
231                         "t2": PullList{
232                                 PullRequest{locator2, []string{"f3", "f4"}},
233                                 PullRequest{locator3, []string{"f4", "f5"}},
234                                 PullRequest{locator4, []string{"f1", "f5"}},
235                         },
236                         "t3": PullList{
237                                 PullRequest{locator3, []string{"f4", "f5"}},
238                                 PullRequest{locator4, []string{"f1", "f5"}},
239                         },
240                         "t4": PullList{
241                                 PullRequest{locator4, []string{"f1", "f5"}},
242                         },
243                 })
244 }
245
246 func (s *MySuite) TestRemoveProtocolPrefix(c *C) {
247         c.Check(RemoveProtocolPrefix("blah"), Equals, "blah")
248         c.Check(RemoveProtocolPrefix("bl/ah"), Equals, "ah")
249         c.Check(RemoveProtocolPrefix("http://blah.com"), Equals, "blah.com")
250         c.Check(RemoveProtocolPrefix("https://blah.com:8900"), Equals, "blah.com:8900")
251 }