From b41537fa69b470b5ade674955b75540bf33a3347 Mon Sep 17 00:00:00 2001 From: Jiri Podivin Date: Mon, 12 Apr 2021 11:50:17 +0200 Subject: [PATCH] Superfluous imports are now mocked Unless explicitly mocked, all dependencies of tested modules are imported by default. This can cause exceptions or even CI issues, especially if the required module is listed present in the test-requirements.txt file, but not in the spec file. Furthermore, unnecessary imports in tests were also removed. Signed-off-by: Jiri Podivin Change-Id: Ic3926c1fd38a670ed37eeba1aa4df64347a34a9b --- .../callback_plugins/test_fail_if_no_hosts.py | 14 ++++++----- .../tests/callback_plugins/test_http_json.py | 9 ++++--- .../callback_plugins/test_validation_json.py | 9 ++++--- .../test_validation_output.py | 11 +++++---- .../test_validation_stdout.py | 12 +++++++--- validations_common/tests/fakes.py | 24 +++++++++++++++++++ .../tests/library/test_advanced_format.py | 10 ++++++-- .../library/test_check_package_update.py | 12 ++++++---- .../tests/library/test_haproxy_conf.py | 10 ++++++-- .../tests/library/test_hiera.py | 10 ++++++-- .../tests/library/test_reportentry.py | 9 +++++-- .../library/test_validations_read_ini.py | 10 ++++++-- validations_common/tests/library/test_warn.py | 10 ++++++-- 13 files changed, 114 insertions(+), 36 deletions(-) create mode 100644 validations_common/tests/fakes.py diff --git a/validations_common/tests/callback_plugins/test_fail_if_no_hosts.py b/validations_common/tests/callback_plugins/test_fail_if_no_hosts.py index 2d29166..35046ff 100644 --- a/validations_common/tests/callback_plugins/test_fail_if_no_hosts.py +++ b/validations_common/tests/callback_plugins/test_fail_if_no_hosts.py @@ -19,16 +19,18 @@ test_fail_if_no_hosts Tests for `fail_if_no_hosts` callback plugin. """ +try: + from unittest import mock +except ImportError: + import mock +from validations_common.tests import base +from validations_common.tests import fakes import validations_common.library.reportentry as validation from validations_common.callback_plugins import fail_if_no_hosts -from validations_common.tests import base from ansible.plugins.callback import CallbackBase -from ansible.executor.stats import AggregateStats - -from unittest import mock class TestFailIfNoHosts(base.TestCase): @@ -64,7 +66,7 @@ class TestFailIfNoHosts(base.TestCase): the callback calls sys.exit. """ callback = fail_if_no_hosts.CallbackModule() - stats = AggregateStats() + stats = mock.MagicMock() callback.v2_playbook_on_stats(stats) mock_exit.assert_called_once_with(10) @@ -82,7 +84,7 @@ class TestFailIfNoHosts(base.TestCase): """ callback = fail_if_no_hosts.CallbackModule() - stats = AggregateStats() + stats = mock.MagicMock() stats.processed = { 'system_foo': 'foo', diff --git a/validations_common/tests/callback_plugins/test_http_json.py b/validations_common/tests/callback_plugins/test_http_json.py index f8e8153..ff22712 100644 --- a/validations_common/tests/callback_plugins/test_http_json.py +++ b/validations_common/tests/callback_plugins/test_http_json.py @@ -19,13 +19,16 @@ test_http_json Tests for `http_json` callback plugin. """ +try: + from unittest import mock +except ImportError: + import mock +from validations_common.tests import base +from validations_common.tests import fakes import validations_common.library.reportentry as validation from validations_common.callback_plugins import http_json -from validations_common.tests import base - -from unittest import mock class TestHttpJson(base.TestCase): diff --git a/validations_common/tests/callback_plugins/test_validation_json.py b/validations_common/tests/callback_plugins/test_validation_json.py index 181e180..6e99db6 100644 --- a/validations_common/tests/callback_plugins/test_validation_json.py +++ b/validations_common/tests/callback_plugins/test_validation_json.py @@ -19,12 +19,15 @@ test_validation_json Tests for `validation_json` callback plugin. """ +try: + from unittest import mock +except ImportError: + import mock +from validations_common.tests import base +from validations_common.tests import fakes import validations_common.library.reportentry as validation -from validations_common.tests import base - -from unittest import mock class TestValidationJson(base.TestCase): diff --git a/validations_common/tests/callback_plugins/test_validation_output.py b/validations_common/tests/callback_plugins/test_validation_output.py index 83d1980..4323bf0 100644 --- a/validations_common/tests/callback_plugins/test_validation_output.py +++ b/validations_common/tests/callback_plugins/test_validation_output.py @@ -19,17 +19,18 @@ test_validation_output Tests for `validation_output` callback plugin. """ +try: + from unittest import mock +except ImportError: + import mock -from unittest import mock +from validations_common.tests import base +from validations_common.tests import fakes -from ansible.executor.stats import AggregateStats -from ansible.parsing.ajson import AnsibleJSONEncoder -from ansible.playbook import Playbook from ansible.plugins.callback import CallbackBase import validations_common.library.reportentry as validation from validations_common.callback_plugins import validation_output -from validations_common.tests import base class MockStats(mock.MagicMock): diff --git a/validations_common/tests/callback_plugins/test_validation_stdout.py b/validations_common/tests/callback_plugins/test_validation_stdout.py index 42398b4..d1d557a 100644 --- a/validations_common/tests/callback_plugins/test_validation_stdout.py +++ b/validations_common/tests/callback_plugins/test_validation_stdout.py @@ -21,13 +21,19 @@ Tests for `validation_stdout` callback plugin. """ import os import re -from unittest import mock -from ansible.plugins.callback import CallbackBase +try: + from unittest import mock +except ImportError: + import mock + +from validations_common.tests import base +from validations_common.tests import fakes import validations_common.library.reportentry as validation from validations_common.callback_plugins import validation_stdout -from validations_common.tests import base + +from ansible.plugins.callback import CallbackBase def is_iso_time(time_string): diff --git a/validations_common/tests/fakes.py b/validations_common/tests/fakes.py new file mode 100644 index 0000000..2a6bb7c --- /dev/null +++ b/validations_common/tests/fakes.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +# 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. + +import sys +try: + from unittest import mock +except ImportError: + import mock + +#This section mocks unnecessary module imports. + +sys.modules['ansible.parsing.ajson'] = mock.MagicMock() +sys.modules['prettytable'] = mock.MagicMock() diff --git a/validations_common/tests/library/test_advanced_format.py b/validations_common/tests/library/test_advanced_format.py index f49d61d..1e0fca1 100644 --- a/validations_common/tests/library/test_advanced_format.py +++ b/validations_common/tests/library/test_advanced_format.py @@ -12,9 +12,15 @@ # License for the specific language governing permissions and limitations # under the License. -from unittest import mock -from validations_common.library import advanced_format +try: + from unittest import mock +except ImportError: + import mock + from validations_common.tests import base +from validations_common.tests import fakes + +from validations_common.library import advanced_format class TestAdvancedFormat(base.TestCase): diff --git a/validations_common/tests/library/test_check_package_update.py b/validations_common/tests/library/test_check_package_update.py index 65cb9ca..2d40d15 100644 --- a/validations_common/tests/library/test_check_package_update.py +++ b/validations_common/tests/library/test_check_package_update.py @@ -12,13 +12,17 @@ # License for the specific language governing permissions and limitations # under the License. -import subprocess -from unittest import mock - -from validations_common.library import check_package_update as cppkg +try: + from unittest import mock +except ImportError: + import mock from validations_common.tests import base +from validations_common.tests import fakes +import subprocess + +from validations_common.library import check_package_update as cppkg PKG_INSTALLED = "foo-package|6.1.5|1|x86_64" PKG_INVALID = "foo-package|6.1.5|x86_64" diff --git a/validations_common/tests/library/test_haproxy_conf.py b/validations_common/tests/library/test_haproxy_conf.py index c6452f0..1a47572 100644 --- a/validations_common/tests/library/test_haproxy_conf.py +++ b/validations_common/tests/library/test_haproxy_conf.py @@ -12,9 +12,15 @@ # License for the specific language governing permissions and limitations # under the License. -from unittest import mock -from validations_common.library import haproxy_conf +try: + from unittest import mock +except ImportError: + import mock + from validations_common.tests import base +from validations_common.tests import fakes + +from validations_common.library import haproxy_conf class TestHaproxyConf(base.TestCase): diff --git a/validations_common/tests/library/test_hiera.py b/validations_common/tests/library/test_hiera.py index 2479089..6fa5a96 100644 --- a/validations_common/tests/library/test_hiera.py +++ b/validations_common/tests/library/test_hiera.py @@ -12,9 +12,15 @@ # License for the specific language governing permissions and limitations # under the License. -from unittest import mock -from validations_common.library import hiera +try: + from unittest import mock +except ImportError: + import mock + from validations_common.tests import base +from validations_common.tests import fakes + +from validations_common.library import hiera class TestHiera(base.TestCase): diff --git a/validations_common/tests/library/test_reportentry.py b/validations_common/tests/library/test_reportentry.py index 92075a5..a46beb1 100644 --- a/validations_common/tests/library/test_reportentry.py +++ b/validations_common/tests/library/test_reportentry.py @@ -19,11 +19,16 @@ test_report_entry Tests for `reportentry` module. """ +try: + from unittest import mock +except ImportError: + import mock + +from validations_common.tests import base +from validations_common.tests import fakes import validations_common.library.reportentry as validation -from validations_common.tests import base -from unittest import mock reason = "Reason #1" recommendation = ['Recommendation #1'] diff --git a/validations_common/tests/library/test_validations_read_ini.py b/validations_common/tests/library/test_validations_read_ini.py index 09875c9..79b2562 100644 --- a/validations_common/tests/library/test_validations_read_ini.py +++ b/validations_common/tests/library/test_validations_read_ini.py @@ -23,9 +23,15 @@ Tests for `validations_read_ini` module. import os import tempfile -import validations_common.library.validations_read_ini as validation -from validations_common.tests import base +try: + from unittest import mock +except ImportError: + import mock +from validations_common.tests import base +from validations_common.tests import fakes + +import validations_common.library.validations_read_ini as validation invalid_content = ''' [DEFAULT# diff --git a/validations_common/tests/library/test_warn.py b/validations_common/tests/library/test_warn.py index 99b07ef..5e5a8d5 100644 --- a/validations_common/tests/library/test_warn.py +++ b/validations_common/tests/library/test_warn.py @@ -12,9 +12,15 @@ # License for the specific language governing permissions and limitations # under the License. -from unittest import mock -from validations_common.library import warn +try: + from unittest import mock +except ImportError: + import mock + from validations_common.tests import base +from validations_common.tests import fakes + +from validations_common.library import warn class TestWarn(base.TestCase):