github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 "net/http" 9 "net/http/httptest" 10 "net/http/httputil" 11 "net/url" 12 "strings" 13 14 jc "github.com/juju/testing/checkers" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/cloud" 18 "github.com/juju/juju/environs" 19 "github.com/juju/juju/environs/config" 20 "github.com/juju/juju/testing" 21 ) 22 23 type EnvironProviderSuite struct { 24 providerSuite 25 } 26 27 var _ = gc.Suite(&EnvironProviderSuite{}) 28 29 func (s *EnvironProviderSuite) cloudSpec() environs.CloudSpec { 30 credential := oauthCredential("aa:bb:cc") 31 return environs.CloudSpec{ 32 Type: "maas", 33 Name: "maas", 34 Endpoint: "http://maas.testing.invalid/maas/", 35 Credential: &credential, 36 } 37 } 38 39 func oauthCredential(token string) cloud.Credential { 40 return cloud.NewCredential( 41 cloud.OAuth1AuthType, 42 map[string]string{ 43 "maas-oauth": token, 44 }, 45 ) 46 } 47 48 func (suite *EnvironProviderSuite) TestPrepareConfig(c *gc.C) { 49 attrs := testing.FakeConfig().Merge(testing.Attrs{"type": "maas"}) 50 config, err := config.New(config.NoDefaults, attrs) 51 c.Assert(err, jc.ErrorIsNil) 52 _, err = providerInstance.PrepareConfig(environs.PrepareConfigParams{ 53 Config: config, 54 Cloud: suite.cloudSpec(), 55 }) 56 c.Assert(err, jc.ErrorIsNil) 57 } 58 59 func (suite *EnvironProviderSuite) TestPrepareConfigInvalidOAuth(c *gc.C) { 60 attrs := testing.FakeConfig().Merge(testing.Attrs{"type": "maas"}) 61 config, err := config.New(config.NoDefaults, attrs) 62 c.Assert(err, jc.ErrorIsNil) 63 spec := suite.cloudSpec() 64 cred := oauthCredential("wrongly-formatted-oauth-string") 65 spec.Credential = &cred 66 _, err = providerInstance.PrepareConfig(environs.PrepareConfigParams{ 67 Config: config, 68 Cloud: spec, 69 }) 70 c.Assert(err, gc.ErrorMatches, ".*malformed maas-oauth.*") 71 } 72 73 func (suite *EnvironProviderSuite) TestPrepareConfigInvalidEndpoint(c *gc.C) { 74 attrs := testing.FakeConfig().Merge(testing.Attrs{"type": "maas"}) 75 config, err := config.New(config.NoDefaults, attrs) 76 c.Assert(err, jc.ErrorIsNil) 77 spec := suite.cloudSpec() 78 spec.Endpoint = "This should have been a URL or host." 79 _, err = providerInstance.PrepareConfig(environs.PrepareConfigParams{ 80 Config: config, 81 Cloud: spec, 82 }) 83 c.Assert(err, gc.ErrorMatches, 84 `validating cloud spec: validating endpoint: endpoint "This should have been a URL or host." not valid`, 85 ) 86 } 87 88 func (suite *EnvironProviderSuite) TestPrepareConfigSetsDefaults(c *gc.C) { 89 attrs := testing.FakeConfig().Merge(testing.Attrs{"type": "maas"}) 90 config, err := config.New(config.NoDefaults, attrs) 91 c.Assert(err, jc.ErrorIsNil) 92 cfg, err := providerInstance.PrepareConfig(environs.PrepareConfigParams{ 93 Config: config, 94 Cloud: suite.cloudSpec(), 95 }) 96 c.Assert(err, jc.ErrorIsNil) 97 src, _ := cfg.StorageDefaultBlockSource() 98 c.Assert(src, gc.Equals, "maas") 99 } 100 101 func (suite *EnvironProviderSuite) TestMAASServerFromEndpointURL(c *gc.C) { 102 suite.testMAASServerFromEndpoint(c, suite.testMAASObject.TestServer.URL) 103 } 104 105 func (suite *EnvironProviderSuite) TestMAASServerFromEndpointHost(c *gc.C) { 106 targetURL, err := url.Parse(suite.testMAASObject.TestServer.URL) 107 c.Assert(err, jc.ErrorIsNil) 108 109 rp := httputil.NewSingleHostReverseProxy(targetURL) 110 rp.Director = func(req *http.Request) { 111 req.URL.Path = strings.TrimPrefix(req.URL.Path, "/MAAS") 112 req.URL.Scheme = targetURL.Scheme 113 req.URL.Host = targetURL.Host 114 } 115 proxy := httptest.NewServer(rp) 116 defer proxy.Close() 117 118 // The proxy's host:port will be formatted into a URL, with a 119 // fixed root path of "/MAAS". 120 proxyURL, err := url.Parse(proxy.URL) 121 c.Assert(err, jc.ErrorIsNil) 122 suite.testMAASServerFromEndpoint(c, proxyURL.Host) 123 } 124 125 func (suite *EnvironProviderSuite) testMAASServerFromEndpoint(c *gc.C, endpoint string) { 126 attrs := testing.FakeConfig().Merge(testing.Attrs{"type": "maas"}) 127 config, err := config.New(config.NoDefaults, attrs) 128 c.Assert(err, jc.ErrorIsNil) 129 130 cloudSpec := suite.cloudSpec() 131 cloudSpec.Endpoint = endpoint 132 env, err := providerInstance.Open(environs.OpenParams{ 133 Config: config, 134 Cloud: cloudSpec, 135 }) 136 c.Assert(err, jc.ErrorIsNil) 137 138 suite.addNode(`{"system_id":"test-allocated"}`) 139 _, err = env.AllInstances() 140 c.Assert(err, jc.ErrorIsNil) 141 } 142 143 // create a temporary file with the given content. The file will be cleaned 144 // up at the end of the test calling this method. 145 func createTempFile(c *gc.C, content []byte) string { 146 file, err := ioutil.TempFile(c.MkDir(), "") 147 c.Assert(err, jc.ErrorIsNil) 148 filename := file.Name() 149 err = ioutil.WriteFile(filename, content, 0644) 150 c.Assert(err, jc.ErrorIsNil) 151 return filename 152 } 153 154 func (suite *EnvironProviderSuite) TestOpenReturnsNilInterfaceUponFailure(c *gc.C) { 155 attrs := testing.FakeConfig().Merge(testing.Attrs{"type": "maas"}) 156 config, err := config.New(config.NoDefaults, attrs) 157 c.Assert(err, jc.ErrorIsNil) 158 spec := suite.cloudSpec() 159 cred := oauthCredential("wrongly-formatted-oauth-string") 160 spec.Credential = &cred 161 env, err := providerInstance.Open(environs.OpenParams{ 162 Cloud: spec, 163 Config: config, 164 }) 165 // When Open() fails (i.e. returns a non-nil error), it returns an 166 // environs.Environ interface object with a nil value and a nil 167 // type. 168 c.Check(env, gc.Equals, nil) 169 c.Check(err, gc.ErrorMatches, ".*malformed maas-oauth.*") 170 }