Merge "Added NOVA Placement API Client and unit tests. This feature is used for updating the placement DB on NOVA side Cyborg DB should be kept up to date with the placement DB all the time."

This commit is contained in:
Zuul 2018-01-31 07:48:30 +00:00 committed by Gerrit Code Review
commit 6c64f63ba7
8 changed files with 455 additions and 0 deletions

View File

@ -146,3 +146,21 @@ class Conflict(CyborgException):
class DuplicateName(Conflict):
_msg_fmt = _("An accelerator with name %(name)s already exists.")
class PlacementEndpointNotFound(NotFound):
message = _("Placement API endpoint not found")
class PlacementResourceProviderNotFound(NotFound):
message = _("Placement resource provider not found %(resource_provider)s.")
class PlacementInventoryNotFound(NotFound):
message = _("Placement inventory not found for resource provider "
"%(resource_provider)s, resource class %(resource_class)s.")
class PlacementInventoryUpdateConflict(Conflict):
message = _("Placement inventory update conflict for resource provider "
"%(resource_provider)s, resource class %(resource_class)s.")

View File

@ -25,3 +25,4 @@ CONF = cfg.CONF
api.register_opts(CONF)
database.register_opts(CONF)
default.register_opts(CONF)
default.register_placement_opts(CONF)

View File

@ -61,8 +61,55 @@ path_opts = [
help=_("Top-level directory for maintaining cyborg's state.")),
]
PLACEMENT_CONF_SECTION = 'placement'
placement_opts = [
cfg.StrOpt('region_name',
help=_('Name of placement region to use. Useful if keystone '
'manages more than one region.')),
cfg.StrOpt('endpoint_type',
default='public',
choices=['public', 'admin', 'internal'],
help=_('Type of the placement endpoint to use. This endpoint '
'will be looked up in the keystone catalog and should '
'be one of public, internal or admin.')),
cfg.BoolOpt('insecure',
default=False,
help="""
If true, the vCenter server certificate is not verified.
If false, then the default CA truststore is used for
verification. Related options:
* ca_file: This option is ignored if "ca_file" is set.
"""),
cfg.StrOpt('cafile',
default=None,
help="""
Specifies the CA bundle file to be used in verifying the
vCenter server certificate.
"""),
cfg.StrOpt('certfile',
default=None,
help="""
Specifies the certificate file to be used in verifying
the vCenter server certificate.
"""),
cfg.StrOpt('keyfile',
default=None,
help="""
Specifies the key file to be used in verifying the vCenter
server certificate.
"""),
cfg.IntOpt('timeout',
default=None,
help=_('Timeout for inactive connections (in seconds)')),
]
def register_opts(conf):
conf.register_opts(exc_log_opts)
conf.register_opts(service_opts)
conf.register_opts(path_opts)
def register_placement_opts(cfg=cfg.CONF):
cfg.register_opts(placement_opts, group=PLACEMENT_CONF_SECTION)

View File

175
cyborg/services/report.py Normal file
View File

@ -0,0 +1,175 @@
# Copyright (c) 2018 Huawei Technologies Co., Ltd
# All Rights Reserved.
#
# 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 functools
from keystoneauth1 import exceptions as k_exc
from keystoneauth1 import loading as k_loading
from oslo_config import cfg
from cyborg.common import exception as c_exc
from oslo_concurrency import lockutils
synchronized = lockutils.synchronized_with_prefix('cyborg-')
PLACEMENT_CLIENT_SEMAPHORE = 'placement_client'
def check_placement_api_available(f):
@functools.wraps(f)
def wrapper(self, *a, **k):
try:
return f(self, *a, **k)
except k_exc.EndpointNotFound:
raise c_exc.PlacementEndpointNotFound()
return wrapper
class SchedulerReportClient(object):
"""Client class for updating the scheduler.
This class is used for updating the placement DB on NOVA side
Cyborg DB should be kept up to date with the placement DB all
the time.
Here is an example on how to use it:
from cyborg.services import report as placement_report_client
p_client = placement_report_client.SchedulerReportClient()
resource_provider = {'name': 'rp_name', 'uuid': 'uuid'}
p_client.create_resource_provider(resource_provider)
"""
keystone_filter = {'service_type': 'placement',
'region_name': cfg.CONF.placement.region_name}
def __init__(self):
self.association_refresh_time = {}
self._client = self._create_client()
self._disabled = False
def _create_client(self):
"""Create the HTTP session accessing the placement service."""
self.association_refresh_time = {}
auth_plugin = k_loading.load_auth_from_conf_options(
cfg.CONF, 'placement')
client = k_loading.load_session_from_conf_options(
cfg.CONF, 'placement', auth=auth_plugin)
client.additional_headers = {'accept': 'application/json'}
return client
def _get(self, url, **kwargs):
return self._client.get(url, endpoint_filter=self.keystone_filter,
**kwargs)
def _post(self, url, data, **kwargs):
return self._client.post(url, json=data,
endpoint_filter=self.keystone_filter,
**kwargs)
def _put(self, url, data, **kwargs):
return self._client.put(url, json=data,
endpoint_filter=self.keystone_filter,
**kwargs)
def _delete(self, url, **kwargs):
return self._client.delete(url, endpoint_filter=self.keystone_filter,
**kwargs)
@check_placement_api_available
def create_resource_provider(self, resource_provider):
"""Create a resource provider.
:param resource_provider: The resource provider
:type resource_provider: dict: name (required), uuid (required)
"""
url = '/resource_providers'
self._post(url, resource_provider)
@check_placement_api_available
def delete_resource_provider(self, resource_provider_uuid):
"""Delete a resource provider.
:param resource_provider_uuid: UUID of the resource provider
:type resource_provider_uuid: str
"""
url = '/resource_providers/%s' % resource_provider_uuid
self._delete(url)
@check_placement_api_available
def create_inventory(self, resource_provider_uuid, inventory):
"""Create an inventory.
:param resource_provider_uuid: UUID of the resource provider
:type resource_provider_uuid: str
:param inventory: The inventory
:type inventory: dict: resource_class (required), total (required),
reserved (required), min_unit (required), max_unit (required),
step_size (required), allocation_ratio (required)
"""
url = '/resource_providers/%s/inventories' % resource_provider_uuid
self._post(url, inventory)
@check_placement_api_available
def get_inventory(self, resource_provider_uuid, resource_class):
"""Get resource provider inventory.
:param resource_provider_uuid: UUID of the resource provider
:type resource_provider_uuid: str
:param resource_class: Resource class name of the inventory to be
returned
:type resource_class: str
:raises c_exc.PlacementInventoryNotFound: For failure to find inventory
for a resource provider
"""
url = '/resource_providers/%s/inventories/%s' % (
resource_provider_uuid, resource_class)
try:
return self._get(url).json()
except k_exc.NotFound as e:
if "No resource provider with uuid" in e.details:
raise c_exc.PlacementResourceProviderNotFound(
resource_provider=resource_provider_uuid)
elif _("No inventory of class") in e.details:
raise c_exc.PlacementInventoryNotFound(
resource_provider=resource_provider_uuid,
resource_class=resource_class)
else:
raise
@check_placement_api_available
def update_inventory(self, resource_provider_uuid, inventory,
resource_class):
"""Update an inventory.
:param resource_provider_uuid: UUID of the resource provider
:type resource_provider_uuid: str
:param inventory: The inventory
:type inventory: dict
:param resource_class: The resource class of the inventory to update
:type resource_class: str
:raises c_exc.PlacementInventoryUpdateConflict: For failure to updste
inventory due to outdated resource_provider_generation
"""
url = '/resource_providers/%s/inventories/%s' % (
resource_provider_uuid, resource_class)
try:
self._put(url, inventory)
except k_exc.Conflict:
raise c_exc.PlacementInventoryUpdateConflict(
resource_provider=resource_provider_uuid,
resource_class=resource_class)

View File

@ -22,6 +22,8 @@ from oslo_db import options
from oslo_log import log
from oslotest import base
import pecan
import contextlib
import mock
from cyborg.common import config as cyborg_config
from cyborg.tests.unit import policy_fixture
@ -78,3 +80,92 @@ class TestCase(base.BaseTestCase):
return os.path.join(root, project_file)
else:
return root
# Test worker cannot survive eventlet's Timeout exception, which effectively
# kills the whole worker, with all test cases scheduled to it. This metaclass
# makes all test cases convert Timeout exceptions into unittest friendly
# failure mode (self.fail).
class DietTestCase(base.BaseTestCase):
"""Same great taste, less filling.
BaseTestCase is responsible for doing lots of plugin-centric setup
that not all tests require (or can tolerate). This class provides
only functionality that is common across all tests.
"""
def setUp(self):
super(DietTestCase, self).setUp()
options.set_defaults(cfg.CONF, connection='sqlite://')
debugger = os.environ.get('OS_POST_MORTEM_DEBUGGER')
if debugger:
self.addOnException(post_mortem_debug.get_exception_handler(
debugger))
self.addCleanup(mock.patch.stopall)
self.addOnException(self.check_for_systemexit)
self.orig_pid = os.getpid()
def addOnException(self, handler):
def safe_handler(*args, **kwargs):
try:
return handler(*args, **kwargs)
except Exception:
with excutils.save_and_reraise_exception(reraise=False) as ctx:
self.addDetail('Failure in exception handler %s' % handler,
testtools.content.TracebackContent(
(ctx.type_, ctx.value, ctx.tb), self))
return super(DietTestCase, self).addOnException(safe_handler)
def check_for_systemexit(self, exc_info):
if isinstance(exc_info[1], SystemExit):
if os.getpid() != self.orig_pid:
# Subprocess - let it just exit
raise
# This makes sys.exit(0) still a failure
self.force_failure = True
@contextlib.contextmanager
def assert_max_execution_time(self, max_execution_time=5):
with eventlet.Timeout(max_execution_time, False):
yield
return
self.fail('Execution of this test timed out')
def assertOrderedEqual(self, expected, actual):
expect_val = self.sort_dict_lists(expected)
actual_val = self.sort_dict_lists(actual)
self.assertEqual(expect_val, actual_val)
def sort_dict_lists(self, dic):
for key, value in dic.items():
if isinstance(value, list):
dic[key] = sorted(value)
elif isinstance(value, dict):
dic[key] = self.sort_dict_lists(value)
return dic
def assertDictSupersetOf(self, expected_subset, actual_superset):
"""Checks that actual dict contains the expected dict.
After checking that the arguments are of the right type, this checks
that each item in expected_subset is in, and matches, what is in
actual_superset. Separate tests are done, so that detailed info can
be reported upon failure.
"""
if not isinstance(expected_subset, dict):
self.fail("expected_subset (%s) is not an instance of dict" %
type(expected_subset))
if not isinstance(actual_superset, dict):
self.fail("actual_superset (%s) is not an instance of dict" %
type(actual_superset))
for k, v in expected_subset.items():
self.assertIn(k, actual_superset)
self.assertEqual(v, actual_superset[k],
"Key %(key)s expected: %(exp)r, actual %(act)r" %
{'key': k, 'exp': v, 'act': actual_superset[k]})

View File

View File

@ -0,0 +1,123 @@
# Copyright (c) 2018 Huawei Technologies Co., Ltd
#
# 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.
from cyborg.tests import base
import mock
from cyborg.services import report as placement_client
from oslo_utils import uuidutils
from cyborg.common import exception as c_exc
from keystoneauth1 import exceptions as ks_exc
from oslo_config import cfg
class PlacementAPIClientTestCase(base.DietTestCase):
"""Test the Placement API client."""
def setUp(self):
super(PlacementAPIClientTestCase, self).setUp()
self.mock_load_auth_p = mock.patch(
'keystoneauth1.loading.load_auth_from_conf_options')
self.mock_load_auth = self.mock_load_auth_p.start()
self.mock_request_p = mock.patch(
'keystoneauth1.session.Session.request')
self.mock_request = self.mock_request_p.start()
self.client = placement_client.SchedulerReportClient()
@mock.patch('keystoneauth1.session.Session')
@mock.patch('keystoneauth1.loading.load_auth_from_conf_options')
def test_constructor(self, load_auth_mock, ks_sess_mock):
placement_client.SchedulerReportClient()
load_auth_mock.assert_called_once_with(cfg.CONF, 'placement')
ks_sess_mock.assert_called_once_with(auth=load_auth_mock.return_value,
cert=None,
timeout=None,
verify=True)
def test_create_resource_provider(self):
expected_payload = 'fake_resource_provider'
self.client.create_resource_provider(expected_payload)
e_filter = {'region_name': mock.ANY, 'service_type': 'placement'}
expected_url = '/resource_providers'
self.mock_request.assert_called_once_with(expected_url, 'POST',
endpoint_filter=e_filter,
json=expected_payload)
def test_delete_resource_provider(self):
rp_uuid = uuidutils.generate_uuid()
self.client.delete_resource_provider(rp_uuid)
e_filter = {'region_name': mock.ANY, 'service_type': 'placement'}
expected_url = '/resource_providers/%s' % rp_uuid
self.mock_request.assert_called_once_with(expected_url, 'DELETE',
endpoint_filter=e_filter)
def test_create_inventory(self):
expected_payload = 'fake_inventory'
rp_uuid = uuidutils.generate_uuid()
e_filter = {'region_name': mock.ANY, 'service_type': 'placement'}
self.client.create_inventory(rp_uuid, expected_payload)
expected_url = '/resource_providers/%s/inventories' % rp_uuid
self.mock_request.assert_called_once_with(expected_url, 'POST',
endpoint_filter=e_filter,
json=expected_payload)
def test_get_inventory(self):
rp_uuid = uuidutils.generate_uuid()
e_filter = {'region_name': mock.ANY, 'service_type': 'placement'}
resource_class = 'fake_resource_class'
self.client.get_inventory(rp_uuid, resource_class)
expected_url = '/resource_providers/%s/inventories/%s' % (
rp_uuid, resource_class)
self.mock_request.assert_called_once_with(expected_url, 'GET',
endpoint_filter=e_filter)
def _test_get_inventory_not_found(self, details, expected_exception):
rp_uuid = uuidutils.generate_uuid()
resource_class = 'fake_resource_class'
self.mock_request.side_effect = ks_exc.NotFound(details=details)
self.assertRaises(expected_exception, self.client.get_inventory,
rp_uuid, resource_class)
def test_get_inventory_not_found_no_resource_provider(self):
self._test_get_inventory_not_found(
"No resource provider with uuid",
c_exc.PlacementResourceProviderNotFound)
def test_get_inventory_not_found_no_inventory(self):
self._test_get_inventory_not_found(
"No inventory of class", c_exc.PlacementInventoryNotFound)
def test_get_inventory_not_found_unknown_cause(self):
self._test_get_inventory_not_found("Unknown cause", ks_exc.NotFound)
def test_update_inventory(self):
expected_payload = 'fake_inventory'
rp_uuid = uuidutils.generate_uuid()
e_filter = {'region_name': mock.ANY, 'service_type': 'placement'}
resource_class = 'fake_resource_class'
self.client.update_inventory(rp_uuid, expected_payload, resource_class)
expected_url = '/resource_providers/%s/inventories/%s' % (
rp_uuid, resource_class)
self.mock_request.assert_called_once_with(expected_url, 'PUT',
endpoint_filter=e_filter,
json=expected_payload)
def test_update_inventory_conflict(self):
rp_uuid = uuidutils.generate_uuid()
expected_payload = 'fake_inventory'
resource_class = 'fake_resource_class'
self.mock_request.side_effect = ks_exc.Conflict
self.assertRaises(c_exc.PlacementInventoryUpdateConflict,
self.client.update_inventory, rp_uuid,
expected_payload, resource_class)