Clean redundant except: blocks in run_test_server.
[arvados.git] / services / datamanager / collection / collection_test.go
1 package collection
2
3 import (
4         "git.curoverse.com/arvados.git/sdk/go/blockdigest"
5         . "gopkg.in/check.v1"
6         "testing"
7 )
8
9 // Gocheck boilerplate
10 func Test(t *testing.T) {
11         TestingT(t)
12 }
13
14 type MySuite struct{}
15
16 var _ = Suite(&MySuite{})
17
18 // This captures the result we expect from
19 // ReadCollections.Summarize().  Because CollectionUuidToIndex is
20 // indeterminate, we replace BlockToCollectionIndices with
21 // BlockToCollectionUuids.
22 type ExpectedSummary struct {
23         OwnerToCollectionSize     map[string]int
24         BlockToDesiredReplication map[blockdigest.DigestWithSize]int
25         BlockToCollectionUuids    map[blockdigest.DigestWithSize][]string
26 }
27
28 func CompareSummarizedReadCollections(c *C,
29         summarized ReadCollections,
30         expected ExpectedSummary) {
31
32         c.Assert(summarized.OwnerToCollectionSize, DeepEquals,
33                 expected.OwnerToCollectionSize)
34
35         c.Assert(summarized.BlockToDesiredReplication, DeepEquals,
36                 expected.BlockToDesiredReplication)
37
38         summarizedBlockToCollectionUuids :=
39                 make(map[blockdigest.DigestWithSize]map[string]struct{})
40         for digest, indices := range summarized.BlockToCollectionIndices {
41                 uuidSet := make(map[string]struct{})
42                 summarizedBlockToCollectionUuids[digest] = uuidSet
43                 for _, index := range indices {
44                         uuidSet[summarized.CollectionIndexToUuid[index]] = struct{}{}
45                 }
46         }
47
48         expectedBlockToCollectionUuids :=
49                 make(map[blockdigest.DigestWithSize]map[string]struct{})
50         for digest, uuidSlice := range expected.BlockToCollectionUuids {
51                 uuidSet := make(map[string]struct{})
52                 expectedBlockToCollectionUuids[digest] = uuidSet
53                 for _, uuid := range uuidSlice {
54                         uuidSet[uuid] = struct{}{}
55                 }
56         }
57
58         c.Assert(summarizedBlockToCollectionUuids, DeepEquals,
59                 expectedBlockToCollectionUuids)
60 }
61
62 func (s *MySuite) TestSummarizeSimple(checker *C) {
63         rc := MakeTestReadCollections([]TestCollectionSpec{TestCollectionSpec{
64                 ReplicationLevel: 5,
65                 Blocks:           []int{1, 2},
66         }})
67
68         rc.Summarize(nil)
69
70         c := rc.UuidToCollection["col0"]
71
72         blockDigest1 := blockdigest.MakeTestDigestWithSize(1)
73         blockDigest2 := blockdigest.MakeTestDigestWithSize(2)
74
75         expected := ExpectedSummary{
76                 OwnerToCollectionSize:     map[string]int{c.OwnerUuid: c.TotalSize},
77                 BlockToDesiredReplication: map[blockdigest.DigestWithSize]int{blockDigest1: 5, blockDigest2: 5},
78                 BlockToCollectionUuids:    map[blockdigest.DigestWithSize][]string{blockDigest1: []string{c.Uuid}, blockDigest2: []string{c.Uuid}},
79         }
80
81         CompareSummarizedReadCollections(checker, rc, expected)
82 }
83
84 func (s *MySuite) TestSummarizeOverlapping(checker *C) {
85         rc := MakeTestReadCollections([]TestCollectionSpec{
86                 TestCollectionSpec{
87                         ReplicationLevel: 5,
88                         Blocks:           []int{1, 2},
89                 },
90                 TestCollectionSpec{
91                         ReplicationLevel: 8,
92                         Blocks:           []int{2, 3},
93                 },
94         })
95
96         rc.Summarize(nil)
97
98         c0 := rc.UuidToCollection["col0"]
99         c1 := rc.UuidToCollection["col1"]
100
101         blockDigest1 := blockdigest.MakeTestDigestWithSize(1)
102         blockDigest2 := blockdigest.MakeTestDigestWithSize(2)
103         blockDigest3 := blockdigest.MakeTestDigestWithSize(3)
104
105         expected := ExpectedSummary{
106                 OwnerToCollectionSize: map[string]int{
107                         c0.OwnerUuid: c0.TotalSize,
108                         c1.OwnerUuid: c1.TotalSize,
109                 },
110                 BlockToDesiredReplication: map[blockdigest.DigestWithSize]int{
111                         blockDigest1: 5,
112                         blockDigest2: 8,
113                         blockDigest3: 8,
114                 },
115                 BlockToCollectionUuids: map[blockdigest.DigestWithSize][]string{
116                         blockDigest1: []string{c0.Uuid},
117                         blockDigest2: []string{c0.Uuid, c1.Uuid},
118                         blockDigest3: []string{c1.Uuid},
119                 },
120         }
121
122         CompareSummarizedReadCollections(checker, rc, expected)
123 }