From 45d4099c1d60dc18cc4332625372db67443daac2 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Nov 22 2018 12:39:48 +0000 Subject: Use pytest-cov for coverage testing, set minimal coverage percentage to 80%. And also fix the warnings found by pytest in both python2 and python3. --- diff --git a/server/odcs/server/auth.py b/server/odcs/server/auth.py index 2929bfb..5c18f12 100644 --- a/server/odcs/server/auth.py +++ b/server/odcs/server/auth.py @@ -223,7 +223,7 @@ def init_auth(login_manager, backend): if backend == 'noauth': # Do not enable any authentication backend working with frontend # authentication module in Apache. - log.warn("Authorization is disabled in ODCS configuration.") + log.warning("Authorization is disabled in ODCS configuration.") return if backend == 'kerberos': _validate_kerberos_config() diff --git a/server/odcs/server/backend.py b/server/odcs/server/backend.py index 701d8c3..05435c8 100644 --- a/server/odcs/server/backend.py +++ b/server/odcs/server/backend.py @@ -141,8 +141,8 @@ class RemoveExpiredComposesThread(BackendThread): # Be nice and don't fail when directory does not exist. if not os.path.exists(toplevel_dir): - log.warn("Cannot remove directory %s, it does not exist", - toplevel_dir) + log.warning("Cannot remove directory %s, it does not exist", + toplevel_dir) return # If toplevel_dir is a symlink, remove the symlink and diff --git a/server/odcs/server/mbs.py b/server/odcs/server/mbs.py index 3cd4dde..e4a177d 100644 --- a/server/odcs/server/mbs.py +++ b/server/odcs/server/mbs.py @@ -99,7 +99,7 @@ class MBS(object): # But otherwise get the latest module in this name:stream from MBS # and add it to new_modules/module_map. for deps in mmd.get_dependencies(): - for name, streams in deps.get_requires().items(): + for name, streams in deps.peek_requires().items(): for stream in streams.get(): key = "%s:%s" % (name, stream) if key not in module_map: diff --git a/server/odcs/server/views.py b/server/odcs/server/views.py index 83a6eab..92256b4 100644 --- a/server/odcs/server/views.py +++ b/server/odcs/server/views.py @@ -84,8 +84,9 @@ api_v1 = { class ODCSAPI(MethodView): def _get_compose_owner(self): if conf.auth_backend == "noauth": - log.warn("Cannot determine the owner of compose, because " - "'noauth' auth_backend is used.") + log.warning( + "Cannot determine the owner of compose, because " + "'noauth' auth_backend is used.") return "unknown" else: return g.user.username diff --git a/server/tests/test_backend.py b/server/tests/test_backend.py index c0b6803..bd49d04 100644 --- a/server/tests/test_backend.py +++ b/server/tests/test_backend.py @@ -20,6 +20,7 @@ # # Written by Jan Kaluza +import six import os import shutil @@ -100,7 +101,7 @@ class TestBackend(ModelsBaseTest): flags=flags) db.session.commit() - with self.assertRaisesRegexp(ModuleLookupError, match): + with six.assertRaisesRegex(self, ModuleLookupError, match): resolve_compose(c) @mock_mbs(1) @@ -145,7 +146,7 @@ class TestBackend(ModelsBaseTest): flags=flags) db.session.commit() - with self.assertRaisesRegexp(ModuleLookupError, match): + with six.assertRaisesRegex(self, ModuleLookupError, match): resolve_compose(c) def test_resolve_compose_module_not_found(self): @@ -228,7 +229,7 @@ class TestBackend(ModelsBaseTest): koji_session = MagicMock() koji_session.getTag.return_value = None - with self.assertRaisesRegexp(ValueError, 'Unknown Koji tag foo.'): + with six.assertRaisesRegex(self, ValueError, 'Unknown Koji tag foo.'): koji_get_inherited_tags(koji_session, "foo") @patch("odcs.server.backend.koji_get_inherited_tags") @@ -528,7 +529,7 @@ gpgcheck=0 c1 = Compose.query.filter(Compose.id == 1).one() self.assertEqual(c1.state, COMPOSE_STATES["failed"]) - self.assertRegexpMatches(c1.state_reason, r'Error while generating compose: Failed to find all the content_sets.*') + six.assertRegex(self, c1.state_reason, r'Error while generating compose: Failed to find all the content_sets.*') @patch("odcs.server.backend.resolve_compose") @patch("odcs.server.backend.generate_pungi_compose") @@ -550,8 +551,8 @@ gpgcheck=0 c1 = Compose.query.filter(Compose.id == 1).one() self.assertEqual(c1.state, COMPOSE_STATES["failed"]) - self.assertRegexpMatches( - c1.state_reason, + six.assertRegex( + self, c1.state_reason, r'Error while generating compose: Expected exception\n' 'Compose failed for unknown reason*') @@ -573,8 +574,8 @@ gpgcheck=0 c1 = Compose.query.filter(Compose.id == 1).one() self.assertEqual(c1.state, COMPOSE_STATES["failed"]) - self.assertRegexpMatches( - c1.state_reason, + six.assertRegex( + self, c1.state_reason, r'Error while generating compose: Expected exception*') @patch('odcs.server.backend.tag_changed', return_value=True) @@ -907,7 +908,7 @@ class TestValidatePungiCompose(ModelsBaseTest): super(TestValidatePungiCompose, self).tearDown() def test_missing_packages(self): - with self.assertRaisesRegexp(RuntimeError, 'not present.+pkg3'): + with six.assertRaisesRegex(self, RuntimeError, 'not present.+pkg3'): validate_pungi_compose(self.c) def test_all_packages_are_included(self): diff --git a/server/tests/test_consumer.py b/server/tests/test_consumer.py index f3e1125..d8ea642 100644 --- a/server/tests/test_consumer.py +++ b/server/tests/test_consumer.py @@ -18,7 +18,6 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -import fedmsg.config import mock from .utils import ModelsBaseTest, ConfigPatcher @@ -33,8 +32,9 @@ class ConsumerBaseTest(ModelsBaseTest): def _create_consumer(self): hub = mock.MagicMock() - hub.config = fedmsg.config.load_config() + hub.config = {} hub.config['odcsconsumer'] = True + hub.config['validate_signatures'] = False return ODCSConsumer(hub) def _compose_state_change_msg(self, id=1, state=None): diff --git a/server/tests/test_utils.py b/server/tests/test_utils.py index 6910265..587bbb3 100644 --- a/server/tests/test_utils.py +++ b/server/tests/test_utils.py @@ -20,6 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import six import unittest from mock import patch @@ -38,8 +39,8 @@ class TestUtilsExecuteCmd(unittest.TestCase): def test_execute_cmd_timeout_called(self): start_time = time.time() - with self.assertRaisesRegexp( - RuntimeError, 'Compose has taken more time.*'): + with six.assertRaisesRegex( + self, RuntimeError, 'Compose has taken more time.*'): execute_cmd(["/usr/bin/sleep", "5"], timeout=1) stop_time = time.time() diff --git a/server/tests/test_views.py b/server/tests/test_views.py index 54f5266..2f14f2e 100644 --- a/server/tests/test_views.py +++ b/server/tests/test_views.py @@ -20,6 +20,7 @@ # # Written by Jan Kaluza +import six import contextlib import json @@ -830,8 +831,8 @@ class TestViews(ViewBaseTest): self.assertEqual(resp.status, '400 BAD REQUEST') self.assertEqual(data['status'], 400) - self.assertRegexpMatches(data['message'], - r"Compose \(id=%s\) can not be removed, its state need to be in .*." % new_c.id) + six.assertRegex(self, data['message'], + r"Compose \(id=%s\) can not be removed, its state need to be in .*." % new_c.id) self.assertEqual(data['error'], 'Bad Request') def test_delete_non_exist_compose(self): @@ -911,8 +912,8 @@ class TestViews(ViewBaseTest): self.assertEqual(resp.status, '202 ACCEPTED') self.assertEqual(resp.status_code, 202) self.assertEqual(data['status'], 202) - self.assertRegexpMatches(data['message'], - r"The delete request for compose \(id=%s\) has been accepted and will be processed by backend later." % c3.id) + six.assertRegex(self, data['message'], + r"The delete request for compose \(id=%s\) has been accepted and will be processed by backend later." % c3.id) @patch.object(odcs.server.config.Config, 'max_seconds_to_live', new_callable=PropertyMock) @patch.object(odcs.server.config.Config, 'seconds_to_live', new_callable=PropertyMock) diff --git a/test-requirements.txt b/test-requirements.txt index 1137b1e..51b709a 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,4 +1,5 @@ pytest +pytest-cov responses tox freezegun diff --git a/tox.ini b/tox.ini index 8b98d35..3ccd869 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ # and then run "tox" from this directory. [tox] -envlist = py27, py3, coverage, flake8, bandit +envlist = py27, py3, flake8, bandit [testenv] # using sitepackages is not a good idea, but Koji... :( @@ -13,21 +13,13 @@ install_command = pip install --force-reinstall --ignore-installed {packages} deps = -r{toxinidir}/test-requirements.txt commands = pip install .[server] - py.test {posargs} + py.test \ + -W "ignore:inspect.getargspec:DeprecationWarning" \ + {posargs} setenv = PYTHONPATH = {toxinidir}/common:{toxinidir}/server:{toxinidir}/client ODCS_DEVELOPER_ENV=1 -[testenv:coverage] -basepython = python3 -deps = - {[testenv]deps} - coverage -commands = - coverage run --parallel-mode -m pytest - coverage combine - coverage report --omit=tests/*,.tox/*,/usr/* -m --skip-covered - [testenv:flake8] basepython = python3 skip_install = true @@ -41,3 +33,16 @@ deps = bandit commands = /bin/bash -c "bandit -r -ll $(find . -mindepth 1 -maxdepth 1 ! -name tests ! -name \.\* -type d -o -name \*.py)" ignore_outcome = True + +[pytest] +addopts = --cov=odcs + +[coverage:report] +skip_covered = 1 +show_missing = 1 +fail_under = 80 +omit = + .tox + .env + tests/* + /usr/*