github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/provider/manual/config_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package manual 5 6 import ( 7 "fmt" 8 "regexp" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/environs/config" 14 coretesting "github.com/juju/juju/testing" 15 ) 16 17 type configSuite struct { 18 coretesting.FakeJujuXDGDataHomeSuite 19 } 20 21 var _ = gc.Suite(&configSuite{}) 22 23 func MinimalConfigValues() map[string]interface{} { 24 return map[string]interface{}{ 25 "name": "test", 26 "type": "manual", 27 "uuid": coretesting.ModelTag.Id(), 28 "controller-uuid": coretesting.ModelTag.Id(), 29 "bootstrap-host": "hostname", 30 "bootstrap-user": "", 31 // While the ca-cert bits aren't entirely minimal, they avoid the need 32 // to set up a fake home. 33 "ca-cert": coretesting.CACert, 34 "ca-private-key": coretesting.CAKey, 35 } 36 } 37 38 func MinimalConfig(c *gc.C) *config.Config { 39 minimal := MinimalConfigValues() 40 testConfig, err := config.New(config.UseDefaults, minimal) 41 c.Assert(err, jc.ErrorIsNil) 42 return testConfig 43 } 44 45 func getModelConfig(c *gc.C, attrs map[string]interface{}) *environConfig { 46 testConfig, err := config.New(config.UseDefaults, attrs) 47 c.Assert(err, jc.ErrorIsNil) 48 envConfig, err := manualProvider{}.validate(testConfig, nil) 49 c.Assert(err, jc.ErrorIsNil) 50 return envConfig 51 } 52 53 func (s *configSuite) TestValidateConfig(c *gc.C) { 54 testConfig := MinimalConfig(c) 55 testConfig, err := testConfig.Apply(map[string]interface{}{"bootstrap-host": ""}) 56 c.Assert(err, jc.ErrorIsNil) 57 _, err = manualProvider{}.Validate(testConfig, nil) 58 c.Assert(err, gc.ErrorMatches, "bootstrap-host must be specified") 59 60 values := MinimalConfigValues() 61 delete(values, "bootstrap-user") 62 testConfig, err = config.New(config.UseDefaults, values) 63 c.Assert(err, jc.ErrorIsNil) 64 65 valid, err := manualProvider{}.Validate(testConfig, nil) 66 c.Assert(err, jc.ErrorIsNil) 67 unknownAttrs := valid.UnknownAttrs() 68 c.Assert(unknownAttrs["bootstrap-host"], gc.Equals, "hostname") 69 c.Assert(unknownAttrs["bootstrap-user"], gc.Equals, "") 70 } 71 72 func (s *configSuite) TestConfigMutability(c *gc.C) { 73 testConfig := MinimalConfig(c) 74 valid, err := manualProvider{}.Validate(testConfig, nil) 75 c.Assert(err, jc.ErrorIsNil) 76 unknownAttrs := valid.UnknownAttrs() 77 78 // Make sure the immutable values can't be changed. It'd be nice to be 79 // able to change these, but that would involve somehow updating the 80 // machine agent's config/upstart config. 81 oldConfig := testConfig 82 for k, v := range map[string]interface{}{ 83 "bootstrap-host": "new-hostname", 84 "bootstrap-user": "new-username", 85 } { 86 testConfig = MinimalConfig(c) 87 testConfig, err = testConfig.Apply(map[string]interface{}{k: v}) 88 c.Assert(err, jc.ErrorIsNil) 89 _, err := manualProvider{}.Validate(testConfig, oldConfig) 90 oldv := unknownAttrs[k] 91 errmsg := fmt.Sprintf("cannot change %s from %q to %q", k, oldv, v) 92 c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(errmsg)) 93 } 94 } 95 96 func (s *configSuite) TestBootstrapHostUser(c *gc.C) { 97 values := MinimalConfigValues() 98 testConfig := getModelConfig(c, values) 99 c.Assert(testConfig.bootstrapHost(), gc.Equals, "hostname") 100 c.Assert(testConfig.bootstrapUser(), gc.Equals, "") 101 values["bootstrap-host"] = "127.0.0.1" 102 values["bootstrap-user"] = "ubuntu" 103 testConfig = getModelConfig(c, values) 104 c.Assert(testConfig.bootstrapHost(), gc.Equals, "127.0.0.1") 105 c.Assert(testConfig.bootstrapUser(), gc.Equals, "ubuntu") 106 }