Merge branch 'master' into 6465-optimize-workbench-integration-tests
[arvados.git] / services / datamanager / collection / testing.go
1 // Code used for testing only.
2
3 package collection
4
5 import (
6         "fmt"
7         "git.curoverse.com/arvados.git/sdk/go/blockdigest"
8 )
9
10 type TestCollectionSpec struct {
11         // The desired replication level
12         ReplicationLevel int
13         // Blocks this contains, represented by ints. Ints repeated will
14         // still only represent one block
15         Blocks []int
16 }
17
18 // Creates a ReadCollections object for testing based on the give
19 // specs.  Only the ReadAllCollections and UuidToCollection fields are
20 // populated.  To populate other fields call rc.Summarize().
21 func MakeTestReadCollections(specs []TestCollectionSpec) (rc ReadCollections) {
22         rc = ReadCollections{
23                 ReadAllCollections: true,
24                 UuidToCollection:   map[string]Collection{},
25         }
26
27         for i, spec := range specs {
28                 c := Collection{
29                         Uuid:              fmt.Sprintf("col%d", i),
30                         OwnerUuid:         fmt.Sprintf("owner%d", i),
31                         ReplicationLevel:  spec.ReplicationLevel,
32                         BlockDigestToSize: map[blockdigest.BlockDigest]int{},
33                 }
34                 rc.UuidToCollection[c.Uuid] = c
35                 for _, j := range spec.Blocks {
36                         c.BlockDigestToSize[blockdigest.MakeTestBlockDigest(j)] = j
37                 }
38                 // We compute the size in a separate loop because the value
39                 // computed in the above loop would be invalid if c.Blocks
40                 // contained duplicates.
41                 for _, size := range c.BlockDigestToSize {
42                         c.TotalSize += size
43                 }
44         }
45         return
46 }
47
48 // Returns a slice giving the collection index of each collection that
49 // was passed in to MakeTestReadCollections. rc.Summarize() must be
50 // called before this method, since Summarize() assigns an index to
51 // each collection.
52 func (rc ReadCollections) CollectionIndicesForTesting() (indices []int) {
53         // TODO(misha): Assert that rc.Summarize() has been called.
54         numCollections := len(rc.CollectionIndexToUuid)
55         indices = make([]int, numCollections)
56         for i := 0; i < numCollections; i++ {
57                 indices[i] = rc.CollectionUuidToIndex[fmt.Sprintf("col%d", i)]
58         }
59         return
60 }