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