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