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