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 {
68 buf := bufs.Get(len(block))
72 return nil, os.ErrNotExist
75 func (v *MockVolume) Put(loc string, block []byte) error {
78 return errors.New("Bad volume")
81 return MethodDisabledError
87 func (v *MockVolume) Touch(loc string) error {
90 return MethodDisabledError
93 v.Timestamps[loc] = time.Now()
96 return errors.New("Touch failed")
99 func (v *MockVolume) Mtime(loc string) (time.Time, error) {
104 err = errors.New("Bad volume")
105 } else if t, ok := v.Timestamps[loc]; ok {
113 func (v *MockVolume) IndexTo(prefix string, w io.Writer) error {
115 for loc, block := range v.Store {
116 if !IsValidLocator(loc) || !strings.HasPrefix(loc, prefix) {
119 _, err := fmt.Fprintf(w, "%s+%d %d\n",
120 loc, len(block), 123456789)
128 func (v *MockVolume) Delete(loc string) error {
131 return MethodDisabledError
133 if _, ok := v.Store[loc]; ok {
134 if time.Since(v.Timestamps[loc]) < blob_signature_ttl {
140 return os.ErrNotExist
143 func (v *MockVolume) Status() *VolumeStatus {
145 for _, block := range v.Store {
146 used = used + uint64(len(block))
148 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
151 func (v *MockVolume) String() string {
152 return "[MockVolume]"
155 func (v *MockVolume) Writable() bool {