12 // MockVolumes are test doubles for Volumes, used to test handlers.
13 type MockVolume struct {
14 Store map[string][]byte
15 Timestamps map[string]time.Time
16 // Bad volumes return an error for every operation.
18 // Touchable volumes' Touch() method succeeds for a locator
19 // that has been Put().
21 // Readonly volumes return an error for Put, Delete, and
28 // CreateMockVolume returns a non-Bad, non-Readonly, Touchable mock
30 func CreateMockVolume() *MockVolume {
32 Store: make(map[string][]byte),
33 Timestamps: make(map[string]time.Time),
37 called: map[string]int{},
41 // CallCount returns how many times the named method has been called.
42 func (v *MockVolume) CallCount(method string) int {
44 defer v.mutex.Unlock()
45 if c, ok := v.called[method]; !ok {
52 func (v *MockVolume) gotCall(method string) {
54 defer v.mutex.Unlock()
55 if _, ok := v.called[method]; !ok {
62 func (v *MockVolume) Get(loc string) ([]byte, error) {
65 return nil, errors.New("Bad volume")
66 } else if block, ok := v.Store[loc]; ok {
69 return nil, os.ErrNotExist
72 func (v *MockVolume) Put(loc string, block []byte) error {
75 return errors.New("Bad volume")
78 return MethodDisabledError
84 func (v *MockVolume) Touch(loc string) error {
87 return MethodDisabledError
90 v.Timestamps[loc] = time.Now()
93 return errors.New("Touch failed")
96 func (v *MockVolume) Mtime(loc string) (time.Time, error) {
101 err = errors.New("Bad volume")
102 } else if t, ok := v.Timestamps[loc]; ok {
110 func (v *MockVolume) Index(prefix string) string {
113 for loc, block := range v.Store {
114 if IsValidLocator(loc) && strings.HasPrefix(loc, prefix) {
115 result = result + fmt.Sprintf("%s+%d %d\n",
116 loc, len(block), 123456789)
122 func (v *MockVolume) Delete(loc string) error {
125 return MethodDisabledError
127 if _, ok := v.Store[loc]; ok {
128 if time.Since(v.Timestamps[loc]) < blob_signature_ttl {
134 return os.ErrNotExist
137 func (v *MockVolume) Status() *VolumeStatus {
139 for _, block := range v.Store {
140 used = used + uint64(len(block))
142 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
145 func (v *MockVolume) String() string {
146 return "[MockVolume]"
149 func (v *MockVolume) Writable() bool {