github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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/environs/config" 14 envtesting "github.com/juju/juju/environs/testing" 15 "github.com/juju/juju/juju/osenv" 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 testJujuHome := c.MkDir() 27 defer osenv.SetJujuHome(osenv.SetJujuHome(testJujuHome)) 28 const oauth = "aa:bb:cc" 29 attrs := testing.FakeConfig().Merge(testing.Attrs{ 30 "type": "maas", 31 "maas-oauth": oauth, 32 "maas-server": "http://maas.testing.invalid/maas/", 33 }) 34 config, err := config.New(config.NoDefaults, attrs) 35 c.Assert(err, jc.ErrorIsNil) 36 37 secretAttrs, err := suite.makeEnviron().Provider().SecretAttrs(config) 38 c.Assert(err, jc.ErrorIsNil) 39 40 expectedAttrs := map[string]string{"maas-oauth": oauth} 41 c.Check(secretAttrs, gc.DeepEquals, expectedAttrs) 42 } 43 44 func (suite *EnvironProviderSuite) TestUnknownAttrsContainAgentName(c *gc.C) { 45 testJujuHome := c.MkDir() 46 defer osenv.SetJujuHome(osenv.SetJujuHome(testJujuHome)) 47 attrs := testing.FakeConfig().Merge(testing.Attrs{ 48 "type": "maas", 49 "maas-oauth": "aa:bb:cc", 50 "maas-server": "http://maas.testing.invalid/maas/", 51 }) 52 config, err := config.New(config.NoDefaults, attrs) 53 c.Assert(err, jc.ErrorIsNil) 54 55 ctx := envtesting.BootstrapContext(c) 56 environ, err := suite.makeEnviron().Provider().PrepareForBootstrap(ctx, config) 57 c.Assert(err, jc.ErrorIsNil) 58 59 preparedConfig := environ.Config() 60 unknownAttrs := preparedConfig.UnknownAttrs() 61 62 uuid, ok := unknownAttrs["maas-agent-name"] 63 64 c.Assert(ok, jc.IsTrue) 65 c.Assert(uuid, jc.Satisfies, utils.IsValidUUIDString) 66 } 67 68 func (suite *EnvironProviderSuite) TestAgentNameShouldNotBeSetByHand(c *gc.C) { 69 testJujuHome := c.MkDir() 70 defer osenv.SetJujuHome(osenv.SetJujuHome(testJujuHome)) 71 attrs := testing.FakeConfig().Merge(testing.Attrs{ 72 "type": "maas", 73 "maas-oauth": "aa:bb:cc", 74 "maas-server": "http://maas.testing.invalid/maas/", 75 "maas-agent-name": "foobar", 76 }) 77 config, err := config.New(config.NoDefaults, attrs) 78 c.Assert(err, jc.ErrorIsNil) 79 80 ctx := envtesting.BootstrapContext(c) 81 _, err = suite.makeEnviron().Provider().PrepareForBootstrap(ctx, config) 82 c.Assert(err, gc.Equals, errAgentNameAlreadySet) 83 } 84 85 // create a temporary file with the given content. The file will be cleaned 86 // up at the end of the test calling this method. 87 func createTempFile(c *gc.C, content []byte) string { 88 file, err := ioutil.TempFile(c.MkDir(), "") 89 c.Assert(err, jc.ErrorIsNil) 90 filename := file.Name() 91 err = ioutil.WriteFile(filename, content, 0644) 92 c.Assert(err, jc.ErrorIsNil) 93 return filename 94 } 95 96 func (suite *EnvironProviderSuite) TestOpenReturnsNilInterfaceUponFailure(c *gc.C) { 97 testJujuHome := c.MkDir() 98 defer osenv.SetJujuHome(osenv.SetJujuHome(testJujuHome)) 99 const oauth = "wrongly-formatted-oauth-string" 100 attrs := testing.FakeConfig().Merge(testing.Attrs{ 101 "type": "maas", 102 "maas-oauth": oauth, 103 "maas-server": "http://maas.testing.invalid/maas/", 104 }) 105 config, err := config.New(config.NoDefaults, attrs) 106 c.Assert(err, jc.ErrorIsNil) 107 env, err := suite.makeEnviron().Provider().Open(config) 108 // When Open() fails (i.e. returns a non-nil error), it returns an 109 // environs.Environ interface object with a nil value and a nil 110 // type. 111 c.Check(env, gc.Equals, nil) 112 c.Check(err, gc.ErrorMatches, ".*malformed maas-oauth.*") 113 }