13 // MockVolumes are test doubles for Volumes, used to test handlers.
14 type MockVolume struct {
15 Store map[string][]byte
16 Timestamps map[string]time.Time
17 // Bad volumes return an error for every operation.
19 // Touchable volumes' Touch() method succeeds for a locator
20 // that has been Put().
22 // Readonly volumes return an error for Put, Delete, and
29 // CreateMockVolume returns a non-Bad, non-Readonly, Touchable mock
31 func CreateMockVolume() *MockVolume {
33 Store: make(map[string][]byte),
34 Timestamps: make(map[string]time.Time),
38 called: map[string]int{},
42 // CallCount returns how many times the named method has been called.
43 func (v *MockVolume) CallCount(method string) int {
45 defer v.mutex.Unlock()
46 if c, ok := v.called[method]; !ok {
53 func (v *MockVolume) gotCall(method string) {
55 defer v.mutex.Unlock()
56 if _, ok := v.called[method]; !ok {
63 func (v *MockVolume) Get(loc string) ([]byte, error) {
66 return nil, errors.New("Bad volume")
67 } else if block, ok := v.Store[loc]; ok {
70 return nil, os.ErrNotExist
73 func (v *MockVolume) Put(loc string, block []byte) error {
76 return errors.New("Bad volume")
79 return MethodDisabledError
85 func (v *MockVolume) Touch(loc string) error {
88 return MethodDisabledError
91 v.Timestamps[loc] = time.Now()
94 return errors.New("Touch failed")
97 func (v *MockVolume) Mtime(loc string) (time.Time, error) {
102 err = errors.New("Bad volume")
103 } else if t, ok := v.Timestamps[loc]; ok {
111 func (v *MockVolume) IndexTo(prefix string, w io.Writer) error {
113 for loc, block := range v.Store {
114 if !IsValidLocator(loc) || !strings.HasPrefix(loc, prefix) {
117 _, err := fmt.Fprintf(w, "%s+%d %d\n",
118 loc, len(block), 123456789)
126 func (v *MockVolume) Delete(loc string) error {
129 return MethodDisabledError
131 if _, ok := v.Store[loc]; ok {
132 if time.Since(v.Timestamps[loc]) < blob_signature_ttl {
138 return os.ErrNotExist
141 func (v *MockVolume) Status() *VolumeStatus {
143 for _, block := range v.Store {
144 used = used + uint64(len(block))
146 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
149 func (v *MockVolume) String() string {
150 return "[MockVolume]"
153 func (v *MockVolume) Writable() bool {