github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/maas/environ_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package maas_test
     5  
     6  import (
     7  	stdtesting "testing"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"launchpad.net/gomaasapi"
    12  
    13  	"github.com/juju/juju/environs/config"
    14  	envtesting "github.com/juju/juju/environs/testing"
    15  	"github.com/juju/juju/provider/maas"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  type environSuite struct {
    20  	coretesting.BaseSuite
    21  	envtesting.ToolsFixture
    22  	testMAASObject  *gomaasapi.TestMAASObject
    23  	restoreTimeouts func()
    24  }
    25  
    26  var _ = gc.Suite(&environSuite{})
    27  
    28  func TestMAAS(t *stdtesting.T) {
    29  	gc.TestingT(t)
    30  }
    31  
    32  // TDOO: jam 2013-12-06 This is copied from the providerSuite which is in a
    33  // whitebox package maas. Either move that into a whitebox test so it can be
    34  // shared, or into a 'testing' package so we can use it here.
    35  func (s *environSuite) SetUpSuite(c *gc.C) {
    36  	s.restoreTimeouts = envtesting.PatchAttemptStrategies(maas.ShortAttempt)
    37  	s.BaseSuite.SetUpSuite(c)
    38  	TestMAASObject := gomaasapi.NewTestMAAS("1.0")
    39  	s.testMAASObject = TestMAASObject
    40  }
    41  
    42  func (s *environSuite) SetUpTest(c *gc.C) {
    43  	s.BaseSuite.SetUpTest(c)
    44  	s.ToolsFixture.SetUpTest(c)
    45  }
    46  
    47  func (s *environSuite) TearDownTest(c *gc.C) {
    48  	s.testMAASObject.TestServer.Clear()
    49  	s.ToolsFixture.TearDownTest(c)
    50  	s.BaseSuite.TearDownTest(c)
    51  }
    52  
    53  func (s *environSuite) TearDownSuite(c *gc.C) {
    54  	s.testMAASObject.Close()
    55  	s.restoreTimeouts()
    56  	s.BaseSuite.TearDownSuite(c)
    57  }
    58  
    59  func getSimpleTestConfig(c *gc.C, extraAttrs coretesting.Attrs) *config.Config {
    60  	attrs := coretesting.FakeConfig()
    61  	attrs["type"] = "maas"
    62  	attrs["maas-server"] = "http://maas.testing.invalid"
    63  	attrs["maas-oauth"] = "a:b:c"
    64  	for k, v := range extraAttrs {
    65  		attrs[k] = v
    66  	}
    67  	cfg, err := config.New(config.NoDefaults, attrs)
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	return cfg
    70  }
    71  
    72  func (*environSuite) TestSetConfigValidatesFirst(c *gc.C) {
    73  	// SetConfig() validates the config change and disallows, for example,
    74  	// changes in the environment name.
    75  	oldCfg := getSimpleTestConfig(c, coretesting.Attrs{"name": "old-name"})
    76  	newCfg := getSimpleTestConfig(c, coretesting.Attrs{"name": "new-name"})
    77  	env, err := maas.NewEnviron(oldCfg)
    78  	c.Assert(err, jc.ErrorIsNil)
    79  
    80  	// SetConfig() fails, even though both the old and the new config are
    81  	// individually valid.
    82  	err = env.SetConfig(newCfg)
    83  	c.Assert(err, gc.NotNil)
    84  	c.Check(err, gc.ErrorMatches, ".*cannot change name.*")
    85  
    86  	// The old config is still in place.  The new config never took effect.
    87  	c.Check(env.Config().Name(), gc.Equals, "old-name")
    88  }
    89  
    90  func (*environSuite) TestSetConfigRefusesChangingAgentName(c *gc.C) {
    91  	oldCfg := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": "agent-one"})
    92  	newCfgTwo := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": "agent-two"})
    93  	env, err := maas.NewEnviron(oldCfg)
    94  	c.Assert(err, jc.ErrorIsNil)
    95  
    96  	// SetConfig() fails, even though both the old and the new config are
    97  	// individually valid.
    98  	err = env.SetConfig(newCfgTwo)
    99  	c.Assert(err, gc.NotNil)
   100  	c.Check(err, gc.ErrorMatches, ".*cannot change maas-agent-name.*")
   101  
   102  	// The old config is still in place.  The new config never took effect.
   103  	c.Check(maas.MAASAgentName(env), gc.Equals, "agent-one")
   104  
   105  	// It also refuses to set it to the empty string:
   106  	err = env.SetConfig(getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": ""}))
   107  	c.Check(err, gc.ErrorMatches, ".*cannot change maas-agent-name.*")
   108  
   109  	// And to nil
   110  	err = env.SetConfig(getSimpleTestConfig(c, nil))
   111  	c.Check(err, gc.ErrorMatches, ".*cannot change maas-agent-name.*")
   112  }
   113  
   114  func (*environSuite) TestSetConfigAllowsEmptyFromNilAgentName(c *gc.C) {
   115  	// bug #1256179 is that when using an older version of Juju (<1.16.2)
   116  	// we didn't include maas-agent-name in the database, so it was 'nil'
   117  	// in the OldConfig. However, when setting an environment, we would set
   118  	// it to "" (because maasEnvironConfig.Validate ensures it is a 'valid'
   119  	// string). We can't create that from NewEnviron or newConfig because
   120  	// both of them Validate the contents. 'cmd/juju/environment
   121  	// SetEnvironmentCommand' instead uses conn.State.EnvironConfig() which
   122  	// just reads the content of the database into a map, so we just create
   123  	// the map ourselves.
   124  
   125  	// Even though we use 'nil' here, it actually stores it as "" because
   126  	// 1.16.2 already validates the value
   127  	baseCfg := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": ""})
   128  	c.Check(baseCfg.UnknownAttrs()["maas-agent-name"], gc.Equals, "")
   129  	env, err := maas.NewEnviron(baseCfg)
   130  	c.Assert(err, jc.ErrorIsNil)
   131  	provider := env.Provider()
   132  
   133  	attrs := coretesting.FakeConfig()
   134  	// These are attrs we need to make it a valid Config, but would usually
   135  	// be set by other infrastructure
   136  	attrs["type"] = "maas"
   137  	nilCfg, err := config.New(config.NoDefaults, attrs)
   138  	c.Assert(err, jc.ErrorIsNil)
   139  	validatedConfig, err := provider.Validate(baseCfg, nilCfg)
   140  	c.Assert(err, jc.ErrorIsNil)
   141  	c.Check(validatedConfig.UnknownAttrs()["maas-agent-name"], gc.Equals, "")
   142  	// However, you can't set it to an actual value if you haven't been using a value
   143  	valueCfg := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": "agent-name"})
   144  	_, err = provider.Validate(valueCfg, nilCfg)
   145  	c.Check(err, gc.ErrorMatches, ".*cannot change maas-agent-name.*")
   146  }
   147  
   148  func (*environSuite) TestSetConfigAllowsChangingNilAgentNameToEmptyString(c *gc.C) {
   149  	oldCfg := getSimpleTestConfig(c, nil)
   150  	newCfgTwo := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": ""})
   151  	env, err := maas.NewEnviron(oldCfg)
   152  	c.Assert(err, jc.ErrorIsNil)
   153  
   154  	err = env.SetConfig(newCfgTwo)
   155  	c.Assert(err, jc.ErrorIsNil)
   156  	c.Check(maas.MAASAgentName(env), gc.Equals, "")
   157  }
   158  
   159  func (*environSuite) TestSetConfigUpdatesConfig(c *gc.C) {
   160  	origAttrs := coretesting.Attrs{
   161  		"server-name":  "http://maas2.testing.invalid",
   162  		"maas-oauth":   "a:b:c",
   163  		"admin-secret": "secret",
   164  	}
   165  	cfg := getSimpleTestConfig(c, origAttrs)
   166  	env, err := maas.NewEnviron(cfg)
   167  	c.Check(err, jc.ErrorIsNil)
   168  	c.Check(env.Config().Name(), gc.Equals, "testenv")
   169  
   170  	anotherServer := "http://maas.testing.invalid"
   171  	anotherOauth := "c:d:e"
   172  	anotherSecret := "secret2"
   173  	newAttrs := coretesting.Attrs{
   174  		"server-name":  anotherServer,
   175  		"maas-oauth":   anotherOauth,
   176  		"admin-secret": anotherSecret,
   177  	}
   178  	cfg2 := getSimpleTestConfig(c, newAttrs)
   179  	errSetConfig := env.SetConfig(cfg2)
   180  	c.Check(errSetConfig, gc.IsNil)
   181  	c.Check(env.Config().Name(), gc.Equals, "testenv")
   182  	authClient, _ := gomaasapi.NewAuthenticatedClient(anotherServer, anotherOauth, maas.APIVersion)
   183  	maasClient := gomaasapi.NewMAAS(*authClient)
   184  	MAASServer := maas.GetMAASClient(env)
   185  	c.Check(MAASServer, gc.DeepEquals, maasClient)
   186  }
   187  
   188  func (*environSuite) TestNewEnvironSetsConfig(c *gc.C) {
   189  	cfg := getSimpleTestConfig(c, nil)
   190  
   191  	env, err := maas.NewEnviron(cfg)
   192  
   193  	c.Check(err, jc.ErrorIsNil)
   194  	c.Check(env.Config().Name(), gc.Equals, "testenv")
   195  }
   196  
   197  var expectedCloudinitConfig = []interface{}{
   198  	"set -xe",
   199  	"mkdir -p '/var/lib/juju'\ninstall -m 755 /dev/null '/var/lib/juju/MAASmachine.txt'\nprintf '%s\\n' ''\"'\"'hostname: testing.invalid\n'\"'\"'' > '/var/lib/juju/MAASmachine.txt'",
   200  	"ifdown eth0",
   201  	`cat >> /etc/network/interfaces << EOF
   202  
   203  iface eth0 inet manual
   204  
   205  auto juju-br0
   206  iface juju-br0 inet dhcp
   207      bridge_ports eth0
   208  EOF
   209  grep -q 'iface eth0 inet dhcp' /etc/network/interfaces && \
   210  sed -i 's/iface eth0 inet dhcp//' /etc/network/interfaces`,
   211  	"ifup juju-br0",
   212  }
   213  
   214  var expectedCloudinitConfigWithoutNetworking = []interface{}{
   215  	"set -xe",
   216  	"mkdir -p '/var/lib/juju'\ninstall -m 755 /dev/null '/var/lib/juju/MAASmachine.txt'\nprintf '%s\\n' ''\"'\"'hostname: testing.invalid\n'\"'\"'' > '/var/lib/juju/MAASmachine.txt'",
   217  }
   218  
   219  func (*environSuite) TestNewCloudinitConfig(c *gc.C) {
   220  	cfg := getSimpleTestConfig(c, nil)
   221  	env, err := maas.NewEnviron(cfg)
   222  	c.Assert(err, jc.ErrorIsNil)
   223  	cloudcfg, err := maas.NewCloudinitConfig(env, "testing.invalid", "eth0", "quantal")
   224  	c.Assert(err, jc.ErrorIsNil)
   225  	c.Assert(cloudcfg.AptUpdate(), jc.IsTrue)
   226  	c.Assert(cloudcfg.RunCmds(), jc.DeepEquals, expectedCloudinitConfig)
   227  }
   228  
   229  func (*environSuite) TestNewCloudinitConfigWithDisabledNetworkManagement(c *gc.C) {
   230  	attrs := coretesting.Attrs{
   231  		"disable-network-management": true,
   232  	}
   233  	cfg := getSimpleTestConfig(c, attrs)
   234  	env, err := maas.NewEnviron(cfg)
   235  	c.Assert(err, jc.ErrorIsNil)
   236  	cloudcfg, err := maas.NewCloudinitConfig(env, "testing.invalid", "eth0", "quantal")
   237  	c.Assert(err, jc.ErrorIsNil)
   238  	c.Assert(cloudcfg.AptUpdate(), jc.IsTrue)
   239  	c.Assert(cloudcfg.RunCmds(), jc.DeepEquals, expectedCloudinitConfigWithoutNetworking)
   240  }