Browse Source

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 <jpodivin@redhat.com>
Change-Id: Ic3926c1fd38a670ed37eeba1aa4df64347a34a9b
changes/44/785844/7
Jiri Podivin 7 months ago
parent
commit
b41537fa69
  1. 14
      validations_common/tests/callback_plugins/test_fail_if_no_hosts.py
  2. 9
      validations_common/tests/callback_plugins/test_http_json.py
  3. 9
      validations_common/tests/callback_plugins/test_validation_json.py
  4. 11
      validations_common/tests/callback_plugins/test_validation_output.py
  5. 12
      validations_common/tests/callback_plugins/test_validation_stdout.py
  6. 24
      validations_common/tests/fakes.py
  7. 10
      validations_common/tests/library/test_advanced_format.py
  8. 12
      validations_common/tests/library/test_check_package_update.py
  9. 10
      validations_common/tests/library/test_haproxy_conf.py
  10. 10
      validations_common/tests/library/test_hiera.py
  11. 9
      validations_common/tests/library/test_reportentry.py
  12. 8
      validations_common/tests/library/test_validations_read_ini.py
  13. 10
      validations_common/tests/library/test_warn.py

14
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',

9
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):

9
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
import validations_common.library.reportentry as validation
from validations_common.tests import base
from validations_common.tests import fakes
from unittest import mock
import validations_common.library.reportentry as validation
class TestValidationJson(base.TestCase):

11
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):

12
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):

24
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()

10
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):

12
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"

10
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):

10
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):

9
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
import validations_common.library.reportentry as validation
from validations_common.tests import base
from validations_common.tests import fakes
import validations_common.library.reportentry as validation
from unittest import mock
reason = "Reason #1"
recommendation = ['Recommendation #1']

8
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
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#

10
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):

Loading…
Cancel
Save