~jameinel/juju-core/api-registry-tracks-type

« back to all changes in this revision

Viewing changes to environs/maas/environprovider_test.go

Merge trunk and resolve conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package maas
 
2
 
 
3
import (
 
4
        "io/ioutil"
 
5
        . "launchpad.net/gocheck"
 
6
        "launchpad.net/goyaml"
 
7
        "launchpad.net/juju-core/environs/config"
 
8
        "launchpad.net/juju-core/state"
 
9
)
 
10
 
 
11
type EnvironProviderSuite struct {
 
12
        ProviderSuite
 
13
}
 
14
 
 
15
var _ = Suite(new(EnvironProviderSuite))
 
16
 
 
17
func (suite *EnvironProviderSuite) TestSecretAttrsReturnsSensitiveMAASAttributes(c *C) {
 
18
        testJujuHome := c.MkDir()
 
19
        defer config.SetJujuHome(config.SetJujuHome(testJujuHome))
 
20
        const oauth = "aa:bb:cc"
 
21
        attrs := map[string]interface{}{
 
22
                "maas-oauth":      oauth,
 
23
                "maas-server":     "http://maas.example.com/maas/",
 
24
                "name":            "wheee",
 
25
                "type":            "maas",
 
26
                "authorized-keys": "I-am-not-a-real-key",
 
27
        }
 
28
        config, err := config.New(attrs)
 
29
        c.Assert(err, IsNil)
 
30
 
 
31
        secretAttrs, err := suite.environ.Provider().SecretAttrs(config)
 
32
        c.Assert(err, IsNil)
 
33
 
 
34
        expectedAttrs := map[string]interface{}{"maas-oauth": oauth}
 
35
        c.Check(secretAttrs, DeepEquals, expectedAttrs)
 
36
}
 
37
 
 
38
// create a temporary file with the given content.  The file will be cleaned
 
39
// up at the end of the test calling this method.
 
40
func createTempFile(c *C, content []byte) string {
 
41
        file, err := ioutil.TempFile(c.MkDir(), "")
 
42
        c.Assert(err, IsNil)
 
43
        filename := file.Name()
 
44
        err = ioutil.WriteFile(filename, content, 0644)
 
45
        c.Assert(err, IsNil)
 
46
        return filename
 
47
}
 
48
 
 
49
// InstanceId returns the instanceId of the machine read from the file
 
50
// _MAASInstanceFilename.
 
51
func (suite *EnvironProviderSuite) TestInstanceIdReadsInstanceIdFromMachineFile(c *C) {
 
52
        instanceId := "instance-id"
 
53
        info := machineInfo{instanceId, "hostname"}
 
54
        yaml, err := goyaml.Marshal(info)
 
55
        c.Assert(err, IsNil)
 
56
        // Create a temporary file to act as the file where the instanceID
 
57
        // is stored.
 
58
        filename := createTempFile(c, yaml)
 
59
        // "Monkey patch" the value of _MAASInstanceFilename with the path
 
60
        // to the temporary file.
 
61
        old_MAASInstanceFilename := _MAASInstanceFilename
 
62
        _MAASInstanceFilename = filename
 
63
        defer func() { _MAASInstanceFilename = old_MAASInstanceFilename }()
 
64
 
 
65
        provider := suite.environ.Provider()
 
66
        returnedInstanceId, err := provider.InstanceId()
 
67
        c.Assert(err, IsNil)
 
68
        c.Check(returnedInstanceId, Equals, state.InstanceId(instanceId))
 
69
}
 
70
 
 
71
// PublicAddress and PrivateAddress return the hostname of the machine read
 
72
// from the file _MAASInstanceFilename.
 
73
func (suite *EnvironProviderSuite) TestPrivatePublicAddressReadsHostnameFromMachineFile(c *C) {
 
74
        hostname := "myhostname"
 
75
        info := machineInfo{"instance-id", hostname}
 
76
        yaml, err := goyaml.Marshal(info)
 
77
        c.Assert(err, IsNil)
 
78
        // Create a temporary file to act as the file where the instanceID
 
79
        // is stored.
 
80
        filename := createTempFile(c, yaml)
 
81
        // "Monkey patch" the value of _MAASInstanceFilename with the path
 
82
        // to the temporary file.
 
83
        old_MAASInstanceFilename := _MAASInstanceFilename
 
84
        _MAASInstanceFilename = filename
 
85
        defer func() { _MAASInstanceFilename = old_MAASInstanceFilename }()
 
86
 
 
87
        provider := suite.environ.Provider()
 
88
        publicAddress, err := provider.PublicAddress()
 
89
        c.Assert(err, IsNil)
 
90
        c.Check(publicAddress, Equals, hostname)
 
91
        privateAddress, err := provider.PrivateAddress()
 
92
        c.Assert(err, IsNil)
 
93
        c.Check(privateAddress, Equals, hostname)
 
94
}