5
. "launchpad.net/gocheck"
7
"launchpad.net/juju-core/environs/config"
8
"launchpad.net/juju-core/state"
11
type EnvironProviderSuite struct {
15
var _ = Suite(new(EnvironProviderSuite))
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{}{
23
"maas-server": "http://maas.example.com/maas/",
26
"authorized-keys": "I-am-not-a-real-key",
28
config, err := config.New(attrs)
31
secretAttrs, err := suite.environ.Provider().SecretAttrs(config)
34
expectedAttrs := map[string]interface{}{"maas-oauth": oauth}
35
c.Check(secretAttrs, DeepEquals, expectedAttrs)
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(), "")
43
filename := file.Name()
44
err = ioutil.WriteFile(filename, content, 0644)
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)
56
// Create a temporary file to act as the file where the instanceID
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 }()
65
provider := suite.environ.Provider()
66
returnedInstanceId, err := provider.InstanceId()
68
c.Check(returnedInstanceId, Equals, state.InstanceId(instanceId))
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)
78
// Create a temporary file to act as the file where the instanceID
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 }()
87
provider := suite.environ.Provider()
88
publicAddress, err := provider.PublicAddress()
90
c.Check(publicAddress, Equals, hostname)
91
privateAddress, err := provider.PrivateAddress()
93
c.Check(privateAddress, Equals, hostname)