github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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) TestValidateUpcallsEnvironsConfigValidate(c *gc.C) { 91 // The base Validate() function will not allow an environment to 92 // change its name. Trigger that error so as to prove that the 93 // environment provider's Validate() calls the base Validate(). 94 baseAttrs := map[string]interface{}{ 95 "maas-server": "http://maas.testing.invalid/maas/", 96 "maas-oauth": "consumer-key:resource-token:resource-secret", 97 } 98 oldCfg, err := newConfig(baseAttrs) 99 c.Assert(err, jc.ErrorIsNil) 100 newName := oldCfg.Name() + "-but-different" 101 newCfg, err := oldCfg.Apply(map[string]interface{}{"name": newName}) 102 c.Assert(err, jc.ErrorIsNil) 103 104 _, err = maasEnvironProvider{}.Validate(newCfg, oldCfg.Config) 105 106 c.Assert(err, gc.NotNil) 107 c.Check(err, gc.ErrorMatches, ".*cannot change name.*") 108 } 109 110 func (*configSuite) TestValidateCannotChangeAgentName(c *gc.C) { 111 baseAttrs := map[string]interface{}{ 112 "maas-server": "http://maas.testing.invalid/maas/", 113 "maas-oauth": "consumer-key:resource-token:resource-secret", 114 "maas-agent-name": "1234-5678", 115 } 116 oldCfg, err := newConfig(baseAttrs) 117 c.Assert(err, jc.ErrorIsNil) 118 newCfg, err := oldCfg.Apply(map[string]interface{}{ 119 "maas-agent-name": "9876-5432", 120 }) 121 c.Assert(err, jc.ErrorIsNil) 122 _, err = maasEnvironProvider{}.Validate(newCfg, oldCfg.Config) 123 c.Assert(err, gc.ErrorMatches, "cannot change maas-agent-name") 124 }