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