github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/environs/config"
    12  	"github.com/juju/juju/testing"
    13  )
    14  
    15  // Ensure MAAS provider supports the expected interfaces.
    16  var (
    17  	_ config.ConfigSchemaSource = (*MaasEnvironProvider)(nil)
    18  )
    19  
    20  type configSuite struct {
    21  	testing.BaseSuite
    22  }
    23  
    24  var _ = gc.Suite(&configSuite{})
    25  
    26  // newConfig creates a MAAS environment config from attributes.
    27  func newConfig(values map[string]interface{}) (*maasModelConfig, error) {
    28  	attrs := testing.FakeConfig().Merge(testing.Attrs{
    29  		"name": "testmodel",
    30  		"type": "maas",
    31  	}).Merge(values)
    32  	cfg, err := config.New(config.NoDefaults, attrs)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return providerInstance.newConfig(cfg)
    37  }
    38  
    39  func (s *configSuite) SetUpTest(c *gc.C) {
    40  	s.BaseSuite.SetUpTest(c)
    41  	mockGetController := func(string, string) (gomaasapi.Controller, error) {
    42  		return nil, gomaasapi.NewUnsupportedVersionError("oops")
    43  	}
    44  	s.PatchValue(&GetMAAS2Controller, mockGetController)
    45  }
    46  
    47  func (*configSuite) TestValidateUpcallsEnvironsConfigValidate(c *gc.C) {
    48  	// The base Validate() function will not allow an environment to
    49  	// change its name.  Trigger that error so as to prove that the
    50  	// environment provider's Validate() calls the base Validate().
    51  	oldCfg, err := newConfig(nil)
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	newName := oldCfg.Name() + "-but-different"
    54  	newCfg, err := oldCfg.Apply(map[string]interface{}{"name": newName})
    55  	c.Assert(err, jc.ErrorIsNil)
    56  
    57  	_, err = MaasEnvironProvider{}.Validate(newCfg, oldCfg.Config)
    58  
    59  	c.Assert(err, gc.NotNil)
    60  	c.Check(err, gc.ErrorMatches, ".*cannot change name.*")
    61  }
    62  
    63  func (*configSuite) TestSchema(c *gc.C) {
    64  	fields := providerInstance.Schema()
    65  	// Check that all the fields defined in environs/config
    66  	// are in the returned schema.
    67  	globalFields, err := config.Schema(nil)
    68  	c.Assert(err, gc.IsNil)
    69  	for name, field := range globalFields {
    70  		c.Check(fields[name], jc.DeepEquals, field)
    71  	}
    72  }