github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/internal/mock/backend.go (about)

     1  package mock
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  
     7  	"github.com/restic/restic/internal/errors"
     8  	"github.com/restic/restic/internal/restic"
     9  )
    10  
    11  // Backend implements a mock backend.
    12  type Backend struct {
    13  	CloseFn      func() error
    14  	IsNotExistFn func(err error) bool
    15  	SaveFn       func(ctx context.Context, h restic.Handle, rd io.Reader) error
    16  	LoadFn       func(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error)
    17  	StatFn       func(ctx context.Context, h restic.Handle) (restic.FileInfo, error)
    18  	ListFn       func(ctx context.Context, t restic.FileType) <-chan string
    19  	RemoveFn     func(ctx context.Context, h restic.Handle) error
    20  	TestFn       func(ctx context.Context, h restic.Handle) (bool, error)
    21  	DeleteFn     func(ctx context.Context) error
    22  	LocationFn   func() string
    23  }
    24  
    25  // Close the backend.
    26  func (m *Backend) Close() error {
    27  	if m.CloseFn == nil {
    28  		return nil
    29  	}
    30  
    31  	return m.CloseFn()
    32  }
    33  
    34  // Location returns a location string.
    35  func (m *Backend) Location() string {
    36  	if m.LocationFn == nil {
    37  		return ""
    38  	}
    39  
    40  	return m.LocationFn()
    41  }
    42  
    43  // IsNotExist returns true if the error is caused by a missing file.
    44  func (m *Backend) IsNotExist(err error) bool {
    45  	if m.IsNotExistFn == nil {
    46  		return false
    47  	}
    48  
    49  	return m.IsNotExistFn(err)
    50  }
    51  
    52  // Save data in the backend.
    53  func (m *Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) error {
    54  	if m.SaveFn == nil {
    55  		return errors.New("not implemented")
    56  	}
    57  
    58  	return m.SaveFn(ctx, h, rd)
    59  }
    60  
    61  // Load loads data from the backend.
    62  func (m *Backend) Load(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
    63  	if m.LoadFn == nil {
    64  		return nil, errors.New("not implemented")
    65  	}
    66  
    67  	return m.LoadFn(ctx, h, length, offset)
    68  }
    69  
    70  // Stat an object in the backend.
    71  func (m *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
    72  	if m.StatFn == nil {
    73  		return restic.FileInfo{}, errors.New("not implemented")
    74  	}
    75  
    76  	return m.StatFn(ctx, h)
    77  }
    78  
    79  // List items of type t.
    80  func (m *Backend) List(ctx context.Context, t restic.FileType) <-chan string {
    81  	if m.ListFn == nil {
    82  		ch := make(chan string)
    83  		close(ch)
    84  		return ch
    85  	}
    86  
    87  	return m.ListFn(ctx, t)
    88  }
    89  
    90  // Remove data from the backend.
    91  func (m *Backend) Remove(ctx context.Context, h restic.Handle) error {
    92  	if m.RemoveFn == nil {
    93  		return errors.New("not implemented")
    94  	}
    95  
    96  	return m.RemoveFn(ctx, h)
    97  }
    98  
    99  // Test for the existence of a specific item.
   100  func (m *Backend) Test(ctx context.Context, h restic.Handle) (bool, error) {
   101  	if m.TestFn == nil {
   102  		return false, errors.New("not implemented")
   103  	}
   104  
   105  	return m.TestFn(ctx, h)
   106  }
   107  
   108  // Delete all data.
   109  func (m *Backend) Delete(ctx context.Context) error {
   110  	if m.DeleteFn == nil {
   111  		return errors.New("not implemented")
   112  	}
   113  
   114  	return m.DeleteFn(ctx)
   115  }
   116  
   117  // Make sure that Backend implements the backend interface.
   118  var _ restic.Backend = &Backend{}