From: Sebastian Wagner Date: Tue, 12 Nov 2019 10:48:54 +0000 (+0100) Subject: mgr/orchestrator: remove tox and move test to parent dir X-Git-Tag: v15.1.0~907^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d6c10192c61a59f6a77d04a9d601c9df2f72d07f;p=ceph.git mgr/orchestrator: remove tox and move test to parent dir Signed-off-by: Sebastian Wagner --- diff --git a/src/pybind/mgr/CMakeLists.txt b/src/pybind/mgr/CMakeLists.txt index ead8453b3ec..f46a00a06d8 100644 --- a/src/pybind/mgr/CMakeLists.txt +++ b/src/pybind/mgr/CMakeLists.txt @@ -3,7 +3,6 @@ if(WITH_MGR_DASHBOARD_FRONTEND) endif() add_subdirectory(insights) add_subdirectory(ansible) -add_subdirectory(orchestrator_cli) if(WITH_TESTS) include(AddCephTest) diff --git a/src/pybind/mgr/ansible/__init__.py b/src/pybind/mgr/ansible/__init__.py index ea61a12fd7e..6dab577a502 100644 --- a/src/pybind/mgr/ansible/__init__.py +++ b/src/pybind/mgr/ansible/__init__.py @@ -1,9 +1,7 @@ from __future__ import absolute_import + import os +if 'UNITTEST' in os.environ: + import tests -if 'UNITTEST' not in os.environ: - from .module import Module -else: - import sys - import mock - sys.modules['ceph_module'] = mock.Mock() +from .module import Module diff --git a/src/pybind/mgr/dashboard/__init__.py b/src/pybind/mgr/dashboard/__init__.py index a1affa2ef58..eae1c27cde7 100644 --- a/src/pybind/mgr/dashboard/__init__.py +++ b/src/pybind/mgr/dashboard/__init__.py @@ -45,15 +45,7 @@ else: os.environ['PATH'] = '{}:{}'.format(os.path.abspath('../../../../build/bin'), os.environ['PATH']) - # Mock ceph module otherwise every module that is involved in a testcase and imports it will - # raise an ImportError - import sys - try: - import mock - except ImportError: - import unittest.mock as mock - - sys.modules['ceph_module'] = mock.Mock() + from tests import mock mgr = mock.Mock() mgr.get_frontend_path.side_effect = lambda: os.path.abspath("./frontend/dist") diff --git a/src/pybind/mgr/orchestrator_cli/CMakeLists.txt b/src/pybind/mgr/orchestrator_cli/CMakeLists.txt deleted file mode 100644 index 68c3346611a..00000000000 --- a/src/pybind/mgr/orchestrator_cli/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -if(WITH_TESTS) - include(AddCephTest) - add_tox_test(mgr-orchestrator_cli) -endif() diff --git a/src/pybind/mgr/orchestrator_cli/requirements.txt b/src/pybind/mgr/orchestrator_cli/requirements.txt deleted file mode 100644 index 2585d05fc1b..00000000000 --- a/src/pybind/mgr/orchestrator_cli/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -tox==2.9.1 -../../../python-common -pytest -mock -requests-mock diff --git a/src/pybind/mgr/orchestrator_cli/test_orchestrator.py b/src/pybind/mgr/orchestrator_cli/test_orchestrator.py deleted file mode 100644 index de50acb0722..00000000000 --- a/src/pybind/mgr/orchestrator_cli/test_orchestrator.py +++ /dev/null @@ -1,79 +0,0 @@ -from __future__ import absolute_import -import json - -import pytest - -from ceph.deployment import inventory -from orchestrator import ReadCompletion, raise_if_exception, RGWSpec -from orchestrator import InventoryNode, ServiceDescription -from orchestrator import OrchestratorValidationError - - -def _test_resource(data, resource_class, extra=None): - # ensure we can deserialize and serialize - rsc = resource_class.from_json(data) - rsc.to_json() - - if extra: - # if there is an unexpected data provided - data.update(extra) - with pytest.raises(OrchestratorValidationError): - resource_class.from_json(data) - - -def test_inventory(): - json_data = { - 'name': 'host0', - 'devices': [ - { - 'sys_api': { - 'rotational': '1', - 'size': 1024, - }, - 'path': '/dev/sda', - 'available': False, - 'rejected_reasons': [], - 'lvs': [] - } - ] - } - _test_resource(json_data, InventoryNode, {'abc': False}) - for devices in json_data['devices']: - _test_resource(devices, inventory.Device) - - json_data = [{}, {'name': 'host0'}, {'devices': []}] - for data in json_data: - with pytest.raises(OrchestratorValidationError): - InventoryNode.from_json(data) - - -def test_service_description(): - json_data = { - 'nodename': 'test', - 'service_type': 'mon', - 'service_instance': 'a' - } - _test_resource(json_data, ServiceDescription, {'abc': False}) - - -def test_raise(): - c = ReadCompletion() - c.exception = ZeroDivisionError() - with pytest.raises(ZeroDivisionError): - raise_if_exception(c) - - -def test_rgwspec(): - """ - { - "rgw_zone": "zonename", - "rgw_frontend_port": 8080, - "rgw_zonegroup": "group", - "rgw_zone_user": "user", - "rgw_realm": "realm", - "count": 3 - } - """ - example = json.loads(test_rgwspec.__doc__.strip()) - spec = RGWSpec.from_json(example) - assert spec.validate_add() is None diff --git a/src/pybind/mgr/requirements.txt b/src/pybind/mgr/requirements.txt index 86a816f125f..103aee73320 100644 --- a/src/pybind/mgr/requirements.txt +++ b/src/pybind/mgr/requirements.txt @@ -1 +1,3 @@ pytest-cov==2.7.1 +mock; python_version <= '3.3' +../../python-common diff --git a/src/pybind/mgr/tests/__init__.py b/src/pybind/mgr/tests/__init__.py new file mode 100644 index 00000000000..666875d0aec --- /dev/null +++ b/src/pybind/mgr/tests/__init__.py @@ -0,0 +1,34 @@ +from __future__ import absolute_import + + +import os + +if 'UNITTEST' in os.environ: + + # Mock ceph_module. Otherwise every module that is involved in a testcase and imports it will + # raise an ImportError + + import sys + + try: + from unittest import mock + except ImportError: + import mock + + class M(object): + def __init__(self, *args): + super(M, self).__init__() + self._ceph_get_version = mock.Mock() + self._ceph_get = mock.MagicMock() + self._ceph_get_module_option = mock.MagicMock() + self._ceph_log = mock.MagicMock() + self._ceph_get_option = mock.MagicMock() + self._ceph_get_store = lambda _: '' + self._ceph_get_store_prefix = lambda _: {} + + + cm = mock.Mock() + cm.BaseMgrModule = M + cm.BaseMgrStandbyModule = M + sys.modules['ceph_module'] = cm + sys.modules['rados'] = mock.Mock() diff --git a/src/pybind/mgr/tests/test_orchestrator.py b/src/pybind/mgr/tests/test_orchestrator.py new file mode 100644 index 00000000000..de50acb0722 --- /dev/null +++ b/src/pybind/mgr/tests/test_orchestrator.py @@ -0,0 +1,79 @@ +from __future__ import absolute_import +import json + +import pytest + +from ceph.deployment import inventory +from orchestrator import ReadCompletion, raise_if_exception, RGWSpec +from orchestrator import InventoryNode, ServiceDescription +from orchestrator import OrchestratorValidationError + + +def _test_resource(data, resource_class, extra=None): + # ensure we can deserialize and serialize + rsc = resource_class.from_json(data) + rsc.to_json() + + if extra: + # if there is an unexpected data provided + data.update(extra) + with pytest.raises(OrchestratorValidationError): + resource_class.from_json(data) + + +def test_inventory(): + json_data = { + 'name': 'host0', + 'devices': [ + { + 'sys_api': { + 'rotational': '1', + 'size': 1024, + }, + 'path': '/dev/sda', + 'available': False, + 'rejected_reasons': [], + 'lvs': [] + } + ] + } + _test_resource(json_data, InventoryNode, {'abc': False}) + for devices in json_data['devices']: + _test_resource(devices, inventory.Device) + + json_data = [{}, {'name': 'host0'}, {'devices': []}] + for data in json_data: + with pytest.raises(OrchestratorValidationError): + InventoryNode.from_json(data) + + +def test_service_description(): + json_data = { + 'nodename': 'test', + 'service_type': 'mon', + 'service_instance': 'a' + } + _test_resource(json_data, ServiceDescription, {'abc': False}) + + +def test_raise(): + c = ReadCompletion() + c.exception = ZeroDivisionError() + with pytest.raises(ZeroDivisionError): + raise_if_exception(c) + + +def test_rgwspec(): + """ + { + "rgw_zone": "zonename", + "rgw_frontend_port": 8080, + "rgw_zonegroup": "group", + "rgw_zone_user": "user", + "rgw_realm": "realm", + "count": 3 + } + """ + example = json.loads(test_rgwspec.__doc__.strip()) + spec = RGWSpec.from_json(example) + assert spec.validate_add() is None diff --git a/src/pybind/mgr/tox.ini b/src/pybind/mgr/tox.ini index a9e83708295..4205dce1345 100644 --- a/src/pybind/mgr/tox.ini +++ b/src/pybind/mgr/tox.ini @@ -3,5 +3,6 @@ envlist = py3 skipsdist = true [testenv] +setenv = UNITTEST = true deps = -rrequirements.txt -commands = pytest --cov --cov-append --cov-report=term --doctest-modules mgr_util.py {posargs} +commands = pytest --cov --cov-append --cov-report=term --doctest-modules {posargs:mgr_util.py tests/} \ No newline at end of file