~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/maas/environ_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "github.com/juju/gomaasapi"
 
10
        jc "github.com/juju/testing/checkers"
 
11
        "github.com/juju/utils/set"
 
12
        gc "gopkg.in/check.v1"
 
13
 
 
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"
 
20
)
 
21
 
 
22
type environSuite struct {
 
23
        coretesting.BaseSuite
 
24
        envtesting.ToolsFixture
 
25
        testMAASObject  *gomaasapi.TestMAASObject
 
26
        restoreTimeouts func()
 
27
}
 
28
 
 
29
var _ = gc.Suite(&environSuite{})
 
30
 
 
31
func TestMAAS(t *stdtesting.T) {
 
32
        gc.TestingT(t)
 
33
}
 
34
 
 
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
 
43
}
 
44
 
 
45
func (s *environSuite) SetUpTest(c *gc.C) {
 
46
        s.BaseSuite.SetUpTest(c)
 
47
        s.ToolsFixture.SetUpTest(c)
 
48
 
 
49
        mockCapabilities := func(client *gomaasapi.MAASObject) (set.Strings, error) {
 
50
                return set.NewStrings("network-deployment-ubuntu"), nil
 
51
        }
 
52
        mockGetController := func(maasServer, apiKey string) (gomaasapi.Controller, error) {
 
53
                return nil, gomaasapi.NewUnsupportedVersionError("oops")
 
54
        }
 
55
        s.PatchValue(&maas.GetCapabilities, mockCapabilities)
 
56
        s.PatchValue(&maas.GetMAAS2Controller, mockGetController)
 
57
}
 
58
 
 
59
func (s *environSuite) TearDownTest(c *gc.C) {
 
60
        s.testMAASObject.TestServer.Clear()
 
61
        s.ToolsFixture.TearDownTest(c)
 
62
        s.BaseSuite.TearDownTest(c)
 
63
}
 
64
 
 
65
func (s *environSuite) TearDownSuite(c *gc.C) {
 
66
        s.testMAASObject.Close()
 
67
        s.restoreTimeouts()
 
68
        s.BaseSuite.TearDownSuite(c)
 
69
}
 
70
 
 
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 {
 
76
                attrs[k] = v
 
77
        }
 
78
        cfg, err := config.New(config.NoDefaults, attrs)
 
79
        c.Assert(err, jc.ErrorIsNil)
 
80
        return cfg
 
81
}
 
82
 
 
83
func getSimpleCloudSpec() environs.CloudSpec {
 
84
        cred := cloud.NewCredential(cloud.OAuth1AuthType, map[string]string{
 
85
                "maas-oauth": "a:b:c",
 
86
        })
 
87
        return environs.CloudSpec{
 
88
                Type:       "maas",
 
89
                Name:       "maas",
 
90
                Endpoint:   "http://maas.testing.invalid",
 
91
                Credential: &cred,
 
92
        }
 
93
}
 
94
 
 
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)
 
102
 
 
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.*")
 
108
 
 
109
        // The old config is still in place.  The new config never took effect.
 
110
        c.Check(env.Config().Name(), gc.Equals, "old-name")
 
111
}
 
112
 
 
113
func (*environSuite) TestSetConfigUpdatesConfig(c *gc.C) {
 
114
        origAttrs := coretesting.Attrs{
 
115
                "apt-mirror": "http://testing1.invalid",
 
116
        }
 
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")
 
121
 
 
122
        newAttrs := coretesting.Attrs{
 
123
                "apt-mirror": "http://testing2.invalid",
 
124
        }
 
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")
 
130
}
 
131
 
 
132
func (*environSuite) TestNewEnvironSetsConfig(c *gc.C) {
 
133
        cfg := getSimpleTestConfig(c, nil)
 
134
 
 
135
        env, err := maas.NewEnviron(getSimpleCloudSpec(), cfg)
 
136
 
 
137
        c.Check(err, jc.ErrorIsNil)
 
138
        c.Check(env.Config().Name(), gc.Equals, "testenv")
 
139
}
 
140
 
 
141
var expectedCloudinitConfig = []string{
 
142
        "set -xe",
 
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'",
 
144
}
 
145
 
 
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)
 
157
}
 
158
 
 
159
func (*environSuite) TestNewCloudinitConfigWithDisabledNetworkManagement(c *gc.C) {
 
160
        attrs := coretesting.Attrs{
 
161
                "disable-network-management": true,
 
162
        }
 
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)
 
170
}