2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-29 13:22:43 +00:00
restic/backend/mock_backend.go

104 lines
2.2 KiB
Go
Raw Normal View History

2015-11-22 14:15:07 +00:00
package backend
2016-01-24 00:00:27 +00:00
import "errors"
2015-11-22 14:15:07 +00:00
// MockBackend implements a backend whose functions can be specified. This
// should only be used for tests.
type MockBackend struct {
2016-01-24 00:00:27 +00:00
CloseFn func() error
LoadFn func(h Handle, p []byte, off int64) (int, error)
2016-01-24 00:15:35 +00:00
SaveFn func(h Handle, p []byte) error
2016-01-24 00:00:27 +00:00
StatFn func(h Handle) (BlobInfo, error)
ListFn func(Type, <-chan struct{}) <-chan string
RemoveFn func(Type, string) error
TestFn func(Type, string) (bool, error)
DeleteFn func() error
LocationFn func() string
2015-11-22 14:15:07 +00:00
}
2016-01-26 20:56:13 +00:00
// Close the backend.
2015-11-22 14:15:07 +00:00
func (m *MockBackend) Close() error {
if m.CloseFn == nil {
return nil
}
return m.CloseFn()
}
2016-01-26 20:56:13 +00:00
// Location returns a location string.
2015-11-22 14:15:07 +00:00
func (m *MockBackend) Location() string {
if m.LocationFn == nil {
return ""
}
return m.LocationFn()
}
2016-01-26 20:56:13 +00:00
// Load loads data from the backend.
2016-01-23 13:12:12 +00:00
func (m *MockBackend) Load(h Handle, p []byte, off int64) (int, error) {
if m.LoadFn == nil {
return 0, errors.New("not implemented")
}
2016-01-23 16:19:47 +00:00
return m.LoadFn(h, p, off)
2016-01-23 13:12:12 +00:00
}
2016-01-26 20:56:13 +00:00
// Save data in the backend.
2016-01-24 00:15:35 +00:00
func (m *MockBackend) Save(h Handle, p []byte) error {
if m.SaveFn == nil {
return errors.New("not implemented")
}
return m.SaveFn(h, p)
}
2016-01-26 20:56:13 +00:00
// Stat an object in the backend.
2016-01-23 22:27:58 +00:00
func (m *MockBackend) Stat(h Handle) (BlobInfo, error) {
if m.StatFn == nil {
return BlobInfo{}, errors.New("not implemented")
}
return m.StatFn(h)
}
2016-01-26 20:56:13 +00:00
// List items of type t.
2015-11-22 14:15:07 +00:00
func (m *MockBackend) List(t Type, done <-chan struct{}) <-chan string {
if m.ListFn == nil {
ch := make(chan string)
close(ch)
return ch
}
return m.ListFn(t, done)
}
2016-01-26 20:56:13 +00:00
// Remove data from the backend.
2015-11-22 14:15:07 +00:00
func (m *MockBackend) Remove(t Type, name string) error {
if m.RemoveFn == nil {
return errors.New("not implemented")
}
return m.RemoveFn(t, name)
}
2016-01-26 20:56:13 +00:00
// Test for the existence of a specific item.
2015-11-22 14:15:07 +00:00
func (m *MockBackend) Test(t Type, name string) (bool, error) {
if m.TestFn == nil {
return false, errors.New("not implemented")
}
return m.TestFn(t, name)
}
2016-01-26 20:56:13 +00:00
// Delete all data.
2015-11-22 14:15:07 +00:00
func (m *MockBackend) Delete() error {
if m.DeleteFn == nil {
return errors.New("not implemented")
}
return m.DeleteFn()
}
// Make sure that MockBackend implements the backend interface.
var _ Backend = &MockBackend{}