1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 // A TestableVolume allows test suites to manipulate the state of an
21 // underlying Volume, in order to test behavior in cases that are
22 // impractical to achieve with a sequence of normal Volume operations.
23 type TestableVolume interface {
25 // [Over]write content for a locator with the given data,
26 // bypassing all constraints like readonly and serialize.
27 PutRaw(locator string, data []byte)
29 // Specify the value Mtime() should return, until the next
30 // call to Touch, TouchWithDate, or Put.
31 TouchWithDate(locator string, lastPut time.Time)
33 // Clean up, delete temporary files.
37 // MockVolumes are test doubles for Volumes, used to test handlers.
38 type MockVolume struct {
39 Store map[string][]byte
40 Timestamps map[string]time.Time
42 // Bad volumes return an error for every operation.
46 // Touchable volumes' Touch() method succeeds for a locator
47 // that has been Put().
50 // Readonly volumes return an error for Put, Delete, and
54 // Gate is a "starting gate", allowing test cases to pause
55 // volume operations long enough to inspect state. Every
56 // operation (except Status) starts by receiving from
57 // Gate. Sending one value unblocks one operation; closing the
58 // channel unblocks all operations. By default, Gate is a
59 // closed channel, so all operations proceed without
60 // blocking. See trash_worker_test.go for an example.
67 // CreateMockVolume returns a non-Bad, non-Readonly, Touchable mock
69 func CreateMockVolume() *MockVolume {
70 gate := make(chan struct{})
73 Store: make(map[string][]byte),
74 Timestamps: make(map[string]time.Time),
78 called: map[string]int{},
83 // CallCount returns how many times the named method has been called.
84 func (v *MockVolume) CallCount(method string) int {
86 defer v.mutex.Unlock()
87 c, ok := v.called[method]
94 func (v *MockVolume) gotCall(method string) {
96 defer v.mutex.Unlock()
97 if _, ok := v.called[method]; !ok {
104 func (v *MockVolume) Compare(ctx context.Context, loc string, buf []byte) error {
108 return v.BadVolumeError
109 } else if block, ok := v.Store[loc]; ok {
110 if fmt.Sprintf("%x", md5.Sum(block)) != loc {
113 if bytes.Compare(buf, block) != 0 {
114 return CollisionError
122 func (v *MockVolume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
126 return 0, v.BadVolumeError
127 } else if block, ok := v.Store[loc]; ok {
128 copy(buf[:len(block)], block)
129 return len(block), nil
131 return 0, os.ErrNotExist
134 func (v *MockVolume) Put(ctx context.Context, loc string, block []byte) error {
138 return v.BadVolumeError
141 return MethodDisabledError
147 func (v *MockVolume) Touch(loc string) error {
151 return MethodDisabledError
154 v.Timestamps[loc] = time.Now()
157 return errors.New("Touch failed")
160 func (v *MockVolume) Mtime(loc string) (time.Time, error) {
166 err = v.BadVolumeError
167 } else if t, ok := v.Timestamps[loc]; ok {
175 func (v *MockVolume) IndexTo(prefix string, w io.Writer) error {
178 for loc, block := range v.Store {
179 if !IsValidLocator(loc) || !strings.HasPrefix(loc, prefix) {
182 _, err := fmt.Fprintf(w, "%s+%d %d\n",
183 loc, len(block), 123456789)
191 func (v *MockVolume) Trash(loc string) error {
195 return MethodDisabledError
197 if _, ok := v.Store[loc]; ok {
198 if time.Since(v.Timestamps[loc]) < time.Duration(theConfig.BlobSignatureTTL) {
204 return os.ErrNotExist
207 func (v *MockVolume) DeviceID() string {
208 return "mock-device-id"
211 func (v *MockVolume) Type() string {
215 func (v *MockVolume) Start() error {
219 func (v *MockVolume) Untrash(loc string) error {
223 func (v *MockVolume) Status() *VolumeStatus {
225 for _, block := range v.Store {
226 used = used + uint64(len(block))
228 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
231 func (v *MockVolume) String() string {
232 return "[MockVolume]"
235 func (v *MockVolume) Writable() bool {
239 func (v *MockVolume) Replication() int {
243 func (v *MockVolume) EmptyTrash() {
246 func (v *MockVolume) GetStorageClasses() []string {