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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import uuid
from keystoneauth1 import exceptions as ks_exc
import requests.exceptions
from openstack.config import cloud_region
from openstack import connection
from openstack import exceptions
from openstack.tests import fakes
from openstack.tests.unit import base
class TestFromConf(base.TestCase):
def _get_conn(self, **from_conf_kwargs):
oslocfg = self._load_ks_cfg_opts()
# Throw name in here to prove **kwargs is working
config = cloud_region.from_conf(
oslocfg,
session=self.cloud.session,
name='from_conf.example.com',
**from_conf_kwargs,
)
self.assertEqual('from_conf.example.com', config.name)
return connection.Connection(config=config, strict_proxies=True)
def test_adapter_opts_set(self):
"""Adapter opts specified in the conf."""
conn = self._get_conn()
discovery = {
"versions": {
"values": [
{
"status": "stable",
"updated": "2019-06-01T00:00:00Z",
"media-types": [
{
"base": "application/json",
"type": "application/vnd.openstack.heat-v2+json", # noqa: E501
}
],
"id": "v2.0",
"links": [
{
"href": "https://example.org:8888/heat/v2",
"rel": "self",
}
],
}
]
}
}
self.register_uris(
[
dict(
method='GET',
uri='https://example.org:8888/heat/v2',
json=discovery,
),
dict(
method='GET',
uri='https://example.org:8888/heat/v2/foo',
json={'foo': {}},
),
]
)
adap = conn.orchestration
self.assertEqual('SpecialRegion', adap.region_name)
self.assertEqual('orchestration', adap.service_type)
self.assertEqual('internal', adap.interface)
self.assertEqual(
'https://example.org:8888/heat/v2', adap.endpoint_override
)
adap.get('/foo')
self.assert_calls()
def test_default_adapter_opts(self):
"""Adapter opts are registered, but all defaulting in conf."""
conn = self._get_conn()
server_id = str(uuid.uuid4())
server_name = self.getUniqueString('name')
fake_server = fakes.make_fake_server(server_id, server_name)
self.register_uris(
[
self.get_nova_discovery_mock_dict(),
dict(
method='GET',
uri=self.get_mock_url(
'compute', 'public', append=['servers', 'detail']
),
json={'servers': [fake_server]},
),
]
)
# Nova has empty adapter config, so these default
adap = conn.compute
self.assertIsNone(adap.region_name)
self.assertEqual('compute', adap.service_type)
self.assertEqual('public', adap.interface)
self.assertIsNone(adap.endpoint_override)
s = next(adap.servers())
self.assertEqual(s.id, server_id)
self.assertEqual(s.name, server_name)
self.assert_calls()
def test_service_not_ready_catalog(self):
"""Adapter opts are registered, but all defaulting in conf."""
conn = self._get_conn()
server_id = str(uuid.uuid4())
server_name = self.getUniqueString('name')
fake_server = fakes.make_fake_server(server_id, server_name)
self.register_uris(
[
dict(
method='GET',
uri='https://compute.example.com/v2.1/',
exc=requests.exceptions.ConnectionError,
),
self.get_nova_discovery_mock_dict(),
dict(
method='GET',
uri=self.get_mock_url(
'compute', 'public', append=['servers', 'detail']
),
json={'servers': [fake_server]},
),
]
)
self.assertRaises(
exceptions.ServiceDiscoveryException, getattr, conn, 'compute'
)
# Nova has empty adapter config, so these default
adap = conn.compute
self.assertIsNone(adap.region_name)
self.assertEqual('compute', adap.service_type)
self.assertEqual('public', adap.interface)
self.assertIsNone(adap.endpoint_override)
s = next(adap.servers())
self.assertEqual(s.id, server_id)
self.assertEqual(s.name, server_name)
self.assert_calls()
def test_name_with_dashes(self):
conn = self._get_conn()
discovery = {
"versions": {
"values": [
{
"status": "stable",
"id": "v1",
"links": [
{
"href": "https://example.org:5050/v1",
"rel": "self",
}
],
}
]
}
}
status = {'finished': True, 'error': None}
self.register_uris(
[
dict(
method='GET',
uri='https://example.org:5050',
json=discovery,
),
# strict-proxies means we're going to fetch the discovery
# doc from the versioned endpoint to verify it works.
dict(
method='GET',
uri='https://example.org:5050/v1',
json=discovery,
),
dict(
method='GET',
uri='https://example.org:5050/v1/introspection/abcd',
json=status,
),
]
)
adap = conn.baremetal_introspection
self.assertEqual('baremetal-introspection', adap.service_type)
self.assertEqual('public', adap.interface)
self.assertEqual('https://example.org:5050/v1', adap.endpoint_override)
self.assertTrue(adap.get_introspection('abcd').is_finished)
def test_service_not_ready_endpoint_override(self):
conn = self._get_conn()
discovery = {
"versions": {
"values": [
{
"status": "stable",
"id": "v1",
"links": [
{
"href": "https://example.org:5050/v1",
"rel": "self",
}
],
}
]
}
}
status = {'finished': True, 'error': None}
self.register_uris(
[
dict(
method='GET',
uri='https://example.org:5050',
exc=requests.exceptions.ConnectTimeout,
),
dict(
method='GET',
uri='https://example.org:5050',
json=discovery,
),
# strict-proxies means we're going to fetch the discovery
# doc from the versioned endpoint to verify it works.
dict(
method='GET',
uri='https://example.org:5050/v1',
json=discovery,
),
dict(
method='GET',
uri='https://example.org:5050/v1/introspection/abcd',
json=status,
),
]
)
self.assertRaises(
exceptions.ServiceDiscoveryException,
getattr,
conn,
'baremetal_introspection',
)
adap = conn.baremetal_introspection
self.assertEqual('baremetal-introspection', adap.service_type)
self.assertEqual('public', adap.interface)
self.assertEqual('https://example.org:5050/v1', adap.endpoint_override)
self.assertTrue(adap.get_introspection('abcd').is_finished)
def assert_service_disabled(
self, service_type, expected_reason, **from_conf_kwargs
):
conn = self._get_conn(**from_conf_kwargs)
# The _ServiceDisabledProxyShim loads up okay...
adap = getattr(conn, service_type)
# ...but freaks out if you try to use it.
ex = self.assertRaises(
exceptions.ServiceDisabledException, getattr, adap, 'get'
)
self.assertIn(
f"Service '{service_type}' is disabled because its configuration "
"could not be loaded.",
ex.message,
)
self.assertIn(expected_reason, ex.message)
def test_no_such_conf_section(self):
"""No conf section (therefore no adapter opts) for service type."""
del self.oslo_config_dict['heat']
self.assert_service_disabled(
'orchestration',
"No section for project 'heat' (service type 'orchestration') was "
"present in the config.",
)
def test_no_such_conf_section_ignore_service_type(self):
"""Ignore absent conf section if service type not requested."""
del self.oslo_config_dict['heat']
self.assert_service_disabled(
'orchestration',
"Not in the list of requested service_types.",
# 'orchestration' absent from this list
service_types=['compute'],
)
def test_no_adapter_opts(self):
"""Conf section present, but opts for service type not registered."""
self.oslo_config_dict['heat'] = None
self.assert_service_disabled(
'orchestration',
"Encountered an exception attempting to process config for "
"project 'heat' (service type 'orchestration'): no such option",
)
def test_no_adapter_opts_ignore_service_type(self):
"""Ignore unregistered conf section if service type not requested."""
self.oslo_config_dict['heat'] = None
self.assert_service_disabled(
'orchestration',
"Not in the list of requested service_types.",
# 'orchestration' absent from this list
service_types=['compute'],
)
def test_invalid_adapter_opts(self):
"""Adapter opts are bogus, in exception-raising ways."""
self.oslo_config_dict['heat'] = {
'interface': 'public',
'valid_interfaces': 'private',
}
self.assert_service_disabled(
'orchestration',
"Encountered an exception attempting to process config for "
"project 'heat' (service type 'orchestration'): interface and "
"valid_interfaces are mutually exclusive.",
)
def test_no_session(self):
# TODO(efried): Currently calling without a Session is not implemented.
self.assertRaises(
exceptions.ConfigException,
cloud_region.from_conf,
self._load_ks_cfg_opts(),
)
def test_no_endpoint(self):
"""Conf contains adapter opts, but service type not in catalog."""
self.os_fixture.v3_token.remove_service('monitoring')
conn = self._get_conn()
# Monasca is not in the service catalog
self.assertRaises(
ks_exc.catalog.EndpointNotFound, getattr, conn, 'monitoring'
)
def test_no_endpoint_ignore_service_type(self):
"""Bogus service type disabled if not in requested service_types."""
self.assert_service_disabled(
'monitoring',
"Not in the list of requested service_types.",
# 'monitoring' absent from this list
service_types={'compute', 'orchestration', 'bogus'},
)
|