47
47
func (s *OpenSuite) TestNewDummyEnviron(c *gc.C) {
48
s.PatchValue(&version.Current, testing.FakeVersionNumber)
48
s.PatchValue(&jujuversion.Current, testing.FakeVersionNumber)
49
49
// matches *Settings.Map()
50
50
cfg, err := config.New(config.NoDefaults, dummySampleConfig())
51
51
c.Assert(err, jc.ErrorIsNil)
52
52
ctx := envtesting.BootstrapContext(c)
53
53
cache := jujuclienttesting.NewMemStore()
54
env, err := environs.Prepare(ctx, configstore.NewMem(), cache, cfg.Name(), environs.PrepareForBootstrapParams{Config: cfg})
54
env, err := environs.Prepare(ctx, cache, environs.PrepareParams{
55
ControllerName: cfg.Name(),
56
BaseConfig: cfg.AllAttrs(),
55
59
c.Assert(err, jc.ErrorIsNil)
57
61
storageDir := c.MkDir()
63
67
c.Assert(err, jc.ErrorIsNil)
65
69
// New controller should have been added to collection.
66
uuid, exists := cfg.UUID()
67
c.Assert(exists, jc.IsTrue)
69
70
foundController, err := cache.ControllerByName(cfg.Name())
70
71
c.Assert(err, jc.ErrorIsNil)
71
c.Assert(foundController.ControllerUUID, gc.DeepEquals, uuid)
72
c.Assert(foundController.ControllerUUID, gc.DeepEquals, cfg.UUID())
74
75
func (s *OpenSuite) TestUpdateEnvInfo(c *gc.C) {
75
store := configstore.NewMem()
76
cache := jujuclienttesting.NewMemStore()
76
store := jujuclienttesting.NewMemStore()
77
77
ctx := envtesting.BootstrapContext(c)
78
78
cfg, err := config.New(config.UseDefaults, map[string]interface{}{
80
"name": "admin-model",
82
c.Assert(err, jc.ErrorIsNil)
83
_, err = environs.Prepare(ctx, store, cache, "controller-name", environs.PrepareForBootstrapParams{Config: cfg})
84
c.Assert(err, jc.ErrorIsNil)
86
info, err := store.ReadInfo("controller-name:admin-model")
87
c.Assert(err, jc.ErrorIsNil)
88
c.Assert(info, gc.NotNil)
89
c.Assert(info.APIEndpoint().CACert, gc.Not(gc.Equals), "")
90
c.Assert(info.APIEndpoint().ModelUUID, gc.Not(gc.Equals), "")
91
c.Assert(info.APICredentials().Password, gc.Not(gc.Equals), "")
92
c.Assert(info.APICredentials().User, gc.Equals, "admin@local")
94
foundController, err := cache.ControllerByName("controller-name")
95
c.Assert(err, jc.ErrorIsNil)
96
c.Assert(foundController, jc.DeepEquals, &jujuclient.ControllerDetails{
97
ControllerUUID: info.APIEndpoint().ServerUUID,
98
CACert: info.APIEndpoint().CACert,
100
foundModel, err := cache.ModelByName("controller-name", "admin@local", "admin-model")
80
"name": "admin-model",
81
"controller-uuid": utils.MustNewUUID().String(),
82
"uuid": utils.MustNewUUID().String(),
84
c.Assert(err, jc.ErrorIsNil)
85
_, err = environs.Prepare(ctx, store, environs.PrepareParams{
86
ControllerName: "controller-name",
87
BaseConfig: cfg.AllAttrs(),
90
c.Assert(err, jc.ErrorIsNil)
92
foundController, err := store.ControllerByName("controller-name")
93
c.Assert(err, jc.ErrorIsNil)
94
c.Assert(foundController.ControllerUUID, gc.Equals, cfg.ControllerUUID())
95
c.Assert(foundController.CACert, gc.Not(gc.Equals), "")
96
foundModel, err := store.ModelByName("controller-name", "admin@local", "admin-model")
101
97
c.Assert(err, jc.ErrorIsNil)
102
98
c.Assert(foundModel, jc.DeepEquals, &jujuclient.ModelDetails{
103
ModelUUID: foundController.ControllerUUID,
99
ModelUUID: cfg.UUID(),
138
134
"ca-private-key",
142
137
cfg, err := config.New(config.NoDefaults, baselineAttrs)
143
138
c.Assert(err, jc.ErrorIsNil)
144
store := configstore.NewMem()
145
139
controllerStore := jujuclienttesting.NewMemStore()
146
140
ctx := envtesting.BootstrapContext(c)
147
env, err := environs.Prepare(ctx, store, controllerStore, cfg.Name(), environs.PrepareForBootstrapParams{Config: cfg})
148
c.Assert(err, jc.ErrorIsNil)
150
// Check that the model info file was correctly created.
151
info, err := store.ReadInfo("erewhemos:erewhemos")
152
c.Assert(err, jc.ErrorIsNil)
153
c.Assert(info.Initialized(), jc.IsTrue)
154
c.Assert(info.BootstrapConfig(), gc.DeepEquals, env.Config().AllAttrs())
155
c.Logf("bootstrap config: %#v", info.BootstrapConfig())
141
env, err := environs.Prepare(ctx, controllerStore, environs.PrepareParams{
142
ControllerName: cfg.Name(),
143
BaseConfig: cfg.AllAttrs(),
146
c.Assert(err, jc.ErrorIsNil)
157
148
// Check that an admin-secret was chosen.
158
149
adminSecret := env.Config().AdminSecret()
170
161
c.Assert(err, jc.ErrorIsNil)
171
162
c.Assert(caCert.Subject.CommonName, gc.Equals, `juju-generated CA for model "`+testing.SampleModelName+`"`)
173
// Check that a uuid was chosen.
174
uuid, exists := env.Config().UUID()
175
c.Assert(exists, jc.IsTrue)
176
c.Assert(uuid, gc.Matches, `[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`)
178
164
// Check that controller was cached
179
165
foundController, err := controllerStore.ControllerByName(cfg.Name())
180
166
c.Assert(err, jc.ErrorIsNil)
181
c.Assert(foundController.ControllerUUID, gc.DeepEquals, uuid)
167
c.Assert(foundController.ControllerUUID, gc.DeepEquals, cfg.UUID())
183
169
// Check we cannot call Prepare again.
184
env, err = environs.Prepare(ctx, store, controllerStore, cfg.Name(), environs.PrepareForBootstrapParams{Config: cfg})
170
env, err = environs.Prepare(ctx, controllerStore, environs.PrepareParams{
171
ControllerName: cfg.Name(),
172
BaseConfig: cfg.AllAttrs(),
185
175
c.Assert(err, jc.Satisfies, errors.IsAlreadyExists)
186
176
c.Assert(err, gc.ErrorMatches, `controller "erewhemos" already exists`)
196
cfg, err := config.New(config.NoDefaults, baselineAttrs)
197
c.Assert(err, jc.ErrorIsNil)
199
187
ctx := envtesting.BootstrapContext(c)
200
env0, err := environs.Prepare(ctx, configstore.NewMem(), jujuclienttesting.NewMemStore(), cfg.Name(), environs.PrepareForBootstrapParams{Config: cfg})
188
env0, err := environs.Prepare(ctx, jujuclienttesting.NewMemStore(), environs.PrepareParams{
189
ControllerName: "erewhemos",
190
BaseConfig: baselineAttrs,
201
193
c.Assert(err, jc.ErrorIsNil)
202
194
adminSecret0 := env0.Config().AdminSecret()
203
195
c.Assert(adminSecret0, gc.HasLen, 32)
204
196
c.Assert(adminSecret0, gc.Matches, "^[0-9a-f]*$")
206
env1, err := environs.Prepare(ctx, configstore.NewMem(), jujuclienttesting.NewMemStore(), cfg.Name(), environs.PrepareForBootstrapParams{Config: cfg})
198
// Allocate a new UUID, or we'll end up with the same config.
199
newUUID := utils.MustNewUUID()
200
baselineAttrs[config.UUIDKey] = newUUID.String()
201
baselineAttrs[config.ControllerUUIDKey] = newUUID.String()
203
env1, err := environs.Prepare(ctx, jujuclienttesting.NewMemStore(), environs.PrepareParams{
204
ControllerName: "erewhemos",
205
BaseConfig: baselineAttrs,
207
208
c.Assert(err, jc.ErrorIsNil)
208
209
adminSecret1 := env1.Config().AdminSecret()
209
210
c.Assert(adminSecret1, gc.HasLen, 32)
223
224
c.Assert(err, jc.ErrorIsNil)
224
store := configstore.NewMem()
225
225
controllerStore := jujuclienttesting.NewMemStore()
226
env, err := environs.Prepare(envtesting.BootstrapContext(c), store, controllerStore, cfg.Name(), environs.PrepareForBootstrapParams{Config: cfg})
226
env, err := environs.Prepare(envtesting.BootstrapContext(c), controllerStore, environs.PrepareParams{
227
ControllerName: cfg.Name(),
228
BaseConfig: cfg.AllAttrs(),
227
231
c.Assert(err, gc.ErrorMatches, "cannot ensure CA certificate: controller configuration with a certificate but no CA private key")
228
232
c.Assert(env, gc.IsNil)
229
// Ensure that the config storage info is cleaned up.
230
_, err = store.ReadInfo(cfg.Name())
231
c.Assert(err, jc.Satisfies, errors.IsNotFound)
234
235
func (*OpenSuite) TestPrepareWithExistingKeyPair(c *gc.C) {
243
244
c.Assert(err, jc.ErrorIsNil)
244
245
ctx := envtesting.BootstrapContext(c)
245
env, err := environs.Prepare(ctx, configstore.NewMem(), jujuclienttesting.NewMemStore(), cfg.Name(), environs.PrepareForBootstrapParams{Config: cfg})
246
env, err := environs.Prepare(ctx, jujuclienttesting.NewMemStore(), environs.PrepareParams{
247
ControllerName: cfg.Name(),
248
BaseConfig: cfg.AllAttrs(),
246
251
c.Assert(err, jc.ErrorIsNil)
247
252
cfgCertPEM, cfgCertOK := env.Config().CACert()
248
253
cfgKeyPEM, cfgKeyOK := env.Config().CAPrivateKey()
262
267
c.Assert(err, jc.ErrorIsNil)
264
configstore := configstore.NewMem()
265
269
store := jujuclienttesting.NewMemStore()
266
270
// Prepare the environment and sanity-check that
267
271
// the config storage info has been made.
268
272
ctx := envtesting.BootstrapContext(c)
269
e, err := environs.Prepare(ctx, configstore, store, "controller-name", environs.PrepareForBootstrapParams{Config: cfg})
270
c.Assert(err, jc.ErrorIsNil)
271
_, err = configstore.ReadInfo("controller-name:erewhemos")
273
e, err := environs.Prepare(ctx, store, environs.PrepareParams{
274
ControllerName: "controller-name",
275
BaseConfig: cfg.AllAttrs(),
272
278
c.Assert(err, jc.ErrorIsNil)
273
279
_, err = store.ControllerByName("controller-name")
274
280
c.Assert(err, jc.ErrorIsNil)
276
err = environs.Destroy("controller-name", e, configstore, store)
282
err = environs.Destroy("controller-name", e, store)
277
283
c.Assert(err, jc.ErrorIsNil)
279
285
// Check that the environment has actually been destroyed
280
// and that the config info has been destroyed too.
286
// and that the controller details been removed too.
281
287
_, err = e.ControllerInstances()
282
c.Assert(err, gc.ErrorMatches, "model has been destroyed")
283
_, err = configstore.ReadInfo("controller-name:erewhemos")
284
c.Assert(err, jc.Satisfies, errors.IsNotFound)
288
c.Assert(err, gc.ErrorMatches, "model is not prepared")
285
289
_, err = store.ControllerByName("controller-name")
286
290
c.Assert(err, jc.Satisfies, errors.IsNotFound)