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