From 6eae6ea8db74c9e0775460c37953a4f452e8f260 Mon Sep 17 00:00:00 2001 From: Michael Rice Date: Wed, 31 May 2017 00:56:41 -0500 Subject: [PATCH] refactor testing directory layout This changes the layout of the tests. Before both unit and functional tests were in the same place. That made it so development really needed to happen on linux because of tests like uptime needing /proc/uptime This change puts those kind of tests into the int testing dir and adds a unit and integration test arg to tox. Change-Id: I922079e4a556a171aadd801a8cc932e1e08f9b5d Signed-off-by: Michael Rice --- tests/__init__.py | 21 -------------- tests/files/test-openstack.ini | 1 - tests/int/__init__.py | 13 +++++++++ tests/{ => int}/test_plugin_uptime.py | 0 tests/unit/__init__.py | 36 ++++++++++++++++++++++++ tests/unit/files/test-openstack.ini | 1 + tests/{ => unit}/test_cli.py | 0 tests/{ => unit}/test_formatters.py | 0 tests/{ => unit}/test_os_utils.py | 4 +-- tests/{ => unit}/test_plugin_kvm.py | 2 +- tests/{ => unit}/test_plugin_memcache.py | 2 +- tests/{ => unit}/test_plugin_os_block.py | 12 ++++---- tests/{ => unit}/test_plugin_os_vm.py | 33 ++++++++++++---------- tests/{ => unit}/test_plugin_process.py | 2 +- tests/{ => unit}/test_utils.py | 0 tox.ini | 11 ++++++-- 16 files changed, 89 insertions(+), 49 deletions(-) delete mode 120000 tests/files/test-openstack.ini create mode 100644 tests/int/__init__.py rename tests/{ => int}/test_plugin_uptime.py (100%) create mode 100644 tests/unit/__init__.py create mode 120000 tests/unit/files/test-openstack.ini rename tests/{ => unit}/test_cli.py (100%) rename tests/{ => unit}/test_formatters.py (100%) rename tests/{ => unit}/test_os_utils.py (98%) rename tests/{ => unit}/test_plugin_kvm.py (98%) rename tests/{ => unit}/test_plugin_memcache.py (99%) rename tests/{ => unit}/test_plugin_os_block.py (94%) rename tests/{ => unit}/test_plugin_os_vm.py (92%) rename tests/{ => unit}/test_plugin_process.py (98%) rename tests/{ => unit}/test_utils.py (100%) diff --git a/tests/__init__.py b/tests/__init__.py index 211b865..cd921de 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # Copyright 2017, Major Hayden # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,27 +14,12 @@ """This an __init__.py.""" import json -import os from click.testing import CliRunner -from monitorstack import utils from monitorstack.cli import cli -def read_config(): - """Load the test config file. - - :returns: dict - """ - os_config_file = os.path.expanduser( - os.path.abspath( - os.path.dirname(__file__) + '/files/test-openstack.ini' - ) - ) - return utils.read_config(os_config_file) - - def runner(module, extra_args=None): """Run click CLI tests. @@ -57,8 +41,3 @@ def runner(module, extra_args=None): return json.loads(result.output) except ValueError: return result - - -def fake_version_info(major, minor, serial): - """Return tuple for fake python version info.""" - return major, minor, serial diff --git a/tests/files/test-openstack.ini b/tests/files/test-openstack.ini deleted file mode 120000 index 9ae3539..0000000 --- a/tests/files/test-openstack.ini +++ /dev/null @@ -1 +0,0 @@ -../../etc/openstack.ini \ No newline at end of file diff --git a/tests/int/__init__.py b/tests/int/__init__.py new file mode 100644 index 0000000..f10eadf --- /dev/null +++ b/tests/int/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2017, Michael Rice +# +# 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. diff --git a/tests/test_plugin_uptime.py b/tests/int/test_plugin_uptime.py similarity index 100% rename from tests/test_plugin_uptime.py rename to tests/int/test_plugin_uptime.py diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..3f13631 --- /dev/null +++ b/tests/unit/__init__.py @@ -0,0 +1,36 @@ +# Copyright 2017, Major Hayden +# +# 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. +"""This an __init__.py.""" + +import os + +from monitorstack import utils + + +def read_config(): + """Load the test config file. + + :returns: dict + """ + os_config_file = os.path.expanduser( + os.path.abspath( + os.path.dirname(__file__) + '/files/test-openstack.ini' + ) + ) + return utils.read_config(os_config_file) + + +def fake_version_info(major, minor, serial): + """Return tuple for fake python version info.""" + return major, minor, serial diff --git a/tests/unit/files/test-openstack.ini b/tests/unit/files/test-openstack.ini new file mode 120000 index 0000000..339bbfa --- /dev/null +++ b/tests/unit/files/test-openstack.ini @@ -0,0 +1 @@ +../../../etc/openstack.ini \ No newline at end of file diff --git a/tests/test_cli.py b/tests/unit/test_cli.py similarity index 100% rename from tests/test_cli.py rename to tests/unit/test_cli.py diff --git a/tests/test_formatters.py b/tests/unit/test_formatters.py similarity index 100% rename from tests/test_formatters.py rename to tests/unit/test_formatters.py diff --git a/tests/test_os_utils.py b/tests/unit/test_os_utils.py similarity index 98% rename from tests/test_os_utils.py rename to tests/unit/test_os_utils.py index 41b2695..586cd20 100644 --- a/tests/test_os_utils.py +++ b/tests/unit/test_os_utils.py @@ -135,7 +135,7 @@ class TestOSUtilsConnection(unittest.TestCase): """Test the OpenStack connection interface.""" # load the base class for these tests. self.osu = os_utils.OpenStack( - os_auth_args=tests.read_config()['keystone'] + os_auth_args=tests.unit.read_config()['keystone'] ) self.assertTrue( isinstance( @@ -152,7 +152,7 @@ class TestOsUtils(unittest.TestCase): """Setup the test.""" # load the base class for these tests. self.osu = os_utils.OpenStack( - os_auth_args=tests.read_config()['keystone'] + os_auth_args=tests.unit.read_config()['keystone'] ) def tearDown(self): diff --git a/tests/test_plugin_kvm.py b/tests/unit/test_plugin_kvm.py similarity index 98% rename from tests/test_plugin_kvm.py rename to tests/unit/test_plugin_kvm.py index 9abcc75..277e1df 100644 --- a/tests/test_plugin_kvm.py +++ b/tests/unit/test_plugin_kvm.py @@ -21,7 +21,7 @@ from click.testing import CliRunner from monitorstack.cli import cli -import tests # Import the test base module +import tests.unit # Import the test base module def _runner(module): diff --git a/tests/test_plugin_memcache.py b/tests/unit/test_plugin_memcache.py similarity index 99% rename from tests/test_plugin_memcache.py rename to tests/unit/test_plugin_memcache.py index 1e3760b..5264109 100644 --- a/tests/test_plugin_memcache.py +++ b/tests/unit/test_plugin_memcache.py @@ -16,7 +16,7 @@ from monitorstack.plugins import memcache as monitorstack_memcache import pymemcache -import tests +import tests.unit class TestMemcache(object): diff --git a/tests/test_plugin_os_block.py b/tests/unit/test_plugin_os_block.py similarity index 94% rename from tests/test_plugin_os_block.py rename to tests/unit/test_plugin_os_block.py index 9ff3100..ab66947 100644 --- a/tests/test_plugin_os_block.py +++ b/tests/unit/test_plugin_os_block.py @@ -15,7 +15,9 @@ from monitorstack.utils.os_utils import OpenStack as Ost -import tests # Import the test base module +import tests.unit + +CONF_FILE = 'tests/unit/files/test-openstack.ini' def get_volume_pool_stats(*args, **kwargs): @@ -54,7 +56,7 @@ class TestOsBlock(object): 'os_block_pools_totals', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) variables = result['variables'] @@ -74,7 +76,7 @@ class TestOsBlock(object): 'os_block_pools_totals', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_block_pools_totals' @@ -91,7 +93,7 @@ class TestOsBlock(object): 'os_block_pools_usage', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) variables = result['variables'] @@ -113,7 +115,7 @@ class TestOsBlock(object): 'os_block_pools_usage', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_block_pools_usage' diff --git a/tests/test_plugin_os_vm.py b/tests/unit/test_plugin_os_vm.py similarity index 92% rename from tests/test_plugin_os_vm.py rename to tests/unit/test_plugin_os_vm.py index 174b234..51400f2 100644 --- a/tests/test_plugin_os_vm.py +++ b/tests/unit/test_plugin_os_vm.py @@ -15,7 +15,10 @@ from monitorstack.utils.os_utils import OpenStack as Ost -import tests # Import the test base module +import tests.unit # Import the test base module + + +CONF_FILE = 'tests/unit/files/test-openstack.ini' class MockProject(object): @@ -94,7 +97,7 @@ class TestOsVm(object): 'os_vm_quota_cores', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_quota_cores' @@ -106,7 +109,7 @@ class TestOsVm(object): 'os_vm_quota_cores', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_quota_cores' @@ -121,7 +124,7 @@ class TestOsVm(object): 'os_vm_quota_instance', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_quota_instance' @@ -133,7 +136,7 @@ class TestOsVm(object): 'os_vm_quota_instance', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_quota_instance' @@ -148,7 +151,7 @@ class TestOsVm(object): 'os_vm_quota_ram', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_quota_ram' @@ -160,7 +163,7 @@ class TestOsVm(object): 'os_vm_quota_ram', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_quota_ram' @@ -177,7 +180,7 @@ class TestOsVm(object): 'os_vm_used_cores', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_used_cores' @@ -190,7 +193,7 @@ class TestOsVm(object): 'os_vm_used_cores', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_used_cores' @@ -207,7 +210,7 @@ class TestOsVm(object): 'os_vm_used_disk', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_used_disk' @@ -220,7 +223,7 @@ class TestOsVm(object): 'os_vm_used_disk', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_used_disk' @@ -237,7 +240,7 @@ class TestOsVm(object): 'os_vm_used_instance', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_used_instance' @@ -250,7 +253,7 @@ class TestOsVm(object): 'os_vm_used_instance', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_used_instance' @@ -267,7 +270,7 @@ class TestOsVm(object): 'os_vm_used_ram', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_used_ram' @@ -281,7 +284,7 @@ class TestOsVm(object): 'os_vm_used_ram', extra_args=[ '--config-file', - 'tests/files/test-openstack.ini' + CONF_FILE ] ) assert result['measurement_name'] == 'os_vm_used_ram' diff --git a/tests/test_plugin_process.py b/tests/unit/test_plugin_process.py similarity index 98% rename from tests/test_plugin_process.py rename to tests/unit/test_plugin_process.py index 730be9e..cd6b521 100644 --- a/tests/test_plugin_process.py +++ b/tests/unit/test_plugin_process.py @@ -17,7 +17,7 @@ import mock from monitorstack.plugins import process -import tests # Import the test base module +import tests.unit # Import the test base module class TestUptime(object): diff --git a/tests/test_utils.py b/tests/unit/test_utils.py similarity index 100% rename from tests/test_utils.py rename to tests/unit/test_utils.py diff --git a/tox.ini b/tox.ini index 2034a40..1bd2345 100644 --- a/tox.ini +++ b/tox.ini @@ -19,6 +19,9 @@ whitelist_externals = bash commands = /usr/bin/find . -type f -name "*.pyc" -delete + +[testenv:functional] +commands = coverage run -m pytest --capture=no --strict {posargs} coverage report -m --omit="*/test*" @@ -47,9 +50,13 @@ deps = commands = {[testenv:pep8]commands} -[testenv:functional] +[testenv:integration] commands = - {[testenv]commands} + coverage run -m pytest --capture=no --strict "tests/int" + +[testenv:unit] +commands = + coverage run -m pytest --capture=no --strict "tests/unit" [testenv:pep8] deps =