1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
"github.com/juju/gomaasapi"
10
jc "github.com/juju/testing/checkers"
11
"github.com/juju/utils/set"
12
gc "gopkg.in/check.v1"
14
"github.com/juju/juju/cloud"
15
"github.com/juju/juju/environs"
16
"github.com/juju/juju/environs/config"
17
envtesting "github.com/juju/juju/environs/testing"
18
"github.com/juju/juju/provider/maas"
19
coretesting "github.com/juju/juju/testing"
22
type environSuite struct {
24
envtesting.ToolsFixture
25
testMAASObject *gomaasapi.TestMAASObject
26
restoreTimeouts func()
29
var _ = gc.Suite(&environSuite{})
31
func TestMAAS(t *stdtesting.T) {
35
// TDOO: jam 2013-12-06 This is copied from the providerSuite which is in a
36
// whitebox package maas. Either move that into a whitebox test so it can be
37
// shared, or into a 'testing' package so we can use it here.
38
func (s *environSuite) SetUpSuite(c *gc.C) {
39
s.restoreTimeouts = envtesting.PatchAttemptStrategies(maas.ShortAttempt)
40
s.BaseSuite.SetUpSuite(c)
41
TestMAASObject := gomaasapi.NewTestMAAS("1.0")
42
s.testMAASObject = TestMAASObject
45
func (s *environSuite) SetUpTest(c *gc.C) {
46
s.BaseSuite.SetUpTest(c)
47
s.ToolsFixture.SetUpTest(c)
49
mockCapabilities := func(client *gomaasapi.MAASObject) (set.Strings, error) {
50
return set.NewStrings("network-deployment-ubuntu"), nil
52
mockGetController := func(maasServer, apiKey string) (gomaasapi.Controller, error) {
53
return nil, gomaasapi.NewUnsupportedVersionError("oops")
55
s.PatchValue(&maas.GetCapabilities, mockCapabilities)
56
s.PatchValue(&maas.GetMAAS2Controller, mockGetController)
59
func (s *environSuite) TearDownTest(c *gc.C) {
60
s.testMAASObject.TestServer.Clear()
61
s.ToolsFixture.TearDownTest(c)
62
s.BaseSuite.TearDownTest(c)
65
func (s *environSuite) TearDownSuite(c *gc.C) {
66
s.testMAASObject.Close()
68
s.BaseSuite.TearDownSuite(c)
71
func getSimpleTestConfig(c *gc.C, extraAttrs coretesting.Attrs) *config.Config {
72
attrs := coretesting.FakeConfig()
73
attrs["type"] = "maas"
74
attrs["bootstrap-timeout"] = "1200"
75
for k, v := range extraAttrs {
78
cfg, err := config.New(config.NoDefaults, attrs)
79
c.Assert(err, jc.ErrorIsNil)
83
func getSimpleCloudSpec() environs.CloudSpec {
84
cred := cloud.NewCredential(cloud.OAuth1AuthType, map[string]string{
85
"maas-oauth": "a:b:c",
87
return environs.CloudSpec{
90
Endpoint: "http://maas.testing.invalid",
95
func (*environSuite) TestSetConfigValidatesFirst(c *gc.C) {
96
// SetConfig() validates the config change and disallows, for example,
97
// changes in the environment name.
98
oldCfg := getSimpleTestConfig(c, coretesting.Attrs{"name": "old-name"})
99
newCfg := getSimpleTestConfig(c, coretesting.Attrs{"name": "new-name"})
100
env, err := maas.NewEnviron(getSimpleCloudSpec(), oldCfg)
101
c.Assert(err, jc.ErrorIsNil)
103
// SetConfig() fails, even though both the old and the new config are
104
// individually valid.
105
err = env.SetConfig(newCfg)
106
c.Assert(err, gc.NotNil)
107
c.Check(err, gc.ErrorMatches, ".*cannot change name.*")
109
// The old config is still in place. The new config never took effect.
110
c.Check(env.Config().Name(), gc.Equals, "old-name")
113
func (*environSuite) TestSetConfigUpdatesConfig(c *gc.C) {
114
origAttrs := coretesting.Attrs{
115
"apt-mirror": "http://testing1.invalid",
117
cfg := getSimpleTestConfig(c, origAttrs)
118
env, err := maas.NewEnviron(getSimpleCloudSpec(), cfg)
119
c.Check(err, jc.ErrorIsNil)
120
c.Check(env.Config().Name(), gc.Equals, "testenv")
122
newAttrs := coretesting.Attrs{
123
"apt-mirror": "http://testing2.invalid",
125
cfg2 := getSimpleTestConfig(c, newAttrs)
126
errSetConfig := env.SetConfig(cfg2)
127
c.Check(errSetConfig, gc.IsNil)
128
c.Check(env.Config().Name(), gc.Equals, "testenv")
129
c.Check(env.Config().AptMirror(), gc.Equals, "http://testing2.invalid")
132
func (*environSuite) TestNewEnvironSetsConfig(c *gc.C) {
133
cfg := getSimpleTestConfig(c, nil)
135
env, err := maas.NewEnviron(getSimpleCloudSpec(), cfg)
137
c.Check(err, jc.ErrorIsNil)
138
c.Check(env.Config().Name(), gc.Equals, "testenv")
141
var expectedCloudinitConfig = []string{
143
"mkdir -p '/var/lib/juju'\ncat > '/var/lib/juju/MAASmachine.txt' << 'EOF'\n'hostname: testing.invalid\n'\nEOF\nchmod 0755 '/var/lib/juju/MAASmachine.txt'",
146
func (*environSuite) TestNewCloudinitConfig(c *gc.C) {
147
cfg := getSimpleTestConfig(c, nil)
148
env, err := maas.NewEnviron(getSimpleCloudSpec(), cfg)
149
c.Assert(err, jc.ErrorIsNil)
150
modifyNetworkScript := maas.RenderEtcNetworkInterfacesScript()
151
script := expectedCloudinitConfig
152
script = append(script, modifyNetworkScript)
153
cloudcfg, err := maas.NewCloudinitConfig(env, "testing.invalid", "quantal")
154
c.Assert(err, jc.ErrorIsNil)
155
c.Assert(cloudcfg.SystemUpdate(), jc.IsTrue)
156
c.Assert(cloudcfg.RunCmds(), jc.DeepEquals, script)
159
func (*environSuite) TestNewCloudinitConfigWithDisabledNetworkManagement(c *gc.C) {
160
attrs := coretesting.Attrs{
161
"disable-network-management": true,
163
cfg := getSimpleTestConfig(c, attrs)
164
env, err := maas.NewEnviron(getSimpleCloudSpec(), cfg)
165
c.Assert(err, jc.ErrorIsNil)
166
cloudcfg, err := maas.NewCloudinitConfig(env, "testing.invalid", "quantal")
167
c.Assert(err, jc.ErrorIsNil)
168
c.Assert(cloudcfg.SystemUpdate(), jc.IsTrue)
169
c.Assert(cloudcfg.RunCmds(), jc.DeepEquals, expectedCloudinitConfig)