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 {
26 // [Over]write content for a locator with the given data,
27 // bypassing all constraints like readonly and serialize.
28 PutRaw(locator string, data []byte)
30 // Returns the strings that a driver uses to record read/write operations.
31 ReadWriteOperationLabelValues() (r, w string)
33 // Specify the value Mtime() should return, until the next
34 // call to Touch, TouchWithDate, or Put.
35 TouchWithDate(locator string, lastPut time.Time)
37 // Clean up, delete temporary files.
41 // MockVolumes are test doubles for Volumes, used to test handlers.
42 type MockVolume struct {
43 Store map[string][]byte
44 Timestamps map[string]time.Time
46 // Bad volumes return an error for every operation.
50 // Touchable volumes' Touch() method succeeds for a locator
51 // that has been Put().
54 // Readonly volumes return an error for Put, Delete, and
58 // Gate is a "starting gate", allowing test cases to pause
59 // volume operations long enough to inspect state. Every
60 // operation (except Status) starts by receiving from
61 // Gate. Sending one value unblocks one operation; closing the
62 // channel unblocks all operations. By default, Gate is a
63 // closed channel, so all operations proceed without
64 // blocking. See trash_worker_test.go for an example.
71 // CreateMockVolume returns a non-Bad, non-Readonly, Touchable mock
73 func CreateMockVolume() *MockVolume {
74 gate := make(chan struct{})
77 Store: make(map[string][]byte),
78 Timestamps: make(map[string]time.Time),
82 called: map[string]int{},
87 // CallCount returns how many times the named method has been called.
88 func (v *MockVolume) CallCount(method string) int {
90 defer v.mutex.Unlock()
91 c, ok := v.called[method]
98 func (v *MockVolume) gotCall(method string) {
100 defer v.mutex.Unlock()
101 if _, ok := v.called[method]; !ok {
108 func (v *MockVolume) Compare(ctx context.Context, loc string, buf []byte) error {
112 return v.BadVolumeError
113 } else if block, ok := v.Store[loc]; ok {
114 if fmt.Sprintf("%x", md5.Sum(block)) != loc {
117 if bytes.Compare(buf, block) != 0 {
118 return CollisionError
126 func (v *MockVolume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
130 return 0, v.BadVolumeError
131 } else if block, ok := v.Store[loc]; ok {
132 copy(buf[:len(block)], block)
133 return len(block), nil
135 return 0, os.ErrNotExist
138 func (v *MockVolume) Put(ctx context.Context, loc string, block []byte) error {
142 return v.BadVolumeError
145 return MethodDisabledError
151 func (v *MockVolume) Touch(loc string) error {
155 return MethodDisabledError
158 v.Timestamps[loc] = time.Now()
161 return errors.New("Touch failed")
164 func (v *MockVolume) Mtime(loc string) (time.Time, error) {
170 err = v.BadVolumeError
171 } else if t, ok := v.Timestamps[loc]; ok {
179 func (v *MockVolume) IndexTo(prefix string, w io.Writer) error {
182 for loc, block := range v.Store {
183 if !IsValidLocator(loc) || !strings.HasPrefix(loc, prefix) {
186 _, err := fmt.Fprintf(w, "%s+%d %d\n",
187 loc, len(block), 123456789)
195 func (v *MockVolume) Trash(loc string) error {
199 return MethodDisabledError
201 if _, ok := v.Store[loc]; ok {
202 if time.Since(v.Timestamps[loc]) < time.Duration(theConfig.BlobSignatureTTL) {
208 return os.ErrNotExist
211 func (v *MockVolume) DeviceID() string {
212 return "mock-device-id"
215 func (v *MockVolume) Type() string {
219 func (v *MockVolume) Start(vm *volumeMetricsVecs) error {
223 func (v *MockVolume) Untrash(loc string) error {
227 func (v *MockVolume) Status() *VolumeStatus {
229 for _, block := range v.Store {
230 used = used + uint64(len(block))
232 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
235 func (v *MockVolume) String() string {
236 return "[MockVolume]"
239 func (v *MockVolume) Writable() bool {
243 func (v *MockVolume) Replication() int {
247 func (v *MockVolume) EmptyTrash() {
250 func (v *MockVolume) GetStorageClasses() []string {