Merge branch 'master' into 8561-node-pairing
[arvados.git] / services / keepstore / volume_test.go
1 package main
2
3 import (
4         "bytes"
5         "crypto/md5"
6         "errors"
7         "fmt"
8         "io"
9         "os"
10         "strings"
11         "sync"
12         "time"
13 )
14
15 // A TestableVolume allows test suites to manipulate the state of an
16 // underlying Volume, in order to test behavior in cases that are
17 // impractical to achieve with a sequence of normal Volume operations.
18 type TestableVolume interface {
19         Volume
20         // [Over]write content for a locator with the given data,
21         // bypassing all constraints like readonly and serialize.
22         PutRaw(locator string, data []byte)
23
24         // Specify the value Mtime() should return, until the next
25         // call to Touch, TouchWithDate, or Put.
26         TouchWithDate(locator string, lastPut time.Time)
27
28         // Clean up, delete temporary files.
29         Teardown()
30 }
31
32 // MockVolumes are test doubles for Volumes, used to test handlers.
33 type MockVolume struct {
34         Store      map[string][]byte
35         Timestamps map[string]time.Time
36
37         // Bad volumes return an error for every operation.
38         Bad bool
39
40         // Touchable volumes' Touch() method succeeds for a locator
41         // that has been Put().
42         Touchable bool
43
44         // Readonly volumes return an error for Put, Delete, and
45         // Touch.
46         Readonly bool
47
48         // Gate is a "starting gate", allowing test cases to pause
49         // volume operations long enough to inspect state. Every
50         // operation (except Status) starts by receiving from
51         // Gate. Sending one value unblocks one operation; closing the
52         // channel unblocks all operations. By default, Gate is a
53         // closed channel, so all operations proceed without
54         // blocking. See trash_worker_test.go for an example.
55         Gate chan struct{}
56
57         called map[string]int
58         mutex  sync.Mutex
59 }
60
61 // CreateMockVolume returns a non-Bad, non-Readonly, Touchable mock
62 // volume.
63 func CreateMockVolume() *MockVolume {
64         gate := make(chan struct{})
65         close(gate)
66         return &MockVolume{
67                 Store:      make(map[string][]byte),
68                 Timestamps: make(map[string]time.Time),
69                 Bad:        false,
70                 Touchable:  true,
71                 Readonly:   false,
72                 called:     map[string]int{},
73                 Gate:       gate,
74         }
75 }
76
77 // CallCount returns how many times the named method has been called.
78 func (v *MockVolume) CallCount(method string) int {
79         v.mutex.Lock()
80         defer v.mutex.Unlock()
81         c, ok := v.called[method]
82         if !ok {
83                 return 0
84         }
85         return c
86 }
87
88 func (v *MockVolume) gotCall(method string) {
89         v.mutex.Lock()
90         defer v.mutex.Unlock()
91         if _, ok := v.called[method]; !ok {
92                 v.called[method] = 1
93         } else {
94                 v.called[method]++
95         }
96 }
97
98 func (v *MockVolume) Compare(loc string, buf []byte) error {
99         v.gotCall("Compare")
100         <-v.Gate
101         if v.Bad {
102                 return errors.New("Bad volume")
103         } else if block, ok := v.Store[loc]; ok {
104                 if fmt.Sprintf("%x", md5.Sum(block)) != loc {
105                         return DiskHashError
106                 }
107                 if bytes.Compare(buf, block) != 0 {
108                         return CollisionError
109                 }
110                 return nil
111         } else {
112                 return NotFoundError
113         }
114 }
115
116 func (v *MockVolume) Get(loc string) ([]byte, error) {
117         v.gotCall("Get")
118         <-v.Gate
119         if v.Bad {
120                 return nil, errors.New("Bad volume")
121         } else if block, ok := v.Store[loc]; ok {
122                 buf := bufs.Get(len(block))
123                 copy(buf, block)
124                 return buf, nil
125         }
126         return nil, os.ErrNotExist
127 }
128
129 func (v *MockVolume) Put(loc string, block []byte) error {
130         v.gotCall("Put")
131         <-v.Gate
132         if v.Bad {
133                 return errors.New("Bad volume")
134         }
135         if v.Readonly {
136                 return MethodDisabledError
137         }
138         v.Store[loc] = block
139         return v.Touch(loc)
140 }
141
142 func (v *MockVolume) Touch(loc string) error {
143         v.gotCall("Touch")
144         <-v.Gate
145         if v.Readonly {
146                 return MethodDisabledError
147         }
148         if v.Touchable {
149                 v.Timestamps[loc] = time.Now()
150                 return nil
151         }
152         return errors.New("Touch failed")
153 }
154
155 func (v *MockVolume) Mtime(loc string) (time.Time, error) {
156         v.gotCall("Mtime")
157         <-v.Gate
158         var mtime time.Time
159         var err error
160         if v.Bad {
161                 err = errors.New("Bad volume")
162         } else if t, ok := v.Timestamps[loc]; ok {
163                 mtime = t
164         } else {
165                 err = os.ErrNotExist
166         }
167         return mtime, err
168 }
169
170 func (v *MockVolume) IndexTo(prefix string, w io.Writer) error {
171         v.gotCall("IndexTo")
172         <-v.Gate
173         for loc, block := range v.Store {
174                 if !IsValidLocator(loc) || !strings.HasPrefix(loc, prefix) {
175                         continue
176                 }
177                 _, err := fmt.Fprintf(w, "%s+%d %d\n",
178                         loc, len(block), 123456789)
179                 if err != nil {
180                         return err
181                 }
182         }
183         return nil
184 }
185
186 func (v *MockVolume) Trash(loc string) error {
187         v.gotCall("Delete")
188         <-v.Gate
189         if v.Readonly {
190                 return MethodDisabledError
191         }
192         if _, ok := v.Store[loc]; ok {
193                 if time.Since(v.Timestamps[loc]) < blobSignatureTTL {
194                         return nil
195                 }
196                 delete(v.Store, loc)
197                 return nil
198         }
199         return os.ErrNotExist
200 }
201
202 // TBD
203 func (v *MockVolume) Untrash(loc string) error {
204         return nil
205 }
206
207 func (v *MockVolume) Status() *VolumeStatus {
208         var used uint64
209         for _, block := range v.Store {
210                 used = used + uint64(len(block))
211         }
212         return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
213 }
214
215 func (v *MockVolume) String() string {
216         return "[MockVolume]"
217 }
218
219 func (v *MockVolume) Writable() bool {
220         return !v.Readonly
221 }
222
223 func (v *MockVolume) Replication() int {
224         return 1
225 }
226
227 func (v *MockVolume) EmptyTrash() {
228 }