From ef86702f918e7cbef94b3815333eb9c2c0ab1af3 Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Fri, 24 May 2019 16:57:54 +0200 Subject: [PATCH] Pass statical analisys wiht Python2 Change-Id: I72c4bcf4a4879a104e44633f97293a6f9edbeb43 --- tobiko/common/_fixture.py | 23 ++++------------ tobiko/common/utils/file.py | 4 +-- tobiko/openstack/heat/_template.py | 2 +- tobiko/shell/sh/_execute.py | 1 + tobiko/tests/unit/__init__.py | 27 ++++++++++--------- tobiko/tests/unit/cmd/test_base.py | 4 ++- tobiko/tests/unit/cmd/test_fixture.py | 8 +++--- tobiko/tests/unit/openstack/__init__.py | 25 ++++++++++------- .../tests/unit/openstack/heat/test_stack.py | 16 ++++++----- .../openstack/keystone/test_credentials.py | 22 ++++++++------- .../unit/openstack/keystone/test_session.py | 4 ++- tobiko/tests/unit/openstack/test_client.py | 2 +- tobiko/tests/unit/shell/test_ssh.py | 2 +- tobiko/tests/unit/test_config.py | 19 +++++++------ tobiko/tests/unit/test_loader.py | 2 +- 15 files changed, 82 insertions(+), 79 deletions(-) diff --git a/tobiko/common/_fixture.py b/tobiko/common/_fixture.py index 6fc381906..fae0bafff 100644 --- a/tobiko/common/_fixture.py +++ b/tobiko/common/_fixture.py @@ -179,9 +179,11 @@ def get_required_fixture(obj): pass if is_test_method(obj): - for default in get_default_param_values(obj): - if is_fixture(default): - required_fixtures.append(get_fixture_name(default)) + defaults = six.get_function_defaults(obj) + if defaults: + for default in defaults: + if is_fixture(default): + required_fixtures.append(get_fixture_name(default)) elif inspect.isclass(obj): # inspect.getmembers() would iterate over such many @@ -271,21 +273,6 @@ def get_object_name(obj): raise TypeError(msg) -def get_default_param_values(obj): - if hasattr(inspect, 'signature'): - try: - signature = inspect.signature(obj) - except ValueError: - pass - else: - return [param.default - for param in signature.parameters.values()] - - # Use old deprecated function 'getargspec' - return list(inspect.getargspec(obj).defaults or # pylint: disable=W1505 - tuple()) - - class FixtureManager(object): def __init__(self): diff --git a/tobiko/common/utils/file.py b/tobiko/common/utils/file.py index 1583406ba..108d1c366 100644 --- a/tobiko/common/utils/file.py +++ b/tobiko/common/utils/file.py @@ -24,7 +24,7 @@ LOG = log.getLogger(__name__) def makedirs(path, mode=777, exist_ok=True): """Creates directory and its parents if directory doesn't exists.""" try: - os.makedirs(path, mode, exist_ok) - except FileExistsError: + os.makedirs(path, mode) + except os.error: if not exist_ok: raise diff --git a/tobiko/openstack/heat/_template.py b/tobiko/openstack/heat/_template.py index d48c9cb6e..422679f25 100644 --- a/tobiko/openstack/heat/_template.py +++ b/tobiko/openstack/heat/_template.py @@ -94,4 +94,4 @@ def find_heat_template_file(template_file, template_dirs): msg = "Template file {!r} not found in directories {!r}".format( template_file, template_dirs) - raise FileNotFoundError(msg) + raise IOError(msg) diff --git a/tobiko/shell/sh/_execute.py b/tobiko/shell/sh/_execute.py index 3178ac6f1..fc354b9f3 100644 --- a/tobiko/shell/sh/_execute.py +++ b/tobiko/shell/sh/_execute.py @@ -228,6 +228,7 @@ def execute_local_command(command, stdin=None, environment=None, timeout=None, # Wait for process execution while reading STDERR and STDOUT streams if timeout: try: + # pylint: disable=unexpected-keyword-arg,no-member stdout, stderr = process.communicate(input=stdin, timeout=timeout) except subprocess.TimeoutExpired: diff --git a/tobiko/tests/unit/__init__.py b/tobiko/tests/unit/__init__.py index d7b316d8f..e96633f94 100644 --- a/tobiko/tests/unit/__init__.py +++ b/tobiko/tests/unit/__init__.py @@ -16,9 +16,10 @@ import shutil import tempfile import mock +from oslo_log import log -from tobiko.tests import base from tobiko.common import _fixture +from tobiko.tests import base class TobikoUnitTest(base.TobikoTest): @@ -26,21 +27,21 @@ class TobikoUnitTest(base.TobikoTest): def setUp(self): super(TobikoUnitTest, self).setUp() # Protect from mis-configuring logging - self.patch('oslo_log.log.setup') + self.patch(log, 'setup') self.fixture_manager = manager = _fixture.FixtureManager() - self.patch_object(_fixture, 'FIXTURES', manager) + self.patch(_fixture, 'FIXTURES', manager) - def patch(self, target, *args, **kwargs): - context = mock.patch(target, *args, **kwargs) - mock_object = context.start() + def patch(self, obj, attribute, value=mock.DEFAULT, spec=None, + create=False, spec_set=None, autospec=None, + new_callable=None, **kwargs): + # pylint: disable=arguments-differ + context = mock.patch.object(target=obj, attribute=attribute, new=value, + spec=spec, create=create, + spec_set=spec_set, autospec=autospec, + new_callable=new_callable, **kwargs) + mocked = context.start() self.addCleanup(context.stop) - return mock_object - - def patch_object(self, target, attribute, *args, **kwargs): - context = mock.patch.object(target, attribute, *args, **kwargs) - mock_object = context.start() - self.addCleanup(context.stop) - return mock_object + return mocked def create_tempdir(self, *args, **kwargs): dir_path = tempfile.mkdtemp(*args, **kwargs) diff --git a/tobiko/tests/unit/cmd/test_base.py b/tobiko/tests/unit/cmd/test_base.py index 91803ec3e..f6c362e78 100644 --- a/tobiko/tests/unit/cmd/test_base.py +++ b/tobiko/tests/unit/cmd/test_base.py @@ -14,6 +14,8 @@ # under the License. from __future__ import absolute_import +import sys + from tobiko.cmd import base from tobiko.tests.unit import openstack @@ -28,4 +30,4 @@ class TobikoCMDTest(openstack.OpenstackTest): return self.command_class() def patch_argv(self, argv=None): - return self.patch('sys.argv', [self.command_name] + (argv or [])) + return self.patch(sys, 'argv', [self.command_name] + (argv or [])) diff --git a/tobiko/tests/unit/cmd/test_fixture.py b/tobiko/tests/unit/cmd/test_fixture.py index 484f69ad9..052809817 100644 --- a/tobiko/tests/unit/cmd/test_fixture.py +++ b/tobiko/tests/unit/cmd/test_fixture.py @@ -14,9 +14,11 @@ # under the License. from __future__ import absolute_import +import argparse import io import os import subprocess +import sys import tobiko from tobiko.cmd import fixture as _fixture @@ -46,7 +48,7 @@ class FixtureUtilTest(unit.TobikoUnitTest): def setUp(self): super(FixtureUtilTest, self).setUp() - self.mock_error = self.patch('argparse.ArgumentParser.error', + self.mock_error = self.patch(argparse.ArgumentParser, 'error', side_effect=self.fail) def patch_argv(self, subcommand=None, arguments=None, @@ -76,7 +78,7 @@ class FixtureUtilTest(unit.TobikoUnitTest): arguments += ['--black-regex', black_regex] if filters: arguments += list(filters) - return self.patch('sys.argv', + return self.patch(sys, 'argv', [self.command_name, subcommand] + arguments) def test_init(self, subcommand=None, arguments=None, filters=None, @@ -200,7 +202,7 @@ class FixtureUtilTest(unit.TobikoUnitTest): blacklist_file=blacklist_file, whitelist_file=whitelist_file, black_regex=black_regex, filters=filters) - stdout = self.patch('sys.stdout', io.StringIO()) + stdout = self.patch(sys, 'stdout', io.StringIO()) _fixture.main() self.mock_error.assert_not_called() return stdout diff --git a/tobiko/tests/unit/openstack/__init__.py b/tobiko/tests/unit/openstack/__init__.py index aa6f4325f..26f2fe55e 100644 --- a/tobiko/tests/unit/openstack/__init__.py +++ b/tobiko/tests/unit/openstack/__init__.py @@ -31,23 +31,28 @@ class OpenstackTest(unit.TobikoUnitTest): def setUp(self): super(OpenstackTest, self).setUp() from tobiko import config - self.patch_object(config.CONF.tobiko, 'keystone', - self.default_keystone_credentials) + + self.patch(config.CONF.tobiko, 'keystone', + self.default_keystone_credentials) def patch_get_heat_client(self, *args, **kwargs): from heatclient import client + from tobiko.openstack import heat + from tobiko.openstack.heat import _client + kwargs.setdefault('return_value', mock.MagicMock(specs=client.Client)) - get_heat_client = self.patch( - 'tobiko.openstack.heat._client.get_heat_client', *args, **kwargs) - self.patch('tobiko.openstack.heat.get_heat_client', get_heat_client) + get_heat_client = self.patch(_client, 'get_heat_client', *args, + **kwargs) + self.patch(heat, 'get_heat_client', get_heat_client) return get_heat_client def patch_get_neutron_client(self, *args, **kwargs): from neutronclient.v2_0 import client + from tobiko.openstack import neutron + from tobiko.openstack.neutron import _client + kwargs.setdefault('return_value', mock.MagicMock(specs=client.Client)) - get_neutron_client = self.patch( - 'tobiko.openstack.neutron._client.get_neutron_client', *args, - **kwargs) - self.patch('tobiko.openstack.neutron.get_neutron_client', - get_neutron_client) + get_neutron_client = self.patch(_client, 'get_neutron_client', *args, + **kwargs) + self.patch(neutron, 'get_neutron_client', get_neutron_client) return get_neutron_client diff --git a/tobiko/tests/unit/openstack/heat/test_stack.py b/tobiko/tests/unit/openstack/heat/test_stack.py index 52c0762e3..a4527b4e2 100644 --- a/tobiko/tests/unit/openstack/heat/test_stack.py +++ b/tobiko/tests/unit/openstack/heat/test_stack.py @@ -15,6 +15,7 @@ from __future__ import absolute_import import collections import os +import time from heatclient.v1 import client as heatclient from heatclient import exc @@ -138,10 +139,12 @@ class HeatStackFixtureTest(openstack.OpenstackTest): template=None, parameters=None, wait_interval=None, stacks=None, create_conflict=False, call_create=True, call_delete=False, call_sleep=False): + from tobiko.openstack.heat import _client + from tobiko.openstack.heat import _template + client = mock.MagicMock(specs=heatclient.Client) - get_heat_client = self.patch( - 'tobiko.openstack.heat._client.get_heat_client', - return_value=client) + get_heat_client = self.patch(_client, 'get_heat_client', + return_value=client) stacks = stacks or [ exc.HTTPNotFound, @@ -154,15 +157,14 @@ class HeatStackFixtureTest(openstack.OpenstackTest): client.stacks.create.return_value = { 'stack': {'id': ''}} - sleep = self.patch('time.sleep') + sleep = self.patch(time, 'sleep') stack = fixture_class(stack_name=stack_name, parameters=parameters, template=template, wait_interval=wait_interval) default_template = heat.HeatTemplate.from_dict( {'default': 'template'}) - get_heat_template = self.patch( - 'tobiko.openstack.heat._template.get_heat_template', - return_value=default_template) + get_heat_template = self.patch(_template, 'get_heat_template', + return_value=default_template) stack.setUp() diff --git a/tobiko/tests/unit/openstack/keystone/test_credentials.py b/tobiko/tests/unit/openstack/keystone/test_credentials.py index 397342cf4..649acb4fe 100644 --- a/tobiko/tests/unit/openstack/keystone/test_credentials.py +++ b/tobiko/tests/unit/openstack/keystone/test_credentials.py @@ -14,6 +14,8 @@ # under the License. from __future__ import absolute_import +import os + import tobiko from tobiko import config from tobiko.openstack import keystone @@ -120,35 +122,35 @@ class EnvironKeystoneCredentialsFixtureTest(openstack.OpenstackTest): self.assertIsNone(fixture.credentials) def test_setup_v2(self): - self.patch('os.environ', V2_ENVIRON) + self.patch(os, 'environ', V2_ENVIRON) fixture = _credentials.EnvironKeystoneCredentialsFixture() fixture.setUp() fixture.credentials.validate() self.assertEqual(V2_PARAMS, fixture.credentials.to_dict()) def test_setup_v2_with_tenant_name(self): - self.patch('os.environ', V2_ENVIRON_WITH_TENANT_NAME) + self.patch(os, 'environ', V2_ENVIRON_WITH_TENANT_NAME) fixture = _credentials.EnvironKeystoneCredentialsFixture() fixture.setUp() fixture.credentials.validate() self.assertEqual(V2_PARAMS, fixture.credentials.to_dict()) def test_setup_v2_with_api_version(self): - self.patch('os.environ', V2_ENVIRON_WITH_VERSION) + self.patch(os, 'environ', V2_ENVIRON_WITH_VERSION) fixture = _credentials.EnvironKeystoneCredentialsFixture() fixture.setUp() fixture.credentials.validate() self.assertEqual(V2_PARAMS, fixture.credentials.to_dict()) def test_setup_v3(self): - self.patch('os.environ', V3_ENVIRON) + self.patch(os, 'environ', V3_ENVIRON) fixture = _credentials.EnvironKeystoneCredentialsFixture() fixture.setUp() fixture.credentials.validate() self.assertEqual(V3_PARAMS, fixture.credentials.to_dict()) def test_setup_v3_without_api_version(self): - self.patch('os.environ', V3_ENVIRON_WITH_VERSION) + self.patch(os, 'environ', V3_ENVIRON_WITH_VERSION) fixture = _credentials.EnvironKeystoneCredentialsFixture() fixture.setUp() fixture.credentials.validate() @@ -159,7 +161,7 @@ class ConfigKeystoneCredentialsFixtureTest(openstack.OpenstackTest): def patch_config(self, params, **kwargs): credentials = make_credentials(params, **kwargs) - return self.patch_object(config.CONF.tobiko, 'keystone', credentials) + return self.patch(config.CONF.tobiko, 'keystone', credentials) def test_init(self): fixture = _credentials.ConfigKeystoneCredentialsFixture() @@ -199,20 +201,20 @@ class DefaultKeystoneCredentialsFixtureTest(openstack.OpenstackTest): def setUp(self): super(DefaultKeystoneCredentialsFixtureTest, self).setUp() self.patch_config({}) - self.patch('os.environ', {}) + self.patch(os, 'environ', {}) tobiko.remove_fixture(_credentials.ConfigKeystoneCredentialsFixture) tobiko.remove_fixture(_credentials.EnvironKeystoneCredentialsFixture) def patch_config(self, params, **kwargs): credentials = make_credentials(params, **kwargs) - return self.patch_object(config.CONF.tobiko, 'keystone', credentials) + return self.patch(config.CONF.tobiko, 'keystone', credentials) def test_init(self): fixture = _credentials.DefaultKeystoneCredentialsFixture() self.assertIsNone(fixture.credentials) def test_setup_from_environ(self): - self.patch('os.environ', V2_ENVIRON) + self.patch(os, 'environ', V2_ENVIRON) fixture = _credentials.DefaultKeystoneCredentialsFixture() fixture.setUp() fixture.credentials.validate() @@ -226,7 +228,7 @@ class DefaultKeystoneCredentialsFixtureTest(openstack.OpenstackTest): self.assertEqual(V2_PARAMS, fixture.credentials.to_dict()) def test_setup_from_environ_and_confif(self): - self.patch('os.environ', V3_ENVIRON) + self.patch(os, 'environ', V3_ENVIRON) self.patch_config(V2_PARAMS) fixture = _credentials.DefaultKeystoneCredentialsFixture() fixture.setUp() diff --git a/tobiko/tests/unit/openstack/keystone/test_session.py b/tobiko/tests/unit/openstack/keystone/test_session.py index 8a7edf375..3727d5e48 100644 --- a/tobiko/tests/unit/openstack/keystone/test_session.py +++ b/tobiko/tests/unit/openstack/keystone/test_session.py @@ -82,8 +82,10 @@ class KeystoneSessionFixtureTest(CheckSessionCredentialsMixin, def setUp(self): super(KeystoneSessionFixtureTest, self).setUp() + from tobiko.openstack.keystone import _credentials + tobiko.remove_fixture(self.default_credentials_fixture) - self.patch(self.default_credentials_fixture, + self.patch(_credentials, 'DefaultKeystoneCredentialsFixture', DefaultCredentialsFixture) def test_init(self, credentials=None): diff --git a/tobiko/tests/unit/openstack/test_client.py b/tobiko/tests/unit/openstack/test_client.py index ba49e155f..b1ba48401 100644 --- a/tobiko/tests/unit/openstack/test_client.py +++ b/tobiko/tests/unit/openstack/test_client.py @@ -103,7 +103,7 @@ class OpenstackClientManagerTest(openstack.OpenstackTest): def setUp(self): super(OpenstackClientManagerTest, self).setUp() - self.patch('tobiko.openstack.keystone.get_keystone_session', + self.patch(keystone, 'get_keystone_session', return_value=DEFAULT_SESSION) def test_init(self, init_client=None): diff --git a/tobiko/tests/unit/shell/test_ssh.py b/tobiko/tests/unit/shell/test_ssh.py index 2ec6b60ad..bda127796 100644 --- a/tobiko/tests/unit/shell/test_ssh.py +++ b/tobiko/tests/unit/shell/test_ssh.py @@ -40,7 +40,7 @@ class SSHClientFixtureTest(unit.TobikoUnitTest): super(SSHClientFixtureTest, self).setUp() tobiko.cleanup_fixture(self.fixture) self.ssh_client = mock.MagicMock(specs=paramiko.SSHClient) - self.patch('paramiko.SSHClient', return_value=self.ssh_client) + self.patch(paramiko, 'SSHClient', return_value=self.ssh_client) def test_init(self): fixture = self.fixture diff --git a/tobiko/tests/unit/test_config.py b/tobiko/tests/unit/test_config.py index 286318fb0..87323e5c7 100644 --- a/tobiko/tests/unit/test_config.py +++ b/tobiko/tests/unit/test_config.py @@ -20,6 +20,9 @@ from tobiko.tests import unit from tobiko import config +CONF = config.CONF + + class HttpProxyFixtureTest(unit.TobikoUnitTest): MY_HTTP_PROXY = 'http://my-server:8080' @@ -27,8 +30,8 @@ class HttpProxyFixtureTest(unit.TobikoUnitTest): def setUp(self): super(HttpProxyFixtureTest, self).setUp() - self.patch('os.environ', {}) - self.patch('tobiko.config.CONF.tobiko.http', + self.patch(os, 'environ', {}) + self.patch(CONF.tobiko, 'http', http_proxy=None, https_proxy=None, no_proxy=None) def test_init(self): @@ -72,8 +75,7 @@ class HttpProxyFixtureTest(unit.TobikoUnitTest): self.assertEqual(self.MY_NO_PROXY, fixture.no_proxy) def test_setup_from_tobiko_conf_http_proxy(self): - self.patch_object(config.CONF.tobiko.http, 'http_proxy', - self.MY_HTTP_PROXY) + self.patch(CONF.tobiko.http, 'http_proxy', self.MY_HTTP_PROXY) fixture = config.HttpProxyFixture() fixture.setUp() @@ -82,8 +84,7 @@ class HttpProxyFixtureTest(unit.TobikoUnitTest): self.assertEqual({'http_proxy': self.MY_HTTP_PROXY}, os.environ) def test_setup_from_tobiko_conf_https_proxy(self): - self.patch_object(config.CONF.tobiko.http, 'https_proxy', - self.MY_HTTP_PROXY) + self.patch(CONF.tobiko.http, 'https_proxy', self.MY_HTTP_PROXY) fixture = config.HttpProxyFixture() fixture.setUp() @@ -92,10 +93,8 @@ class HttpProxyFixtureTest(unit.TobikoUnitTest): self.assertEqual({'https_proxy': self.MY_HTTP_PROXY}, os.environ) def test_setup_from_tobiko_conf_no_proxy(self): - self.patch_object(config.CONF.tobiko.http, 'http_proxy', - self.MY_HTTP_PROXY) - self.patch_object(config.CONF.tobiko.http, 'no_proxy', - self.MY_NO_PROXY) + self.patch(CONF.tobiko.http, 'http_proxy', self.MY_HTTP_PROXY) + self.patch(CONF.tobiko.http, 'no_proxy', self.MY_NO_PROXY) fixture = config.HttpProxyFixture() fixture.setUp() diff --git a/tobiko/tests/unit/test_loader.py b/tobiko/tests/unit/test_loader.py index 9e75a6011..31e4accf7 100644 --- a/tobiko/tests/unit/test_loader.py +++ b/tobiko/tests/unit/test_loader.py @@ -34,7 +34,7 @@ class TestLoader(TobikoUnitTest): def setUp(self): super(TestLoader, self).setUp() self.manager = loader.LoaderManager() - self.patch('tobiko.common.managers.loader.LOADERS', self.manager) + self.patch(loader, 'LOADERS', self.manager) def test_load_object_with_none(self): object_id = '.'.join([__name__, 'SOME_NONE'])