Fix hacking rule 302 and enable it

H302: import only modules.

Change-Id: I9f18abdc5f52f0a2ee3b8dbb7479dac8de3a0a7a
This commit is contained in:
ZhiQiang Fan 2014-06-19 15:32:19 +08:00
parent a2139d8518
commit 18eec72865
20 changed files with 99 additions and 97 deletions

View File

@ -53,10 +53,11 @@ except ImportError:
try:
# Swift > 1.7.5 ... module exists but doesn't contain class.
from swift.common.utils import InputProxy
except ImportError:
InputProxy = utils.InputProxy
except AttributeError:
# Swift <= 1.7.5 ... module exists and has class.
from swift.common.middleware.proxy_logging import InputProxy
from swift.common.middleware import proxy_logging
InputProxy = proxy_logging.InputProxy
from ceilometer.openstack.common import context
from ceilometer.openstack.common import timeutils

View File

@ -27,7 +27,7 @@ from ceilometer.openstack.common import timeutils
from ceilometer.publisher import utils
from ceilometer import sample
from ceilometer.tests import api as acl
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import db as tests_db
VALID_TOKEN = '4562138218392831'
@ -71,7 +71,7 @@ class FakeMemcache(object):
pass
class TestAPIACL(FunctionalTest,
class TestAPIACL(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def setUp(self):

View File

@ -29,11 +29,11 @@ from six import moves
from ceilometer.alarm.storage import models
from ceilometer import messaging
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import db as tests_db
class TestListEmptyAlarms(FunctionalTest,
class TestListEmptyAlarms(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def test_empty(self):
@ -41,7 +41,7 @@ class TestListEmptyAlarms(FunctionalTest,
self.assertEqual([], data)
class TestAlarms(FunctionalTest,
class TestAlarms(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def setUp(self):

View File

@ -29,7 +29,7 @@ from ceilometer.openstack.common.fixture import config
from ceilometer.openstack.common import gettextutils
from ceilometer import service
from ceilometer.tests import api as acl
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import base
@ -78,7 +78,7 @@ class TestApp(base.BaseTestCase):
os.unlink(tmpfile)
class TestPecanApp(FunctionalTest):
class TestPecanApp(v2.FunctionalTest):
def test_pecan_extension_guessing_unset(self):
# check Pecan does not assume .jpg is an extension
@ -86,7 +86,7 @@ class TestPecanApp(FunctionalTest):
self.assertEqual('application/json', response.content_type)
class TestApiMiddleware(FunctionalTest):
class TestApiMiddleware(v2.FunctionalTest):
no_lang_translated_error = 'No lang translated error'
en_US_translated_error = 'en-US translated error'

View File

@ -23,11 +23,11 @@ import mock
from ceilometer.openstack.common import timeutils
from ceilometer.storage import models
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import db as tests_db
class TestComputeDurationByResource(FunctionalTest,
class TestComputeDurationByResource(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def setUp(self):

View File

@ -18,13 +18,13 @@ import datetime
from ceilometer.openstack.common import timeutils
from ceilometer.storage import models
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import db as tests_db
headers = {"X-Roles": "admin"}
class EventTestBase(FunctionalTest,
class EventTestBase(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def setUp(self):

View File

@ -25,11 +25,11 @@ import webtest.app
from ceilometer.openstack.common import timeutils
from ceilometer.publisher import utils
from ceilometer import sample
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import db as tests_db
class TestListEvents(FunctionalTest,
class TestListEvents(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def setUp(self):

View File

@ -25,11 +25,11 @@ import webtest.app
from ceilometer.publisher import utils
from ceilometer import sample
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import db as tests_db
class TestListEmptyMeters(FunctionalTest,
class TestListEmptyMeters(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def test_empty(self):
@ -37,7 +37,7 @@ class TestListEmptyMeters(FunctionalTest,
self.assertEqual([], data)
class TestValidateUserInput(FunctionalTest,
class TestValidateUserInput(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def test_list_meters_query_float_metadata(self):
@ -61,7 +61,7 @@ class TestValidateUserInput(FunctionalTest,
'type': 'integer'}])
class TestListMeters(FunctionalTest,
class TestListMeters(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def setUp(self):

View File

@ -25,11 +25,11 @@ import six
from ceilometer.openstack.common import timeutils
from ceilometer.publisher import utils
from ceilometer import sample
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import db as tests_db
class TestListResources(FunctionalTest,
class TestListResources(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def test_empty(self):

View File

@ -24,11 +24,11 @@ import mock
from ceilometer.openstack.common.fixture import mockpatch
from ceilometer.openstack.common import timeutils
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import db as tests_db
class TestPostSamples(FunctionalTest,
class TestPostSamples(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
def fake_cast(self, ctxt, target, data):
for m in data:

View File

@ -21,7 +21,7 @@ import mock
import wsme
from ceilometer.api.controllers import v2 as api
from ceilometer.openstack.common.fixture.mockpatch import PatchObject
from ceilometer.openstack.common.fixture import mockpatch
from ceilometer.openstack.common import test
from ceilometer.openstack.common import timeutils
from ceilometer import storage
@ -175,10 +175,10 @@ class TestValidateGroupByFields(test.BaseTestCase):
class TestQueryToKwArgs(tests_base.BaseTestCase):
def setUp(self):
super(TestQueryToKwArgs, self).setUp()
self.useFixture(PatchObject(api, '_sanitize_query',
side_effect=lambda x, y, **z: x))
self.useFixture(PatchObject(api, '_verify_query_segregation',
side_effect=lambda x, **z: x))
self.useFixture(mockpatch.PatchObject(
api, '_sanitize_query', side_effect=lambda x, y, **z: x))
self.useFixture(mockpatch.PatchObject(
api, '_verify_query_segregation', side_effect=lambda x, **z: x))
def test_sample_filter_single(self):
q = [api.Query(field='user_id',

View File

@ -20,11 +20,11 @@ import datetime
from ceilometer.publisher import utils
from ceilometer import sample
from ceilometer.tests.api.v2 import FunctionalTest
from ceilometer.tests.api import v2
from ceilometer.tests import db as tests_db
class TestMaxProjectVolume(FunctionalTest,
class TestMaxProjectVolume(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
PATH = '/meters/volume.size/statistics'
@ -122,7 +122,7 @@ class TestMaxProjectVolume(FunctionalTest,
self.assertEqual(1, data[0]['count'])
class TestMaxResourceVolume(FunctionalTest,
class TestMaxResourceVolume(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
PATH = '/meters/volume.size/statistics'
@ -243,7 +243,7 @@ class TestMaxResourceVolume(FunctionalTest,
self.assertEqual(1, data[0]['count'])
class TestSumProjectVolume(FunctionalTest,
class TestSumProjectVolume(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
PATH = '/meters/volume.size/statistics'
@ -343,7 +343,7 @@ class TestSumProjectVolume(FunctionalTest,
self.assertEqual(1, data[0]['count'])
class TestSumResourceVolume(FunctionalTest,
class TestSumResourceVolume(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
PATH = '/meters/volume.size/statistics'
@ -469,7 +469,7 @@ class TestSumResourceVolume(FunctionalTest,
self.assertEqual(1, data[0]['count'])
class TestGroupByInstance(FunctionalTest,
class TestGroupByInstance(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
PATH = '/meters/instance/statistics'
@ -1214,7 +1214,7 @@ class TestGroupByInstance(FunctionalTest,
@tests_db.run_with('mongodb', 'hbase', 'db2')
class TestGroupBySource(FunctionalTest,
class TestGroupBySource(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
# FIXME(terriyu): We have to put test_group_by_source in its own class
@ -1318,7 +1318,7 @@ class TestGroupBySource(FunctionalTest,
self.assertEqual(4, r['avg'])
class TestSelectableAggregates(FunctionalTest,
class TestSelectableAggregates(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
PATH = '/meters/instance/statistics'
@ -1562,7 +1562,7 @@ class TestSelectableAggregates(FunctionalTest,
@tests_db.run_with('mongodb', 'hbase', 'db2')
class TestUnparameterizedAggregates(FunctionalTest,
class TestUnparameterizedAggregates(v2.FunctionalTest,
tests_db.MixinTestsWithBackendScenarios):
# We put the stddev test case in a separate class so that we

View File

@ -22,7 +22,7 @@ import mock
from ceilometer.central import manager
from ceilometer.energy import kwapi
from ceilometer.openstack.common import context
from ceilometer.openstack.common.fixture.mockpatch import PatchObject
from ceilometer.openstack.common.fixture import mockpatch
from ceilometer.openstack.common import test
@ -67,8 +67,8 @@ class TestKwapi(test.BaseTestCase):
raise exceptions.EndpointNotFound("fake keystone exception")
def test_endpoint_not_exist(self):
with PatchObject(kwapi._Base, 'get_kwapi_client',
side_effect=self.fake_get_kwapi_client):
with mockpatch.PatchObject(kwapi._Base, 'get_kwapi_client',
side_effect=self.fake_get_kwapi_client):
pollster = kwapi.EnergyPollster()
samples = list(pollster.get_samples(self.manager, {}))
@ -82,8 +82,8 @@ class TestEnergyPollster(test.BaseTestCase):
super(TestEnergyPollster, self).setUp()
self.context = context.get_admin_context()
self.manager = TestManager()
self.useFixture(PatchObject(kwapi._Base, '_iter_probes',
side_effect=self.fake_iter_probes))
self.useFixture(mockpatch.PatchObject(
kwapi._Base, '_iter_probes', side_effect=self.fake_iter_probes))
@staticmethod
def fake_iter_probes(ksclient, cache):
@ -142,8 +142,8 @@ class TestPowerPollster(test.BaseTestCase):
super(TestPowerPollster, self).setUp()
self.context = context.get_admin_context()
self.manager = TestManager()
self.useFixture(PatchObject(kwapi._Base, '_iter_probes',
side_effect=self.fake_iter_probes))
self.useFixture(mockpatch.PatchObject(
kwapi._Base, '_iter_probes', side_effect=self.fake_iter_probes))
@staticmethod
def fake_iter_probes(ksclient, cache):

View File

@ -25,7 +25,7 @@ import testscenarios.testcase
from ceilometer.central import manager
from ceilometer.objectstore import swift
from ceilometer.openstack.common.fixture.mockpatch import PatchObject
from ceilometer.openstack.common.fixture import mockpatch
from ceilometer.openstack.common import test
HEAD_ACCOUNTS = [('tenant-000', {'x-account-object-count': 12,
@ -100,8 +100,8 @@ class TestSwiftPollster(testscenarios.testcase.WithScenarios,
def test_iter_accounts_no_cache(self):
cache = {}
with PatchObject(self.factory, '_get_account_info',
return_value=[]):
with mockpatch.PatchObject(self.factory, '_get_account_info',
return_value=[]):
data = list(self.pollster._iter_accounts(mock.Mock(), cache))
self.assertTrue(self.pollster.CACHE_KEY_TENANT in cache)
@ -118,8 +118,8 @@ class TestSwiftPollster(testscenarios.testcase.WithScenarios,
)
api_method = '%s_account' % self.pollster.METHOD
with PatchObject(swift_client, api_method, new=ksclient):
with PatchObject(self.factory, '_neaten_url'):
with mockpatch.PatchObject(swift_client, api_method, new=ksclient):
with mockpatch.PatchObject(self.factory, '_neaten_url'):
Tenant = collections.namedtuple('Tenant', 'id')
cache = {
self.pollster.CACHE_KEY_TENANT: [
@ -148,23 +148,24 @@ class TestSwiftPollster(testscenarios.testcase.WithScenarios,
self.assertEqual(standard_url, url)
def test_metering(self):
with PatchObject(self.factory, '_iter_accounts',
side_effect=self.fake_iter_accounts):
with mockpatch.PatchObject(self.factory, '_iter_accounts',
side_effect=self.fake_iter_accounts):
samples = list(self.pollster.get_samples(self.manager, {}))
self.assertEqual(2, len(samples))
def test_get_meter_names(self):
with PatchObject(self.factory, '_iter_accounts',
side_effect=self.fake_iter_accounts):
with mockpatch.PatchObject(self.factory, '_iter_accounts',
side_effect=self.fake_iter_accounts):
samples = list(self.pollster.get_samples(self.manager, {}))
self.assertEqual(set([samples[0].name]),
set([s.name for s in samples]))
def test_endpoint_notfound(self):
with PatchObject(self.manager.keystone.service_catalog, 'url_for',
side_effect=self.fake_ks_service_catalog_url_for):
with mockpatch.PatchObject(
self.manager.keystone.service_catalog, 'url_for',
side_effect=self.fake_ks_service_catalog_url_for):
samples = list(self.pollster.get_samples(self.manager, {}))
self.assertEqual(0, len(samples))

View File

@ -23,7 +23,7 @@
running the tests. Make sure the Thrift server is running on that server.
"""
from mock import patch
import mock
from ceilometer.storage.hbase import inmemory as hbase_inmemory
from ceilometer.storage import impl_hbase as hbase
@ -50,8 +50,8 @@ class ConnectionTest(tests_db.TestBase,
def get_connection_pool(conf):
return TestConn(conf['host'], conf['port'])
with patch.object(hbase.Connection, '_get_connection_pool',
side_effect=get_connection_pool):
with mock.patch.object(hbase.Connection, '_get_connection_pool',
side_effect=get_connection_pool):
conn = hbase.Connection('hbase://test_hbase:9090')
self.assertIsInstance(conn.conn_pool, TestConn)

View File

@ -25,7 +25,7 @@
import datetime
import repr
from mock import patch
import mock
from ceilometer.openstack.common import timeutils
from ceilometer.storage import impl_sqlalchemy
@ -154,7 +154,7 @@ class EventTest(tests_db.TestBase):
m = [models.Event("1", "Foo", now, []),
models.Event("2", "Zoo", now, [])]
with patch.object(self.conn, "_record_event") as mock_save:
with mock.patch.object(self.conn, "_record_event") as mock_save:
mock_save.side_effect = MyException("Boom")
problem_events = self.conn.record_events(m)
self.assertEqual(2, len(problem_events))
@ -177,7 +177,7 @@ class RelationshipTest(scenarios.DBTestBase):
# Note: Do not derive from SQLAlchemyEngineTestBase, since we
# don't want to automatically inherit all the Meter setup.
@patch.object(timeutils, 'utcnow')
@mock.patch.object(timeutils, 'utcnow')
def test_clear_metering_data_meta_tables(self, mock_utcnow):
mock_utcnow.return_value = datetime.datetime(2012, 7, 2, 10, 45)
self.conn.clear_expired_metering_data(3 * 60)

View File

@ -16,8 +16,7 @@ import contextlib
import copy
import datetime
from mock import call
from mock import patch
import mock
import pymongo
from ceilometer.openstack.common.gettextutils import _
@ -72,8 +71,8 @@ class CompatibilityTest(test_storage_scenarios.DBTestBase,
self.db.meter.insert(record)
# Stubout with the old version DB schema, the one w/o 'counter_unit'
with patch.object(self.conn, 'record_metering_data',
side_effect=old_record_metering_data):
with mock.patch.object(self.conn, 'record_metering_data',
side_effect=old_record_metering_data):
self.counters = []
c = sample.Sample(
'volume.size',
@ -187,15 +186,16 @@ class CompatibilityTest(test_storage_scenarios.DBTestBase,
pool = pymongo_utils.ConnectionPool()
with contextlib.nested(
patch('pymongo.MongoClient',
side_effect=pymongo.errors.ConnectionFailure('foo')),
patch.object(pymongo_utils.LOG, 'error'),
patch.object(pymongo_utils.LOG, 'warn'),
patch.object(pymongo_utils.time, 'sleep')
mock.patch(
'pymongo.MongoClient',
side_effect=pymongo.errors.ConnectionFailure('foo')),
mock.patch.object(pymongo_utils.LOG, 'error'),
mock.patch.object(pymongo_utils.LOG, 'warn'),
mock.patch.object(pymongo_utils.time, 'sleep')
) as (MockMongo, MockLOGerror, MockLOGwarn, Mocksleep):
self.assertRaises(pymongo.errors.ConnectionFailure,
pool.connect, self.CONF.database.connection)
Mocksleep.assert_has_calls([call(retry_interval)
Mocksleep.assert_has_calls([mock.call(retry_interval)
for i in range(max_retries)])
MockLOGwarn.assert_any_call(
_('Unable to connect to the database server: %(errmsg)s.'

View File

@ -14,7 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from mock import patch
import mock
from ceilometer import neutron_client
from ceilometer.openstack.common import test
@ -42,8 +42,8 @@ class TestNeutronClient(test.BaseTestCase):
]}
def test_port_get_all(self):
with patch.object(self.nc.client, 'list_ports',
side_effect=self.fake_ports_list):
with mock.patch.object(self.nc.client, 'list_ports',
side_effect=self.fake_ports_list):
ports = self.nc.port_get_all()
self.assertEqual(1, len(ports))
@ -67,8 +67,8 @@ class TestNeutronClient(test.BaseTestCase):
]}
def test_network_get_all(self):
with patch.object(self.nc.client, 'list_networks',
side_effect=self.fake_networks_list):
with mock.patch.object(self.nc.client, 'list_networks',
side_effect=self.fake_networks_list):
networks = self.nc.network_get_all()
self.assertEqual(1, len(networks))
@ -94,8 +94,8 @@ class TestNeutronClient(test.BaseTestCase):
]}
def test_pool_list(self):
with patch.object(self.nc.client, 'list_pools',
side_effect=self.fake_pool_list):
with mock.patch.object(self.nc.client, 'list_pools',
side_effect=self.fake_pool_list):
pools = self.nc.pool_get_all()
self.assertEqual(1, len(pools))
@ -122,8 +122,8 @@ class TestNeutronClient(test.BaseTestCase):
]}
def test_vip_list(self):
with patch.object(self.nc.client, 'list_vips',
side_effect=self.fake_vip_list):
with mock.patch.object(self.nc.client, 'list_vips',
side_effect=self.fake_vip_list):
vips = self.nc.vip_get_all()
self.assertEqual(1, len(vips))
@ -144,8 +144,8 @@ class TestNeutronClient(test.BaseTestCase):
]}
def test_member_list(self):
with patch.object(self.nc.client, 'list_members',
side_effect=self.fake_member_list):
with mock.patch.object(self.nc.client, 'list_members',
side_effect=self.fake_member_list):
members = self.nc.member_get_all()
self.assertEqual(1, len(members))
@ -166,8 +166,8 @@ class TestNeutronClient(test.BaseTestCase):
}]}
def test_monitor_list(self):
with patch.object(self.nc.client, 'list_health_monitors',
side_effect=self.fake_monitors_list):
with mock.patch.object(self.nc.client, 'list_health_monitors',
side_effect=self.fake_monitors_list):
monitors = self.nc.health_monitor_get_all()
self.assertEqual(1, len(monitors))
@ -184,8 +184,8 @@ class TestNeutronClient(test.BaseTestCase):
}]}
def test_pool_stats(self):
with patch.object(self.nc.client, 'retrieve_pool_stats',
side_effect=self.fake_pool_stats):
with mock.patch.object(self.nc.client, 'retrieve_pool_stats',
side_effect=self.fake_pool_stats):
stats = self.nc.pool_stats('fake_pool')['stats']
self.assertEqual(1, len(stats))

View File

@ -17,7 +17,6 @@
# under the License.
import mock
from mock import patch
import novaclient
from ceilometer import nova_client
@ -85,8 +84,8 @@ class TestNovaClient(test.BaseTestCase):
return [a, b]
def test_instance_get_all_by_host(self):
with patch.object(self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list):
with mock.patch.object(self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list):
instances = self.nv.instance_get_all_by_host('foobar')
self.assertEqual(2, len(instances))
@ -104,8 +103,9 @@ class TestNovaClient(test.BaseTestCase):
return [a]
def test_instance_get_all_by_host_unknown_flavor(self):
with patch.object(self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list_unknown_flavor):
with mock.patch.object(
self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list_unknown_flavor):
instances = self.nv.instance_get_all_by_host('foobar')
self.assertEqual(1, len(instances))
@ -136,8 +136,9 @@ class TestNovaClient(test.BaseTestCase):
return [a]
def test_instance_get_all_by_host_unknown_image(self):
with patch.object(self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list_unknown_image):
with mock.patch.object(
self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list_unknown_image):
instances = self.nv.instance_get_all_by_host('foobar')
self.assertEqual(1, len(instances))

View File

@ -37,8 +37,7 @@ commands = python setup.py build_sphinx
commands = {posargs}
[flake8]
# TODO fix H302
ignore = H302
ignore = None
builtins = _
exclude=.venv,.git,.tox,dist,doc,./ceilometer/openstack/common,*lib/python*,*egg,tools,nova_tests,build
show-source = True