Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[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 var _ = Suite(&RootSorterSuite{})
12
13 func FakeSvcRoot(i uint64) (string) {
14         return fmt.Sprintf("https://%x.svc/", i)
15 }
16
17 func FakeSvcUuid(i uint64) (string) {
18         return fmt.Sprintf("zzzzz-bi6l4-%015x", i)
19 }
20
21 func FakeServiceRoots(n uint64) (map[string]string) {
22         sr := map[string]string{}
23         for i := uint64(0); i < n; i ++ {
24                 sr[FakeSvcUuid(i)] = FakeSvcRoot(i)
25         }
26         return sr
27 }
28
29 func (*RootSorterSuite) EmptyRoots(c *C) {
30         rs := NewRootSorter(map[string]string{}, Md5String("foo"))
31         c.Check(rs.GetSortedRoots(), Equals, []string{})
32 }
33
34 func (*RootSorterSuite) JustOneRoot(c *C) {
35         rs := NewRootSorter(FakeServiceRoots(1), Md5String("foo"))
36         c.Check(rs.GetSortedRoots(), Equals, []string{FakeSvcRoot(0)})
37 }
38
39 func (*RootSorterSuite) ReferenceSet(c *C) {
40         fakeroots := FakeServiceRoots(16)
41         // These reference probe orders are explained further in
42         // ../../python/tests/test_keep_client.py:
43         expected_orders := []string{
44                 "3eab2d5fc9681074",
45                 "097dba52e648f1c3",
46                 "c5b4e023f8a7d691",
47                 "9d81c02e76a3bf54",
48         }
49         for h, expected_order := range expected_orders {
50                 hash := Md5String(fmt.Sprintf("%064x", h))
51                 roots := NewRootSorter(fakeroots, hash).GetSortedRoots()
52                 for i, svc_id_s := range strings.Split(expected_order, "") {
53                         svc_id, err := strconv.ParseUint(svc_id_s, 16, 64)
54                         c.Assert(err, Equals, nil)
55                         c.Check(roots[i], Equals, FakeSvcRoot(svc_id))
56                 }
57         }
58 }