github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/provider/maas/environprovider_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package maas 5 6 import ( 7 "io/ioutil" 8 9 jc "github.com/juju/testing/checkers" 10 "github.com/juju/utils" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/cloud" 14 "github.com/juju/juju/environs" 15 "github.com/juju/juju/environs/config" 16 "github.com/juju/juju/testing" 17 ) 18 19 type EnvironProviderSuite struct { 20 providerSuite 21 } 22 23 var _ = gc.Suite(&EnvironProviderSuite{}) 24 25 func (suite *EnvironProviderSuite) TestSecretAttrsReturnsSensitiveMAASAttributes(c *gc.C) { 26 const oauth = "aa:bb:cc" 27 attrs := testing.FakeConfig().Merge(testing.Attrs{ 28 "type": "maas", 29 "maas-oauth": oauth, 30 "maas-server": "http://maas.testing.invalid/maas/", 31 }) 32 config, err := config.New(config.NoDefaults, attrs) 33 c.Assert(err, jc.ErrorIsNil) 34 35 secretAttrs, err := providerInstance.SecretAttrs(config) 36 c.Assert(err, jc.ErrorIsNil) 37 38 expectedAttrs := map[string]string{"maas-oauth": oauth} 39 c.Check(secretAttrs, gc.DeepEquals, expectedAttrs) 40 } 41 42 func (suite *EnvironProviderSuite) TestCredentialsSetup(c *gc.C) { 43 attrs := testing.FakeConfig().Merge(testing.Attrs{ 44 "type": "maas", 45 }) 46 config, err := config.New(config.NoDefaults, attrs) 47 c.Assert(err, jc.ErrorIsNil) 48 49 cfg, err := providerInstance.BootstrapConfig(environs.BootstrapConfigParams{ 50 Config: config, 51 CloudEndpoint: "http://maas.testing.invalid/maas/", 52 Credentials: cloud.NewCredential( 53 cloud.OAuth1AuthType, 54 map[string]string{ 55 "maas-oauth": "aa:bb:cc", 56 }, 57 ), 58 }) 59 c.Assert(err, jc.ErrorIsNil) 60 61 attrs = cfg.UnknownAttrs() 62 server, ok := attrs["maas-server"] 63 c.Assert(ok, jc.IsTrue) 64 c.Assert(server, gc.Equals, "http://maas.testing.invalid/maas/") 65 oauth, ok := attrs["maas-oauth"] 66 c.Assert(ok, jc.IsTrue) 67 c.Assert(oauth, gc.Equals, "aa:bb:cc") 68 } 69 70 func (suite *EnvironProviderSuite) TestUnknownAttrsContainAgentName(c *gc.C) { 71 attrs := testing.FakeConfig().Merge(testing.Attrs{ 72 "type": "maas", 73 }) 74 config, err := config.New(config.NoDefaults, attrs) 75 c.Assert(err, jc.ErrorIsNil) 76 77 cfg, err := providerInstance.BootstrapConfig(environs.BootstrapConfigParams{ 78 Config: config, 79 CloudEndpoint: "http://maas.testing.invalid/maas/", 80 Credentials: cloud.NewCredential( 81 cloud.OAuth1AuthType, 82 map[string]string{ 83 "maas-oauth": "aa:bb:cc", 84 }, 85 ), 86 }) 87 c.Assert(err, jc.ErrorIsNil) 88 89 unknownAttrs := cfg.UnknownAttrs() 90 c.Assert(unknownAttrs["maas-server"], gc.Equals, "http://maas.testing.invalid/maas/") 91 92 uuid, ok := unknownAttrs["maas-agent-name"] 93 94 c.Assert(ok, jc.IsTrue) 95 c.Assert(uuid, jc.Satisfies, utils.IsValidUUIDString) 96 } 97 98 func (suite *EnvironProviderSuite) TestMAASServerFromRegion(c *gc.C) { 99 attrs := testing.FakeConfig().Merge(testing.Attrs{ 100 "type": "maas", 101 }) 102 config, err := config.New(config.NoDefaults, attrs) 103 c.Assert(err, jc.ErrorIsNil) 104 105 cfg, err := providerInstance.BootstrapConfig(environs.BootstrapConfigParams{ 106 Config: config, 107 CloudRegion: "maas.testing", 108 Credentials: cloud.NewCredential( 109 cloud.OAuth1AuthType, 110 map[string]string{ 111 "maas-oauth": "aa:bb:cc", 112 }, 113 ), 114 }) 115 c.Assert(err, jc.ErrorIsNil) 116 117 unknownAttrs := cfg.UnknownAttrs() 118 c.Assert(unknownAttrs["maas-server"], gc.Equals, "http://maas.testing/MAAS") 119 } 120 121 func (suite *EnvironProviderSuite) TestPrepareSetsAgentName(c *gc.C) { 122 attrs := testing.FakeConfig().Merge(testing.Attrs{ 123 "type": "maas", 124 "maas-oauth": "aa:bb:cc", 125 "maas-server": "http://maas.testing.invalid/maas/", 126 }) 127 config, err := config.New(config.NoDefaults, attrs) 128 c.Assert(err, jc.ErrorIsNil) 129 130 config, err = providerInstance.PrepareForCreateEnvironment(config) 131 c.Assert(err, jc.ErrorIsNil) 132 133 uuid, ok := config.UnknownAttrs()["maas-agent-name"] 134 c.Assert(ok, jc.IsTrue) 135 c.Assert(uuid, jc.Satisfies, utils.IsValidUUIDString) 136 } 137 138 func (suite *EnvironProviderSuite) TestPrepareExistingAgentName(c *gc.C) { 139 attrs := testing.FakeConfig().Merge(testing.Attrs{ 140 "type": "maas", 141 "maas-oauth": "aa:bb:cc", 142 "maas-server": "http://maas.testing.invalid/maas/", 143 "maas-agent-name": "foobar", 144 }) 145 config, err := config.New(config.NoDefaults, attrs) 146 c.Assert(err, jc.ErrorIsNil) 147 148 _, err = providerInstance.PrepareForCreateEnvironment(config) 149 c.Assert(err, gc.Equals, errAgentNameAlreadySet) 150 } 151 152 func (suite *EnvironProviderSuite) TestAgentNameShouldNotBeSetByHand(c *gc.C) { 153 attrs := testing.FakeConfig().Merge(testing.Attrs{ 154 "type": "maas", 155 "maas-agent-name": "foobar", 156 }) 157 config, err := config.New(config.NoDefaults, attrs) 158 c.Assert(err, jc.ErrorIsNil) 159 160 _, err = providerInstance.BootstrapConfig(environs.BootstrapConfigParams{ 161 Config: config, 162 CloudEndpoint: "http://maas.testing.invalid/maas/", 163 Credentials: cloud.NewCredential( 164 cloud.OAuth1AuthType, 165 map[string]string{ 166 "maas-oauth": "aa:bb:cc", 167 }, 168 ), 169 }) 170 c.Assert(err, gc.Equals, errAgentNameAlreadySet) 171 } 172 173 // create a temporary file with the given content. The file will be cleaned 174 // up at the end of the test calling this method. 175 func createTempFile(c *gc.C, content []byte) string { 176 file, err := ioutil.TempFile(c.MkDir(), "") 177 c.Assert(err, jc.ErrorIsNil) 178 filename := file.Name() 179 err = ioutil.WriteFile(filename, content, 0644) 180 c.Assert(err, jc.ErrorIsNil) 181 return filename 182 } 183 184 func (suite *EnvironProviderSuite) TestOpenReturnsNilInterfaceUponFailure(c *gc.C) { 185 const oauth = "wrongly-formatted-oauth-string" 186 attrs := testing.FakeConfig().Merge(testing.Attrs{ 187 "type": "maas", 188 "maas-oauth": oauth, 189 "maas-server": "http://maas.testing.invalid/maas/", 190 }) 191 config, err := config.New(config.NoDefaults, attrs) 192 c.Assert(err, jc.ErrorIsNil) 193 env, err := providerInstance.Open(config) 194 // When Open() fails (i.e. returns a non-nil error), it returns an 195 // environs.Environ interface object with a nil value and a nil 196 // type. 197 c.Check(env, gc.Equals, nil) 198 c.Check(err, gc.ErrorMatches, ".*malformed maas-oauth.*") 199 }