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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/environs/open_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:
6
6
import (
7
7
        "github.com/juju/errors"
8
8
        jc "github.com/juju/testing/checkers"
 
9
        "github.com/juju/utils"
9
10
        gc "gopkg.in/check.v1"
10
11
 
11
12
        "github.com/juju/juju/cert"
12
13
        "github.com/juju/juju/environs"
13
14
        "github.com/juju/juju/environs/bootstrap"
14
15
        "github.com/juju/juju/environs/config"
15
 
        "github.com/juju/juju/environs/configstore"
16
16
        "github.com/juju/juju/environs/filestorage"
17
17
        sstesting "github.com/juju/juju/environs/simplestreams/testing"
18
18
        envtesting "github.com/juju/juju/environs/testing"
22
22
        "github.com/juju/juju/jujuclient/jujuclienttesting"
23
23
        "github.com/juju/juju/provider/dummy"
24
24
        "github.com/juju/juju/testing"
25
 
        "github.com/juju/juju/version"
 
25
        jujuversion "github.com/juju/juju/version"
26
26
)
27
27
 
28
28
type OpenSuite struct {
45
45
}
46
46
 
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(),
 
57
                CloudName:      "dummy",
 
58
        })
55
59
        c.Assert(err, jc.ErrorIsNil)
56
60
 
57
61
        storageDir := c.MkDir()
63
67
        c.Assert(err, jc.ErrorIsNil)
64
68
 
65
69
        // New controller should have been added to collection.
66
 
        uuid, exists := cfg.UUID()
67
 
        c.Assert(exists, jc.IsTrue)
68
 
 
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())
72
73
}
73
74
 
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{}{
79
 
                "type": "dummy",
80
 
                "name": "admin-model",
81
 
        })
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)
85
 
 
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")
93
 
 
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,
99
 
        })
100
 
        foundModel, err := cache.ModelByName("controller-name", "admin@local", "admin-model")
 
79
                "type":            "dummy",
 
80
                "name":            "admin-model",
 
81
                "controller-uuid": utils.MustNewUUID().String(),
 
82
                "uuid":            utils.MustNewUUID().String(),
 
83
        })
 
84
        c.Assert(err, jc.ErrorIsNil)
 
85
        _, err = environs.Prepare(ctx, store, environs.PrepareParams{
 
86
                ControllerName: "controller-name",
 
87
                BaseConfig:     cfg.AllAttrs(),
 
88
                CloudName:      "dummy",
 
89
        })
 
90
        c.Assert(err, jc.ErrorIsNil)
 
91
 
 
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(),
104
100
        })
105
101
}
106
102
 
137
133
                "ca-cert",
138
134
                "ca-private-key",
139
135
                "admin-secret",
140
 
                "uuid",
141
136
        )
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)
149
 
 
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(),
 
144
                CloudName:      "dummy",
 
145
        })
 
146
        c.Assert(err, jc.ErrorIsNil)
156
147
 
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+`"`)
172
163
 
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}`)
177
 
 
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())
182
168
 
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(),
 
173
                CloudName:      "dummy",
 
174
        })
185
175
        c.Assert(err, jc.Satisfies, errors.IsAlreadyExists)
186
176
        c.Assert(err, gc.ErrorMatches, `controller "erewhemos" already exists`)
187
177
}
193
183
        }).Delete(
194
184
                "admin-secret",
195
185
        )
196
 
        cfg, err := config.New(config.NoDefaults, baselineAttrs)
197
 
        c.Assert(err, jc.ErrorIsNil)
198
186
 
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,
 
191
                CloudName:      "dummy",
 
192
        })
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]*$")
205
197
 
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()
 
202
 
 
203
        env1, err := environs.Prepare(ctx, jujuclienttesting.NewMemStore(), environs.PrepareParams{
 
204
                ControllerName: "erewhemos",
 
205
                BaseConfig:     baselineAttrs,
 
206
                CloudName:      "dummy",
 
207
        })
207
208
        c.Assert(err, jc.ErrorIsNil)
208
209
        adminSecret1 := env1.Config().AdminSecret()
209
210
        c.Assert(adminSecret1, gc.HasLen, 32)
221
222
                },
222
223
        ))
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(),
 
229
                CloudName:      "dummy",
 
230
        })
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)
232
233
}
233
234
 
234
235
func (*OpenSuite) TestPrepareWithExistingKeyPair(c *gc.C) {
242
243
        ))
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(),
 
249
                CloudName:      "dummy",
 
250
        })
246
251
        c.Assert(err, jc.ErrorIsNil)
247
252
        cfgCertPEM, cfgCertOK := env.Config().CACert()
248
253
        cfgKeyPEM, cfgKeyOK := env.Config().CAPrivateKey()
261
266
        ))
262
267
        c.Assert(err, jc.ErrorIsNil)
263
268
 
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(),
 
276
                CloudName:      "dummy",
 
277
        })
272
278
        c.Assert(err, jc.ErrorIsNil)
273
279
        _, err = store.ControllerByName("controller-name")
274
280
        c.Assert(err, jc.ErrorIsNil)
275
281
 
276
 
        err = environs.Destroy("controller-name", e, configstore, store)
 
282
        err = environs.Destroy("controller-name", e, store)
277
283
        c.Assert(err, jc.ErrorIsNil)
278
284
 
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)
287
291
}