Updated Openstack plugin logging and tests 80/5480/1
authorHelena McGough <helena.mcgough@intel.com>
Sun, 1 Oct 2017 11:03:53 +0000 (12:03 +0100)
committerHelena McGough <helena.mcgough@intel.com>
Sun, 1 Oct 2017 14:05:13 +0000 (15:05 +0100)
 - Logged Aodh/Gnocchi response messages
 - Included tests for the API calls for Gnocchi/Aodh

Signed-off-by: Helena McGough <helena.mcgough@intel.com>
13 files changed:
.testr.conf [new file with mode: 0644]
plugins/OpenStack/Aodh/alarming.py
plugins/OpenStack/Gnocchi/metrics.py
plugins/OpenStack/common.py
requirements.txt
setup.cfg [new file with mode: 0644]
setup.py
test-requirements.txt [new file with mode: 0644]
test/OpenStack/__init__.py [new file with mode: 0644]
test/OpenStack/test_common.py [new file with mode: 0644]
test/__init__.py [new file with mode: 0644]
tools/pretty_tox.sh [new file with mode: 0644]
tox.ini [new file with mode: 0644]

diff --git a/.testr.conf b/.testr.conf
new file mode 100644 (file)
index 0000000..523e7db
--- /dev/null
@@ -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
index 71ee743..d409d71 100644 (file)
@@ -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')
index 453b83b..a1d58fd 100644 (file)
@@ -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"
index fe72acc..c892a30 100644 (file)
@@ -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)
index 772e8d8..35f7137 100644 (file)
@@ -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 (file)
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
index 7b810ef..01f6dea 100644 (file)
--- 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 (file)
index 0000000..0873ca4
--- /dev/null
@@ -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 (file)
index 0000000..d25e458
--- /dev/null
@@ -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 (file)
index 0000000..29e9558
--- /dev/null
@@ -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 (file)
index 0000000..d25e458
--- /dev/null
@@ -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 (file)
index 0000000..3899b58
--- /dev/null
@@ -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 (file)
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