From 5b25c0e6400f00d8665eb3071c71ea797662cf51 Mon Sep 17 00:00:00 2001 From: Juha Kosonen Date: Mon, 29 Jan 2018 15:57:11 +0200 Subject: Fix pylint errors/warnings in rally JIRA: FUNCTEST-906 Change-Id: I484f9a291d5adbedb855450e4ff067dfa4a3b2ae Signed-off-by: Juha Kosonen --- functest/opnfv_tests/openstack/rally/rally.py | 21 ++++++--- functest/tests/unit/openstack/rally/test_rally.py | 56 ++++++++++------------- tox.ini | 4 +- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/functest/opnfv_tests/openstack/rally/rally.py b/functest/opnfv_tests/openstack/rally/rally.py index 59a670952..2fd7d7f67 100644 --- a/functest/opnfv_tests/openstack/rally/rally.py +++ b/functest/opnfv_tests/openstack/rally/rally.py @@ -44,6 +44,7 @@ LOGGER = logging.getLogger(__name__) class RallyBase(testcase.TestCase): """Base class form Rally testcases implementation.""" + # pylint: disable=too-many-instance-attributes TESTS = ['authenticate', 'glance', 'ceilometer', 'cinder', 'heat', 'keystone', 'neutron', 'nova', 'quotas', 'vm', 'all'] GLANCE_IMAGE_NAME = CONST.__getattribute__('openstack_image_name') @@ -110,6 +111,7 @@ class RallyBase(testcase.TestCase): self.compute_cnt = 0 def _build_task_args(self, test_file_name): + """Build arguments for the Rally task.""" task_args = {'service_list': [test_file_name]} task_args['image_name'] = self.image_name task_args['flavor_name'] = self.flavor_name @@ -140,6 +142,7 @@ class RallyBase(testcase.TestCase): return task_args def _prepare_test_list(self, test_name): + """Build the list of test cases to be executed.""" test_yaml_file_name = 'opnfv-{}.yaml'.format(test_name) scenario_file_name = os.path.join(self.RALLY_SCENARIO_DIR, test_yaml_file_name) @@ -231,7 +234,7 @@ class RallyBase(testcase.TestCase): in_it(installer_type, installers)): tests = item['tests'] black_tests.extend(tests) - except Exception: + except Exception: # pylint: disable=broad-except LOGGER.debug("Scenario exclusion not applied.") return black_tests @@ -254,8 +257,8 @@ class RallyBase(testcase.TestCase): # match if regex pattern is set and found in the needle if pattern and re.search(pattern, needle) is not None: return True - else: - return False + + return False def excl_func(self): """Exclude functionalities.""" @@ -399,6 +402,7 @@ class RallyBase(testcase.TestCase): LOGGER.info('Test scenario: "{}" Failed.'.format(test_name) + "\n") def _append_summary(self, json_raw, test_name): + """Update statistics summary info.""" nb_tests = 0 nb_success = 0 overall_duration = 0.0 @@ -421,6 +425,7 @@ class RallyBase(testcase.TestCase): self.summary.append(scenario_summary) def _prepare_env(self): + """Create resources needed by test scenarios.""" LOGGER.debug('Validating the test name...') if self.test_name not in self.TESTS: raise Exception("Test name '%s' is invalid" % self.test_name) @@ -472,8 +477,7 @@ class RallyBase(testcase.TestCase): segmentation_id=rally_segmentation_id, subnet_settings=[SubnetConfig( name=subnet_name, - cidr=self.RALLY_PRIVATE_SUBNET_CIDR) - ])) + cidr=self.RALLY_PRIVATE_SUBNET_CIDR)])) if network_creator is None: raise Exception("Failed to create private network") self.priv_net_id = network_creator.get_network().id @@ -508,6 +512,7 @@ class RallyBase(testcase.TestCase): self.creators.append(flavor_alt_creator) def _run_tests(self): + """Execute tests.""" if self.test_name == 'all': for test in self.TESTS: if test == 'all' or test == 'vm': @@ -517,6 +522,7 @@ class RallyBase(testcase.TestCase): self._run_task(self.test_name) def _generate_report(self): + """Generate test execution summary report.""" total_duration = 0.0 total_nb_tests = 0 total_nb_success = 0 @@ -569,11 +575,12 @@ class RallyBase(testcase.TestCase): self.details = payload def _clean_up(self): + """Cleanup all OpenStack objects. Should be called on completion.""" for creator in reversed(self.creators): try: creator.clean() - except Exception as e: - LOGGER.error('Unexpected error cleaning - %s', e) + except Exception as exc: # pylint: disable=broad-except + LOGGER.error('Unexpected error cleaning - %s', exc) @energy.enable_recording def run(self, **kwargs): diff --git a/functest/tests/unit/openstack/rally/test_rally.py b/functest/tests/unit/openstack/rally/test_rally.py index 92177f361..08ca7e395 100644 --- a/functest/tests/unit/openstack/rally/test_rally.py +++ b/functest/tests/unit/openstack/rally/test_rally.py @@ -5,7 +5,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 -# pylint: disable=missing-docstring +# pylint: disable=missing-docstring,protected-access,invalid-name import json import logging @@ -22,14 +22,16 @@ from snaps.openstack.os_credentials import OSCreds class OSRallyTesting(unittest.TestCase): + # pylint: disable=too-many-public-methods def setUp(self): os_creds = OSCreds( username='user', password='pass', auth_url='http://foo.com:5000/v3', project_name='bar') with mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.' - 'get_credentials', return_value=os_creds) as m: + 'get_credentials', + return_value=os_creds) as mock_get_creds: self.rally_base = rally.RallyBase() - self.assertTrue(m.called) + self.assertTrue(mock_get_creds.called) def test_build_task_args_missing_floating_network(self): CONST.__setattr__('OS_AUTH_URL', None) @@ -230,6 +232,7 @@ class OSRallyTesting(unittest.TestCase): @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen') @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error') def test_run_task_taskid_missing(self, mock_logger_error, *args): + # pylint: disable=unused-argument self.rally_base._run_task('test_name') text = 'Failed to retrieve task_id, validating task...' mock_logger_error.assert_any_call(text) @@ -257,6 +260,7 @@ class OSRallyTesting(unittest.TestCase): @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error') def test_run_task_default(self, mock_logger_error, mock_logger_info, *args): + # pylint: disable=unused-argument self.rally_base._run_task('test_name') text = 'Test scenario: "test_name" OK.\n' mock_logger_info.assert_any_call(text) @@ -311,18 +315,13 @@ class OSRallyTesting(unittest.TestCase): @mock.patch('snaps.openstack.utils.deploy_utils.create_network') @mock.patch('snaps.openstack.utils.deploy_utils.create_router', return_value=None) - def test_prepare_env_router_creation_failed( - self, mock_create_router, mock_create_net, mock_get_img, - mock_get_net, mock_get_comp_cnt): + def test_prepare_env_router_creation_failed(self, *args): self.rally_base.TESTS = ['test1', 'test2'] self.rally_base.test_name = 'test1' with self.assertRaises(Exception): self.rally_base._prepare_env() - mock_create_net.assert_called() - mock_get_img.assert_called() - mock_get_net.assert_called() - mock_create_router.assert_called() - mock_get_comp_cnt.assert_called() + for func in args: + func.assert_called() @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.' 'get_active_compute_cnt') @@ -333,18 +332,14 @@ class OSRallyTesting(unittest.TestCase): @mock.patch('snaps.openstack.utils.deploy_utils.create_router') @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create', return_value=None) - def test_prepare_env_flavor_creation_failed( - self, mock_create_flavor, mock_create_router, mock_create_net, - mock_get_img, mock_get_net, mock_get_comp_cnt): + def test_prepare_env_flavor_creation_failed(self, mock_create_flavor, + *args): self.rally_base.TESTS = ['test1', 'test2'] self.rally_base.test_name = 'test1' with self.assertRaises(Exception): self.rally_base._prepare_env() - mock_create_net.assert_called() - mock_get_img.assert_called() - mock_get_net.assert_called() - mock_create_router.assert_called() - mock_get_comp_cnt.assert_called() + for func in args: + func.assert_called() mock_create_flavor.assert_called_once() @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.' @@ -356,18 +351,14 @@ class OSRallyTesting(unittest.TestCase): @mock.patch('snaps.openstack.utils.deploy_utils.create_router') @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create', side_effect=[mock.Mock, None]) - def test_prepare_env_flavor_alt_creation_failed( - self, mock_create_flavor, mock_create_router, mock_create_net, - mock_get_img, mock_get_net, mock_get_comp_cnt): + def test_prepare_env_flavor_alt_creation_failed(self, mock_create_flavor, + *args): self.rally_base.TESTS = ['test1', 'test2'] self.rally_base.test_name = 'test1' with self.assertRaises(Exception): self.rally_base._prepare_env() - mock_create_net.assert_called() - mock_get_img.assert_called() - mock_get_net.assert_called() - mock_create_router.assert_called() - mock_get_comp_cnt.assert_called() + for func in args: + func.assert_called() self.assertEqual(mock_create_flavor.call_count, 2) @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.' @@ -407,7 +398,8 @@ class OSRallyTesting(unittest.TestCase): '_clean_up') def test_run_default(self, *args): self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_OK) - map(lambda m: m.assert_called(), args) + for func in args: + func.assert_called() @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' 'create_rally_deployment', side_effect=Exception) @@ -415,12 +407,12 @@ class OSRallyTesting(unittest.TestCase): self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR) mock_create_rally_dep.assert_called() - @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.' - '_prepare_env', side_effect=Exception) @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' 'create_rally_deployment', return_value=mock.Mock()) - def test_run_exception_prepare_env(self, mock_create_rally_dep, - mock_prep_env): + @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.' + '_prepare_env', side_effect=Exception) + def test_run_exception_prepare_env(self, mock_prep_env, *args): + # pylint: disable=unused-argument self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR) mock_prep_env.assert_called() diff --git a/tox.ini b/tox.ini index bf542a9b3..01de45c83 100644 --- a/tox.ini +++ b/tox.ini @@ -34,6 +34,7 @@ modules = functest.cli functest.core functest.energy + functest.opnfv_tests.openstack.rally functest.opnfv_tests.openstack.refstack_client functest.opnfv_tests.openstack.snaps functest.opnfv_tests.openstack.vping @@ -44,6 +45,7 @@ modules = functest.tests.unit.core functest.tests.unit.energy functest.tests.unit.odl + functest.tests.unit.openstack.rally functest.tests.unit.openstack.refstack_client functest.tests.unit.openstack.snaps functest.tests.unit.openstack.vping @@ -59,7 +61,7 @@ commands = pylint -f parseable --disable=locally-disabled functest | \ tee pylint.out | sed -ne '/Raw metrics/,//p'" pylint --reports=n --errors-only functest - pylint --disable=locally-disabled --reports=n {[testenv:pylint]modules} + pylint --disable=locally-disabled --ignore-imports=y --reports=n {[testenv:pylint]modules} [testenv:yamllint] basepython = python2.7 -- cgit 1.2.3-korg