launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  	gc "launchpad.net/gocheck"
    11  
    12  	"launchpad.net/juju-core/environs/config"
    13  	coretesting "launchpad.net/juju-core/testing"
    14  	jc "launchpad.net/juju-core/testing/checkers"
    15  	"launchpad.net/juju-core/testing/testbase"
    16  )
    17  
    18  type configSuite struct {
    19  	testbase.LoggingSuite
    20  }
    21  
    22  var _ = gc.Suite(&configSuite{})
    23  
    24  func MinimalConfigValues() map[string]interface{} {
    25  	return map[string]interface{}{
    26  		"name":             "test",
    27  		"type":             "manual",
    28  		"bootstrap-host":   "hostname",
    29  		"storage-auth-key": "whatever",
    30  		// Not strictly necessary, but simplifies testing by disabling
    31  		// ssh storage by default.
    32  		"use-sshstorage": false,
    33  		// While the ca-cert bits aren't entirely minimal, they avoid the need
    34  		// to set up a fake home.
    35  		"ca-cert":        coretesting.CACert,
    36  		"ca-private-key": coretesting.CAKey,
    37  	}
    38  }
    39  
    40  func MinimalConfig(c *gc.C) *config.Config {
    41  	minimal := MinimalConfigValues()
    42  	testConfig, err := config.New(config.UseDefaults, minimal)
    43  	c.Assert(err, gc.IsNil)
    44  	return testConfig
    45  }
    46  
    47  func getEnvironConfig(c *gc.C, attrs map[string]interface{}) *environConfig {
    48  	testConfig, err := config.New(config.UseDefaults, attrs)
    49  	c.Assert(err, gc.IsNil)
    50  	envConfig, err := manualProvider{}.validate(testConfig, nil)
    51  	c.Assert(err, gc.IsNil)
    52  	return envConfig
    53  }
    54  
    55  func (s *configSuite) TestValidateConfig(c *gc.C) {
    56  	testConfig := MinimalConfig(c)
    57  	testConfig, err := testConfig.Apply(map[string]interface{}{"bootstrap-host": ""})
    58  	c.Assert(err, gc.IsNil)
    59  	_, err = manualProvider{}.Validate(testConfig, nil)
    60  	c.Assert(err, gc.ErrorMatches, "bootstrap-host must be specified")
    61  
    62  	testConfig, err = testConfig.Apply(map[string]interface{}{"storage-auth-key": nil})
    63  	c.Assert(err, gc.IsNil)
    64  	_, err = manualProvider{}.Validate(testConfig, nil)
    65  	c.Assert(err, gc.ErrorMatches, "storage-auth-key: expected string, got nothing")
    66  
    67  	testConfig = MinimalConfig(c)
    68  	valid, err := manualProvider{}.Validate(testConfig, nil)
    69  	c.Assert(err, gc.IsNil)
    70  
    71  	unknownAttrs := valid.UnknownAttrs()
    72  	c.Assert(unknownAttrs["bootstrap-host"], gc.Equals, "hostname")
    73  	c.Assert(unknownAttrs["bootstrap-user"], gc.Equals, "")
    74  	c.Assert(unknownAttrs["storage-listen-ip"], gc.Equals, "")
    75  	c.Assert(unknownAttrs["storage-port"], gc.Equals, int64(8040))
    76  }
    77  
    78  func (s *configSuite) TestConfigMutability(c *gc.C) {
    79  	testConfig := MinimalConfig(c)
    80  	valid, err := manualProvider{}.Validate(testConfig, nil)
    81  	c.Assert(err, gc.IsNil)
    82  	unknownAttrs := valid.UnknownAttrs()
    83  
    84  	// Make sure the immutable values can't be changed. It'd be nice to be
    85  	// able to change these, but that would involve somehow updating the
    86  	// machine agent's config/upstart config.
    87  	oldConfig := testConfig
    88  	for k, v := range map[string]interface{}{
    89  		"bootstrap-host":    "new-hostname",
    90  		"bootstrap-user":    "new-username",
    91  		"storage-listen-ip": "10.0.0.123",
    92  		"storage-port":      int64(1234),
    93  	} {
    94  		testConfig = MinimalConfig(c)
    95  		testConfig, err = testConfig.Apply(map[string]interface{}{k: v})
    96  		c.Assert(err, gc.IsNil)
    97  		_, err := manualProvider{}.Validate(testConfig, oldConfig)
    98  		oldv := unknownAttrs[k]
    99  		errmsg := fmt.Sprintf("cannot change %s from %q to %q", k, oldv, v)
   100  		c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(errmsg))
   101  	}
   102  }
   103  
   104  func (s *configSuite) TestBootstrapHostUser(c *gc.C) {
   105  	values := MinimalConfigValues()
   106  	testConfig := getEnvironConfig(c, values)
   107  	c.Assert(testConfig.bootstrapHost(), gc.Equals, "hostname")
   108  	c.Assert(testConfig.bootstrapUser(), gc.Equals, "")
   109  	values["bootstrap-host"] = "127.0.0.1"
   110  	values["bootstrap-user"] = "ubuntu"
   111  	testConfig = getEnvironConfig(c, values)
   112  	c.Assert(testConfig.bootstrapHost(), gc.Equals, "127.0.0.1")
   113  	c.Assert(testConfig.bootstrapUser(), gc.Equals, "ubuntu")
   114  }
   115  
   116  func (s *configSuite) TestStorageParams(c *gc.C) {
   117  	values := MinimalConfigValues()
   118  	testConfig := getEnvironConfig(c, values)
   119  	c.Assert(testConfig.storageAddr(), gc.Equals, "hostname:8040")
   120  	c.Assert(testConfig.storageListenAddr(), gc.Equals, ":8040")
   121  	values["storage-listen-ip"] = "10.0.0.123"
   122  	values["storage-port"] = int64(1234)
   123  	testConfig = getEnvironConfig(c, values)
   124  	c.Assert(testConfig.storageAddr(), gc.Equals, "hostname:1234")
   125  	c.Assert(testConfig.storageListenAddr(), gc.Equals, "10.0.0.123:1234")
   126  }
   127  
   128  func (s *configSuite) TestStorageCompat(c *gc.C) {
   129  	// Older environment configurations will not have the
   130  	// use-sshstorage attribute. We treat them as if they
   131  	// have use-sshstorage=false.
   132  	values := MinimalConfigValues()
   133  	delete(values, "use-sshstorage")
   134  	cfg, err := config.New(config.UseDefaults, values)
   135  	c.Assert(err, gc.IsNil)
   136  	envConfig := newEnvironConfig(cfg, values)
   137  	c.Assert(err, gc.IsNil)
   138  	c.Assert(envConfig.useSSHStorage(), jc.IsFalse)
   139  }