| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from keystoneauth1 import fixture |
| 14 |
|
| 15 |
from keystoneclient import access |
| 16 |
from keystoneclient import exceptions |
| 17 |
from keystoneclient.tests.unit import utils as test_utils |
| 18 |
from keystoneclient.tests.unit.v3 import client_fixtures |
| 19 |
from keystoneclient.tests.unit.v3 import utils |
| 20 |
|
| 21 |
|
| 22 |
class ServiceCatalogTest(utils.TestCase): |
| 23 |
def setUp(self): |
| 24 |
super(ServiceCatalogTest, self).setUp() |
| 25 |
self.AUTH_RESPONSE_BODY = client_fixtures.auth_response_body() |
| 26 |
self.RESPONSE = test_utils.test_response( |
| 27 |
headers=client_fixtures.AUTH_RESPONSE_HEADERS |
| 28 |
) |
| 29 |
|
| 30 |
self.north_endpoints = {'public': |
| 31 |
'http://glance.north.host/glanceapi/public', |
| 32 |
'internal': |
| 33 |
'http://glance.north.host/glanceapi/internal', |
| 34 |
'admin': |
| 35 |
'http://glance.north.host/glanceapi/admin'} |
| 36 |
|
| 37 |
self.south_endpoints = {'public': |
| 38 |
'http://glance.south.host/glanceapi/public', |
| 39 |
'internal': |
| 40 |
'http://glance.south.host/glanceapi/internal', |
| 41 |
'admin': |
| 42 |
'http://glance.south.host/glanceapi/admin'} |
| 43 |
|
| 44 |
def test_building_a_service_catalog(self): |
| 45 |
auth_ref = access.AccessInfo.factory(self.RESPONSE, |
| 46 |
self.AUTH_RESPONSE_BODY) |
| 47 |
sc = auth_ref.service_catalog |
| 48 |
|
| 49 |
self.assertEqual(sc.url_for(service_type='compute'), |
| 50 |
"https://compute.north.host/novapi/public") |
| 51 |
self.assertEqual(sc.url_for(service_type='compute', |
| 52 |
endpoint_type='internal'), |
| 53 |
"https://compute.north.host/novapi/internal") |
| 54 |
|
| 55 |
self.assertRaises(exceptions.EndpointNotFound, sc.url_for, "region", |
| 56 |
"South", service_type='compute') |
| 57 |
|
| 58 |
def test_service_catalog_endpoints(self): |
| 59 |
auth_ref = access.AccessInfo.factory(self.RESPONSE, |
| 60 |
self.AUTH_RESPONSE_BODY) |
| 61 |
sc = auth_ref.service_catalog |
| 62 |
|
| 63 |
public_ep = sc.get_endpoints(service_type='compute', |
| 64 |
endpoint_type='public') |
| 65 |
self.assertEqual(public_ep['compute'][0]['region'], 'North') |
| 66 |
self.assertEqual(public_ep['compute'][0]['url'], |
| 67 |
"https://compute.north.host/novapi/public") |
| 68 |
|
| 69 |
def test_service_catalog_regions(self): |
| 70 |
self.AUTH_RESPONSE_BODY['token']['region_name'] = "North" |
| 71 |
|
| 72 |
with self.deprecations.expect_deprecations_here(): |
| 73 |
auth_ref = access.AccessInfo.factory(self.RESPONSE, |
| 74 |
self.AUTH_RESPONSE_BODY) |
| 75 |
sc = auth_ref.service_catalog |
| 76 |
|
| 77 |
url = sc.url_for(service_type='image', endpoint_type='public') |
| 78 |
self.assertEqual(url, "http://glance.north.host/glanceapi/public") |
| 79 |
|
| 80 |
self.AUTH_RESPONSE_BODY['token']['region_name'] = "South" |
| 81 |
|
| 82 |
with self.deprecations.expect_deprecations_here(): |
| 83 |
auth_ref = access.AccessInfo.factory(self.RESPONSE, |
| 84 |
self.AUTH_RESPONSE_BODY) |
| 85 |
sc = auth_ref.service_catalog |
| 86 |
url = sc.url_for(service_type='image', endpoint_type='internal') |
| 87 |
self.assertEqual(url, "http://glance.south.host/glanceapi/internal") |
| 88 |
|
| 89 |
def test_service_catalog_empty(self): |
| 90 |
self.AUTH_RESPONSE_BODY['token']['catalog'] = [] |
| 91 |
auth_ref = access.AccessInfo.factory(self.RESPONSE, |
| 92 |
self.AUTH_RESPONSE_BODY) |
| 93 |
self.assertRaises(exceptions.EmptyCatalog, |
| 94 |
auth_ref.service_catalog.url_for, |
| 95 |
service_type='image', |
| 96 |
endpoint_type='internalURL') |
| 97 |
|
| 98 |
def test_service_catalog_get_endpoints_region_names(self): |
| 99 |
auth_ref = access.AccessInfo.factory(None, self.AUTH_RESPONSE_BODY) |
| 100 |
sc = auth_ref.service_catalog |
| 101 |
|
| 102 |
endpoints = sc.get_endpoints(service_type='image', region_name='North') |
| 103 |
self.assertEqual(len(endpoints), 1) |
| 104 |
for endpoint in endpoints['image']: |
| 105 |
self.assertEqual(endpoint['url'], |
| 106 |
self.north_endpoints[endpoint['interface']]) |
| 107 |
|
| 108 |
endpoints = sc.get_endpoints(service_type='image', region_name='South') |
| 109 |
self.assertEqual(len(endpoints), 1) |
| 110 |
for endpoint in endpoints['image']: |
| 111 |
self.assertEqual(endpoint['url'], |
| 112 |
self.south_endpoints[endpoint['interface']]) |
| 113 |
|
| 114 |
endpoints = sc.get_endpoints(service_type='compute') |
| 115 |
self.assertEqual(len(endpoints['compute']), 3) |
| 116 |
|
| 117 |
endpoints = sc.get_endpoints(service_type='compute', |
| 118 |
region_name='North') |
| 119 |
self.assertEqual(len(endpoints['compute']), 3) |
| 120 |
|
| 121 |
endpoints = sc.get_endpoints(service_type='compute', |
| 122 |
region_name='West') |
| 123 |
self.assertEqual(len(endpoints['compute']), 0) |
| 124 |
|
| 125 |
def test_service_catalog_url_for_region_names(self): |
| 126 |
auth_ref = access.AccessInfo.factory(None, self.AUTH_RESPONSE_BODY) |
| 127 |
sc = auth_ref.service_catalog |
| 128 |
|
| 129 |
url = sc.url_for(service_type='image', region_name='North') |
| 130 |
self.assertEqual(url, self.north_endpoints['public']) |
| 131 |
|
| 132 |
url = sc.url_for(service_type='image', region_name='South') |
| 133 |
self.assertEqual(url, self.south_endpoints['public']) |
| 134 |
|
| 135 |
self.assertRaises(exceptions.EndpointNotFound, sc.url_for, |
| 136 |
service_type='image', region_name='West') |
| 137 |
|
| 138 |
def test_servcie_catalog_get_url_region_names(self): |
| 139 |
auth_ref = access.AccessInfo.factory(None, self.AUTH_RESPONSE_BODY) |
| 140 |
sc = auth_ref.service_catalog |
| 141 |
|
| 142 |
urls = sc.get_urls(service_type='image') |
| 143 |
self.assertEqual(len(urls), 2) |
| 144 |
|
| 145 |
urls = sc.get_urls(service_type='image', region_name='North') |
| 146 |
self.assertEqual(len(urls), 1) |
| 147 |
self.assertEqual(urls[0], self.north_endpoints['public']) |
| 148 |
|
| 149 |
urls = sc.get_urls(service_type='image', region_name='South') |
| 150 |
self.assertEqual(len(urls), 1) |
| 151 |
self.assertEqual(urls[0], self.south_endpoints['public']) |
| 152 |
|
| 153 |
urls = sc.get_urls(service_type='image', region_name='West') |
| 154 |
self.assertIsNone(urls) |
| 155 |
|
| 156 |
def test_service_catalog_param_overrides_body_region(self): |
| 157 |
self.AUTH_RESPONSE_BODY['token']['region_name'] = "North" |
| 158 |
|
| 159 |
with self.deprecations.expect_deprecations_here(): |
| 160 |
auth_ref = access.AccessInfo.factory(None, self.AUTH_RESPONSE_BODY) |
| 161 |
sc = auth_ref.service_catalog |
| 162 |
|
| 163 |
url = sc.url_for(service_type='image') |
| 164 |
self.assertEqual(url, self.north_endpoints['public']) |
| 165 |
|
| 166 |
url = sc.url_for(service_type='image', region_name='South') |
| 167 |
self.assertEqual(url, self.south_endpoints['public']) |
| 168 |
|
| 169 |
endpoints = sc.get_endpoints(service_type='image') |
| 170 |
self.assertEqual(len(endpoints['image']), 3) |
| 171 |
for endpoint in endpoints['image']: |
| 172 |
self.assertEqual(endpoint['url'], |
| 173 |
self.north_endpoints[endpoint['interface']]) |
| 174 |
|
| 175 |
endpoints = sc.get_endpoints(service_type='image', region_name='South') |
| 176 |
self.assertEqual(len(endpoints['image']), 3) |
| 177 |
for endpoint in endpoints['image']: |
| 178 |
self.assertEqual(endpoint['url'], |
| 179 |
self.south_endpoints[endpoint['interface']]) |
| 180 |
|
| 181 |
def test_service_catalog_service_name(self): |
| 182 |
auth_ref = access.AccessInfo.factory(resp=None, |
| 183 |
body=self.AUTH_RESPONSE_BODY) |
| 184 |
sc = auth_ref.service_catalog |
| 185 |
|
| 186 |
url = sc.url_for(service_name='glance', endpoint_type='public', |
| 187 |
service_type='image', region_name='North') |
| 188 |
self.assertEqual('http://glance.north.host/glanceapi/public', url) |
| 189 |
|
| 190 |
url = sc.url_for(service_name='glance', endpoint_type='public', |
| 191 |
service_type='image', region_name='South') |
| 192 |
self.assertEqual('http://glance.south.host/glanceapi/public', url) |
| 193 |
|
| 194 |
self.assertRaises(exceptions.EndpointNotFound, sc.url_for, |
| 195 |
service_name='glance', service_type='compute') |
| 196 |
|
| 197 |
urls = sc.get_urls(service_type='image', service_name='glance', |
| 198 |
endpoint_type='public') |
| 199 |
|
| 200 |
self.assertIn('http://glance.north.host/glanceapi/public', urls) |
| 201 |
self.assertIn('http://glance.south.host/glanceapi/public', urls) |
| 202 |
|
| 203 |
urls = sc.get_urls(service_type='image', service_name='Servers', |
| 204 |
endpoint_type='public') |
| 205 |
|
| 206 |
self.assertIsNone(urls) |
| 207 |
|
| 208 |
def test_service_catalog_without_name(self): |
| 209 |
pr_auth_ref = access.AccessInfo.factory( |
| 210 |
resp=None, |
| 211 |
body=client_fixtures.project_scoped_token()) |
| 212 |
pr_sc = pr_auth_ref.service_catalog |
| 213 |
|
| 214 |
|
| 215 |
url_ref = 'http://public.com:8774/v2/225da22d3ce34b15877ea70b2a575f58' |
| 216 |
url = pr_sc.url_for(service_type='compute', service_name='NotExist', |
| 217 |
endpoint_type='public') |
| 218 |
self.assertEqual(url_ref, url) |
| 219 |
|
| 220 |
ab_auth_ref = access.AccessInfo.factory(resp=None, |
| 221 |
body=self.AUTH_RESPONSE_BODY) |
| 222 |
ab_sc = ab_auth_ref.service_catalog |
| 223 |
|
| 224 |
|
| 225 |
self.assertRaises(exceptions.EndpointNotFound, ab_sc.url_for, |
| 226 |
service_type='compute', service_name='NotExist', |
| 227 |
endpoint_type='public') |
| 228 |
|
| 229 |
|
| 230 |
class ServiceCatalogV3Test(ServiceCatalogTest): |
| 231 |
|
| 232 |
def test_building_a_service_catalog(self): |
| 233 |
auth_ref = access.AccessInfo.factory(self.RESPONSE, |
| 234 |
self.AUTH_RESPONSE_BODY) |
| 235 |
sc = auth_ref.service_catalog |
| 236 |
|
| 237 |
self.assertEqual(sc.url_for(service_type='compute'), |
| 238 |
'https://compute.north.host/novapi/public') |
| 239 |
self.assertEqual(sc.url_for(service_type='compute', |
| 240 |
endpoint_type='internal'), |
| 241 |
'https://compute.north.host/novapi/internal') |
| 242 |
|
| 243 |
self.assertRaises(exceptions.EndpointNotFound, sc.url_for, 'region_id', |
| 244 |
'South', service_type='compute') |
| 245 |
|
| 246 |
def test_service_catalog_endpoints(self): |
| 247 |
auth_ref = access.AccessInfo.factory(self.RESPONSE, |
| 248 |
self.AUTH_RESPONSE_BODY) |
| 249 |
sc = auth_ref.service_catalog |
| 250 |
|
| 251 |
public_ep = sc.get_endpoints(service_type='compute', |
| 252 |
endpoint_type='public') |
| 253 |
self.assertEqual(public_ep['compute'][0]['region_id'], 'North') |
| 254 |
self.assertEqual(public_ep['compute'][0]['url'], |
| 255 |
'https://compute.north.host/novapi/public') |
| 256 |
|
| 257 |
def test_service_catalog_multiple_service_types(self): |
| 258 |
token = fixture.V3Token() |
| 259 |
token.set_project_scope() |
| 260 |
|
| 261 |
for i in range(3): |
| 262 |
s = token.add_service('compute') |
| 263 |
s.add_standard_endpoints(public='public-%d' % i, |
| 264 |
admin='admin-%d' % i, |
| 265 |
internal='internal-%d' % i, |
| 266 |
region='region-%d' % i) |
| 267 |
|
| 268 |
auth_ref = access.AccessInfo.factory(resp=None, body=token) |
| 269 |
|
| 270 |
urls = auth_ref.service_catalog.get_urls(service_type='compute', |
| 271 |
endpoint_type='public') |
| 272 |
|
| 273 |
self.assertEqual(set(['public-0', 'public-1', 'public-2']), set(urls)) |
| 274 |
|
| 275 |
urls = auth_ref.service_catalog.get_urls(service_type='compute', |
| 276 |
endpoint_type='public', |
| 277 |
region_name='region-1') |
| 278 |
|
| 279 |
self.assertEqual(('public-1', ), urls) |