github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/state/backups/testing/fakes.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"io"
     8  
     9  	"github.com/juju/errors"
    10  	jc "github.com/juju/testing/checkers"
    11  	"github.com/juju/utils/filestorage"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/juju/names.v2"
    14  
    15  	"github.com/juju/juju/core/instance"
    16  	"github.com/juju/juju/state/backups"
    17  )
    18  
    19  // FakeBackups is an implementation of Backups to use for testing.
    20  // TODO: (hml) 2018-04-25
    21  // Let's change FakeBackups to using gomock or base.APICaller.
    22  // Checking calls made and arguments is a pain.
    23  type FakeBackups struct {
    24  	// Calls contains the order in which methods were called.
    25  	Calls []string
    26  
    27  	// Meta holds the Metadata to return.
    28  	Meta *backups.Metadata
    29  	// MetaList holds the Metadata list to return.
    30  	MetaList []*backups.Metadata
    31  	// Archive holds the archive file to return.
    32  	Archive io.ReadCloser
    33  	// Error holds the error to return.
    34  	Error error
    35  	// Filename holds the name of the file to return.
    36  	Filename string
    37  
    38  	// IDArg holds the ID that was passed in.
    39  	IDArg string
    40  	// PathsArg holds the Paths that was passed in.
    41  	PathsArg *backups.Paths
    42  	// DBInfoArg holds the ConnInfo that was passed in.
    43  	DBInfoArg *backups.DBInfo
    44  	// MetaArg holds the backup metadata that was passed in.
    45  	MetaArg *backups.Metadata
    46  	// PrivateAddr Holds the address for the internal network of the machine.
    47  	PrivateAddr string
    48  	// InstanceId is the id of the machine to be restored.
    49  	InstanceId instance.Id
    50  	// ArchiveArg holds the backup archive that was passed in.
    51  	ArchiveArg io.Reader
    52  	// KeepCopy holds the keepCopy bool that was passed in.
    53  	KeepCopy bool
    54  	// NoDownload holds the noDownload bool that was passed in.
    55  	NoDownload bool
    56  }
    57  
    58  var _ backups.Backups = (*FakeBackups)(nil)
    59  
    60  // Create creates and stores a new juju backup archive and returns
    61  // its associated metadata.
    62  func (b *FakeBackups) Create(
    63  	meta *backups.Metadata,
    64  	paths *backups.Paths,
    65  	dbInfo *backups.DBInfo,
    66  	keepCopy, noDownload bool,
    67  ) (string, error) {
    68  	b.Calls = append(b.Calls, "Create")
    69  
    70  	b.PathsArg = paths
    71  	b.DBInfoArg = dbInfo
    72  	b.MetaArg = meta
    73  	b.KeepCopy = keepCopy
    74  	b.NoDownload = noDownload
    75  
    76  	if b.Meta != nil {
    77  		*meta = *b.Meta
    78  	}
    79  
    80  	return b.Filename, b.Error
    81  }
    82  
    83  // Add stores the backup and returns its new ID.
    84  func (b *FakeBackups) Add(archive io.Reader, meta *backups.Metadata) (string, error) {
    85  	b.Calls = append(b.Calls, "Add")
    86  	b.ArchiveArg = archive
    87  	b.MetaArg = meta
    88  	id := ""
    89  	if b.Meta != nil {
    90  		id = b.Meta.ID()
    91  	}
    92  	return id, b.Error
    93  }
    94  
    95  // Get returns the metadata and archive file associated with the ID.
    96  func (b *FakeBackups) Get(id string) (*backups.Metadata, io.ReadCloser, error) {
    97  	b.Calls = append(b.Calls, "Get")
    98  	b.IDArg = id
    99  	return b.Meta, b.Archive, b.Error
   100  }
   101  
   102  // List returns the metadata for all stored backups.
   103  func (b *FakeBackups) List() ([]*backups.Metadata, error) {
   104  	b.Calls = append(b.Calls, "List")
   105  	return b.MetaList, b.Error
   106  }
   107  
   108  // Remove deletes the backup from storage.
   109  func (b *FakeBackups) Remove(id string) error {
   110  	b.Calls = append(b.Calls, "Remove")
   111  	b.IDArg = id
   112  	return errors.Trace(b.Error)
   113  }
   114  
   115  // Restore restores a machine to a backed up status.
   116  func (b *FakeBackups) Restore(bkpId string, args backups.RestoreArgs) (names.Tag, error) {
   117  	b.Calls = append(b.Calls, "Restore")
   118  	b.PrivateAddr = args.PrivateAddress
   119  	b.InstanceId = args.NewInstId
   120  	return nil, errors.Trace(b.Error)
   121  }
   122  
   123  // TODO(ericsnow) FakeStorage should probably move over to the utils repo.
   124  
   125  // FakeStorage is a FileStorage implementation to use when testing
   126  // backups.
   127  type FakeStorage struct {
   128  	// Calls contains the order in which methods were called.
   129  	Calls []string
   130  
   131  	// ID is the stored backup ID to return.
   132  	ID string
   133  	// Meta holds the Metadata to return.
   134  	Meta filestorage.Metadata
   135  	// MetaList holds the Metadata list to return.
   136  	MetaList []filestorage.Metadata
   137  	// File holds the stored file to return.
   138  	File io.ReadCloser
   139  	// Error holds the error to return.
   140  	Error error
   141  
   142  	// IDArg holds the ID that was passed in.
   143  	IDArg string
   144  	// MetaArg holds the Metadata that was passed in.
   145  	MetaArg filestorage.Metadata
   146  	// FileArg holds the file that was passed in.
   147  	FileArg io.Reader
   148  }
   149  
   150  // CheckCalled verifies that the fake was called as expected.
   151  func (s *FakeStorage) CheckCalled(c *gc.C, id string, meta filestorage.Metadata, file io.Reader, calls ...string) {
   152  	c.Check(s.Calls, jc.DeepEquals, calls)
   153  	c.Check(s.IDArg, gc.Equals, id)
   154  	c.Check(s.MetaArg, gc.Equals, meta)
   155  	c.Check(s.FileArg, gc.Equals, file)
   156  }
   157  
   158  func (s *FakeStorage) Metadata(id string) (filestorage.Metadata, error) {
   159  	s.Calls = append(s.Calls, "Metadata")
   160  	s.IDArg = id
   161  	return s.Meta, s.Error
   162  }
   163  
   164  func (s *FakeStorage) Get(id string) (filestorage.Metadata, io.ReadCloser, error) {
   165  	s.Calls = append(s.Calls, "Get")
   166  	s.IDArg = id
   167  	return s.Meta, s.File, s.Error
   168  }
   169  
   170  func (s *FakeStorage) List() ([]filestorage.Metadata, error) {
   171  	s.Calls = append(s.Calls, "List")
   172  	return s.MetaList, s.Error
   173  }
   174  
   175  func (s *FakeStorage) Add(meta filestorage.Metadata, file io.Reader) (string, error) {
   176  	s.Calls = append(s.Calls, "Add")
   177  	s.MetaArg = meta
   178  	s.FileArg = file
   179  	return s.ID, s.Error
   180  }
   181  
   182  func (s *FakeStorage) SetFile(id string, file io.Reader) error {
   183  	s.Calls = append(s.Calls, "SetFile")
   184  	s.IDArg = id
   185  	s.FileArg = file
   186  	return s.Error
   187  }
   188  
   189  func (s *FakeStorage) Remove(id string) error {
   190  	s.Calls = append(s.Calls, "Remove")
   191  	s.IDArg = id
   192  	return s.Error
   193  }
   194  
   195  func (s *FakeStorage) Close() error {
   196  	s.Calls = append(s.Calls, "Close")
   197  	return s.Error
   198  }