~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/controller/register_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
        "github.com/juju/juju/api"
25
25
        "github.com/juju/juju/apiserver/params"
26
26
        "github.com/juju/juju/cmd/juju/controller"
27
 
        "github.com/juju/juju/environs/configstore"
28
27
        "github.com/juju/juju/jujuclient"
29
28
        "github.com/juju/juju/jujuclient/jujuclienttesting"
30
29
        "github.com/juju/juju/testing"
32
31
 
33
32
type RegisterSuite struct {
34
33
        testing.FakeJujuXDGDataHomeSuite
35
 
        apiConnection         *mockAPIConnection
36
 
        legacystore           configstore.Storage
37
 
        store                 jujuclient.ClientStore
38
 
        apiOpenError          error
39
 
        apiOpenControllerName string
40
 
        apiOpenAccountName    string
41
 
        apiOpenModelName      string
42
 
        server                *httptest.Server
43
 
        httpHandler           http.Handler
 
34
        apiConnection               *mockAPIConnection
 
35
        store                       *jujuclienttesting.MemStore
 
36
        apiOpenError                error
 
37
        refreshModels               func(jujuclient.ClientStore, string, string) error
 
38
        refreshModelsControllerName string
 
39
        refreshModelsAccountName    string
 
40
        server                      *httptest.Server
 
41
        httpHandler                 http.Handler
44
42
}
45
43
 
46
44
var _ = gc.Suite(&RegisterSuite{})
60
58
                controllerTag: testing.ModelTag,
61
59
                addr:          serverURL.Host,
62
60
        }
63
 
        s.apiOpenControllerName = ""
64
 
        s.apiOpenAccountName = ""
65
 
        s.apiOpenModelName = ""
 
61
        s.refreshModelsControllerName = ""
 
62
        s.refreshModelsAccountName = ""
 
63
        s.refreshModels = func(store jujuclient.ClientStore, controllerName, accountName string) error {
 
64
                s.refreshModelsControllerName = controllerName
 
65
                s.refreshModelsAccountName = accountName
 
66
                return nil
 
67
        }
66
68
 
67
 
        s.legacystore = configstore.NewMem()
68
69
        s.store = jujuclienttesting.NewMemStore()
69
 
        s.PatchValue(&configstore.Default, func() (configstore.Storage, error) {
70
 
                return s.legacystore, nil
71
 
        })
72
70
}
73
71
 
74
72
func (s *RegisterSuite) TearDownTest(c *gc.C) {
85
83
        return s.apiConnection, nil
86
84
}
87
85
 
88
 
func (s *RegisterSuite) newAPIRoot(store jujuclient.ClientStore, controllerName, accountName, modelName string) (api.Connection, error) {
89
 
        if s.apiOpenError != nil {
90
 
                return nil, s.apiOpenError
91
 
        }
92
 
        s.apiOpenControllerName = controllerName
93
 
        s.apiOpenAccountName = accountName
94
 
        s.apiOpenModelName = modelName
95
 
        return s.apiConnection, nil
96
 
}
97
 
 
98
86
func (s *RegisterSuite) run(c *gc.C, stdin io.Reader, args ...string) (*cmd.Context, error) {
99
 
        command := controller.NewRegisterCommandForTest(s.apiOpen, s.newAPIRoot, s.store)
 
87
        command := controller.NewRegisterCommandForTest(s.apiOpen, s.refreshModels, s.store)
100
88
        err := testing.InitCommand(command, args)
101
89
        c.Assert(err, jc.ErrorIsNil)
102
90
        ctx := testing.Context(c)
141
129
}
142
130
 
143
131
func (s *RegisterSuite) TestRegister(c *gc.C) {
 
132
        ctx := s.testRegister(c)
 
133
        c.Assert(s.refreshModelsControllerName, gc.Equals, "controller-name")
 
134
        c.Assert(s.refreshModelsAccountName, gc.Equals, "bob@local")
 
135
        stderr := testing.Stderr(ctx)
 
136
        c.Assert(stderr, gc.Equals, `
 
137
Please set a name for this controller: 
 
138
Enter password: 
 
139
Confirm password: 
 
140
 
 
141
Welcome, bob. You are now logged into "controller-name".
 
142
 
 
143
There are no models available. You can create models with
 
144
"juju create-model", or you can ask an administrator or owner
 
145
of a model to grant access to that model with "juju grant".
 
146
 
 
147
`[1:])
 
148
}
 
149
 
 
150
func (s *RegisterSuite) TestRegisterOneModel(c *gc.C) {
 
151
        s.refreshModels = func(store jujuclient.ClientStore, controller, account string) error {
 
152
                err := store.UpdateModel(controller, account, "theoneandonly", jujuclient.ModelDetails{
 
153
                        ModelUUID: "df136476-12e9-11e4-8a70-b2227cce2b54",
 
154
                })
 
155
                c.Assert(err, jc.ErrorIsNil)
 
156
                return nil
 
157
        }
 
158
        s.testRegister(c)
 
159
        c.Assert(
 
160
                s.store.Models["controller-name"].AccountModels["bob@local"].CurrentModel,
 
161
                gc.Equals, "theoneandonly",
 
162
        )
 
163
}
 
164
 
 
165
func (s *RegisterSuite) TestRegisterMultipleModels(c *gc.C) {
 
166
        s.refreshModels = func(store jujuclient.ClientStore, controller, account string) error {
 
167
                for _, name := range [...]string{"model1", "model2"} {
 
168
                        err := store.UpdateModel(controller, account, name, jujuclient.ModelDetails{
 
169
                                ModelUUID: "df136476-12e9-11e4-8a70-b2227cce2b54",
 
170
                        })
 
171
                        c.Assert(err, jc.ErrorIsNil)
 
172
                }
 
173
                return nil
 
174
        }
 
175
        ctx := s.testRegister(c)
 
176
 
 
177
        // When there are multiple models, no current model will be set.
 
178
        // Instead, the command will output the list of models and inform
 
179
        // the user how to set the current model.
 
180
        _, err := s.store.CurrentModel("controller-name", "bob@local")
 
181
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
 
182
 
 
183
        stderr := testing.Stderr(ctx)
 
184
        c.Assert(stderr, gc.Equals, `
 
185
Please set a name for this controller: 
 
186
Enter password: 
 
187
Confirm password: 
 
188
 
 
189
Welcome, bob. You are now logged into "controller-name".
 
190
 
 
191
There are 2 models available. Use "juju switch" to select
 
192
one of them:
 
193
  - juju switch model1
 
194
  - juju switch model2
 
195
 
 
196
`[1:])
 
197
}
 
198
 
 
199
func (s *RegisterSuite) testRegister(c *gc.C) *cmd.Context {
144
200
        secretKey := []byte(strings.Repeat("X", 32))
145
201
        respNonce := []byte(strings.Repeat("X", 24))
146
202
 
168
224
 
169
225
        registrationData := s.encodeRegistrationData(c, "bob", secretKey)
170
226
        stdin := strings.NewReader("controller-name\nhunter2\nhunter2\n")
171
 
        _, err = s.run(c, stdin, registrationData)
 
227
        ctx, err := s.run(c, stdin, registrationData)
172
228
        c.Assert(err, jc.ErrorIsNil)
173
229
 
174
230
        // There should have been one POST command to "/register".
204
260
                User:     "bob@local",
205
261
                Password: "hunter2",
206
262
        })
207
 
 
208
 
        // The command should have logged into the controller with the
209
 
        // information we checked above.
210
 
        c.Assert(s.apiOpenControllerName, gc.Equals, "controller-name")
211
 
        c.Assert(s.apiOpenAccountName, gc.Equals, "bob@local")
212
 
        c.Assert(s.apiOpenModelName, gc.Equals, "")
 
263
        return ctx
213
264
}
214
265
 
215
266
func (s *RegisterSuite) TestRegisterInvalidRegistrationData(c *gc.C) {
229
280
}
230
281
 
231
282
func (s *RegisterSuite) TestRegisterControllerNameExists(c *gc.C) {
232
 
        err := s.legacystore.CreateInfo("controller-name").Write()
233
 
        c.Assert(err, jc.ErrorIsNil)
 
283
        err := s.store.UpdateController("controller-name", jujuclient.ControllerDetails{
 
284
                ControllerUUID: "df136476-12e9-11e4-8a70-b2227cce2b54",
 
285
                CACert:         testing.CACert,
 
286
        })
234
287
 
235
288
        secretKey := []byte(strings.Repeat("X", 32))
236
289
        registrationData := s.encodeRegistrationData(c, "bob", secretKey)
281
334
        _, err = s.run(c, stdin, registrationData)
282
335
        c.Assert(err, gc.ErrorMatches, "xyz")
283
336
 
284
 
        _, err = s.legacystore.ReadInfo("controller-name")
 
337
        _, err = s.store.ControllerByName("controller-name")
285
338
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
286
339
}