Pass statical analisys wiht Python2

Change-Id: I72c4bcf4a4879a104e44633f97293a6f9edbeb43
This commit is contained in:
Federico Ressi 2019-05-24 16:57:54 +02:00
parent 01e64a0af0
commit ef86702f91
15 changed files with 82 additions and 79 deletions

View File

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

View File

@ -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

View File

@ -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)

View File

@ -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:

View File

@ -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)

View File

@ -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 []))

View File

@ -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

View File

@ -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

View File

@ -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': '<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()

View File

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

View File

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

View File

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

View File

@ -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

View File

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

View File

@ -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'])