github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 "io" 8 "net/http" 9 "net/http/httptest" 10 stdtesting "testing" 11 12 "github.com/juju/collections/set" 13 "github.com/juju/errors" 14 "github.com/juju/gomaasapi" 15 jc "github.com/juju/testing/checkers" 16 gc "gopkg.in/check.v1" 17 18 "github.com/juju/juju/cloud" 19 "github.com/juju/juju/environs" 20 "github.com/juju/juju/environs/config" 21 envtesting "github.com/juju/juju/environs/testing" 22 "github.com/juju/juju/provider/maas" 23 coretesting "github.com/juju/juju/testing" 24 ) 25 26 type environSuite struct { 27 coretesting.BaseSuite 28 envtesting.ToolsFixture 29 restoreTimeouts func() 30 } 31 32 var _ = gc.Suite(&environSuite{}) 33 34 func TestMAAS(t *stdtesting.T) { 35 gc.TestingT(t) 36 } 37 38 // TDOO: jam 2013-12-06 This is copied from the providerSuite which is in a 39 // whitebox package maas. Either move that into a whitebox test so it can be 40 // shared, or into a 'testing' package so we can use it here. 41 func (s *environSuite) SetUpSuite(c *gc.C) { 42 s.restoreTimeouts = envtesting.PatchAttemptStrategies(maas.ShortAttempt) 43 s.BaseSuite.SetUpSuite(c) 44 } 45 46 func (s *environSuite) SetUpTest(c *gc.C) { 47 s.BaseSuite.SetUpTest(c) 48 s.ToolsFixture.SetUpTest(c) 49 50 mockGetController := func(string, string) (gomaasapi.Controller, error) { 51 return nil, gomaasapi.NewUnsupportedVersionError("oops") 52 } 53 s.PatchValue(&maas.GetMAAS2Controller, mockGetController) 54 } 55 56 func (s *environSuite) TearDownTest(c *gc.C) { 57 s.ToolsFixture.TearDownTest(c) 58 s.BaseSuite.TearDownTest(c) 59 } 60 61 func (s *environSuite) TearDownSuite(c *gc.C) { 62 s.restoreTimeouts() 63 s.BaseSuite.TearDownSuite(c) 64 } 65 66 func getSimpleTestConfig(c *gc.C, extraAttrs coretesting.Attrs) *config.Config { 67 attrs := coretesting.FakeConfig() 68 attrs["type"] = "maas" 69 attrs["bootstrap-timeout"] = "1200" 70 for k, v := range extraAttrs { 71 attrs[k] = v 72 } 73 cfg, err := config.New(config.NoDefaults, attrs) 74 c.Assert(err, jc.ErrorIsNil) 75 return cfg 76 } 77 78 func getSimpleCloudSpec() environs.CloudSpec { 79 cred := cloud.NewCredential(cloud.OAuth1AuthType, map[string]string{ 80 "maas-oauth": "a:b:c", 81 }) 82 return environs.CloudSpec{ 83 Type: "maas", 84 Name: "maas", 85 Endpoint: "http://maas.testing.invalid", 86 Credential: &cred, 87 } 88 } 89 90 func (*environSuite) TestSetConfigValidatesFirst(c *gc.C) { 91 // SetConfig() validates the config change and disallows, for example, 92 // changes in the environment name. 93 oldCfg := getSimpleTestConfig(c, coretesting.Attrs{"name": "old-name"}) 94 newCfg := getSimpleTestConfig(c, coretesting.Attrs{"name": "new-name"}) 95 env, err := maas.NewEnviron(getSimpleCloudSpec(), oldCfg, fakeGetCapabilities) 96 c.Assert(err, jc.ErrorIsNil) 97 98 // SetConfig() fails, even though both the old and the new config are 99 // individually valid. 100 err = env.SetConfig(newCfg) 101 c.Assert(err, gc.NotNil) 102 c.Check(err, gc.ErrorMatches, ".*cannot change name.*") 103 104 // The old config is still in place. The new config never took effect. 105 c.Check(env.Config().Name(), gc.Equals, "old-name") 106 } 107 108 func fakeGetCapabilities(client *gomaasapi.MAASObject, serverURL string) (set.Strings, error) { 109 return set.NewStrings("network-deployment-ubuntu"), nil 110 } 111 112 func (*environSuite) TestSetConfigUpdatesConfig(c *gc.C) { 113 origAttrs := coretesting.Attrs{ 114 "apt-mirror": "http://testing1.invalid", 115 } 116 cfg := getSimpleTestConfig(c, origAttrs) 117 env, err := maas.NewEnviron(getSimpleCloudSpec(), cfg, fakeGetCapabilities) 118 c.Check(err, jc.ErrorIsNil) 119 c.Check(env.Config().Name(), gc.Equals, "testmodel") 120 121 newAttrs := coretesting.Attrs{ 122 "apt-mirror": "http://testing2.invalid", 123 } 124 cfg2 := getSimpleTestConfig(c, newAttrs) 125 errSetConfig := env.SetConfig(cfg2) 126 c.Check(errSetConfig, gc.IsNil) 127 c.Check(env.Config().Name(), gc.Equals, "testmodel") 128 c.Check(env.Config().AptMirror(), gc.Equals, "http://testing2.invalid") 129 } 130 131 func (*environSuite) TestNewEnvironSetsConfig(c *gc.C) { 132 cfg := getSimpleTestConfig(c, nil) 133 134 env, err := maas.NewEnviron(getSimpleCloudSpec(), cfg, fakeGetCapabilities) 135 136 c.Check(err, jc.ErrorIsNil) 137 c.Check(env.Config().Name(), gc.Equals, "testmodel") 138 } 139 140 var expectedCloudinitConfig = []string{ 141 "set -xe", 142 "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'", 143 } 144 145 func (*environSuite) TestNewCloudinitConfig(c *gc.C) { 146 cfg := getSimpleTestConfig(c, nil) 147 env, err := maas.NewEnviron(getSimpleCloudSpec(), cfg, fakeGetCapabilities) 148 c.Assert(err, jc.ErrorIsNil) 149 script := expectedCloudinitConfig 150 cloudcfg, err := maas.NewCloudinitConfig(env, "testing.invalid", "quantal") 151 c.Assert(err, jc.ErrorIsNil) 152 c.Assert(cloudcfg.SystemUpdate(), jc.IsTrue) 153 c.Assert(cloudcfg.RunCmds(), jc.DeepEquals, script) 154 } 155 156 type badEndpointSuite struct { 157 coretesting.BaseSuite 158 159 fakeServer *httptest.Server 160 cloudSpec environs.CloudSpec 161 } 162 163 var _ = gc.Suite(&badEndpointSuite{}) 164 165 func (s *badEndpointSuite) SetUpSuite(c *gc.C) { 166 s.BaseSuite.SetUpSuite(c) 167 always404 := func(w http.ResponseWriter, req *http.Request) { 168 w.WriteHeader(http.StatusNotFound) 169 io.WriteString(w, "uh-oh") 170 } 171 s.fakeServer = httptest.NewServer(http.HandlerFunc(always404)) 172 } 173 174 func (s *badEndpointSuite) SetUpTest(c *gc.C) { 175 s.BaseSuite.SetUpTest(c) 176 cred := cloud.NewCredential(cloud.OAuth1AuthType, map[string]string{ 177 "maas-oauth": "a:b:c", 178 }) 179 s.cloudSpec = environs.CloudSpec{ 180 Type: "maas", 181 Name: "maas", 182 Endpoint: s.fakeServer.URL, 183 Credential: &cred, 184 } 185 } 186 187 func (s *badEndpointSuite) TestBadEndpointMessageNoMAAS(c *gc.C) { 188 cfg := getSimpleTestConfig(c, coretesting.Attrs{}) 189 env, err := maas.NewEnviron(s.cloudSpec, cfg, nil) 190 c.Assert(env, gc.IsNil) 191 c.Assert(err, gc.ErrorMatches, `could not connect to MAAS controller - check the endpoint is correct \(it normally ends with /MAAS\)`) 192 c.Assert(err, jc.Satisfies, errors.IsNotSupported) 193 } 194 195 func (s *badEndpointSuite) TestBadEndpointMessageWithMAAS(c *gc.C) { 196 cfg := getSimpleTestConfig(c, coretesting.Attrs{}) 197 s.cloudSpec.Endpoint += "/MAAS" 198 env, err := maas.NewEnviron(s.cloudSpec, cfg, nil) 199 c.Assert(env, gc.IsNil) 200 c.Assert(err, gc.ErrorMatches, `could not connect to MAAS controller - check the endpoint is correct`) 201 c.Assert(err, jc.Satisfies, errors.IsNotSupported) 202 } 203 204 func (s *badEndpointSuite) TestBadEndpointMessageWithMAASAndSlash(c *gc.C) { 205 cfg := getSimpleTestConfig(c, coretesting.Attrs{}) 206 s.cloudSpec.Endpoint += "/MAAS/" 207 env, err := maas.NewEnviron(s.cloudSpec, cfg, nil) 208 c.Assert(env, gc.IsNil) 209 c.Assert(err, gc.ErrorMatches, `could not connect to MAAS controller - check the endpoint is correct`) 210 c.Assert(err, jc.Satisfies, errors.IsNotSupported) 211 }