From 95b92b3dacfd93fa1649a5f87dafd2fa6553a086 Mon Sep 17 00:00:00 2001 From: Helena McGough Date: Sun, 1 Oct 2017 12:03:53 +0100 Subject: [PATCH] Updated Openstack plugin logging and tests - Logged Aodh/Gnocchi response messages - Included tests for the API calls for Gnocchi/Aodh Signed-off-by: Helena McGough --- .testr.conf | 25 +++++++++ plugins/OpenStack/Aodh/alarming.py | 4 ++ plugins/OpenStack/Gnocchi/metrics.py | 7 ++- plugins/OpenStack/common.py | 9 ++-- requirements.txt | 1 - setup.cfg | 48 +++++++++++++++++ setup.py | 3 +- test-requirements.txt | 29 ++++++++++ test/OpenStack/__init__.py | 22 ++++++++ test/OpenStack/test_common.py | 80 ++++++++++++++++++++++++++++ test/__init__.py | 22 ++++++++ tools/pretty_tox.sh | 29 ++++++++++ tox.ini | 60 +++++++++++++++++++++ 13 files changed, 330 insertions(+), 9 deletions(-) create mode 100644 .testr.conf create mode 100644 setup.cfg create mode 100644 test-requirements.txt create mode 100644 test/OpenStack/__init__.py create mode 100644 test/OpenStack/test_common.py create mode 100644 test/__init__.py create mode 100644 tools/pretty_tox.sh create mode 100644 tox.ini diff --git a/.testr.conf b/.testr.conf new file mode 100644 index 0000000..523e7db --- /dev/null +++ b/.testr.conf @@ -0,0 +1,25 @@ +# Copyright 2017 Intel Research and Development Ireland Limited +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Intel Corporation + +# 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. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: helena.mcgough@intel.com or adrian.hoban@intel.com +## +[DEFAULT] +test_command=python -m subunit.run discover . $LISTOPT $IDOPTION +test_id_option=--load-list $IDFILE +test_list_option=--list diff --git a/plugins/OpenStack/Aodh/alarming.py b/plugins/OpenStack/Aodh/alarming.py index 71ee743..d409d71 100644 --- a/plugins/OpenStack/Aodh/alarming.py +++ b/plugins/OpenStack/Aodh/alarming.py @@ -110,6 +110,7 @@ class Alarming(object): 'create_alarm_response', status=alarm_status, alarm_id=alarm_id, cor_id=alarm_details['correlation_id']) + log.info("Response Message: %s", resp_message) self._producer.create_alarm_response( 'create_alarm_resonse', resp_message, 'alarm_response') @@ -129,6 +130,7 @@ class Alarming(object): resp_message = self._response.generate_response( 'list_alarm_response', alarm_list=alarm_list, cor_id=list_details['correlation_id']) + log.info("Response Message: %s", resp_message) self._producer.list_alarm_response( 'list_alarm_response', resp_message, 'alarm_response') @@ -148,6 +150,7 @@ class Alarming(object): 'delete_alarm_response', alarm_id=alarm_id, status=resp_status, cor_id=request_details['correlation_id']) + log.info("Response message: %s", resp_message) self._producer.delete_alarm_response( 'delete_alarm_response', resp_message, 'alarm_response') @@ -180,6 +183,7 @@ class Alarming(object): 'update_alarm_response', alarm_id=alarm_id, cor_id=alarm_details['correlation_id'], status=status) + log.info("Response message: %s", resp_message) self._producer.update_alarm_response( 'update_alarm_response', resp_message, 'alarm_response') diff --git a/plugins/OpenStack/Gnocchi/metrics.py b/plugins/OpenStack/Gnocchi/metrics.py index 453b83b..a1d58fd 100644 --- a/plugins/OpenStack/Gnocchi/metrics.py +++ b/plugins/OpenStack/Gnocchi/metrics.py @@ -102,6 +102,7 @@ class Metrics(object): 'create_metric_response', status=status, cor_id=values['correlation_id'], metric_id=metric_id, r_id=resource_id) + log.info("Response messages: %s", resp_message) self._producer.create_metrics_resp( 'create_metric_response', resp_message, 'metric_response') @@ -122,6 +123,7 @@ class Metrics(object): r_id=values['resource_uuid'], cor_id=values['correlation_id'], times=timestamps, metrics=metric_data) + log.info("Response message: %s", resp_message) self._producer.read_metric_data_response( 'read_metric_data_response', resp_message, 'metric_response') @@ -141,6 +143,7 @@ class Metrics(object): m_name=values['metric_name'], status=status, r_id=values['resource_uuid'], cor_id=values['correlation_id']) + log.info("Response message: %s", resp_message) self._producer.delete_metric_response( 'delete_metric_response', resp_message, 'metric_response') @@ -164,6 +167,7 @@ class Metrics(object): 'update_metric_response', status=False, cor_id=values['correlation_id'], r_id=resource_id, m_id=metric_id) + log.info("Response message: %s", resp_message) self._producer.update_metric_response( 'update_metric_response', resp_message, 'metric_response') @@ -181,6 +185,7 @@ class Metrics(object): resp_message = self._response.generate_response( 'list_metric_response', m_list=metric_list, cor_id=list_details['correlation_id']) + log.info("Response message: %s", resp_message) self._producer.list_metric_response( 'list_metric_response', resp_message, 'metric_response') @@ -374,7 +379,7 @@ class Metrics(object): diff = PERIOD_MS[collection_unit] else: diff = collection_period * PERIOD_MS[collection_unit] - s_time = (end_time - diff)/1000.0 + s_time = (end_time - diff) / 1000.0 start_time = datetime.datetime.fromtimestamp(s_time).strftime( '%Y-%m-%dT%H:%M:%S.%f') base_url = "{}/v1/metric/%(0)s/measures?start=%(1)s&stop=%(2)s" diff --git a/plugins/OpenStack/common.py b/plugins/OpenStack/common.py index fe72acc..c892a30 100644 --- a/plugins/OpenStack/common.py +++ b/plugins/OpenStack/common.py @@ -82,16 +82,11 @@ class Common(object): headers = {'X-Auth-Token': auth_token, 'Content-type': 'application/json'} # perform request and return its result - response = None try: if req_type == "put": response = requests.put( url, data=payload, headers=headers, timeout=1) - elif req_type == "post": - response = requests.post( - url, data=payload, headers=headers, - timeout=1) elif req_type == "get": response = requests.get( url, params=params, headers=headers, timeout=1) @@ -99,7 +94,9 @@ class Common(object): response = requests.delete( url, headers=headers, timeout=1) else: - log.warn("Invalid request type") + response = requests.post( + url, data=payload, headers=headers, + timeout=1) except Exception as e: log.warn("Exception thrown on request", e) diff --git a/requirements.txt b/requirements.txt index 772e8d8..35f7137 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,7 +26,6 @@ logutils cherrypy jsmin jsonschema -mysql_python python-openstackclient python-novaclient python-keystoneclient diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..56c44cd --- /dev/null +++ b/setup.cfg @@ -0,0 +1,48 @@ +# Copyright 2017 Intel Research and Development Ireland Limited +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Intel Corporation + +# 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. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: helena.mcgough@intel.com or adrian.hoban@intel.com +## + +[metadata] +name = MON +summary = Monitoring module for OSM. +description-file = + README.rst +author = OSM +home-page = https://osm.etsi.org/ +classifier = + Environment :: OSM + Intended Audience :: Information Technology + Intended Audience :: System Administrators + License :: ETSI Approved :: Apache Software License + Operating System :: POSIX :: Linux + Programming Language :: Python + Programming Language :: Python :: 2 + Programming Language :: Python :: 2.7 + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.5 + +[test] +test_suite=test + +[files] +packages = + Aodh + Gnocchi diff --git a/setup.py b/setup.py index 7b810ef..01f6dea 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ _license = 'Apache 2.0' _copyright = 'Intel Research and Development Ireland' _url = 'https://osm.etsi.org/gitweb/?p=osm/MON.git;a=tree' _requirements = [ - "MySQL-python", + "MySQL-python", "requests", "logutils", "cherrypy", @@ -57,6 +57,7 @@ _requirements = [ "pyopenssl", "cherrypy", "bottle", + "six", ] setup(name="mon_core", diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..0873ca4 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1,29 @@ +# Copyright 2017 Intel Research and Development Ireland Limited +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Intel Corporation + +# 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. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: helena.mcgough@intel.com or adrian.hoban@intel.com +## + +flake8 +mock +os-testr +testrepository +testscenarios +testtools +kafka diff --git a/test/OpenStack/__init__.py b/test/OpenStack/__init__.py new file mode 100644 index 0000000..d25e458 --- /dev/null +++ b/test/OpenStack/__init__.py @@ -0,0 +1,22 @@ +# Copyright 2017 Intel Research and Development Ireland Limited +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Intel Corporation + +# 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. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: helena.mcgough@intel.com or adrian.hoban@intel.com +## + diff --git a/test/OpenStack/test_common.py b/test/OpenStack/test_common.py new file mode 100644 index 0000000..29e9558 --- /dev/null +++ b/test/OpenStack/test_common.py @@ -0,0 +1,80 @@ +# Copyright 2017 Intel Research and Development Ireland Limited +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Intel Corporation + +# 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. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: helena.mcgough@intel.com or adrian.hoban@intel.com +## +"""Tests for all common OpenStack methods.""" + +import unittest + +import mock + +from plugins.OpenStack.common import Common + +import requests + + +class TestCommon(unittest.TestCase): + """Test the common class for OpenStack plugins.""" + + def setUp(self): + """Test Setup.""" + super(TestCommon, self).setUp() + self.common = Common() + + @mock.patch.object(requests, 'post') + def test_post_req(self, post): + """Testing a post request.""" + self.common._perform_request("url", "auth_token", req_type="post", + payload="payload") + + post.assert_called_with("url", data="payload", headers=mock.ANY, + timeout=mock.ANY) + + @mock.patch.object(requests, 'get') + def test_get_req(self, get): + """Testing a get request.""" + # Run the defualt get request without any parameters + self.common._perform_request("url", "auth_token", req_type="get") + + get.assert_called_with("url", params=None, headers=mock.ANY, + timeout=mock.ANY) + + # Test with some parameters specified + get.reset_mock() + self.common._perform_request("url", "auth_token", req_type="get", + params="some parameters") + + get.assert_called_with("url", params="some parameters", + headers=mock.ANY, timeout=mock.ANY) + + @mock.patch.object(requests, 'put') + def test_put_req(self, put): + """Testing a put request.""" + self.common._perform_request("url", "auth_token", req_type="put", + payload="payload") + put.assert_called_with("url", data="payload", headers=mock.ANY, + timeout=mock.ANY) + + @mock.patch.object(requests, 'delete') + def test_delete_req(self, delete): + """Testing a delete request.""" + self.common._perform_request("url", "auth_token", req_type="delete") + + delete.assert_called_with("url", headers=mock.ANY, timeout=mock.ANY) diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..d25e458 --- /dev/null +++ b/test/__init__.py @@ -0,0 +1,22 @@ +# Copyright 2017 Intel Research and Development Ireland Limited +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Intel Corporation + +# 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. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: helena.mcgough@intel.com or adrian.hoban@intel.com +## + diff --git a/tools/pretty_tox.sh b/tools/pretty_tox.sh new file mode 100644 index 0000000..3899b58 --- /dev/null +++ b/tools/pretty_tox.sh @@ -0,0 +1,29 @@ +# Copyright 2017 Intel Research and Development Ireland Limited +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Intel Corporation + +# 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. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: helena.mcgough@intel.com or adrian.hoban@intel.com +## + +#! /bin/sh + +TESTRARGS=$1 + +exec 3>&1 +status=$(exec 4>&1 >&3; ( python setup.py testr --slowest --testr-args="--subunit $TESTRARGS"; echo $? >&4 ) | subunit-trace -f) && exit $status + diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..17e4fab --- /dev/null +++ b/tox.ini @@ -0,0 +1,60 @@ +# Copyright 2017 Intel Research and Development Ireland Limited +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Intel Corporation + +# 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. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: helena.mcgough@intel.com or adrian.hoban@intel.com +## + +# Tox (http://tox.testrun.org/) is a tool for running tests +# in multiple virtualenvs. This configuration file will run the +# test suite on all supported python versions. To use it, "pip install tox" +# and then run "tox" from this directory. + +[tox] +minversion = 1.6 +envlist = py27 +skipsdist = True + +[testenv] +usedevelop = True +install_command = pip install -r requirements.txt -U {opts} {packages} +commands = sh tools/pretty_tox.sh '{posargs}' +deps = -r{toxinidir}/test-requirements.txt +whitelist_externals = sh +setenv = + VIRTUAL_ENV={envdir} + +[testenv:pep8] +commands = flake8 test + +[testenv:venv] +commands = {posargs} + +[testenv:cover] +commands = python setup.py test --coverage + +[pep8] +max-line-length = 80 + +[flake8] +# E123, E125 skipped as they are invalid PEP-8. +max-line-length = 80 +show-source = True +ignore = E123,E125,E241 +builtins = _ +exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build,core/*,devops_stages/*,plugins/CloudWatch/*, plugins/vRealiseOps/*,.rst -- 2.25.1