github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/provider/maas/environprovider_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  	"io/ioutil"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	"github.com/juju/utils"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/environs/config"
    14  	envtesting "github.com/juju/juju/environs/testing"
    15  	"github.com/juju/juju/testing"
    16  )
    17  
    18  type EnvironProviderSuite struct {
    19  	providerSuite
    20  }
    21  
    22  var _ = gc.Suite(&EnvironProviderSuite{})
    23  
    24  func (suite *EnvironProviderSuite) TestSecretAttrsReturnsSensitiveMAASAttributes(c *gc.C) {
    25  	const oauth = "aa:bb:cc"
    26  	attrs := testing.FakeConfig().Merge(testing.Attrs{
    27  		"type":        "maas",
    28  		"maas-oauth":  oauth,
    29  		"maas-server": "http://maas.testing.invalid/maas/",
    30  	})
    31  	config, err := config.New(config.NoDefaults, attrs)
    32  	c.Assert(err, jc.ErrorIsNil)
    33  
    34  	secretAttrs, err := providerInstance.SecretAttrs(config)
    35  	c.Assert(err, jc.ErrorIsNil)
    36  
    37  	expectedAttrs := map[string]string{"maas-oauth": oauth}
    38  	c.Check(secretAttrs, gc.DeepEquals, expectedAttrs)
    39  }
    40  
    41  func (suite *EnvironProviderSuite) TestUnknownAttrsContainAgentName(c *gc.C) {
    42  	attrs := testing.FakeConfig().Merge(testing.Attrs{
    43  		"type":        "maas",
    44  		"maas-oauth":  "aa:bb:cc",
    45  		"maas-server": "http://maas.testing.invalid/maas/",
    46  	})
    47  	config, err := config.New(config.NoDefaults, attrs)
    48  	c.Assert(err, jc.ErrorIsNil)
    49  
    50  	ctx := envtesting.BootstrapContext(c)
    51  	environ, err := providerInstance.PrepareForBootstrap(ctx, config)
    52  	c.Assert(err, jc.ErrorIsNil)
    53  
    54  	preparedConfig := environ.Config()
    55  	unknownAttrs := preparedConfig.UnknownAttrs()
    56  
    57  	uuid, ok := unknownAttrs["maas-agent-name"]
    58  
    59  	c.Assert(ok, jc.IsTrue)
    60  	c.Assert(uuid, jc.Satisfies, utils.IsValidUUIDString)
    61  }
    62  
    63  func (suite *EnvironProviderSuite) TestPrepareSetsAgentName(c *gc.C) {
    64  	attrs := testing.FakeConfig().Merge(testing.Attrs{
    65  		"type":        "maas",
    66  		"maas-oauth":  "aa:bb:cc",
    67  		"maas-server": "http://maas.testing.invalid/maas/",
    68  	})
    69  	config, err := config.New(config.NoDefaults, attrs)
    70  	c.Assert(err, jc.ErrorIsNil)
    71  
    72  	config, err = providerInstance.PrepareForCreateEnvironment(config)
    73  	c.Assert(err, jc.ErrorIsNil)
    74  
    75  	uuid, ok := config.UnknownAttrs()["maas-agent-name"]
    76  	c.Assert(ok, jc.IsTrue)
    77  	c.Assert(uuid, jc.Satisfies, utils.IsValidUUIDString)
    78  }
    79  
    80  func (suite *EnvironProviderSuite) TestPrepareExistingAgentName(c *gc.C) {
    81  	attrs := testing.FakeConfig().Merge(testing.Attrs{
    82  		"type":            "maas",
    83  		"maas-oauth":      "aa:bb:cc",
    84  		"maas-server":     "http://maas.testing.invalid/maas/",
    85  		"maas-agent-name": "foobar",
    86  	})
    87  	config, err := config.New(config.NoDefaults, attrs)
    88  	c.Assert(err, jc.ErrorIsNil)
    89  
    90  	_, err = providerInstance.PrepareForCreateEnvironment(config)
    91  	c.Assert(err, gc.Equals, errAgentNameAlreadySet)
    92  }
    93  
    94  func (suite *EnvironProviderSuite) TestAgentNameShouldNotBeSetByHand(c *gc.C) {
    95  	attrs := testing.FakeConfig().Merge(testing.Attrs{
    96  		"type":            "maas",
    97  		"maas-oauth":      "aa:bb:cc",
    98  		"maas-server":     "http://maas.testing.invalid/maas/",
    99  		"maas-agent-name": "foobar",
   100  	})
   101  	config, err := config.New(config.NoDefaults, attrs)
   102  	c.Assert(err, jc.ErrorIsNil)
   103  
   104  	ctx := envtesting.BootstrapContext(c)
   105  	_, err = providerInstance.PrepareForBootstrap(ctx, config)
   106  	c.Assert(err, gc.Equals, errAgentNameAlreadySet)
   107  }
   108  
   109  // create a temporary file with the given content.  The file will be cleaned
   110  // up at the end of the test calling this method.
   111  func createTempFile(c *gc.C, content []byte) string {
   112  	file, err := ioutil.TempFile(c.MkDir(), "")
   113  	c.Assert(err, jc.ErrorIsNil)
   114  	filename := file.Name()
   115  	err = ioutil.WriteFile(filename, content, 0644)
   116  	c.Assert(err, jc.ErrorIsNil)
   117  	return filename
   118  }
   119  
   120  func (suite *EnvironProviderSuite) TestOpenReturnsNilInterfaceUponFailure(c *gc.C) {
   121  	const oauth = "wrongly-formatted-oauth-string"
   122  	attrs := testing.FakeConfig().Merge(testing.Attrs{
   123  		"type":        "maas",
   124  		"maas-oauth":  oauth,
   125  		"maas-server": "http://maas.testing.invalid/maas/",
   126  	})
   127  	config, err := config.New(config.NoDefaults, attrs)
   128  	c.Assert(err, jc.ErrorIsNil)
   129  	env, err := providerInstance.Open(config)
   130  	// When Open() fails (i.e. returns a non-nil error), it returns an
   131  	// environs.Environ interface object with a nil value and a nil
   132  	// type.
   133  	c.Check(env, gc.Equals, nil)
   134  	c.Check(err, gc.ErrorMatches, ".*malformed maas-oauth.*")
   135  }