~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/jujuclient/accounts_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        jc "github.com/juju/testing/checkers"
 
10
        gc "gopkg.in/check.v1"
 
11
 
 
12
        "github.com/juju/juju/jujuclient"
 
13
        "github.com/juju/juju/testing"
 
14
)
 
15
 
 
16
type AccountsSuite struct {
 
17
        testing.FakeJujuXDGDataHomeSuite
 
18
        store jujuclient.AccountStore
 
19
}
 
20
 
 
21
var _ = gc.Suite(&AccountsSuite{})
 
22
 
 
23
func (s *AccountsSuite) SetUpTest(c *gc.C) {
 
24
        s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
 
25
        s.store = jujuclient.NewFileClientStore()
 
26
        writeTestAccountsFile(c)
 
27
}
 
28
 
 
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)
 
35
}
 
36
 
 
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)
 
41
}
 
42
 
 
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)
 
48
}
 
49
 
 
50
/*
 
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)
 
57
}
 
58
 
 
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)
 
63
}
 
64
 
 
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")
 
69
}
 
70
 
 
71
func (s *AccountsSuite) TestCurrentAccountNotSet(c *gc.C) {
 
72
        _, err := s.store.CurrentAccount("ctrl")
 
73
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
 
74
}
 
75
 
 
76
func (s *AccountsSuite) TestCurrentAccountControllerNotFound(c *gc.C) {
 
77
        _, err := s.store.CurrentAccount("not-found")
 
78
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
 
79
}
 
80
 
 
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)
 
84
}
 
85
 
 
86
func (s *AccountsSuite) TestSetCurrentAccountAccountNotFound(c *gc.C) {
 
87
        err := s.store.SetCurrentAccount("kontroll", "admin@nowhere")
 
88
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
 
89
}
 
90
 
 
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")
 
97
}
 
98
 
 
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,
 
107
        })
 
108
}
 
109
 
 
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)
 
119
}
 
120
 
 
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)
 
127
        }
 
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,
 
135
        })
 
136
}
 
137
 
 
138
func (s *AccountsSuite) TestUpdateAccountOverwrites(c *gc.C) {
 
139
        testAccountDetails := jujuclient.AccountDetails{
 
140
                User:     "admin@local",
 
141
                Password: "fnord",
 
142
        }
 
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)
 
151
        }
 
152
}
 
153
 
 
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)
 
159
}
 
160
 
 
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)
 
164
}
 
165
 
 
166
func (s *AccountsSuite) TestRemoveAccountNotFound(c *gc.C) {
 
167
        err := s.store.RemoveAccount("kontroll", "admin@nowhere")
 
168
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
 
169
}
 
170
 
 
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)
 
176
}
 
177
 
 
178
func (s *AccountsSuite) TestRemoveControllerRemovesaccounts(c *gc.C) {
 
179
        store := jujuclient.NewFileClientStore()
 
180
        err := store.UpdateController("kontroll", jujuclient.ControllerDetails{
 
181
                ControllerUUID: "abc",
 
182
                CACert:         "woop",
 
183
        })
 
184
        c.Assert(err, jc.ErrorIsNil)
 
185
        err = store.RemoveController("kontroll")
 
186
        c.Assert(err, jc.ErrorIsNil)
 
187
 
 
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
 
192
}
 
193
*/