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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
|
# 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 os
from unittest import mock
import fixtures
from keystoneauth1 import session
import openstack.config
from openstack import connection
from openstack import proxy
from openstack import service_description
from openstack.tests import fakes
from openstack.tests.unit import base
from openstack.tests.unit.fake import fake_service
CONFIG_AUTH_URL = "https://identity.example.com/"
CONFIG_USERNAME = "BozoTheClown"
CONFIG_PASSWORD = "TopSecret"
CONFIG_PROJECT = "TheGrandPrizeGame"
CONFIG_CACERT = "TrustMe"
CLOUD_CONFIG = f"""
clouds:
sample-cloud:
region_name: RegionOne
auth:
auth_url: {CONFIG_AUTH_URL}
username: {CONFIG_USERNAME}
password: {CONFIG_PASSWORD}
project_name: {CONFIG_PROJECT}
insecure-cloud:
auth:
auth_url: {CONFIG_AUTH_URL}
username: {CONFIG_USERNAME}
password: {CONFIG_PASSWORD}
project_name: {CONFIG_PROJECT}
cacert: {CONFIG_CACERT}
verify: False
insecure-cloud-alternative-format:
auth:
auth_url: {CONFIG_AUTH_URL}
username: {CONFIG_USERNAME}
password: {CONFIG_PASSWORD}
project_name: {CONFIG_PROJECT}
insecure: True
cacert-cloud:
auth:
auth_url: {CONFIG_AUTH_URL}
username: {CONFIG_USERNAME}
password: {CONFIG_PASSWORD}
project_name: {CONFIG_PROJECT}
cacert: {CONFIG_CACERT}
profiled-cloud:
profile: dummy
auth:
username: {CONFIG_USERNAME}
password: {CONFIG_PASSWORD}
project_name: {CONFIG_PROJECT}
cacert: {CONFIG_CACERT}
"""
VENDOR_CONFIG = f"""
{{
"name": "dummy",
"profile": {{
"auth": {{
"auth_url": "{CONFIG_AUTH_URL}"
}},
"vendor_hook": "openstack.tests.unit.test_connection:vendor_hook"
}}
}}
"""
PUBLIC_CLOUDS_YAML = f"""
public-clouds:
dummy:
auth:
auth_url: {CONFIG_AUTH_URL}
vendor_hook: openstack.tests.unit.test_connection:vendor_hook
"""
class _TestConnectionBase(base.TestCase):
def setUp(self):
super().setUp()
# Create a temporary directory where our test config will live
# and insert it into the search path via OS_CLIENT_CONFIG_FILE.
config_dir = self.useFixture(fixtures.TempDir()).path
config_path = os.path.join(config_dir, "clouds.yaml")
with open(config_path, "w") as conf:
conf.write(CLOUD_CONFIG)
self.useFixture(
fixtures.EnvironmentVariable("OS_CLIENT_CONFIG_FILE", config_path)
)
self.use_keystone_v2()
class TestConnection(_TestConnectionBase):
def test_other_parameters(self):
conn = connection.Connection(cloud='sample-cloud', cert='cert')
self.assertEqual(conn.session.cert, 'cert')
def test_session_provided(self):
mock_session = mock.Mock(spec=session.Session)
mock_session.auth = mock.Mock()
mock_session.auth.auth_url = 'https://auth.example.com'
conn = connection.Connection(session=mock_session, cert='cert')
self.assertEqual(mock_session, conn.session)
self.assertEqual('auth.example.com', conn.config.name)
def test_create_session(self):
conn = connection.Connection(cloud='sample-cloud')
self.assertIsNotNone(conn)
# TODO(mordred) Rework this - we need to provide requests-mock
# entries for each of the proxies below
# self.assertEqual('openstack.proxy',
# conn.alarm.__class__.__module__)
# self.assertEqual('openstack.clustering.v1._proxy',
# conn.clustering.__class__.__module__)
# self.assertEqual('openstack.compute.v2._proxy',
# conn.compute.__class__.__module__)
# self.assertEqual('openstack.database.v1._proxy',
# conn.database.__class__.__module__)
# self.assertEqual('openstack.identity.v2._proxy',
# conn.identity.__class__.__module__)
# self.assertEqual('openstack.image.v2._proxy',
# conn.image.__class__.__module__)
# self.assertEqual('openstack.object_store.v1._proxy',
# conn.object_store.__class__.__module__)
# self.assertEqual('openstack.load_balancer.v2._proxy',
# conn.load_balancer.__class__.__module__)
# self.assertEqual('openstack.orchestration.v1._proxy',
# conn.orchestration.__class__.__module__)
# self.assertEqual('openstack.workflow.v2._proxy',
# conn.workflow.__class__.__module__)
def test_create_unknown_proxy(self):
self.register_uris(
[
self.get_placement_discovery_mock_dict(),
]
)
def closure():
return self.cloud.placement
self.assertIsInstance(self.cloud.placement, proxy.Proxy)
self.assert_calls()
def test_create_connection_version_param_default(self):
c1 = connection.Connection(cloud='sample-cloud')
conn = connection.Connection(session=c1.session)
self.assertEqual(
'openstack.identity.v3._proxy', conn.identity.__class__.__module__
)
def test_create_connection_version_param_string(self):
c1 = connection.Connection(cloud='sample-cloud')
conn = connection.Connection(
session=c1.session, identity_api_version='2'
)
self.assertEqual(
'openstack.identity.v2._proxy', conn.identity.__class__.__module__
)
def test_create_connection_version_param_int(self):
c1 = connection.Connection(cloud='sample-cloud')
conn = connection.Connection(
session=c1.session, identity_api_version=3
)
self.assertEqual(
'openstack.identity.v3._proxy', conn.identity.__class__.__module__
)
def test_create_connection_version_param_bogus(self):
c1 = connection.Connection(cloud='sample-cloud')
conn = connection.Connection(
session=c1.session, identity_api_version='red'
)
# TODO(mordred) This is obviously silly behavior
self.assertEqual(
'openstack.identity.v3._proxy', conn.identity.__class__.__module__
)
def test_from_config_given_config(self):
cloud_region = openstack.config.OpenStackConfig().get_one(
"sample-cloud"
)
sot = connection.from_config(config=cloud_region)
self.assertEqual(
CONFIG_USERNAME, sot.config.config['auth']['username']
)
self.assertEqual(
CONFIG_PASSWORD, sot.config.config['auth']['password']
)
self.assertEqual(
CONFIG_AUTH_URL, sot.config.config['auth']['auth_url']
)
self.assertEqual(
CONFIG_PROJECT, sot.config.config['auth']['project_name']
)
def test_from_config_given_cloud(self):
sot = connection.from_config(cloud="sample-cloud")
self.assertEqual(
CONFIG_USERNAME, sot.config.config['auth']['username']
)
self.assertEqual(
CONFIG_PASSWORD, sot.config.config['auth']['password']
)
self.assertEqual(
CONFIG_AUTH_URL, sot.config.config['auth']['auth_url']
)
self.assertEqual(
CONFIG_PROJECT, sot.config.config['auth']['project_name']
)
def test_from_config_given_cloud_config(self):
cloud_region = openstack.config.OpenStackConfig().get_one(
"sample-cloud"
)
sot = connection.from_config(cloud_config=cloud_region)
self.assertEqual(
CONFIG_USERNAME, sot.config.config['auth']['username']
)
self.assertEqual(
CONFIG_PASSWORD, sot.config.config['auth']['password']
)
self.assertEqual(
CONFIG_AUTH_URL, sot.config.config['auth']['auth_url']
)
self.assertEqual(
CONFIG_PROJECT, sot.config.config['auth']['project_name']
)
def test_from_config_given_cloud_name(self):
sot = connection.from_config(cloud_name="sample-cloud")
self.assertEqual(
CONFIG_USERNAME, sot.config.config['auth']['username']
)
self.assertEqual(
CONFIG_PASSWORD, sot.config.config['auth']['password']
)
self.assertEqual(
CONFIG_AUTH_URL, sot.config.config['auth']['auth_url']
)
self.assertEqual(
CONFIG_PROJECT, sot.config.config['auth']['project_name']
)
def test_from_config_verify(self):
sot = connection.from_config(cloud="insecure-cloud")
self.assertFalse(sot.session.verify)
sot = connection.from_config(cloud="cacert-cloud")
self.assertEqual(CONFIG_CACERT, sot.session.verify)
def test_from_config_insecure(self):
# Ensure that the "insecure=True" flag implies "verify=False"
sot = connection.from_config("insecure-cloud-alternative-format")
self.assertFalse(sot.session.verify)
class TestOsloConfig(_TestConnectionBase):
def test_from_conf(self):
c1 = connection.Connection(cloud='sample-cloud')
conn = connection.Connection(
session=c1.session, oslo_conf=self._load_ks_cfg_opts()
)
# There was no config for keystone
self.assertIsInstance(
conn.identity, service_description._ServiceDisabledProxyShim
)
# But nova was in there
self.assertEqual(
'openstack.compute.v2._proxy', conn.compute.__class__.__module__
)
def test_from_conf_filter_service_types(self):
c1 = connection.Connection(cloud='sample-cloud')
conn = connection.Connection(
session=c1.session,
oslo_conf=self._load_ks_cfg_opts(),
service_types={'orchestration', 'i-am-ignored'},
)
# There was no config for keystone
self.assertIsInstance(
conn.identity, service_description._ServiceDisabledProxyShim
)
# Nova was in there, but disabled because not requested
self.assertIsInstance(
conn.compute, service_description._ServiceDisabledProxyShim
)
class TestNetworkConnection(base.TestCase):
# Verify that if the catalog has the suffix we don't mess things up.
def test_network_proxy(self):
self.os_fixture.v3_token.remove_service('network')
svc = self.os_fixture.v3_token.add_service('network')
svc.add_endpoint(
interface='public',
url='https://network.example.com/v2.0',
region='RegionOne',
)
self.use_keystone_v3()
self.assertEqual(
'openstack.network.v2._proxy',
self.cloud.network.__class__.__module__,
)
self.assert_calls()
self.assertEqual(
"https://network.example.com/v2.0",
self.cloud.network.get_endpoint(),
)
class TestNetworkConnectionSuffix(base.TestCase):
# We need to do the neutron adapter test differently because it needs
# to actually get a catalog.
def test_network_proxy(self):
self.assertEqual(
'openstack.network.v2._proxy',
self.cloud.network.__class__.__module__,
)
self.assert_calls()
self.assertEqual(
"https://network.example.com/v2.0",
self.cloud.network.get_endpoint(),
)
class TestAuthorize(base.TestCase):
def test_authorize_works(self):
res = self.cloud.authorize()
self.assertEqual('KeystoneToken-1', res)
def test_authorize_failure(self):
self.use_broken_keystone()
self.assertRaises(
openstack.exceptions.SDKException, self.cloud.authorize
)
class TestNewService(base.TestCase):
def test_add_service_v1(self):
svc = self.os_fixture.v3_token.add_service('fake')
svc.add_endpoint(
interface='public',
region='RegionOne',
url=f'https://fake.example.com/v1/{fakes.PROJECT_ID}',
)
self.use_keystone_v3()
conn = self.cloud
service = fake_service.FakeService('fake')
conn.add_service(service)
# Ensure no discovery calls made
self.assertEqual(0, len(self.adapter.request_history))
self.register_uris(
[
dict(
method='GET',
uri='https://fake.example.com',
status_code=404,
),
dict(
method='GET',
uri='https://fake.example.com/v1/',
status_code=404,
),
dict(
method='GET',
uri=self.get_mock_url('fake'),
status_code=404,
),
]
)
self.assertEqual(
'openstack.tests.unit.fake.v1._proxy',
conn.fake.__class__.__module__,
)
self.assertTrue(conn.fake.dummy())
def test_add_service_v2(self):
svc = self.os_fixture.v3_token.add_service('fake')
svc.add_endpoint(
interface='public',
region='RegionOne',
url=f'https://fake.example.com/v2/{fakes.PROJECT_ID}',
)
self.use_keystone_v3()
conn = self.cloud
self.register_uris(
[
dict(
method='GET',
uri='https://fake.example.com',
status_code=404,
),
dict(
method='GET',
uri='https://fake.example.com/v2/',
status_code=404,
),
dict(
method='GET',
uri=self.get_mock_url('fake'),
status_code=404,
),
]
)
service = fake_service.FakeService('fake')
conn.add_service(service)
self.assertEqual(
'openstack.tests.unit.fake.v2._proxy',
conn.fake.__class__.__module__,
)
self.assertFalse(conn.fake.dummy())
def test_replace_system_service(self):
svc = self.os_fixture.v3_token.add_service('fake')
svc.add_endpoint(
interface='public',
region='RegionOne',
url=f'https://fake.example.com/v2/{fakes.PROJECT_ID}',
)
self.use_keystone_v3()
conn = self.cloud
# delete native dns service
delattr(conn, 'dns')
self.register_uris(
[
dict(
method='GET',
uri='https://fake.example.com',
status_code=404,
),
dict(
method='GET',
uri='https://fake.example.com/v2/',
status_code=404,
),
dict(
method='GET',
uri=self.get_mock_url('fake'),
status_code=404,
),
]
)
# add fake service with alias 'DNS'
service = fake_service.FakeService('fake', aliases=['dns'])
conn.add_service(service)
# ensure dns service responds as we expect from replacement
self.assertFalse(conn.dns.dummy())
def vendor_hook(conn):
setattr(conn, 'test', 'test_val')
class TestVendorProfile(base.TestCase):
def setUp(self):
super().setUp()
# Create a temporary directory where our test config will live
# and insert it into the search path via OS_CLIENT_CONFIG_FILE.
config_dir = self.useFixture(fixtures.TempDir()).path
config_path = os.path.join(config_dir, "clouds.yaml")
public_clouds = os.path.join(config_dir, "clouds-public.yaml")
with open(config_path, "w") as conf:
conf.write(CLOUD_CONFIG)
with open(public_clouds, "w") as conf:
conf.write(PUBLIC_CLOUDS_YAML)
self.useFixture(
fixtures.EnvironmentVariable("OS_CLIENT_CONFIG_FILE", config_path)
)
self.use_keystone_v2()
self.config = openstack.config.loader.OpenStackConfig(
vendor_files=[public_clouds]
)
def test_conn_from_profile(self):
self.cloud = self.config.get_one(cloud='profiled-cloud')
conn = connection.Connection(config=self.cloud)
self.assertIsNotNone(conn)
def test_hook_from_profile(self):
self.cloud = self.config.get_one(cloud='profiled-cloud')
conn = connection.Connection(config=self.cloud)
self.assertEqual('test_val', conn.test)
def test_hook_from_connection_param(self):
conn = connection.Connection(
cloud='sample-cloud',
vendor_hook='openstack.tests.unit.test_connection:vendor_hook',
)
self.assertEqual('test_val', conn.test)
def test_hook_from_connection_ignore_missing(self):
conn = connection.Connection(
cloud='sample-cloud',
vendor_hook='openstack.tests.unit.test_connection:missing',
)
self.assertIsNotNone(conn)
|