1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
package client_test
import (
"fmt"
"net/http"
"strconv"
gc "gopkg.in/check.v1"
)
type versionHandler struct {
authBody string
port string
}
type makeServiceURLTest struct {
serviceType string
version string
parts []string
success bool
URL string
err string
}
func (s *localLiveSuite) makeServiceURLTests() []makeServiceURLTest {
return []makeServiceURLTest{
{
// As a special case, if no version is specified
// then we use whatever URL is recoded in the
// service catalogue verbatim.
serviceType: "compute",
version: "",
parts: []string{},
success: true,
URL: "http://127.0.0.1:%s",
},
{
serviceType: "compute",
version: "v2.1",
parts: []string{"foo", "bar/"},
success: true,
URL: "http://127.0.0.1:%s/v2.1/foo/bar/",
},
{
serviceType: "compute",
version: "v2.0",
parts: []string{},
success: true,
URL: "http://127.0.0.1:%s/v2.0",
},
{
serviceType: "compute",
version: "v2.0",
parts: []string{"foo", "bar/"},
success: true,
URL: "http://127.0.0.1:%s/v2.0/foo/bar/",
},
{
serviceType: "compute",
version: "v2",
parts: []string{"foo", "bar/"},
success: true,
URL: "http://127.0.0.1:%s/v2.1/foo/bar/",
},
{
serviceType: "object-store",
version: "",
parts: []string{"foo", "bar"},
success: true,
URL: "http://127.0.0.1:%s/swift/v1/foo/bar",
},
{
serviceType: "object-store",
version: "q2.0",
parts: []string{"foo", "bar/"},
success: false,
err: "strconv.Atoi: parsing \"q2\": invalid syntax",
},
{
serviceType: "object-store",
version: "v1.7",
parts: []string{"foo", "bar/"},
success: false,
err: "could not find matching URL",
},
{
serviceType: "juju-container-test",
version: "v1",
parts: []string{"foo", "bar/"},
success: true,
URL: "http://127.0.0.1:%s/swift/v1/foo/bar/",
},
{
serviceType: "juju-container-test",
version: "v0",
parts: []string{"foo", "bar/"},
success: false,
err: "could not find matching URL",
},
{
serviceType: "juju-container-test",
version: "",
parts: []string{"foo", "bar/"},
success: true,
URL: "http://127.0.0.1:%s/swift/v1/foo/bar/",
},
{
serviceType: "compute",
version: "v25.4",
parts: []string{},
success: false,
err: "could not find matching URL",
},
{
serviceType: "no-such-service",
version: "",
parts: []string{},
success: false,
err: "no endpoints known for service type: no-such-service",
},
}
}
func (s *localLiveSuite) TestMakeServiceURL(c *gc.C) {
port := "3000"
cl := s.assertAuthenticationSuccess(c, port)
tests := s.makeServiceURLTests()
testCount := len(tests)
for i, t := range tests {
c.Logf("#%d of %d. %s %s %v", i+1, testCount, t.serviceType, t.version, t.parts)
URL, err := cl.MakeServiceURL(t.serviceType, t.version, t.parts)
if t.success {
c.Assert(err, gc.IsNil)
c.Assert(URL, gc.Equals, fmt.Sprintf(t.URL, port))
// Run twice to ensure the version caching is working
URL, err = cl.MakeServiceURL(t.serviceType, t.version, t.parts)
c.Assert(err, gc.IsNil)
c.Assert(URL, gc.Equals, fmt.Sprintf(t.URL, port))
} else {
c.Assert(err, gc.ErrorMatches, t.err)
}
}
}
func (s *localLiveSuite) TestMakeServiceURLAPIVersionDiscoveryDisabled(c *gc.C) {
port := "3000"
cl := s.assertAuthenticationSuccess(c, port)
wasEnabled := cl.SetVersionDiscoveryEnabled(false)
c.Assert(wasEnabled, gc.Equals, true)
url, err := cl.MakeServiceURL("compute", "v2.1", []string{"foo", "bar/"})
c.Assert(err, gc.IsNil)
c.Assert(url, gc.Equals, fmt.Sprintf("http://127.0.0.1:%s/foo/bar/", port))
}
func (s *localLiveSuite) TestMakeServiceURLNoAPIVersionEndpoint(c *gc.C) {
// See https://bugs.launchpad.net/juju/+bug/1638304
// Some OpenStacks don't support version discovery.
port := "3005"
cl := s.assertAuthenticationSuccess(c, port)
url, err := cl.MakeServiceURL("compute", "v2.1", []string{"foo", "bar/"})
c.Assert(err, gc.IsNil)
c.Assert(url, gc.Equals, fmt.Sprintf("http://127.0.0.1:%s/foo/bar/", port))
}
func (s *localLiveSuite) TestMakeServiceURLValues(c *gc.C) {
port := "3003"
cl := s.assertAuthenticationSuccess(c, port)
tests := s.makeServiceURLTests()
testCount := len(tests)
for i, t := range tests {
c.Logf("#%d of %d. %s %s %v", i+1, testCount, t.serviceType, t.version, t.parts)
URL, err := cl.MakeServiceURL(t.serviceType, t.version, t.parts)
if t.success {
c.Assert(err, gc.IsNil)
c.Assert(URL, gc.Equals, fmt.Sprintf(t.URL, port))
// Run twice to ensure the version caching is working
URL, err = cl.MakeServiceURL(t.serviceType, t.version, t.parts)
c.Assert(err, gc.IsNil)
c.Assert(URL, gc.Equals, fmt.Sprintf(t.URL, port))
} else {
c.Assert(err, gc.ErrorMatches, t.err)
}
}
}
const authInformationBody = `{"versions": [` +
`{"status": "stable", "updated": "2015-03-30T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v3+json"}], "id": "v3.4", "links": [{"href": "%s/v3/", "rel": "self"}]},` +
`{"status": "stable", "updated": "2014-04-17T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v2.0+json"}], "id": "v2.0", "links": [{"href": "%s/v2.0/", "rel": "self"}, {"href": "http://docs.openstack.org/", "type": "text/html", "rel": "describedby"}]},` +
`{"status": "stable", "updated": "2015-03-30T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v3+json"}], "id": "v2.1", "links": [{"href": "%s/v2.1/", "rel": "self"}]}` +
`]}`
const authValuesInformationBody = `{"versions": {"values": [` +
`{"status": "stable", "updated": "2015-03-30T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v3+json"}], "id": "v3.4", "links": [{"href": "%s/v3/", "rel": "self"}]},` +
`{"status": "stable", "updated": "2014-04-17T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v2.0+json"}], "id": "v2.0", "links": [{"href": "%s/v2.0/", "rel": "self"}, {"href": "http://docs.openstack.org/", "type": "text/html", "rel": "describedby"}]},` +
`{"status": "stable", "updated": "2015-03-30T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v3+json"}], "id": "v2.1", "links": [{"href": "%s/v2.1/", "rel": "self"}]}` +
`]}}`
func (vh *versionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if vh.authBody == "" {
body := `{"message":"Api does not exist","request_id":"83A781AE-9A0C-43C7-B405-310A5A94566E"}`
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(body))
return
}
body := []byte(fmt.Sprintf(vh.authBody, "http://127.0.0.1:"+vh.port, "http://127.0.0.1:"+vh.port, "http://127.0.0.1:"+vh.port))
// workaround for https://code.google.com/p/go/issues/detail?id=4454
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
w.WriteHeader(http.StatusMultipleChoices)
w.Write(body)
}
func startApiVersionMux(vh versionHandler) string {
mux := http.NewServeMux()
mux.Handle("/", &vh)
go http.ListenAndServe(":"+vh.port, mux)
return fmt.Sprintf("Listening on 127.0.0.1:%s...\n", vh.port)
}
|