1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
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"
15
"github.com/juju/juju/instance"
16
"github.com/juju/juju/state/backups"
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.
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.
30
// Error holds the error to return.
33
// IDArg holds the ID that was passed in.
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.
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.
49
var _ backups.Backups = (*FakeBackups)(nil)
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")
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
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")
83
return b.Meta, b.Archive, b.Error
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
92
// Remove deletes the backup from storage.
93
func (b *FakeBackups) Remove(id string) error {
94
b.Calls = append(b.Calls, "Remove")
96
return errors.Trace(b.Error)
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)
107
// TODO(ericsnow) FakeStorage should probably move over to the utils repo.
109
// FakeStorage is a FileStorage implementation to use when testing
111
type FakeStorage struct {
112
// Calls contains the order in which methods were called.
115
// ID is the stored backup ID to return.
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.
123
// Error holds the error to return.
126
// IDArg holds the ID that was passed in.
128
// MetaArg holds the Metadata that was passed in.
129
MetaArg filestorage.Metadata
130
// FileArg holds the file that was passed in.
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)
142
func (s *FakeStorage) Metadata(id string) (filestorage.Metadata, error) {
143
s.Calls = append(s.Calls, "Metadata")
145
return s.Meta, s.Error
148
func (s *FakeStorage) Get(id string) (filestorage.Metadata, io.ReadCloser, error) {
149
s.Calls = append(s.Calls, "Get")
151
return s.Meta, s.File, s.Error
154
func (s *FakeStorage) List() ([]filestorage.Metadata, error) {
155
s.Calls = append(s.Calls, "List")
156
return s.MetaList, s.Error
159
func (s *FakeStorage) Add(meta filestorage.Metadata, file io.Reader) (string, error) {
160
s.Calls = append(s.Calls, "Add")
166
func (s *FakeStorage) SetFile(id string, file io.Reader) error {
167
s.Calls = append(s.Calls, "SetFile")
173
func (s *FakeStorage) Remove(id string) error {
174
s.Calls = append(s.Calls, "Remove")
179
func (s *FakeStorage) Close() error {
180
s.Calls = append(s.Calls, "Close")