~rogpeppe/juju-core/438-local-instance-Addresses

« back to all changes in this revision

Viewing changes to provider/maas/environ_test.go

  • Committer: John Arbash Meinel
  • Date: 2013-09-15 07:36:52 UTC
  • mfrom: (1817 juju-core)
  • mto: This revision was merged to the branch mainline in revision 1882.
  • Revision ID: john@arbash-meinel.com-20130915073652-80jnupeguvr1klea
Merge trunk, resolve conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import (
7
7
        "encoding/base64"
8
8
        "fmt"
 
9
        "io/ioutil"
9
10
        "net/url"
10
11
 
11
12
        gc "launchpad.net/gocheck"
16
17
        "launchpad.net/juju-core/environs"
17
18
        "launchpad.net/juju-core/environs/bootstrap"
18
19
        "launchpad.net/juju-core/environs/config"
 
20
        "launchpad.net/juju-core/environs/imagemetadata"
 
21
        "launchpad.net/juju-core/environs/simplestreams"
19
22
        envtesting "launchpad.net/juju-core/environs/testing"
 
23
        envtools "launchpad.net/juju-core/environs/tools"
20
24
        "launchpad.net/juju-core/errors"
21
25
        "launchpad.net/juju-core/instance"
22
26
        "launchpad.net/juju-core/provider"
48
52
        return ecfg.Config
49
53
}
50
54
 
51
 
// makeEnviron creates a functional maasEnviron for a test.  Its configuration
52
 
// is a bit arbitrary and none of the test code's business.
 
55
// makeEnviron creates a functional maasEnviron for a test.
53
56
func (suite *environSuite) makeEnviron() *maasEnviron {
54
 
        config, err := config.New(map[string]interface{}{
55
 
                "name":            suite.environ.Name(),
56
 
                "type":            "maas",
57
 
                "admin-secret":    "local-secret",
58
 
                "authorized-keys": "foo",
59
 
                "agent-version":   version.CurrentNumber().String(),
60
 
                "maas-oauth":      "a:b:c",
61
 
                "maas-server":     suite.testMAASObject.TestServer.URL,
62
 
                // These are not needed by MAAS, but juju-core breaks without them. Needs
63
 
                // fixing there.
64
 
                "ca-cert":        testing.CACert,
65
 
                "ca-private-key": testing.CAKey,
 
57
        attrs := testing.FakeConfig().Merge(testing.Attrs{
 
58
                "name":        suite.environ.Name(),
 
59
                "type":        "maas",
 
60
                "maas-oauth":  "a:b:c",
 
61
                "maas-server": suite.testMAASObject.TestServer.URL,
66
62
        })
 
63
        cfg, err := config.New(config.NoDefaults, attrs)
67
64
        if err != nil {
68
65
                panic(err)
69
66
        }
70
 
        env, err := NewEnviron(config)
 
67
        env, err := NewEnviron(cfg)
71
68
        if err != nil {
72
69
                panic(err)
73
70
        }
474
471
        err := bootstrap.Bootstrap(env, constraints.Value{})
475
472
        c.Assert(err, gc.IsNil)
476
473
}
 
474
 
 
475
func assertSourceContents(c *gc.C, source simplestreams.DataSource, filename string, content []byte) {
 
476
        rc, _, err := source.Fetch(filename)
 
477
        c.Assert(err, gc.IsNil)
 
478
        defer rc.Close()
 
479
        retrieved, err := ioutil.ReadAll(rc)
 
480
        c.Assert(err, gc.IsNil)
 
481
        c.Assert(retrieved, gc.DeepEquals, content)
 
482
}
 
483
 
 
484
func (suite *environSuite) TestGetImageMetadataSources(c *gc.C) {
 
485
        env := suite.makeEnviron()
 
486
        // Add a dummy file to storage so we can use that to check the
 
487
        // obtained source later.
 
488
        data := makeRandomBytes(10)
 
489
        suite.testMAASObject.TestServer.NewFile("filename", data)
 
490
        sources, err := imagemetadata.GetMetadataSources(env)
 
491
        c.Assert(err, gc.IsNil)
 
492
        c.Assert(len(sources), gc.Equals, 2)
 
493
        assertSourceContents(c, sources[0], "filename", data)
 
494
        url, err := sources[1].URL("")
 
495
        c.Assert(err, gc.IsNil)
 
496
        c.Assert(url, gc.Equals, imagemetadata.DefaultBaseURL+"/")
 
497
}
 
498
 
 
499
func (suite *environSuite) TestGetToolsMetadataSources(c *gc.C) {
 
500
        env := suite.makeEnviron()
 
501
        // Add a dummy file to storage so we can use that to check the
 
502
        // obtained source later.
 
503
        data := makeRandomBytes(10)
 
504
        suite.testMAASObject.TestServer.NewFile("tools/filename", data)
 
505
        sources, err := envtools.GetMetadataSources(env)
 
506
        c.Assert(err, gc.IsNil)
 
507
        c.Assert(len(sources), gc.Equals, 1)
 
508
        assertSourceContents(c, sources[0], "filename", data)
 
509
}