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