11870: minor update
[arvados.git] / sdk / go / keepclient / root_sorter_test.go
1 package keepclient
2
3 import (
4         "fmt"
5         . "gopkg.in/check.v1"
6         "strconv"
7         "strings"
8 )
9
10 type RootSorterSuite struct{}
11
12 var _ = Suite(&RootSorterSuite{})
13
14 func FakeSvcRoot(i uint64) string {
15         return fmt.Sprintf("https://%x.svc/", i)
16 }
17
18 func FakeSvcUuid(i uint64) string {
19         return fmt.Sprintf("zzzzz-bi6l4-%015x", i)
20 }
21
22 func FakeServiceRoots(n uint64) map[string]string {
23         sr := map[string]string{}
24         for i := uint64(0); i < n; i++ {
25                 sr[FakeSvcUuid(i)] = FakeSvcRoot(i)
26         }
27         return sr
28 }
29
30 func (*RootSorterSuite) EmptyRoots(c *C) {
31         rs := NewRootSorter(map[string]string{}, Md5String("foo"))
32         c.Check(rs.GetSortedRoots(), Equals, []string{})
33 }
34
35 func (*RootSorterSuite) JustOneRoot(c *C) {
36         rs := NewRootSorter(FakeServiceRoots(1), Md5String("foo"))
37         c.Check(rs.GetSortedRoots(), Equals, []string{FakeSvcRoot(0)})
38 }
39
40 func (*RootSorterSuite) ReferenceSet(c *C) {
41         fakeroots := FakeServiceRoots(16)
42         // These reference probe orders are explained further in
43         // ../../python/tests/test_keep_client.py:
44         expected_orders := []string{
45                 "3eab2d5fc9681074",
46                 "097dba52e648f1c3",
47                 "c5b4e023f8a7d691",
48                 "9d81c02e76a3bf54",
49         }
50         for h, expected_order := range expected_orders {
51                 hash := Md5String(fmt.Sprintf("%064x", h))
52                 roots := NewRootSorter(fakeroots, hash).GetSortedRoots()
53                 for i, svc_id_s := range strings.Split(expected_order, "") {
54                         svc_id, err := strconv.ParseUint(svc_id_s, 16, 64)
55                         c.Assert(err, Equals, nil)
56                         c.Check(roots[i], Equals, FakeSvcRoot(svc_id))
57                 }
58         }
59 }