github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/backups/restore_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package backups_test 5 6 import ( 7 "github.com/juju/errors" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/juju/apiserver/params" 11 "github.com/juju/juju/cloud" 12 "github.com/juju/juju/cmd/juju/backups" 13 "github.com/juju/juju/environs" 14 "github.com/juju/juju/environs/bootstrap" 15 "github.com/juju/juju/instance" 16 "github.com/juju/juju/jujuclient" 17 "github.com/juju/juju/jujuclient/jujuclienttesting" 18 _ "github.com/juju/juju/provider/dummy" 19 "github.com/juju/juju/testing" 20 ) 21 22 type restoreSuite struct { 23 BaseBackupsSuite 24 store *jujuclienttesting.MemStore 25 } 26 27 var _ = gc.Suite(&restoreSuite{}) 28 29 func (s *restoreSuite) SetUpTest(c *gc.C) { 30 s.BaseBackupsSuite.SetUpTest(c) 31 s.store = jujuclienttesting.NewMemStore() 32 s.store.Controllers["testing"] = jujuclient.ControllerDetails{ 33 ControllerUUID: testing.ModelTag.Id(), 34 CACert: testing.CACert, 35 } 36 s.store.Models["testing"] = jujuclient.ControllerAccountModels{ 37 AccountModels: map[string]*jujuclient.AccountModels{ 38 "admin@local": { 39 Models: map[string]jujuclient.ModelDetails{ 40 "admin": {"test1-uuid"}, 41 }, 42 CurrentModel: "admin", 43 }, 44 }, 45 } 46 s.store.Accounts["testing"] = &jujuclient.ControllerAccounts{ 47 Accounts: map[string]jujuclient.AccountDetails{ 48 "admin@local": { 49 User: "current-user@local", 50 Password: "old-password", 51 }, 52 }, 53 CurrentAccount: "admin@local", 54 } 55 s.store.BootstrapConfig["testing"] = jujuclient.BootstrapConfig{ 56 Cloud: "dummy", 57 //Credential: "me", 58 Config: map[string]interface{}{ 59 "type": "dummy", 60 "name": "admin", 61 }, 62 } 63 s.store.Credentials["dummy"] = cloud.CloudCredential{ 64 AuthCredentials: map[string]cloud.Credential{ 65 "me": cloud.NewCredential(cloud.UserPassAuthType, map[string]string{ 66 "username": "user", 67 "password": "sekret", 68 }), 69 }, 70 } 71 } 72 73 func (s *restoreSuite) TestRestoreArgs(c *gc.C) { 74 s.command = backups.NewRestoreCommandForTest(s.store, nil, nil, nil) 75 _, err := testing.RunCommand(c, s.command, "restore") 76 c.Assert(err, gc.ErrorMatches, "you must specify either a file or a backup id.") 77 78 _, err = testing.RunCommand(c, s.command, "restore", "--id", "anid", "--file", "afile") 79 c.Assert(err, gc.ErrorMatches, "you must specify either a file or a backup id but not both.") 80 81 _, err = testing.RunCommand(c, s.command, "restore", "--id", "anid", "-b") 82 c.Assert(err, gc.ErrorMatches, "it is not possible to rebootstrap and restore from an id.") 83 } 84 85 // TODO(wallyworld) - add more api related unit tests 86 type mockRestoreAPI struct { 87 backups.RestoreAPI 88 } 89 90 func (*mockRestoreAPI) Close() error { 91 return nil 92 } 93 94 type mockArchiveReader struct { 95 backups.ArchiveReader 96 } 97 98 func (*mockArchiveReader) Close() error { 99 return nil 100 } 101 102 func (s *restoreSuite) TestRestoreReboostrapControllerExists(c *gc.C) { 103 fakeEnv := fakeEnviron{controllerInstances: []instance.Id{"1"}} 104 s.command = backups.NewRestoreCommandForTest( 105 s.store, &mockRestoreAPI{}, 106 func(string) (backups.ArchiveReader, *params.BackupsMetadataResult, error) { 107 return &mockArchiveReader{}, ¶ms.BackupsMetadataResult{}, nil 108 }, 109 func(string, *params.BackupsMetadataResult) (environs.Environ, error) { 110 return fakeEnv, nil 111 }) 112 _, err := testing.RunCommand(c, s.command, "restore", "--file", "afile", "-b") 113 c.Assert(err, gc.ErrorMatches, ".*still seems to exist.*") 114 } 115 116 func (s *restoreSuite) TestRestoreReboostrapNoControllers(c *gc.C) { 117 fakeEnv := fakeEnviron{} 118 s.command = backups.NewRestoreCommandForTest( 119 s.store, &mockRestoreAPI{}, 120 func(string) (backups.ArchiveReader, *params.BackupsMetadataResult, error) { 121 return &mockArchiveReader{}, ¶ms.BackupsMetadataResult{}, nil 122 }, 123 func(string, *params.BackupsMetadataResult) (environs.Environ, error) { 124 return fakeEnv, nil 125 }) 126 s.PatchValue(&backups.BootstrapFunc, func(ctx environs.BootstrapContext, environ environs.Environ, args bootstrap.BootstrapParams) error { 127 return errors.New("failed to bootstrap new controller") 128 }) 129 130 _, err := testing.RunCommand(c, s.command, "restore", "--file", "afile", "-b") 131 c.Assert(err, gc.ErrorMatches, ".*failed to bootstrap new controller") 132 } 133 134 func (s *restoreSuite) TestRestoreReboostrapReadsMetadata(c *gc.C) { 135 metadata := params.BackupsMetadataResult{ 136 CACert: testing.CACert, 137 CAPrivateKey: testing.CAKey, 138 } 139 s.command = backups.NewRestoreCommandForTest( 140 s.store, &mockRestoreAPI{}, 141 func(string) (backups.ArchiveReader, *params.BackupsMetadataResult, error) { 142 return &mockArchiveReader{}, &metadata, nil 143 }, 144 nil) 145 s.PatchValue(&backups.BootstrapFunc, func(ctx environs.BootstrapContext, environ environs.Environ, args bootstrap.BootstrapParams) error { 146 attr := environ.Config().AllAttrs() 147 c.Assert(attr["ca-cert"], gc.Equals, testing.CACert) 148 return errors.New("failed to bootstrap new controller") 149 }) 150 151 _, err := testing.RunCommand(c, s.command, "restore", "-m", "testing:test1", "--file", "afile", "-b") 152 c.Assert(err, gc.ErrorMatches, ".*failed to bootstrap new controller") 153 } 154 155 type fakeInstance struct { 156 instance.Instance 157 id instance.Id 158 } 159 160 type fakeEnviron struct { 161 environs.Environ 162 controllerInstances []instance.Id 163 } 164 165 func (f fakeEnviron) ControllerInstances() ([]instance.Id, error) { 166 return f.controllerInstances, nil 167 } 168 169 func (f fakeEnviron) Instances(ids []instance.Id) ([]instance.Instance, error) { 170 return []instance.Instance{fakeInstance{id: "1"}}, nil 171 }