diff --git a/nova/tests/unit/virt/zvm/test_zvm.py b/nova/tests/unit/virt/zvm/test_zvm.py index 721012b..916a4d3 100644 --- a/nova/tests/unit/virt/zvm/test_zvm.py +++ b/nova/tests/unit/virt/zvm/test_zvm.py @@ -29,7 +29,6 @@ from nova.compute import power_state from nova import context from nova import db from nova import exception as nova_exception -from nova.i18n import _ from nova.image import glance from nova.network import model from nova import test @@ -56,7 +55,8 @@ class FakeXCATConn(object): def __init__(self): pass - def request(self, one, two, three=None, four={}): + def request(self, one, two, three=None, four=None): + four = four or {} pass @@ -287,7 +287,7 @@ class ZVMDriverTestCases(ZVMTestCase): self._set_fake_xcat_responses([self._fake_instance_list_data()]) inst_list = self.driver.list_instances() self.mox.VerifyAll() - self.assertTrue("os000001" in inst_list) + self.assertIn("os000001", inst_list) def test_list_instances_exclude_xcat_master(self): self.flags(zvm_xcat_master='xcat') @@ -297,8 +297,8 @@ class ZVMDriverTestCases(ZVMTestCase): {'data': [{'data': fake_inst_list}]}]) inst_list = self.driver.list_instances() self.mox.VerifyAll() - self.assertTrue("os000001" in inst_list) - self.assertTrue("xcat" not in inst_list) + self.assertIn("os000001", inst_list) + self.assertNotIn("xcat", inst_list) def test_get_available_resource(self): self._set_fake_xcat_responses([self._fake_host_rinv_info(), @@ -2044,7 +2044,7 @@ class ZVMUtilsTestCases(ZVMTestCase): 'node': ['Warn ...'], 'error': []} self.mox.StubOutWithMock(zvmutils.LOG, 'info') - msg = _("Warning from xCAT: %s") + msg = ("Warning from xCAT: %s") zvmutils.LOG.info(msg % str(resp['node'])) self.mox.ReplayAll() @@ -2114,7 +2114,7 @@ class ZVMUtilsTestCases(ZVMTestCase): 'NETTYPE="qeth"\nONBOOT="yes"\nPORTNAME="PORT1000"\n' 'OPTIONS="layer2=1"\nSUBCHANNELS=' '"0.0.1000,0.0.1001,0.0.1002"\n') - self.assertEqual(cmd_str, None) + self.assertIsNone(cmd_str) self.assertEqual(dns_str, '') self.assertEqual(route_str, '') @@ -2360,7 +2360,7 @@ class FCPTestCase(ZVMTestCase): def test_init(self): self.assertEqual('b83d', self.fcp.get_dev_no()) - self.assertEqual(None, self.fcp.get_npiv_port()) + self.assertIsNone(self.fcp.get_npiv_port()) self.assertEqual('5A', self.fcp.get_chpid()) self.assertEqual('20076d8500005181', self.fcp.get_physical_port()) self.assertTrue(self.fcp.is_valid()) @@ -3682,26 +3682,26 @@ class ZVMDistManagerTestCases(test.TestCase): 'REDHAT6.4'] for v in os_versions: d = self.dist_manager.get_linux_dist(v)() - self.assertTrue(isinstance(d, dist.rhel6)) + self.assertIsInstance(d, dist.rhel6) def test_rhel7(self): os_versions = ['rhel7.1', 'red hat7.1', 'redhat7.1', 'RHEL7.1'] for v in os_versions: d = self.dist_manager.get_linux_dist(v)() - self.assertTrue(isinstance(d, dist.rhel7)) + self.assertIsInstance(d, dist.rhel7) def test_sles11(self): os_versions = ['sles11sp2', 'sles11sp3', 'sles11.2', 'sles11.3', 'Sles11sp3', 'SLES11.2'] for v in os_versions: d = self.dist_manager.get_linux_dist(v)() - self.assertTrue(isinstance(d, dist.sles11)) + self.assertIsInstance(d, dist.sles11) def test_sles12(self): os_versions = ['sles12', 'sles12.0', 'Sles12', 'SLES12.0'] for v in os_versions: d = self.dist_manager.get_linux_dist(v)() - self.assertTrue(isinstance(d, dist.sles12)) + self.assertIsInstance(d, dist.sles12) def test_invalid(self): os_versions = ['', 'sles 11.0', 'sles13.0', 'sles10', 'rhel8', diff --git a/nova/virt/zvm/driver.py b/nova/virt/zvm/driver.py index 6747fde..ee0609b 100644 --- a/nova/virt/zvm/driver.py +++ b/nova/virt/zvm/driver.py @@ -31,7 +31,7 @@ from nova.compute import power_state from nova.compute import task_states from nova.compute import vm_mode from nova import exception as nova_exception -from nova.i18n import _, _LI +from nova.i18n import _, _LI, _LW from nova.image import glance from nova import utils from nova.virt import configdrive @@ -56,20 +56,20 @@ zvm_opts = [ help='Host name or IP address of xCAT management_node'), cfg.StrOpt('zvm_xcat_username', default=None, - help='xCAT username'), + help='XCAT username'), cfg.StrOpt('zvm_xcat_password', default=None, secret=True, help='Password of the xCAT user'), cfg.StrOpt('zvm_diskpool', default=None, - help='z/VM disk pool for ephemeral disks'), + help='Z/VM disk pool for ephemeral disks'), cfg.StrOpt('zvm_host', default=None, - help='z/VM host that managed by xCAT MN.'), + help='Z/VM host that managed by xCAT MN.'), cfg.StrOpt('zvm_xcat_group', default='all', - help='xCAT group for OpenStack'), + help='XCAT group for OpenStack'), cfg.StrOpt('zvm_scsi_pool', default='xcatzfcp', help='Default zfcp scsi disk pool'), @@ -87,7 +87,7 @@ zvm_opts = [ help='Sets the admin password in the config drive'), cfg.StrOpt('zvm_vmrelocate_force', default=None, - help='force can be: (ARCHITECTURE) attempt relocation even ' + help='Force can be: (ARCHITECTURE) attempt relocation even ' 'though hardware architecture facilities or CP features ' 'are not available on destination system, ' '(DOMAIN) attempt relocation even though VM would be ' @@ -97,7 +97,7 @@ zvm_opts = [ 'resources on destination system.'), cfg.StrOpt('zvm_vmrelocate_immediate', default='yes', - help='immediate can be: (YES) VMRELOCATE command will do ' + help='Immediate can be: (YES) VMRELOCATE command will do ' 'one early pass through virtual machine storage and ' 'then go directly to the quiesce stage, ' 'or (NO) specifies immediate processing.'), @@ -113,7 +113,7 @@ zvm_opts = [ help='Timeout(seconds) when start an instance.'), cfg.IntOpt('zvm_xcat_connection_timeout', default=3600, - help='xCAT connection read timeout(seconds)'), + help='XCAT connection read timeout(seconds)'), cfg.IntOpt('zvm_console_log_size', default=100, help='Max console log size(kilobyte) get from xCAT'), @@ -199,7 +199,7 @@ class ZVMDriver(driver.ComputeDriver): except Exception as e: # Ignore any exceptions and log as warning emsg = zvmutils.format_exception_msg(e) - LOG.warn(_("Exception raised while initializing z/VM driver: %s") + LOG.warn(_LW("Exception raised while initializing z/VM driver: %s") % emsg) self._zvm_images = imageop.ZVMImages() @@ -217,7 +217,7 @@ class ZVMDriver(driver.ComputeDriver): self._volumeop.init_host(self._host_stats) except Exception as e: emsg = zvmutils.format_exception_msg(e) - LOG.warn(_("Exception raised while initializing z/VM driver: %s") + LOG.warn(_LW("Exception raised while initializing z/VM driver: %s") % emsg) def get_info(self, instance): @@ -240,7 +240,7 @@ class ZVMDriver(driver.ComputeDriver): emsg = err.format_message() if (emsg.__contains__("Invalid nodes and/or groups") and emsg.__contains__("Forbidden")): - LOG.warn(_("z/VM instance %s does not exist") % inst_name, + LOG.warn(_LW("z/VM instance %s does not exist") % inst_name, instance=instance) raise nova_exception.InstanceNotFound(instance_id=inst_name) else: @@ -652,7 +652,7 @@ class ZVMDriver(driver.ComputeDriver): zvm_inst.clean_volume_boot(context, instance, bdm, root_mount_device) except exception.ZVMBaseException as err: - LOG.warn(_("Failed to detach volume: %s") % + LOG.warn(_LW("Failed to detach volume: %s") % err.format_message(), instance=instance) if network_info: @@ -660,13 +660,13 @@ class ZVMDriver(driver.ComputeDriver): for vif in network_info: self._networkop.clean_mac_switch_host(inst_name) except exception.ZVMNetworkError: - LOG.warn(_("Clean MAC and VSWITCH failed while destroying " - "z/VM instance %s") % inst_name, + LOG.warn(_LW("Clean MAC and VSWITCH failed while " + "destroying z/VM instance %s") % inst_name, instance=instance) zvm_inst.delete_userid(self._get_hcp_info()['nodename']) else: - LOG.warn(_('Instance %s does not exist') % inst_name, + LOG.warn(_LW('Instance %s does not exist') % inst_name, instance=instance) def manage_image_cache(self, context, filtered_instances): @@ -769,7 +769,7 @@ class ZVMDriver(driver.ComputeDriver): try: self.power_on({}, instance, []) except nova_exception.InstancePowerOnFailure as err: - LOG.warning(_("Power On instance %(inst)s fail after capture, " + LOG.warn(_LW("Power On instance %(inst)s fail after capture, " "please check manually. The error is: %(err)s") % {'inst': instance['name'], 'err': err.format_message()}, instance=instance) @@ -779,7 +779,7 @@ class ZVMDriver(driver.ComputeDriver): except (exception.ZVMXCATRequestFailed, exception.ZVMInvalidXCATResponseDataError, exception.ZVMXCATInternalError) as err: - LOG.warning(_("Pause instance %(inst)s fail after capture, " + LOG.warn(_LW("Pause instance %(inst)s fail after capture, " "please check manually. The error is: %(err)s") % {'inst': instance['name'], 'err': err.format_message()}, instance=instance) @@ -1534,7 +1534,7 @@ class ZVMDriver(driver.ComputeDriver): instance, mountpoint, is_active, rollback=False) except exception.ZVMVolumeError: - LOG.warn(_("Failed to detach volume from %s") % + LOG.warn(_LW("Failed to detach volume from %s") % instance['name'], instance=instance) def _capture_disk_for_instance(self, context, instance): @@ -1714,7 +1714,7 @@ class ZVMDriver(driver.ComputeDriver): old_userid) if not self._is_nic_granted(new_inst._name): - msg = _("Failed to bind vswitch") + msg = _LW("Failed to bind vswitch") LOG.warn(msg, instance=instance) else: if power_on: @@ -1896,7 +1896,7 @@ class ZVMDriver(driver.ComputeDriver): """Wait until NIC is uncoupled from vswitch.""" if (CONF.zvm_reachable_timeout and timeutils.utcnow() > expiration): - LOG.warn(_("NIC update check failed.")) + LOG.warn(_LW("NIC update check failed.")) raise loopingcall.LoopingCallDone() is_granted = True @@ -1948,7 +1948,7 @@ class ZVMDriver(driver.ComputeDriver): console_log = zvm_inst.get_console_log(logsize) except exception.ZVMXCATInternalError: # Ignore no console log avaiable error - LOG.warn(_("No new console log avaiable.")) + LOG.warn(_LW("No new console log avaiable.")) log_path = self._pathutils.get_console_log_path(CONF.zvm_host, zvm_inst._name) append_to_log(console_log, log_path) diff --git a/nova/virt/zvm/imageop.py b/nova/virt/zvm/imageop.py index aadab3e..4d00585 100644 --- a/nova/virt/zvm/imageop.py +++ b/nova/virt/zvm/imageop.py @@ -26,7 +26,7 @@ from oslo_log import log as logging from oslo_utils import excutils from nova import exception as nova_exception -from nova.i18n import _ +from nova.i18n import _, _LW from nova.image import glance from nova import utils from nova.virt import images @@ -152,7 +152,7 @@ class ZVMImages(object): except (exception.ZVMXCATInternalError, exception.ZVMInvalidXCATResponseDataError, exception.ZVMXCATRequestFailed): - LOG.warn(_("Failed to delete image file %s from xCAT") % + LOG.warn(_LW("Failed to delete image file %s from xCAT") % image_name_xcat) def _delete_image_object_from_xcat(self, image_name_xcat): @@ -169,7 +169,7 @@ class ZVMImages(object): except (exception.ZVMXCATInternalError, exception.ZVMInvalidXCATResponseDataError, exception.ZVMXCATRequestFailed): - LOG.warn(_("Failed to delete image definition %s from xCAT") % + LOG.warn(_LW("Failed to delete image definition %s from xCAT") % image_name_xcat) def get_image_file_path_from_image_name(self, image_name_xcat): @@ -199,7 +199,7 @@ class ZVMImages(object): if os.path.exists(manifest_xml): xml_file = Dom.parse(manifest_xml) else: - LOG.warn(_('manifest.xml does not exist')) + LOG.warn(_LW('manifest.xml does not exist')) manifest['imagename'] = '' manifest['imagetype'] = '' manifest['osarch'] = '' @@ -516,7 +516,7 @@ class ZVMImages(object): except (exception.ZVMXCATRequestFailed, exception.ZVMInvalidXCATResponseDataError, exception.ZVMXCATInternalError) as err: - LOG.warn(_("Illegal date for last_use_date %s") % + LOG.warn(_LW("Illegal date for last_use_date %s") % err.format_message()) return last_use_date_string @@ -531,16 +531,16 @@ class ZVMImages(object): re.match(timere, last_use_date_string)): LOG.debug("The format for last_use_date is valid ") else: - LOG.warn(_("The format for image %s record in xcat table osimage's" - " last_used_date is not valid. The correct format is " - "auto:last_use_date:yyyy-mm-dd") % image_name) + LOG.warn(_LW("The format for image %s record in xcat table " + "osimage's last_used_date is not valid. The correct " + "format is auto:last_use_date:yyyy-mm-dd") % image_name) return try: last_use_date_datetime = datetime.datetime.strptime( last_use_date_string, '%Y-%m-%d') except Exception as err: - LOG.warn(_("Illegal date for last_use_date %(msg)s") % err) + LOG.warn(_LW("Illegal date for last_use_date %(msg)s") % err) return return last_use_date_datetime.date() diff --git a/nova/virt/zvm/instance.py b/nova/virt/zvm/instance.py index d51ba52..064037b 100644 --- a/nova/virt/zvm/instance.py +++ b/nova/virt/zvm/instance.py @@ -23,7 +23,7 @@ from oslo_utils import timeutils from nova.compute import power_state from nova import exception as nova_exception -from nova.i18n import _ +from nova.i18n import _, _LW from nova.virt import hardware from nova.virt.zvm import const from nova.virt.zvm import exception @@ -37,8 +37,9 @@ CONF = cfg.CONF class ZVMInstance(object): '''OpenStack instance that running on of z/VM hypervisor.''' - def __init__(self, instance={}): + def __init__(self, instance=None): """Initialize instance attributes for database.""" + instance = instance or {} self._xcat_url = zvmutils.XCATUrl() self._xcat_conn = zvmutils.XCATConnection() self._instance = instance @@ -54,7 +55,7 @@ class ZVMInstance(object): if ("Return Code: 200" in err_str and "Reason Code: 12" in err_str): # Instance already not active - LOG.warn(_("z/VM instance %s not active") % self._name) + LOG.warn(_LW("z/VM instance %s not active") % self._name) return else: msg = _("Failed to power off instance: %s") % err_str @@ -70,7 +71,7 @@ class ZVMInstance(object): if ("Return Code: 200" in err_str and "Reason Code: 8" in err_str): # Instance already not active - LOG.warn(_("z/VM instance %s already active") % self._name) + LOG.warn(_LW("z/VM instance %s already active") % self._name) return self._wait_for_reachable() @@ -88,7 +89,7 @@ class ZVMInstance(object): if ("Return Code: 200" in err_str and "Reason Code: 12" in err_str): # Be able to reset in power state of SHUTDOWN - LOG.warn(_("Reset z/VM instance %s from SHUTDOWN state") % + LOG.warn(_LW("Reset z/VM instance %s from SHUTDOWN state") % self._name) return else: @@ -146,7 +147,8 @@ class ZVMInstance(object): _instance_info.cpu_time_ns = cpu_time except exception.ZVMInvalidXCATResponseDataError: - LOG.warn(_("Failed to get inventory info for %s") % self._name) + LOG.warn(_LW("Failed to get inventory info for %s") + % self._name) _instance_info.state = power_stat _instance_info.max_mem_kb = max_mem_kb _instance_info.mem_kb = max_mem_kb @@ -295,7 +297,8 @@ class ZVMInstance(object): exception.ZVMVolumeError, exception.ZVMDriverError) as err: emsg = err.format_message() - msg = _("Failed to clean boot from volume preparations: %s") % emsg + msg = _LW("Failed to clean boot from volume " + "preparations: %s") % emsg LOG.warn(msg) raise exception.ZVMVolumeError(msg=msg) diff --git a/nova/virt/zvm/utils.py b/nova/virt/zvm/utils.py index 7601f52..9e347a3 100644 --- a/nova/virt/zvm/utils.py +++ b/nova/virt/zvm/utils.py @@ -28,8 +28,7 @@ from oslo_utils import excutils from nova import block_device from nova.compute import power_state from nova import exception as nova_exception -from nova.i18n import _ -from nova.i18n import _LE +from nova.i18n import _, _LE, _LI from nova.virt import driver from nova.virt.zvm import const from nova.virt.zvm import exception @@ -195,7 +194,7 @@ class XCATConnection(object): self.conn = httplib.HTTPSConnection(self.host, timeout=CONF.zvm_xcat_connection_timeout) - def request(self, method, url, body=None, headers={}): + def request(self, method, url, body=None, headers=None): """Send https request to xCAT server. Will return a python dictionary including: @@ -204,6 +203,7 @@ class XCATConnection(object): 'message': response message} """ + headers = headers or [] if body is not None: body = jsonutils.dumps(body) headers = {'content-type': 'text/plain', @@ -261,7 +261,8 @@ class XCATConnection(object): return resp -def xcat_request(method, url, body=None, headers={}): +def xcat_request(method, url, body=None, headers=None): + headers = headers or {} conn = XCATConnection() resp = conn.request(method, url, body, headers) return load_xcat_resp(resp['message']) @@ -325,7 +326,7 @@ def except_xcat_call_failed_and_reraise(exc, **kwargs): exception.ZVMXCATInternalError) as err: msg = err.format_message() kwargs['msg'] = msg - LOG.error('XCAT response return error: %s', msg) + LOG.error(_LE('XCAT response return error: %s'), msg) raise exc(**kwargs) @@ -428,7 +429,7 @@ def _log_warnings(resp): for msg in (resp['info'], resp['node'], resp['data']): msgstr = str(msg) if 'warn' in msgstr.lower(): - LOG.info(_("Warning from xCAT: %s") % msgstr) + LOG.info(_LI("Warning from xCAT: %s") % msgstr) def _is_warning_or_recoverable_issue(err_str): diff --git a/nova/virt/zvm/volumeop.py b/nova/virt/zvm/volumeop.py index f6c530a..efb7638 100644 --- a/nova/virt/zvm/volumeop.py +++ b/nova/virt/zvm/volumeop.py @@ -22,7 +22,7 @@ from oslo_serialization import jsonutils from oslo_utils import excutils import nova.context -from nova.i18n import _ +from nova.i18n import _, _LW from nova.objects import block_device as block_device_obj from nova.objects import instance as instance_obj from nova.virt.zvm import const @@ -45,7 +45,7 @@ class VolumeOperator(object): try: self._svc_driver.init_host(host_stats) except (exception.ZVMDriverError, exception.ZVMVolumeError) as err: - LOG.warning(_("Initialize zhcp failed. Reason: %s") % + LOG.warning(_LW("Initialize zhcp failed. Reason: %s") % err.format_message()) def attach_volume_to_instance(self, context, connection_info, instance, diff --git a/tox.ini b/tox.ini index 4cbd125..f0ade4d 100644 --- a/tox.ini +++ b/tox.ini @@ -31,5 +31,6 @@ ignore = E121,E122,E123,E124,E125,E126,E127,E128,E129,E131,E251,H405 exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,tools [hacking] +local-check-factory = nova.hacking.checks.factory import_exceptions = nova.i18n