github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/jujuclient/accounts_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuclient_test 5 6 import ( 7 "os" 8 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/jujuclient" 14 "github.com/juju/juju/testing" 15 ) 16 17 type AccountsSuite struct { 18 testing.FakeJujuXDGDataHomeSuite 19 store jujuclient.AccountStore 20 } 21 22 var _ = gc.Suite(&AccountsSuite{}) 23 24 func (s *AccountsSuite) SetUpTest(c *gc.C) { 25 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 26 s.store = jujuclient.NewFileClientStore() 27 writeTestAccountsFile(c) 28 } 29 30 func (s *AccountsSuite) TestAccountByNameNoFile(c *gc.C) { 31 err := os.Remove(jujuclient.JujuAccountsPath()) 32 c.Assert(err, jc.ErrorIsNil) 33 details, err := s.store.AccountByName("not-found", "admin@local") 34 c.Assert(err, gc.ErrorMatches, "controller not-found not found") 35 c.Assert(details, gc.IsNil) 36 } 37 38 func (s *AccountsSuite) TestAccountByNameControllerNotFound(c *gc.C) { 39 details, err := s.store.AccountByName("not-found", "admin@local") 40 c.Assert(err, gc.ErrorMatches, "controller not-found not found") 41 c.Assert(details, gc.IsNil) 42 } 43 44 func (s *AccountsSuite) TestAccountByNameAccountNotFound(c *gc.C) { 45 details, err := s.store.AccountByName("kontroll", "admin@nowhere") 46 c.Assert(err, gc.ErrorMatches, "account kontroll:admin@nowhere not found") 47 c.Assert(details, gc.IsNil) 48 } 49 50 func (s *AccountsSuite) TestAccountByName(c *gc.C) { 51 details, err := s.store.AccountByName("kontroll", "admin@local") 52 c.Assert(err, jc.ErrorIsNil) 53 c.Assert(details, gc.NotNil) 54 c.Assert(*details, jc.DeepEquals, testControllerAccounts["kontroll"].Accounts["admin@local"]) 55 } 56 57 func (s *AccountsSuite) TestAllAccountsNoFile(c *gc.C) { 58 err := os.Remove(jujuclient.JujuAccountsPath()) 59 c.Assert(err, jc.ErrorIsNil) 60 accounts, err := s.store.AllAccounts("not-found") 61 c.Assert(err, gc.ErrorMatches, "accounts for controller not-found not found") 62 c.Assert(accounts, gc.HasLen, 0) 63 } 64 65 func (s *AccountsSuite) TestAllAccounts(c *gc.C) { 66 accounts, err := s.store.AllAccounts("kontroll") 67 c.Assert(err, jc.ErrorIsNil) 68 c.Assert(accounts, jc.DeepEquals, testControllerAccounts["kontroll"].Accounts) 69 } 70 71 func (s *AccountsSuite) TestCurrentAccount(c *gc.C) { 72 current, err := s.store.CurrentAccount("kontroll") 73 c.Assert(err, jc.ErrorIsNil) 74 c.Assert(current, gc.Equals, "admin@local") 75 } 76 77 func (s *AccountsSuite) TestCurrentAccountNotSet(c *gc.C) { 78 _, err := s.store.CurrentAccount("ctrl") 79 c.Assert(err, jc.Satisfies, errors.IsNotFound) 80 } 81 82 func (s *AccountsSuite) TestCurrentAccountControllerNotFound(c *gc.C) { 83 _, err := s.store.CurrentAccount("not-found") 84 c.Assert(err, jc.Satisfies, errors.IsNotFound) 85 } 86 87 func (s *AccountsSuite) TestSetCurrentAccountControllerNotFound(c *gc.C) { 88 err := s.store.SetCurrentAccount("not-found", "admin@local") 89 c.Assert(err, jc.Satisfies, errors.IsNotFound) 90 } 91 92 func (s *AccountsSuite) TestSetCurrentAccountAccountNotFound(c *gc.C) { 93 err := s.store.SetCurrentAccount("kontroll", "admin@nowhere") 94 c.Assert(err, jc.Satisfies, errors.IsNotFound) 95 } 96 97 func (s *AccountsSuite) TestSetCurrentAccount(c *gc.C) { 98 err := s.store.SetCurrentAccount("kontroll", "admin@local") 99 c.Assert(err, jc.ErrorIsNil) 100 accounts, err := jujuclient.ReadAccountsFile(jujuclient.JujuAccountsPath()) 101 c.Assert(err, jc.ErrorIsNil) 102 c.Assert(accounts["kontroll"].CurrentAccount, gc.Equals, "admin@local") 103 } 104 105 func (s *AccountsSuite) TestUpdateAccountNewController(c *gc.C) { 106 testAccountDetails := jujuclient.AccountDetails{User: "admin@local"} 107 err := s.store.UpdateAccount("new-controller", "admin@local", testAccountDetails) 108 c.Assert(err, jc.ErrorIsNil) 109 accounts, err := s.store.AllAccounts("new-controller") 110 c.Assert(err, jc.ErrorIsNil) 111 c.Assert(accounts, jc.DeepEquals, map[string]jujuclient.AccountDetails{ 112 "admin@local": testAccountDetails, 113 }) 114 } 115 116 func (s *AccountsSuite) TestUpdateAccountExistingControllerMultipleAccounts(c *gc.C) { 117 testAccountDetails := jujuclient.AccountDetails{User: "bob@environs"} 118 err := s.store.UpdateAccount("kontroll", "bob@environs", testAccountDetails) 119 c.Assert(err, jc.Satisfies, errors.IsAlreadyExists) 120 c.Assert(err, gc.ErrorMatches, "alternative account for controller kontroll already exists") 121 accounts, err := s.store.AllAccounts("kontroll") 122 c.Assert(err, jc.ErrorIsNil) 123 _, ok := accounts["bob@environs"] 124 c.Assert(ok, jc.IsFalse) 125 } 126 127 func (s *AccountsSuite) TestUpdateAccountExistingControllerNewAccount(c *gc.C) { 128 accounts, err := s.store.AllAccounts("kontroll") 129 c.Assert(err, jc.ErrorIsNil) 130 for account := range accounts { 131 err := s.store.RemoveAccount("kontroll", account) 132 c.Assert(err, jc.ErrorIsNil) 133 } 134 testAccountDetails := jujuclient.AccountDetails{User: "bob@environs"} 135 err = s.store.UpdateAccount("kontroll", "bob@environs", testAccountDetails) 136 c.Assert(err, jc.ErrorIsNil) 137 accounts, err = s.store.AllAccounts("kontroll") 138 c.Assert(err, jc.ErrorIsNil) 139 c.Assert(accounts, jc.DeepEquals, map[string]jujuclient.AccountDetails{ 140 "bob@environs": testAccountDetails, 141 }) 142 } 143 144 func (s *AccountsSuite) TestUpdateAccountOverwrites(c *gc.C) { 145 testAccountDetails := jujuclient.AccountDetails{ 146 User: "admin@local", 147 Password: "fnord", 148 } 149 for i := 0; i < 2; i++ { 150 // Twice so we exercise the code path of updating with 151 // identical details. 152 err := s.store.UpdateAccount("kontroll", "admin@local", testAccountDetails) 153 c.Assert(err, jc.ErrorIsNil) 154 details, err := s.store.AccountByName("kontroll", "admin@local") 155 c.Assert(err, jc.ErrorIsNil) 156 c.Assert(*details, jc.DeepEquals, testAccountDetails) 157 } 158 } 159 160 func (s *AccountsSuite) TestRemoveAccountNoFile(c *gc.C) { 161 err := os.Remove(jujuclient.JujuAccountsPath()) 162 c.Assert(err, jc.ErrorIsNil) 163 err = s.store.RemoveAccount("not-found", "admin@nowhere") 164 c.Assert(err, jc.Satisfies, errors.IsNotFound) 165 } 166 167 func (s *AccountsSuite) TestRemoveAccountControllerNotFound(c *gc.C) { 168 err := s.store.RemoveAccount("not-found", "admin@nowhere") 169 c.Assert(err, jc.Satisfies, errors.IsNotFound) 170 } 171 172 func (s *AccountsSuite) TestRemoveAccountNotFound(c *gc.C) { 173 err := s.store.RemoveAccount("kontroll", "admin@nowhere") 174 c.Assert(err, jc.Satisfies, errors.IsNotFound) 175 } 176 177 func (s *AccountsSuite) TestRemoveAccount(c *gc.C) { 178 err := s.store.RemoveAccount("kontroll", "admin@local") 179 c.Assert(err, jc.ErrorIsNil) 180 _, err = s.store.AccountByName("kontroll", "admin@local") 181 c.Assert(err, jc.Satisfies, errors.IsNotFound) 182 } 183 184 func (s *AccountsSuite) TestRemoveControllerRemovesaccounts(c *gc.C) { 185 store := jujuclient.NewFileClientStore() 186 err := store.UpdateController("kontroll", jujuclient.ControllerDetails{ 187 ControllerUUID: "abc", 188 CACert: "woop", 189 }) 190 c.Assert(err, jc.ErrorIsNil) 191 err = store.RemoveController("kontroll") 192 c.Assert(err, jc.ErrorIsNil) 193 194 accounts, err := jujuclient.ReadAccountsFile(jujuclient.JujuAccountsPath()) 195 c.Assert(err, jc.ErrorIsNil) 196 _, ok := accounts["kontroll"] 197 c.Assert(ok, jc.IsFalse) // kontroll accounts are removed 198 }