1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package jujuclient_test
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
12
"github.com/juju/juju/jujuclient"
13
"github.com/juju/juju/testing"
16
type AccountsSuite struct {
17
testing.FakeJujuXDGDataHomeSuite
18
store jujuclient.AccountStore
21
var _ = gc.Suite(&AccountsSuite{})
23
func (s *AccountsSuite) SetUpTest(c *gc.C) {
24
s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
25
s.store = jujuclient.NewFileClientStore()
26
writeTestAccountsFile(c)
29
func (s *AccountsSuite) TestAccountDetailsNoFile(c *gc.C) {
30
err := os.Remove(jujuclient.JujuAccountsPath())
31
c.Assert(err, jc.ErrorIsNil)
32
details, err := s.store.AccountDetails("not-found")
33
c.Assert(err, gc.ErrorMatches, "account details for controller not-found not found")
34
c.Assert(details, gc.IsNil)
37
func (s *AccountsSuite) TestAccountDetailsControllerNotFound(c *gc.C) {
38
details, err := s.store.AccountDetails("not-found")
39
c.Assert(err, gc.ErrorMatches, "account details for controller not-found not found")
40
c.Assert(details, gc.IsNil)
43
func (s *AccountsSuite) TestAccountDetails(c *gc.C) {
44
details, err := s.store.AccountDetails("kontroll")
45
c.Assert(err, jc.ErrorIsNil)
46
c.Assert(details, gc.NotNil)
47
c.Assert(*details, jc.DeepEquals, kontrollBobRemoteAccountDetails)
51
func (s *AccountsSuite) TestAllAccountsNoFile(c *gc.C) {
52
err := os.Remove(jujuclient.JujuAccountsPath())
53
c.Assert(err, jc.ErrorIsNil)
54
accounts, err := s.store.AllAccounts("not-found")
55
c.Assert(err, gc.ErrorMatches, "accounts for controller not-found not found")
56
c.Assert(accounts, gc.HasLen, 0)
59
func (s *AccountsSuite) TestAllAccounts(c *gc.C) {
60
accounts, err := s.store.AllAccounts("kontroll")
61
c.Assert(err, jc.ErrorIsNil)
62
c.Assert(accounts, jc.DeepEquals, testControllerAccounts["kontroll"].Accounts)
65
func (s *AccountsSuite) TestCurrentAccount(c *gc.C) {
66
current, err := s.store.CurrentAccount("kontroll")
67
c.Assert(err, jc.ErrorIsNil)
68
c.Assert(current, gc.Equals, "admin@local")
71
func (s *AccountsSuite) TestCurrentAccountNotSet(c *gc.C) {
72
_, err := s.store.CurrentAccount("ctrl")
73
c.Assert(err, jc.Satisfies, errors.IsNotFound)
76
func (s *AccountsSuite) TestCurrentAccountControllerNotFound(c *gc.C) {
77
_, err := s.store.CurrentAccount("not-found")
78
c.Assert(err, jc.Satisfies, errors.IsNotFound)
81
func (s *AccountsSuite) TestSetCurrentAccountControllerNotFound(c *gc.C) {
82
err := s.store.SetCurrentAccount("not-found", "admin@local")
83
c.Assert(err, jc.Satisfies, errors.IsNotFound)
86
func (s *AccountsSuite) TestSetCurrentAccountAccountNotFound(c *gc.C) {
87
err := s.store.SetCurrentAccount("kontroll", "admin@nowhere")
88
c.Assert(err, jc.Satisfies, errors.IsNotFound)
91
func (s *AccountsSuite) TestSetCurrentAccount(c *gc.C) {
92
err := s.store.SetCurrentAccount("kontroll", "admin@local")
93
c.Assert(err, jc.ErrorIsNil)
94
accounts, err := jujuclient.ReadAccountsFile(jujuclient.JujuAccountsPath())
95
c.Assert(err, jc.ErrorIsNil)
96
c.Assert(accounts["kontroll"].CurrentAccount, gc.Equals, "admin@local")
99
func (s *AccountsSuite) TestUpdateAccountNewController(c *gc.C) {
100
testAccountDetails := jujuclient.AccountDetails{User: "admin@local"}
101
err := s.store.UpdateAccount("new-controller", "admin@local", testAccountDetails)
102
c.Assert(err, jc.ErrorIsNil)
103
accounts, err := s.store.AllAccounts("new-controller")
104
c.Assert(err, jc.ErrorIsNil)
105
c.Assert(accounts, jc.DeepEquals, map[string]jujuclient.AccountDetails{
106
"admin@local": testAccountDetails,
110
func (s *AccountsSuite) TestUpdateAccountExistingControllerMultipleAccounts(c *gc.C) {
111
testAccountDetails := jujuclient.AccountDetails{User: "bob@environs"}
112
err := s.store.UpdateAccount("kontroll", "bob@environs", testAccountDetails)
113
c.Assert(err, jc.Satisfies, errors.IsAlreadyExists)
114
c.Assert(err, gc.ErrorMatches, "alternative account for controller kontroll already exists")
115
accounts, err := s.store.AllAccounts("kontroll")
116
c.Assert(err, jc.ErrorIsNil)
117
_, ok := accounts["bob@environs"]
118
c.Assert(ok, jc.IsFalse)
121
func (s *AccountsSuite) TestUpdateAccountExistingControllerNewAccount(c *gc.C) {
122
accounts, err := s.store.AllAccounts("kontroll")
123
c.Assert(err, jc.ErrorIsNil)
124
for account := range accounts {
125
err := s.store.RemoveAccount("kontroll", account)
126
c.Assert(err, jc.ErrorIsNil)
128
testAccountDetails := jujuclient.AccountDetails{User: "bob@environs"}
129
err = s.store.UpdateAccount("kontroll", "bob@environs", testAccountDetails)
130
c.Assert(err, jc.ErrorIsNil)
131
accounts, err = s.store.AllAccounts("kontroll")
132
c.Assert(err, jc.ErrorIsNil)
133
c.Assert(accounts, jc.DeepEquals, map[string]jujuclient.AccountDetails{
134
"bob@environs": testAccountDetails,
138
func (s *AccountsSuite) TestUpdateAccountOverwrites(c *gc.C) {
139
testAccountDetails := jujuclient.AccountDetails{
143
for i := 0; i < 2; i++ {
144
// Twice so we exercise the code path of updating with
145
// identical details.
146
err := s.store.UpdateAccount("kontroll", "admin@local", testAccountDetails)
147
c.Assert(err, jc.ErrorIsNil)
148
details, err := s.store.AccountDetails("kontroll", "admin@local")
149
c.Assert(err, jc.ErrorIsNil)
150
c.Assert(*details, jc.DeepEquals, testAccountDetails)
154
func (s *AccountsSuite) TestRemoveAccountNoFile(c *gc.C) {
155
err := os.Remove(jujuclient.JujuAccountsPath())
156
c.Assert(err, jc.ErrorIsNil)
157
err = s.store.RemoveAccount("not-found", "admin@nowhere")
158
c.Assert(err, jc.Satisfies, errors.IsNotFound)
161
func (s *AccountsSuite) TestRemoveAccountControllerNotFound(c *gc.C) {
162
err := s.store.RemoveAccount("not-found", "admin@nowhere")
163
c.Assert(err, jc.Satisfies, errors.IsNotFound)
166
func (s *AccountsSuite) TestRemoveAccountNotFound(c *gc.C) {
167
err := s.store.RemoveAccount("kontroll", "admin@nowhere")
168
c.Assert(err, jc.Satisfies, errors.IsNotFound)
171
func (s *AccountsSuite) TestRemoveAccount(c *gc.C) {
172
err := s.store.RemoveAccount("kontroll", "admin@local")
173
c.Assert(err, jc.ErrorIsNil)
174
_, err = s.store.AccountDetails("kontroll", "admin@local")
175
c.Assert(err, jc.Satisfies, errors.IsNotFound)
178
func (s *AccountsSuite) TestRemoveControllerRemovesaccounts(c *gc.C) {
179
store := jujuclient.NewFileClientStore()
180
err := store.UpdateController("kontroll", jujuclient.ControllerDetails{
181
ControllerUUID: "abc",
184
c.Assert(err, jc.ErrorIsNil)
185
err = store.RemoveController("kontroll")
186
c.Assert(err, jc.ErrorIsNil)
188
accounts, err := jujuclient.ReadAccountsFile(jujuclient.JujuAccountsPath())
189
c.Assert(err, jc.ErrorIsNil)
190
_, ok := accounts["kontroll"]
191
c.Assert(ok, jc.IsFalse) // kontroll accounts are removed