The OpenStackClients class provides a convenient way to create and cache client instances. The idea behind this code comes from Magnum [0]. The OpenStackClients class will act as the manager of other project's clients, providing an easy way to fetch instances of said clients. This will allow the clients to be cached. An instance of OpenStackClients is created for every call that comes into the decision engine and the applier, using the request context to pass needed (domain id) parameters to get a Keystone session. This instance should be shared as much as possible to avoid additional unneccessary connections to the other services. This class will also allow for the version of each client to be configurable via the watcher.conf file. The method by which a Keystone session is also changed to use the keystoneauth1.loading library. In order to avoid DuplicateOptErrors with the keystone_authtoken group used for the keystonemiddleware in the API code, a new conf group named "watcher_clients_auth" is created. A typical configuration using a password authentication scheme will look like: [watcher_clients_auth] auth_type = password auth_url = http://<server-ip>:<port> username = <username> password = <password> project_domain_id = default user_domain_id = default [0]: https://github.com/openstack/magnum/blob/master/magnum/common/clients.py DocImpact Change-Id: Iab9d0b304099686da2e9e2b19e8b1de4332ff378 Implements: blueprint external-api-versioning Closes-Bug: #1530790 Closes-Bug: #1539670 Closes-Bug: #1522774
99 lines
3.7 KiB
Python
99 lines
3.7 KiB
Python
# -*- encoding: utf-8 -*-
|
|
# Copyright (c) 2015 b<>com
|
|
#
|
|
# 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 __future__ import absolute_import
|
|
from __future__ import unicode_literals
|
|
|
|
import mock
|
|
from oslo_config import cfg
|
|
|
|
from watcher.common import ceilometer_helper
|
|
from watcher.common import clients
|
|
from watcher.tests import base
|
|
|
|
CONF = cfg.CONF
|
|
|
|
|
|
@mock.patch.object(clients.OpenStackClients, 'ceilometer')
|
|
class TestCeilometerHelper(base.BaseTestCase):
|
|
|
|
def test_build_query(self, mock_ceilometer):
|
|
mock_ceilometer.return_value = mock.MagicMock()
|
|
cm = ceilometer_helper.CeilometerHelper()
|
|
expected = [{'field': 'user_id', 'op': 'eq', 'value': u'user_id'},
|
|
{'field': 'project_id', 'op': 'eq', 'value': u'tenant_id'},
|
|
{'field': 'resource_id', 'op': 'eq',
|
|
'value': u'resource_id'}]
|
|
|
|
query = cm.build_query(user_id="user_id",
|
|
tenant_id="tenant_id",
|
|
resource_id="resource_id",
|
|
user_ids=["user_ids"],
|
|
tenant_ids=["tenant_ids"],
|
|
resource_ids=["resource_ids"])
|
|
self.assertEqual(query, expected)
|
|
|
|
def test_statistic_aggregation(self, mock_ceilometer):
|
|
cm = ceilometer_helper.CeilometerHelper()
|
|
ceilometer = mock.MagicMock()
|
|
statistic = mock.MagicMock()
|
|
expected_result = 100
|
|
statistic[-1]._info = {'aggregate': {'avg': expected_result}}
|
|
ceilometer.statistics.list.return_value = statistic
|
|
mock_ceilometer.return_value = ceilometer
|
|
cm = ceilometer_helper.CeilometerHelper()
|
|
val = cm.statistic_aggregation(
|
|
resource_id="VM_ID",
|
|
meter_name="cpu_util",
|
|
period="7300"
|
|
)
|
|
self.assertEqual(val, expected_result)
|
|
|
|
def test_get_last_sample(self, mock_ceilometer):
|
|
ceilometer = mock.MagicMock()
|
|
statistic = mock.MagicMock()
|
|
expected_result = 100
|
|
statistic[-1]._info = {'counter_volume': expected_result}
|
|
ceilometer.samples.list.return_value = statistic
|
|
mock_ceilometer.return_value = ceilometer
|
|
cm = ceilometer_helper.CeilometerHelper()
|
|
val = cm.get_last_sample_value(
|
|
resource_id="id",
|
|
meter_name="compute.node.percent"
|
|
)
|
|
self.assertEqual(val, expected_result)
|
|
|
|
def test_get_last_sample_none(self, mock_ceilometer):
|
|
ceilometer = mock.MagicMock()
|
|
expected = []
|
|
ceilometer.samples.list.return_value = expected
|
|
mock_ceilometer.return_value = ceilometer
|
|
cm = ceilometer_helper.CeilometerHelper()
|
|
val = cm.get_last_sample_values(
|
|
resource_id="id",
|
|
meter_name="compute.node.percent"
|
|
)
|
|
self.assertEqual(val, expected)
|
|
|
|
def test_statistic_list(self, mock_ceilometer):
|
|
ceilometer = mock.MagicMock()
|
|
expected_value = []
|
|
ceilometer.statistics.list.return_value = expected_value
|
|
mock_ceilometer.return_value = ceilometer
|
|
cm = ceilometer_helper.CeilometerHelper()
|
|
val = cm.statistic_list(meter_name="cpu_util")
|
|
self.assertEqual(val, expected_value)
|