github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/provider/maas/config_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 jc "github.com/juju/testing/checkers" 8 "github.com/juju/utils" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/environs" 12 "github.com/juju/juju/testing" 13 ) 14 15 type configSuite struct { 16 testing.BaseSuite 17 } 18 19 var _ = gc.Suite(&configSuite{}) 20 21 // copyAttrs copies values from src into dest. If src contains a key that was 22 // already in dest, its value in dest will still be updated to the one from 23 // src. 24 func copyAttrs(src, dest map[string]interface{}) { 25 for k, v := range src { 26 dest[k] = v 27 } 28 } 29 30 // newConfig creates a MAAS environment config from attributes. 31 func newConfig(values map[string]interface{}) (*maasEnvironConfig, error) { 32 attrs := testing.FakeConfig().Merge(testing.Attrs{ 33 "name": "testenv", 34 "type": "maas", 35 }).Merge(values) 36 env, err := environs.NewFromAttrs(attrs) 37 if err != nil { 38 return nil, err 39 } 40 return env.(*maasEnviron).ecfg(), nil 41 } 42 43 func (*configSuite) TestParsesMAASSettings(c *gc.C) { 44 server := "http://maas.testing.invalid/maas/" 45 oauth := "consumer-key:resource-token:resource-secret" 46 future := "futurama" 47 48 uuid, err := utils.NewUUID() 49 c.Assert(err, jc.ErrorIsNil) 50 ecfg, err := newConfig(map[string]interface{}{ 51 "maas-server": server, 52 "maas-oauth": oauth, 53 "maas-agent-name": uuid.String(), 54 "future-key": future, 55 }) 56 c.Assert(err, jc.ErrorIsNil) 57 c.Check(ecfg.maasServer(), gc.Equals, server) 58 c.Check(ecfg.maasOAuth(), gc.DeepEquals, oauth) 59 c.Check(ecfg.maasAgentName(), gc.Equals, uuid.String()) 60 c.Check(ecfg.UnknownAttrs()["future-key"], gc.DeepEquals, future) 61 } 62 63 func (*configSuite) TestMaasAgentNameDefault(c *gc.C) { 64 ecfg, err := newConfig(map[string]interface{}{ 65 "maas-server": "http://maas.testing.invalid/maas/", 66 "maas-oauth": "consumer-key:resource-token:resource-secret", 67 }) 68 c.Assert(err, jc.ErrorIsNil) 69 c.Check(ecfg.maasAgentName(), gc.Equals, "") 70 } 71 72 func (*configSuite) TestChecksWellFormedMaasServer(c *gc.C) { 73 _, err := newConfig(map[string]interface{}{ 74 "maas-server": "This should have been a URL.", 75 "maas-oauth": "consumer-key:resource-token:resource-secret", 76 }) 77 c.Assert(err, gc.NotNil) 78 c.Check(err, gc.ErrorMatches, ".*malformed maas-server.*") 79 } 80 81 func (*configSuite) TestChecksWellFormedMaasOAuth(c *gc.C) { 82 _, err := newConfig(map[string]interface{}{ 83 "maas-server": "http://maas.testing.invalid/maas/", 84 "maas-oauth": "This should have been a 3-part token.", 85 }) 86 c.Assert(err, gc.NotNil) 87 c.Check(err, gc.ErrorMatches, ".*malformed maas-oauth.*") 88 } 89 90 func (*configSuite) TestBlockStorageProviderDefault(c *gc.C) { 91 ecfg, err := newConfig(map[string]interface{}{ 92 "maas-server": "http://maas.testing.invalid/maas/", 93 "maas-oauth": "consumer-key:resource-token:resource-secret", 94 }) 95 c.Assert(err, jc.ErrorIsNil) 96 src, _ := ecfg.StorageDefaultBlockSource() 97 c.Assert(src, gc.Equals, "maas") 98 } 99 100 func (*configSuite) TestValidateUpcallsEnvironsConfigValidate(c *gc.C) { 101 // The base Validate() function will not allow an environment to 102 // change its name. Trigger that error so as to prove that the 103 // environment provider's Validate() calls the base Validate(). 104 baseAttrs := map[string]interface{}{ 105 "maas-server": "http://maas.testing.invalid/maas/", 106 "maas-oauth": "consumer-key:resource-token:resource-secret", 107 } 108 oldCfg, err := newConfig(baseAttrs) 109 c.Assert(err, jc.ErrorIsNil) 110 newName := oldCfg.Name() + "-but-different" 111 newCfg, err := oldCfg.Apply(map[string]interface{}{"name": newName}) 112 c.Assert(err, jc.ErrorIsNil) 113 114 _, err = maasEnvironProvider{}.Validate(newCfg, oldCfg.Config) 115 116 c.Assert(err, gc.NotNil) 117 c.Check(err, gc.ErrorMatches, ".*cannot change name.*") 118 } 119 120 func (*configSuite) TestValidateCannotChangeAgentName(c *gc.C) { 121 baseAttrs := map[string]interface{}{ 122 "maas-server": "http://maas.testing.invalid/maas/", 123 "maas-oauth": "consumer-key:resource-token:resource-secret", 124 "maas-agent-name": "1234-5678", 125 } 126 oldCfg, err := newConfig(baseAttrs) 127 c.Assert(err, jc.ErrorIsNil) 128 newCfg, err := oldCfg.Apply(map[string]interface{}{ 129 "maas-agent-name": "9876-5432", 130 }) 131 c.Assert(err, jc.ErrorIsNil) 132 _, err = maasEnvironProvider{}.Validate(newCfg, oldCfg.Config) 133 c.Assert(err, gc.ErrorMatches, "cannot change maas-agent-name") 134 }