github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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/gomaasapi"
     8  	jc "github.com/juju/testing/checkers"
     9  	"github.com/juju/utils/set"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  // Ensure MAAS provider supports the expected interfaces.
    17  var (
    18  	_ config.ConfigSchemaSource = (*maasEnvironProvider)(nil)
    19  )
    20  
    21  type configSuite struct {
    22  	testing.BaseSuite
    23  }
    24  
    25  var _ = gc.Suite(&configSuite{})
    26  
    27  // copyAttrs copies values from src into dest.  If src contains a key that was
    28  // already in dest, its value in dest will still be updated to the one from
    29  // src.
    30  func copyAttrs(src, dest map[string]interface{}) {
    31  	for k, v := range src {
    32  		dest[k] = v
    33  	}
    34  }
    35  
    36  // newConfig creates a MAAS environment config from attributes.
    37  func newConfig(values map[string]interface{}) (*maasModelConfig, error) {
    38  	attrs := testing.FakeConfig().Merge(testing.Attrs{
    39  		"name": "testenv",
    40  		"type": "maas",
    41  	}).Merge(values)
    42  	cfg, err := config.New(config.NoDefaults, attrs)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	return providerInstance.newConfig(cfg)
    47  }
    48  
    49  func (s *configSuite) SetUpTest(c *gc.C) {
    50  	s.BaseSuite.SetUpTest(c)
    51  	mockCapabilities := func(*gomaasapi.MAASObject, string) (set.Strings, error) {
    52  		return set.NewStrings("network-deployment-ubuntu"), nil
    53  	}
    54  	s.PatchValue(&GetCapabilities, mockCapabilities)
    55  	mockGetController := func(string, string) (gomaasapi.Controller, error) {
    56  		return nil, gomaasapi.NewUnsupportedVersionError("oops")
    57  	}
    58  	s.PatchValue(&GetMAAS2Controller, mockGetController)
    59  }
    60  
    61  func (*configSuite) TestValidateUpcallsEnvironsConfigValidate(c *gc.C) {
    62  	// The base Validate() function will not allow an environment to
    63  	// change its name.  Trigger that error so as to prove that the
    64  	// environment provider's Validate() calls the base Validate().
    65  	oldCfg, err := newConfig(nil)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	newName := oldCfg.Name() + "-but-different"
    68  	newCfg, err := oldCfg.Apply(map[string]interface{}{"name": newName})
    69  	c.Assert(err, jc.ErrorIsNil)
    70  
    71  	_, err = maasEnvironProvider{}.Validate(newCfg, oldCfg.Config)
    72  
    73  	c.Assert(err, gc.NotNil)
    74  	c.Check(err, gc.ErrorMatches, ".*cannot change name.*")
    75  }
    76  
    77  func (*configSuite) TestSchema(c *gc.C) {
    78  	fields := providerInstance.Schema()
    79  	// Check that all the fields defined in environs/config
    80  	// are in the returned schema.
    81  	globalFields, err := config.Schema(nil)
    82  	c.Assert(err, gc.IsNil)
    83  	for name, field := range globalFields {
    84  		c.Check(fields[name], jc.DeepEquals, field)
    85  	}
    86  }