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.
45 // Touchable volumes' Touch() method succeeds for a locator
46 // that has been Put().
49 // Readonly volumes return an error for Put, Delete, and
53 // Gate is a "starting gate", allowing test cases to pause
54 // volume operations long enough to inspect state. Every
55 // operation (except Status) starts by receiving from
56 // Gate. Sending one value unblocks one operation; closing the
57 // channel unblocks all operations. By default, Gate is a
58 // closed channel, so all operations proceed without
59 // blocking. See trash_worker_test.go for an example.
66 // CreateMockVolume returns a non-Bad, non-Readonly, Touchable mock
68 func CreateMockVolume() *MockVolume {
69 gate := make(chan struct{})
72 Store: make(map[string][]byte),
73 Timestamps: make(map[string]time.Time),
77 called: map[string]int{},
82 // CallCount returns how many times the named method has been called.
83 func (v *MockVolume) CallCount(method string) int {
85 defer v.mutex.Unlock()
86 c, ok := v.called[method]
93 func (v *MockVolume) gotCall(method string) {
95 defer v.mutex.Unlock()
96 if _, ok := v.called[method]; !ok {
103 func (v *MockVolume) Compare(ctx context.Context, loc string, buf []byte) error {
107 return errors.New("Bad volume")
108 } else if block, ok := v.Store[loc]; ok {
109 if fmt.Sprintf("%x", md5.Sum(block)) != loc {
112 if bytes.Compare(buf, block) != 0 {
113 return CollisionError
121 func (v *MockVolume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
125 return 0, errors.New("Bad volume")
126 } else if block, ok := v.Store[loc]; ok {
127 copy(buf[:len(block)], block)
128 return len(block), nil
130 return 0, os.ErrNotExist
133 func (v *MockVolume) Put(ctx context.Context, loc string, block []byte) error {
137 return errors.New("Bad volume")
140 return MethodDisabledError
146 func (v *MockVolume) Touch(loc string) error {
150 return MethodDisabledError
153 v.Timestamps[loc] = time.Now()
156 return errors.New("Touch failed")
159 func (v *MockVolume) Mtime(loc string) (time.Time, error) {
165 err = errors.New("Bad volume")
166 } else if t, ok := v.Timestamps[loc]; ok {
174 func (v *MockVolume) IndexTo(prefix string, w io.Writer) error {
177 for loc, block := range v.Store {
178 if !IsValidLocator(loc) || !strings.HasPrefix(loc, prefix) {
181 _, err := fmt.Fprintf(w, "%s+%d %d\n",
182 loc, len(block), 123456789)
190 func (v *MockVolume) Trash(loc string) error {
194 return MethodDisabledError
196 if _, ok := v.Store[loc]; ok {
197 if time.Since(v.Timestamps[loc]) < time.Duration(theConfig.BlobSignatureTTL) {
203 return os.ErrNotExist
206 func (v *MockVolume) DeviceID() string {
207 return "mock-device-id"
210 func (v *MockVolume) Type() string {
214 func (v *MockVolume) Start() error {
218 func (v *MockVolume) Untrash(loc string) error {
222 func (v *MockVolume) Status() *VolumeStatus {
224 for _, block := range v.Store {
225 used = used + uint64(len(block))
227 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
230 func (v *MockVolume) String() string {
231 return "[MockVolume]"
234 func (v *MockVolume) Writable() bool {
238 func (v *MockVolume) Replication() int {
242 func (v *MockVolume) EmptyTrash() {
245 func (v *MockVolume) GetStorageClasses() []string {