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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/juju/testing/conn.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:
18
18
        "github.com/juju/utils"
19
19
        "github.com/juju/utils/arch"
20
20
        "github.com/juju/utils/series"
 
21
        "github.com/juju/version"
21
22
        gc "gopkg.in/check.v1"
22
23
        "gopkg.in/juju/charm.v6-unstable"
23
24
        "gopkg.in/juju/charmrepo.v2-unstable"
29
30
        "github.com/juju/juju/environs"
30
31
        "github.com/juju/juju/environs/bootstrap"
31
32
        "github.com/juju/juju/environs/config"
32
 
        "github.com/juju/juju/environs/configstore"
33
33
        "github.com/juju/juju/environs/filestorage"
34
34
        sstesting "github.com/juju/juju/environs/simplestreams/testing"
35
35
        "github.com/juju/juju/environs/storage"
41
41
        "github.com/juju/juju/mongo"
42
42
        "github.com/juju/juju/provider/dummy"
43
43
        "github.com/juju/juju/state"
 
44
        "github.com/juju/juju/state/binarystorage"
44
45
        statestorage "github.com/juju/juju/state/storage"
45
 
        "github.com/juju/juju/state/toolstorage"
46
46
        "github.com/juju/juju/testcharms"
47
47
        "github.com/juju/juju/testing"
48
48
        "github.com/juju/juju/testing/factory"
49
 
        "github.com/juju/juju/version"
 
49
        jujuversion "github.com/juju/juju/version"
50
50
)
51
51
 
 
52
const ControllerName = "kontroll"
 
53
 
52
54
// JujuConnSuite provides a freshly bootstrapped juju.Conn
53
55
// for each test. It also includes testing.BaseSuite.
54
56
//
55
57
// It also sets up RootDir to point to a directory hierarchy
56
58
// mirroring the intended juju directory structure, including
57
59
// the following:
58
 
//     RootDir/home/ubuntu/.local/share/juju/models/cache.yaml
59
 
//         The dummy cache.yaml file, holding a default
60
 
//         controller and environment named "dummymodel"
61
 
//         which uses the "dummy" provider.
62
60
//     RootDir/var/lib/juju
63
61
//         An empty directory returned as DataDir - the
64
62
//         root of the juju data storage space.
84
82
        Environ            environs.Environ
85
83
        APIState           api.Connection
86
84
        apiStates          []api.Connection // additional api.Connections to close on teardown
87
 
        ConfigStore        configstore.Storage
88
85
        ControllerStore    jujuclient.ClientStore
89
86
        BackingState       *state.State // The State being used by the API server
90
87
        RootDir            string       // The faked-up root directory.
100
97
func (s *JujuConnSuite) SetUpSuite(c *gc.C) {
101
98
        s.MgoSuite.SetUpSuite(c)
102
99
        s.FakeJujuXDGDataHomeSuite.SetUpSuite(c)
 
100
        s.PatchValue(&utils.OutgoingAccessAllowed, false)
103
101
}
104
102
 
105
103
func (s *JujuConnSuite) TearDownSuite(c *gc.C) {
227
225
 
228
226
        err = os.MkdirAll(s.DataDir(), 0777)
229
227
        c.Assert(err, jc.ErrorIsNil)
230
 
        s.PatchEnvironment(osenv.JujuModelEnvKey, "dummymodel")
 
228
        s.PatchEnvironment(osenv.JujuModelEnvKey, "admin")
231
229
 
232
230
        cfg, err := config.New(config.UseDefaults, (map[string]interface{})(s.sampleConfig()))
233
231
        c.Assert(err, jc.ErrorIsNil)
234
232
 
235
 
        store, err := configstore.Default()
236
 
        c.Assert(err, jc.ErrorIsNil)
237
 
        s.ConfigStore = store
238
 
 
239
233
        s.ControllerStore = jujuclient.NewFileClientStore()
240
234
 
241
235
        ctx := testing.Context(c)
242
236
        environ, err := environs.Prepare(
243
237
                modelcmd.BootstrapContext(ctx),
244
 
                s.ConfigStore,
245
238
                s.ControllerStore,
246
 
                "dummymodel",
247
 
                environs.PrepareForBootstrapParams{
248
 
                        Config:      cfg,
249
 
                        Credentials: cloud.NewEmptyCredential(),
 
239
                environs.PrepareParams{
 
240
                        BaseConfig:     cfg.AllAttrs(),
 
241
                        Credential:     cloud.NewEmptyCredential(),
 
242
                        ControllerName: ControllerName,
 
243
                        CloudName:      "dummy",
250
244
                },
251
245
        )
252
246
        c.Assert(err, jc.ErrorIsNil)
253
247
        // sanity check we've got the correct environment.
254
 
        c.Assert(environ.Config().Name(), gc.Equals, "dummymodel")
 
248
        c.Assert(environ.Config().Name(), gc.Equals, "admin")
255
249
        s.PatchValue(&dummy.DataDir, s.DataDir())
256
250
        s.LogDir = c.MkDir()
257
251
        s.PatchValue(&dummy.LogDir, s.LogDir)
258
252
 
259
253
        versions := PreferredDefaultVersions(environ.Config(), version.Binary{
260
 
                Number: version.Current,
 
254
                Number: jujuversion.Current,
261
255
                Arch:   "amd64",
262
256
                Series: "precise",
263
257
        })
264
258
        current := version.Binary{
265
 
                Number: version.Current,
 
259
                Number: jujuversion.Current,
266
260
                Arch:   arch.HostArch(),
267
261
                Series: series.HostSeries(),
268
262
        }
298
292
        err = s.State.SetAPIHostPorts(s.APIState.APIHostPorts())
299
293
        c.Assert(err, jc.ErrorIsNil)
300
294
 
301
 
        // Make sure the config store has the api endpoint address set
302
 
        controller, err := s.ControllerStore.ControllerByName("dummymodel")
 
295
        // Make sure the controller store has the controller api endpoint address set
 
296
        controller, err := s.ControllerStore.ControllerByName(ControllerName)
303
297
        c.Assert(err, jc.ErrorIsNil)
304
298
        controller.APIEndpoints = []string{s.APIState.APIHostPorts()[0][0].String()}
305
 
        err = s.ControllerStore.UpdateController("dummymodel", *controller)
306
 
        c.Assert(err, jc.ErrorIsNil)
307
 
        err = modelcmd.WriteCurrentController("dummymodel")
308
 
        c.Assert(err, jc.ErrorIsNil)
309
 
 
310
 
        // TODO (anastasiamac 2016-02-08) START REMOVE with cache.yaml
311
 
        info, err := s.ConfigStore.ReadInfo("dummymodel:dummymodel")
312
 
        c.Assert(err, jc.ErrorIsNil)
313
 
        endpoint := info.APIEndpoint()
314
 
        endpoint.Addresses = []string{s.APIState.APIHostPorts()[0][0].String()}
315
 
        info.SetAPIEndpoint(endpoint)
316
 
        err = info.Write()
317
 
        c.Assert(err, jc.ErrorIsNil)
318
 
        // END REMOVE with cache.yaml
319
 
 
320
 
        // Make sure the jenv file has the local host ports.
321
 
        c.Logf("jenv host ports: %#v", s.APIState.APIHostPorts())
 
299
        err = s.ControllerStore.UpdateController(ControllerName, *controller)
 
300
        c.Assert(err, jc.ErrorIsNil)
 
301
        err = modelcmd.WriteCurrentController(ControllerName)
 
302
        c.Assert(err, jc.ErrorIsNil)
322
303
 
323
304
        s.Environ = environ
324
305
 
342
323
        for _, v := range versions {
343
324
                content := v.String()
344
325
                hash := fmt.Sprintf("sha256(%s)", content)
345
 
                err := stor.AddTools(strings.NewReader(content), toolstorage.Metadata{
346
 
                        Version: v,
 
326
                err := stor.Add(strings.NewReader(content), binarystorage.Metadata{
 
327
                        Version: v.String(),
347
328
                        Size:    int64(len(content)),
348
329
                        SHA256:  hash,
349
330
                })
352
333
}
353
334
 
354
335
// AddDefaultToolsToState adds tools to tools storage for
355
 
// {Number: version.Current.Number, Arch: amd64}, for the
 
336
// {Number: jujuversion.Current.Number, Arch: amd64}, for the
356
337
// "precise" series and the environment's preferred series.
357
338
// The preferred series is default-series if specified,
358
339
// otherwise the latest LTS.
359
340
func (s *JujuConnSuite) AddDefaultToolsToState(c *gc.C) {
360
341
        preferredVersion := version.Binary{
361
 
                Number: version.Current,
 
342
                Number: jujuversion.Current,
362
343
                Arch:   "amd64",
363
344
                Series: series.HostSeries(),
364
345
        }
365
346
        current := version.Binary{
366
 
                Number: version.Current,
 
347
                Number: jujuversion.Current,
367
348
                Arch:   arch.HostArch(),
368
349
                Series: series.HostSeries(),
369
350
        }
385
366
        if password == "" {
386
367
                return nil, fmt.Errorf("cannot connect without admin-secret")
387
368
        }
388
 
        modelUUID, ok := config.UUID()
389
 
        if !ok {
390
 
                return nil, fmt.Errorf("cannot connect without model UUID")
391
 
        }
392
 
        modelTag := names.NewModelTag(modelUUID)
 
369
        modelTag := names.NewModelTag(config.UUID())
393
370
 
394
371
        mongoInfo.Password = password
395
372
        opts := mongo.DefaultDialOpts()
524
501
                s.DummyConfig = dummy.SampleConfig()
525
502
        }
526
503
        attrs := s.DummyConfig.Merge(testing.Attrs{
527
 
                "name":           "dummymodel",
 
504
                "name":           "admin",
528
505
                "admin-secret":   AdminSecret,
529
 
                "agent-version":  version.Current.String(),
 
506
                "agent-version":  jujuversion.Current.String(),
530
507
                "ca-cert":        testing.CACert,
531
508
                "ca-private-key": testing.CAKey,
532
509
        })
640
617
                agent.AgentConfigParams{
641
618
                        Paths:             paths,
642
619
                        Tag:               tag,
643
 
                        UpgradedToVersion: version.Current,
 
620
                        UpgradedToVersion: jujuversion.Current,
644
621
                        Password:          password,
645
622
                        Nonce:             "nonce",
646
623
                        StateAddresses:    s.MongoInfo(c).Addrs,