From d371f2c5902df94a3648ea05f2fc24ffef6f6c91 Mon Sep 17 00:00:00 2001 From: Mark Syms Date: Thu, 30 May 2024 13:25:29 +0100 Subject: [PATCH] CP-49689: load error defs from sibling file Signed-off-by: Mark Syms --- drivers/xs_errors.py | 9 ++-- tests/test_BaseISCSI.py | 1 - tests/test_FileSR.py | 18 ++----- tests/test_HBASR.py | 8 --- tests/test_ISOSR.py | 42 ++++----------- tests/test_LVHDoFCoESR.py | 7 ++- tests/test_LVHDoHBASR.py | 4 -- tests/test_LVHDoISCSISR.py | 8 --- tests/test_NFSSR.py | 4 -- tests/test_SMBSR.py | 15 ++---- tests/test_SR.py | 6 +-- tests/test_SRCommand.py | 3 +- tests/test_blktap2.py | 1 - tests/test_cbt.py | 101 ++++++++----------------------------- tests/test_mpath_dmp.py | 32 ++---------- tests/test_srmetadata.py | 3 -- tests/test_testlib.py | 28 ---------- tests/test_trim_util.py | 13 ----- tests/test_util.py | 2 - tests/test_vhdutil.py | 8 +-- tests/test_xs_errors.py | 14 +++-- tests/testlib.py | 24 --------- 22 files changed, 67 insertions(+), 284 deletions(-) diff --git a/drivers/xs_errors.py b/drivers/xs_errors.py index 95f59f412..081f5f106 100644 --- a/drivers/xs_errors.py +++ b/drivers/xs_errors.py @@ -22,7 +22,8 @@ import util import xmlrpc.client -XML_DEFS = '/opt/xensource/sm/XE_SR_ERRORCODES.xml' +DEF_LOC = os.path.dirname(__file__) +XML_DEFS = os.path.join(DEF_LOC, 'XE_SR_ERRORCODES.xml') class SRException(Exception): @@ -33,7 +34,8 @@ def __init__(self, reason): Exception.__init__(self, reason) def toxml(self): - return xmlrpc.client.dumps(xmlrpc.client.Fault(int(self.errno), str(self)), "", True) + return xmlrpc.client.dumps(xmlrpc.client.Fault( + int(self.errno), str(self)), "", True) class SROSError(SRException): @@ -68,7 +70,8 @@ def __new__(self, key, opterr=None): errormessage = subdict['description'] if opterr is not None: errormessage += " [opterr=%s]" % opterr - util.SMlog("Raising exception [%d, %s]" % (errorcode, errormessage)) + util.SMlog("Raising exception [%d, %s]" % + (errorcode, errormessage)) return SROSError(errorcode, errormessage) # development error diff --git a/tests/test_BaseISCSI.py b/tests/test_BaseISCSI.py index 7cf0e42df..cc489a46a 100644 --- a/tests/test_BaseISCSI.py +++ b/tests/test_BaseISCSI.py @@ -85,7 +85,6 @@ def test_attach_tgt_present_path_found(self, mock_init_paths): self.subject.attach(self.sr_uuid) @mock.patch('BaseISCSI.BaseISCSISR._initPaths', autospec=True) - @mock.patch('BaseISCSI.xs_errors.XML_DEFS', "drivers/XE_SR_ERRORCODES.xml") def test_attach_tgt_present_path_not_found(self, mock_init_paths): # Arrange self.mock_util._testHost.return_value = None diff --git a/tests/test_FileSR.py b/tests/test_FileSR.py index e1133087f..87d2ea8fd 100644 --- a/tests/test_FileSR.py +++ b/tests/test_FileSR.py @@ -47,10 +47,6 @@ def setUp(self): gethidden_patch = mock.patch('FileSR.vhdutil.getHidden') self.mock_gethidden = gethidden_patch.start() - errors_patcher = mock.patch('FileSR.xs_errors.XML_DEFS', - "drivers/XE_SR_ERRORCODES.xml") - errors_patcher.start() - fist_patcher = mock.patch('FileSR.util.FistPoint.is_active', autospec=True) self.mock_fist = fist_patcher.start() @@ -254,7 +250,8 @@ def my_stat(tgt): return real_stat(tgt) # Act - with self.assertRaises(xs_errors.SROSError) as srose, mock.patch('FileSR.os.stat') as mock_stat: + with self.assertRaises(xs_errors.SROSError), \ + mock.patch('FileSR.os.stat') as mock_stat: mock_stat.side_effect = my_stat clone_xml = vdi.clone(sr_uuid, vdi_uuid) @@ -301,7 +298,8 @@ def my_stat(tgt): return real_stat(tgt) # Act - with self.assertRaises(xs_errors.SROSError) as srose, mock.patch('FileSR.os.stat') as mock_stat: + with self.assertRaises(xs_errors.SROSError), \ + mock.patch('FileSR.os.stat') as mock_stat: mock_stat.side_effect = my_stat clone_xml = vdi.clone(sr_uuid, vdi_uuid) @@ -520,9 +518,7 @@ def test_attach_fist_active(self): self.mock_session.xenapi.message.create.assert_called_with( 'sr_does_not_support_hardlinks', 2, "SR", self.sr_uuid, mock.ANY) - @testlib.with_context - def test_attach_not_writable(self, context): - context.setup_error_codes() + def test_attach_not_writable(self): test_sr = self.create_test_sr() with mock.patch('FileSR.open') as mock_open: @@ -566,10 +562,6 @@ def setUp(self): pread_patcher = mock.patch('FileSR.util.pread') self.mock_pread = pread_patcher.start() - errors_patcher = mock.patch('FileSR.xs_errors.XML_DEFS', - "drivers/XE_SR_ERRORCODES.xml") - errors_patcher.start() - sr_init_patcher = mock.patch('SR.SR.__init__') def fake_sr_init(self, srcmd, sr_uuid): self.sr_ref = False diff --git a/tests/test_HBASR.py b/tests/test_HBASR.py index 7fd6095d5..119506ada 100644 --- a/tests/test_HBASR.py +++ b/tests/test_HBASR.py @@ -145,8 +145,6 @@ def test__init_hbadict(self, mock_cacheSCSIidentifiers, self.assertEqual(sr2.devs, "123445") @mock.patch('HBASR.HBASR.__init__', mock_init) - @mock.patch('LVHDoHBASR.xs_errors.XML_DEFS', - "drivers/XE_SR_ERRORCODES.xml") @mock.patch('HBASR.HBASR._probe_hba', autospec=True) @mock.patch('HBASR.xml.dom.minidom.parseString', autospec=True) def test__init_hbahostname_assert(self, mock_parseString, mock_probe_hba): @@ -167,8 +165,6 @@ def test__init_hbahostname(self): self.assertEqual(res, "20-00-00-e0-8b-18-20-8b") @mock.patch('HBASR.HBASR.__init__', mock_init) - @mock.patch('LVHDoHBASR.xs_errors.XML_DEFS', - "drivers/XE_SR_ERRORCODES.xml") @mock.patch('HBASR.HBASR._probe_hba', autospec=True) @mock.patch('HBASR.xml.dom.minidom.parseString', autospec=True) def test__init_hbas_assert(self, mock_parseString, mock_probe_hba): @@ -190,8 +186,6 @@ def test__init_hbas(self): 'host1': '50-01-43-80-24-26-ba-f4'}) @mock.patch('HBASR.HBASR.__init__', mock_init) - @mock.patch('LVHDoHBASR.xs_errors.XML_DEFS', - "drivers/XE_SR_ERRORCODES.xml") @mock.patch('HBASR.util.pread', autospec=True) def test__probe_hba_assert(self, mock_pread): sr = HBASR.HBASR() @@ -203,8 +197,6 @@ def test__probe_hba_assert(self, mock_pread): "[opterr=HBA probe failed]") @mock.patch('HBASR.HBASR.__init__', mock_init) - @mock.patch('LVHDoHBASR.xs_errors.XML_DEFS', - "drivers/XE_SR_ERRORCODES.xml") @mock.patch('HBASR.util.pread', autospec=True) @mock.patch('HBASR.util.listdir', autospec=True) def test__probe_hba(self, mock_listdir, mock_pread): diff --git a/tests/test_ISOSR.py b/tests/test_ISOSR.py index 928f73d30..3aea79633 100644 --- a/tests/test_ISOSR.py +++ b/tests/test_ISOSR.py @@ -155,14 +155,14 @@ def test_attach_nfs_no_server( sr_uuid='asr_uuid') _checkmount.side_effect = [False] - testHost.side_effect = xs_errors.SROSError(140, 'Incorrect DNS name, unable to resolve.') + testHost.side_effect = xs_errors.SROSError( + 140, 'Incorrect DNS name, unable to resolve.') with self.assertRaises(xs_errors.SROSError) as ose: isosr.attach(None) self.assertEqual(140, ose.exception.errno) - @testlib.with_context @mock.patch('util.gen_uuid', autospec=True) @mock.patch('nfs.soft_mount', autospec=True) @mock.patch('util._convertDNS', autospec=True) @@ -173,10 +173,8 @@ def test_attach_nfs_no_server( # Can't use autospec due to http://bugs.python.org/issue17826 @mock.patch('ISOSR.ISOSR._checkmount') def test_attach_nfs_wrong_version( - self, context, _checkmount, check_server_tcp, testHost, makedirs, + self, _checkmount, check_server_tcp, testHost, makedirs, validate_nfsversion, convertDNS, soft_mount, gen_uuid): - context.setup_error_codes() - isosr = self.create_isosr(location='aServer:/aLocation', atype='nfs_iso', sr_uuid='asr_uuid') @@ -231,7 +229,6 @@ def test_attach_with_smb_version_1(self, context, _checkmount, pread, """ Positive case, over XC/XE CLI with version 1.0. """ - context.setup_error_codes() smbsr = self.create_smbisosr(atype='cifs', vers='1.0') _checkmount.side_effect = [False, True] smbsr.attach(None) @@ -250,7 +247,6 @@ def test_attach_with_smb_credentials(self, context, _checkmount, pread, """ Positive case, over XC/XE CLI with version 1.0. """ - context.setup_error_codes() update = {'username': 'dot', 'cifspassword': 'winter2019'} smbsr = self.create_smbisosr(atype='cifs', vers='1.0', dconf_update=update) @@ -272,7 +268,6 @@ def test_attach_with_smb_credentials_domain(self, context, """ Positive case, over XC/XE CLI with version 1.0. """ - context.setup_error_codes() update = {'username': r'citrix\jsmith', 'cifspassword': 'winter2019'} smbsr = self.create_smbisosr(atype='cifs', vers='1.0', dconf_update=update) @@ -293,7 +288,6 @@ def test_attach_with_smb_version_3(self, context, _checkmount, pread, """ Positive case, over XC/XE CLI with version 3.0. """ - context.setup_error_codes() smbsr = self.create_smbisosr(atype='cifs', vers='3.0') _checkmount.side_effect = [False, True] smbsr.attach(None) @@ -314,7 +308,6 @@ def test_attach_with_smb_no_version(self, context, """ Positive case, over XC/XE CLI without version. """ - context.setup_error_codes() smbsr = self.create_smbisosr(atype='cifs') _checkmount.side_effect = [False, True] smbsr.attach(None) @@ -333,7 +326,6 @@ def test_attach_smb_via_xemount_version_1(self, context, pread, _checkmount, """ Positive case, over xe-sr-mount CLI with version 1.0. """ - context.setup_error_codes() smbsr = self.create_smbisosr(options='-o username=administrator,password=password,vers=1.0') smbsr.attach(None) self.assertEqual(0, pread.call_count) @@ -350,7 +342,6 @@ def test_attach_smb_via_xemount_version_3(self, context, pread, """ Positive case, over xe-sr-mount CLI with version 3.0. """ - context.setup_error_codes() smbsr = self.create_smbisosr(options='-o username=administrator,password=password,vers=3.0') smbsr.attach(None) self.assertEqual(0, pread.call_count) @@ -370,24 +361,20 @@ def test_attach_smb_via_xemount_no_version(self, context, pread, """ Positive case, without version from xe-sr-mount. """ - context.setup_error_codes() smbsr = self.create_smbisosr(options='-o username=administrator,password=password') smbsr.attach(None) self.assertEqual(0, pread.call_count) - @testlib.with_context @mock.patch('util.gen_uuid') @mock.patch('util.makedirs') @mock.patch('ISOSR.ISOSR._checkTargetStr') @mock.patch('util.pread', autospec=True) - def test_attach_smb_wrongversion(self, context, pread, _checkTargetStr, + def test_attach_smb_wrongversion(self, pread, _checkTargetStr, makedirs, gen_uuid): """ Unsupported version from XC/XE CLI. """ - context.setup_error_codes() smbsr = self.create_smbisosr(atype='cifs', vers='2.0') - raised_exception = None with self.assertRaises(xs_errors.SROSError) as context: smbsr.attach(None) self.assertEqual(context.exception.errno, 227) @@ -396,17 +383,15 @@ def test_attach_smb_wrongversion(self, context, pread, _checkTargetStr, 'Given SMB version is not allowed. Choose either 1.0 or 3.0' ) - @testlib.with_context @mock.patch('util.gen_uuid') @mock.patch('util.makedirs') @mock.patch('ISOSR.ISOSR._checkTargetStr') - def test_attach_smb_wrongversion_via_xemount(self, context, + def test_attach_smb_wrongversion_via_xemount(self, _checkTargetStr, makedirs, gen_uuid): """ Unsupported version from xe-sr-mount. """ - context.setup_error_codes() smbsr = self.create_smbisosr(options='-o vers=2.0') with self.assertRaises(xs_errors.SROSError) as context: smbsr.attach(None) @@ -428,7 +413,6 @@ def test_attach_smb_version_fallback_with_smb_3_disabled(self, context, """ Fall back scenario from XC/XE CLI with smb3 diabled and smb1 enabled. """ - context.setup_error_codes() smbsr = self.create_smbisosr(atype='cifs') pread.side_effect = iter([util.CommandException(errno.EHOSTDOWN), " "]) _checkmount.side_effect = [False, True] @@ -437,13 +421,12 @@ def test_attach_smb_version_fallback_with_smb_3_disabled(self, context, '/var/run/sr-mount/asr_uuid', '-o', 'cache=none,guest,vers=1.0'], True, new_env=None) - @testlib.with_context @mock.patch('util.gen_uuid') @mock.patch('util.makedirs') @mock.patch('ISOSR.ISOSR._checkTargetStr') @mock.patch('util.pread', autospec=True) @mock.patch('ISOSR.ISOSR._checkmount') - def test_attach_smb_version_fallback_with_smb_1_3_disabled(self, context, + def test_attach_smb_version_fallback_with_smb_1_3_disabled(self, _checkmount, pread, _checkTargetStr, @@ -452,7 +435,6 @@ def test_attach_smb_version_fallback_with_smb_1_3_disabled(self, context, """ Fall back scenario from XC/XE CLI with smb3 diabled and smb1 disabled. """ - context.setup_error_codes() smbsr = self.create_smbisosr(atype='cifs') pread.side_effect = iter([util.CommandException(errno.EHOSTDOWN), \ util.CommandException(errno.EHOSTDOWN), util.CommandException(errno.EHOSTDOWN)]) @@ -465,20 +447,18 @@ def test_attach_smb_version_fallback_with_smb_1_3_disabled(self, context, 'Could not mount the directory specified in Device Configuration [opterr=exec failed]' ) - @testlib.with_context @mock.patch('util.gen_uuid') @mock.patch('util.makedirs') @mock.patch('ISOSR.ISOSR._checkTargetStr') @mock.patch('util.pread', autospec=True) @mock.patch('ISOSR.ISOSR._checkmount') - def test_attach_smb_via_xemount_no_version_fallback(self, context, + def test_attach_smb_via_xemount_no_version_fallback(self, _checkmount, pread, _checkTargetStr, makedirs, gen_uuid): """ Fall back scenario from xe-sr-mount with smb3 diabled and smb1 enabled. """ - context.setup_error_codes() smbsr = self.create_smbisosr(options='-o username=administrator,password=password') pread.side_effect = iter([util.CommandException(errno.EHOSTDOWN), " "]) @@ -493,7 +473,6 @@ def test_attach_smb_version_fallback_error(self, context, _checkmount, """ Fall back scenario negative case from xe-sr-mount with smb3 diabled and smb1 disabled. """ - context.setup_error_codes() smbsr = self.create_smbisosr(atype='cifs') pread.side_effect = iter([util.CommandException(errno.EHOSTDOWN), util.CommandException(errno.EHOSTDOWN)]) @@ -501,23 +480,22 @@ def test_attach_smb_version_fallback_error(self, context, _checkmount, with self.assertRaises(Exception): smbsr.attach(None) - @testlib.with_context @mock.patch('util.makedirs') @mock.patch('ISOSR.ISOSR._checkTargetStr') @mock.patch('util.pread', autospec=True) @mock.patch('util.find_my_pbd') @mock.patch('ISOSR.ISOSR._checkmount') - def test_mountoversmb_will_raise_on_error(self, context, _checkmount, find_my_pbd, pread, _checkTargetStr, makedirs): + def test_mountoversmb_will_raise_on_error( + self, _checkmount, find_my_pbd, pread, _checkTargetStr, makedirs): """ Test failure to store SMB version inside PBD config will raise exception """ - context.setup_error_codes() smbsr = self.create_smbisosr(atype='cifs') find_my_pbd.return_value = None _checkmount.side_effect = [False, True] with self.assertRaises(xs_errors.SROSError) as exp: smbsr.attach(None) - self.assertEqual(exp.exception.errno, context.get_error_code("SMBMount")) + self.assertEqual(exp.exception.errno, 111) class TestISOSR_functions(unittest.TestCase): diff --git a/tests/test_LVHDoFCoESR.py b/tests/test_LVHDoFCoESR.py index 5a9ca83d9..a3feec8f0 100644 --- a/tests/test_LVHDoFCoESR.py +++ b/tests/test_LVHDoFCoESR.py @@ -41,13 +41,12 @@ def create_fcoesr(self, path="/dev/example", SCSIid="abcd", @mock.patch('SR.driver', autospec=True) @mock.patch('util.find_my_pbd', autospec=True) @mock.patch('LVHDoFCoESR.LVHDoHBASR.HBASR.HBASR.print_devs', autospec=True) - @testlib.with_context - def test_load_no_scsiid(self, context, print_devs, find_my_pbd, driver): - context.setup_error_codes() + def test_load_no_scsiid(self, print_devs, find_my_pbd, driver): find_my_pbd.return_value = ['pbd_ref', 'pbd'] parameters = {} parameters['device_config'] = "" - self.assertRaises(xs_errors.SROSError, self.create_fcoesr, SCSIid="", params=parameters) + self.assertRaises(xs_errors.SROSError, self.create_fcoesr, + SCSIid="", params=parameters) @mock.patch('SR.driver', autospec=True) @mock.patch('util.find_my_pbd', autospec=True) diff --git a/tests/test_LVHDoHBASR.py b/tests/test_LVHDoHBASR.py index a05279adc..3bc7196b6 100644 --- a/tests/test_LVHDoHBASR.py +++ b/tests/test_LVHDoHBASR.py @@ -47,8 +47,6 @@ def test_generate_config(self, mpath_handle) @mock.patch('LVHDoHBASR.LVHDoHBASR', autospec=True) - @mock.patch('LVHDoHBASR.xs_errors.XML_DEFS', - "drivers/XE_SR_ERRORCODES.xml") @mock.patch('LVHDoHBASR.LVHDoHBAVDI.__init__', mock_init) @mock.patch('LVHDoHBASR.lvutil._checkLV', autospec=True) def test_generate_config_bad_path_assert(self, @@ -125,8 +123,6 @@ def mock_parse(self): srcmd.parse() return srcmd - @mock.patch('LVHDoHBASR.xs_errors.XML_DEFS', - "drivers/XE_SR_ERRORCODES.xml") @mock.patch("builtins.open", new_callable=mock.mock_open()) @mock.patch('LVHDoHBASR.glob.glob', autospec=True) def test_sr_delete_no_multipath(self, mock_glob, mock_open): diff --git a/tests/test_LVHDoISCSISR.py b/tests/test_LVHDoISCSISR.py index 875a35ab8..3b5e1c420 100644 --- a/tests/test_LVHDoISCSISR.py +++ b/tests/test_LVHDoISCSISR.py @@ -85,13 +85,9 @@ def setUp(self): self.addCleanup(mock.patch.stopall) @mock.patch('iscsilib.ensure_daemon_running_ok') - @testlib.with_context def test_1st_try_block_raise_XenError( self, - context, mock_iscsilib_ensure_daemon_running_ok): - context.setup_error_codes() - mock_iscsilib_ensure_daemon_running_ok.side_effect = xs_errors.XenError( 'ISCSIInitiator', 'Raise XenError' @@ -107,13 +103,9 @@ def test_1st_try_block_raise_XenError( ) @mock.patch('iscsilib.ensure_daemon_running_ok') - @testlib.with_context def test_1st_try_block_raise_RandomError( self, - context, mock_iscsilib_ensure_daemon_running_ok): - context.setup_error_codes() - mock_iscsilib_ensure_daemon_running_ok.side_effect = RandomError( 'Raise RandomError' ) diff --git a/tests/test_NFSSR.py b/tests/test_NFSSR.py index 418b2f709..fa92d5e23 100644 --- a/tests/test_NFSSR.py +++ b/tests/test_NFSSR.py @@ -92,7 +92,6 @@ def test_sr_create(self, validate_nfsversion, check_server_tcp, _testhost, @mock.patch('util._testHost') @mock.patch('nfs.check_server_tcp') @mock.patch('nfs.validate_nfsversion') - @mock.patch('SR.xs_errors.XML_DEFS', "drivers/XE_SR_ERRORCODES.xml") def test_sr_create_readonly(self, validate_nfsversion, check_server_tcp, _testhost, soft_mount, Lock, makedirs): # Arrange @@ -119,7 +118,6 @@ def mock_makedirs(path): @mock.patch('util._testHost') @mock.patch('nfs.check_server_tcp') @mock.patch('nfs.validate_nfsversion') - @mock.patch('SR.xs_errors.XML_DEFS', "drivers/XE_SR_ERRORCODES.xml") def test_sr_create_noperm(self, validate_nfsversion, check_server_tcp, _testhost, soft_mount, Lock, makedirs): # Arrange @@ -148,8 +146,6 @@ def mock_makedirs(path): @mock.patch('util._testHost') @mock.patch('nfs.check_server_tcp') @mock.patch('nfs.validate_nfsversion') - @mock.patch('NFSSR.xs_errors.XML_DEFS', - 'drivers/XE_SR_ERRORCODES.xml') def test_sr_create_mount_error( self, validate_nfsversion, check_server_tcp, _testhost, soft_mount, Lock, mock_rmdir): diff --git a/tests/test_SMBSR.py b/tests/test_SMBSR.py index c7ab41edb..d20bcc0c6 100644 --- a/tests/test_SMBSR.py +++ b/tests/test_SMBSR.py @@ -71,13 +71,11 @@ def create_smbsr(self, sr_uuid='asr_uuid', server='\\aServer', serverpath='/aSer return smbsr #Attach - @testlib.with_context @mock.patch('SMBSR.SMBSR.checkmount', autospec=True) @mock.patch('SMBSR.SMBSR.mount', autospec=True) @mock.patch('SMBSR.Lock', autospec=True) - def test_attach_smbexception_raises_xenerror(self, context, mock_lock, mock_mount, mock_checkmount): - context.setup_error_codes() - + def test_attach_smbexception_raises_xenerror( + self, mock_lock, mock_mount, mock_checkmount): smbsr = self.create_smbsr() mock_mount.side_effect = SMBSR.SMBException("mount raised SMBException") mock_checkmount.return_value = False @@ -218,15 +216,14 @@ def test_attach_misc_mount_failure(self, mock_pathexists, mock_unlink, mock_unlink.assert_not_called() #Detach - @testlib.with_context @mock.patch('SMBSR.SMBSR.checkmount', return_value=True, autospec=True) @mock.patch('SMBSR.SMBSR.unmount', autospec=True) @mock.patch('SMBSR.Lock', autospec=True) @mock.patch('SMBSR.os.chdir', autospec=True) @mock.patch('SMBSR.cleanup', autospec=True) - def test_detach_smbexception_raises_xenerror(self, context, mock_cleanup, mock_chdir, mock_lock, mock_unmount, mock_checkmount): - context.setup_error_codes() - + def test_detach_smbexception_raises_xenerror( + self, mock_cleanup, mock_chdir, mock_lock, + mock_unmount, mock_checkmount): smbsr = self.create_smbsr() mock_unmount.side_effect = SMBSR.SMBException("unmount raised SMBException") with self.assertRaises(xs_errors.SROSError) as cm: @@ -286,7 +283,6 @@ def test_create_success(self, symlink, lock, restrict, makedirs): @mock.patch('util.get_pool_restrictions', autospec=True) @mock.patch('SMBSR.Lock', autospec=True) @mock.patch('SMBSR.os.symlink', autospec=True) - @mock.patch('SR.xs_errors.XML_DEFS', "drivers/XE_SR_ERRORCODES.xml") def test_create_read_only(self, symlink, lock, restrict, makedirs): # Arrange smbsr = self.create_smbsr() @@ -316,7 +312,6 @@ def mock_makedirs(path): @mock.patch('util.get_pool_restrictions', autospec=True) @mock.patch('SMBSR.Lock', autospec=True) @mock.patch('SMBSR.os.symlink', autospec=True) - @mock.patch('SR.xs_errors.XML_DEFS', "drivers/XE_SR_ERRORCODES.xml") def test_create_nospace(self, symlink, lock, restrict, makedirs): # Arrange smbsr = self.create_smbsr() diff --git a/tests/test_SR.py b/tests/test_SR.py index 4137c4d5c..b139f4c5f 100644 --- a/tests/test_SR.py +++ b/tests/test_SR.py @@ -40,14 +40,13 @@ def test_device_check_success(self): checker.verify() - @mock.patch('SR.xs_errors.XML_DEFS', "drivers/XE_SR_ERRORCODES.xml") def test_device_check_nodevice(self): """ Test the device check decorator with no device configured """ checker = TestSR.deviceTest() - with self.assertRaises(xs_errors.SROSError) as sre: + with self.assertRaises(xs_errors.SROSError): checker.verify() @mock.patch('SR.SR.scan', autospec=True) @@ -74,7 +73,8 @@ def test_after_master_attach_vdi_not_available( sr1 = self.create_SR("sr_create", {'ISCSIid': '12333423'}, {'session_ref': 'session1'}) - mock_scan.side_effect = xs_errors.SROSError(46, "The VDI is not available") + mock_scan.side_effect = xs_errors.SROSError( + 46, "The VDI is not available") sr1.after_master_attach('dummy uuid') diff --git a/tests/test_SRCommand.py b/tests/test_SRCommand.py index bda850d9d..e32623590 100644 --- a/tests/test_SRCommand.py +++ b/tests/test_SRCommand.py @@ -62,7 +62,8 @@ def test_run_print_xml_error_if_SRException( mock_run_statics, mock_logException): - """ If an SRException is thrown, assert that print .toxml()" is called. + """ If an SRException is thrown, assert that print + .toxml() is called. """ import sys diff --git a/tests/test_blktap2.py b/tests/test_blktap2.py index 3dfcb2fef..d0e36daf1 100644 --- a/tests/test_blktap2.py +++ b/tests/test_blktap2.py @@ -89,7 +89,6 @@ def test_list(self): results[0].path) self.assertEqual('vhd', results[0].type) - @mock.patch('SR.xs_errors.XML_DEFS', "drivers/XE_SR_ERRORCODES.xml") @mock.patch('blktap2.Tapdisk.cgclassify') def test_open_empty_cd(self, mock_cgclassify): blktap = mock.MagicMock() diff --git a/tests/test_cbt.py b/tests/test_cbt.py index 10d460e94..9093f2162 100644 --- a/tests/test_cbt.py +++ b/tests/test_cbt.py @@ -80,8 +80,6 @@ def setUp(self): @mock.patch('blktap2.VDI', autospec=True) @mock.patch('VDI.VDI._cbt_op', autospec=True) def test_configure_blocktracking_enable_success(self, context, mock_cbt, mock_bt_vdi): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, False) @@ -94,8 +92,6 @@ def test_configure_blocktracking_enable_success(self, context, mock_cbt, mock_bt @mock.patch('blktap2.VDI', autospec=True) @mock.patch('VDI.cbtutil', autospec=True) def test_configure_blocktracking_enable_already_enabled(self, context, mock_cbt, mock_bt_vdi): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) @@ -112,8 +108,6 @@ def test_configure_blocktracking_enable_already_enabled(self, context, mock_cbt, @mock.patch('lock.LockImplementation') def test_configure_blocktracking_disable_when_enabled_without_parent( self, context, mock_lock, mock_cbt, mock_logchecker, mock_bt_vdi): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) @@ -130,8 +124,6 @@ def test_configure_blocktracking_disable_when_enabled_without_parent( @mock.patch('lock.LockImplementation') def test_configure_blocktracking_disable_when_enabled_with_parent( self, context, mock_lock, mock_logcheck, mock_cbt, mock_bt_vdi): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) @@ -153,8 +145,6 @@ def test_configure_blocktracking_disable_when_enabled_with_parent( @testlib.with_context @mock.patch('blktap2.VDI', autospec=True) def test_configure_blocktracking_disable_already_disabled(self, context, mock_bt_vdi): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) @@ -165,10 +155,7 @@ def test_configure_blocktracking_disable_already_disabled(self, context, mock_bt self._check_setting_not_changed() self._check_tapdisk_not_modified(mock_bt_vdi) - @testlib.with_context - def test_configure_blocktracking_enable_raw_vdi(self, context): - context.setup_error_codes() - + def test_configure_blocktracking_enable_raw_vdi(self): # Create the test object self.vdi = VDI.VDI(self.sr, self.vdi_uuid) self.vdi.path = "/mock/sr_path/" + str(self.vdi_uuid) @@ -176,10 +163,7 @@ def test_configure_blocktracking_enable_raw_vdi(self, context): with self.assertRaises(xs_errors.SROSError): self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) - @testlib.with_context - def test_configure_blocktracking_enable_snapshot(self, context): - context.setup_error_codes() - + def test_configure_blocktracking_enable_snapshot(self): # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self.xenapi.VDI.get_is_a_snapshot.return_value = True @@ -187,13 +171,11 @@ def test_configure_blocktracking_enable_snapshot(self, context): with self.assertRaises(xs_errors.SROSError): self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) - @testlib.with_context @mock.patch('blktap2.VDI', autospec=True) @mock.patch('VDI.util', autospec=True) @mock.patch('VDI.cbtutil', autospec=True) - def test_configure_blocktracking_enable_pause_fail(self, context, mock_cbt, mock_util, mock_bt_vdi): - context.setup_error_codes() - + def test_configure_blocktracking_enable_pause_fail( + self, mock_cbt, mock_util, mock_bt_vdi): # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) @@ -203,12 +185,10 @@ def test_configure_blocktracking_enable_pause_fail(self, context, mock_cbt, mock with self.assertRaises(xs_errors.SROSError): self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) - @testlib.with_context @mock.patch('blktap2.VDI', autospec=True) @mock.patch('VDI.util', autospec=True) - def test_configure_blocktracking_disable_pause_fail(self, context, mock_util, mock_bt_vdi): - context.setup_error_codes() - + def test_configure_blocktracking_disable_pause_fail( + self, mock_util, mock_bt_vdi): # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) @@ -218,11 +198,8 @@ def test_configure_blocktracking_disable_pause_fail(self, context, mock_util, mo with self.assertRaises(xs_errors.SROSError): self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, False) - @testlib.with_context @mock.patch('VDI.util', autospec=True) - def test_configure_blocktracking_enable_metadata_no_space(self, context, mock_util): - context.setup_error_codes() - + def test_configure_blocktracking_enable_metadata_no_space(self, mock_util): # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self.vdi.state_mock._ensure_cbt_space.side_effect = [xs_errors.XenError('SRNoSpace')] @@ -232,12 +209,10 @@ def test_configure_blocktracking_enable_metadata_no_space(self, context, mock_ut with self.assertRaises(xs_errors.SROSError): self.vdi.configure_blocktracking(self.sr_uuid, self.vdi_uuid, True) - @testlib.with_context @mock.patch('blktap2.VDI', autospec=True) @mock.patch('VDI.cbtutil', autospec=True) - def test_configure_blocktracking_enable_metadata_creation_fail(self, context, mock_cbt, mock_bt_vdi): - context.setup_error_codes() - + def test_configure_blocktracking_enable_metadata_creation_fail( + self, mock_cbt, mock_bt_vdi): # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, False) @@ -249,12 +224,10 @@ def test_configure_blocktracking_enable_metadata_creation_fail(self, context, mo self._check_tapdisk_not_modified(mock_bt_vdi) self._check_setting_state(self.vdi, False) - @testlib.with_context @mock.patch('blktap2.VDI', autospec=True) @mock.patch('VDI.cbtutil', autospec=True) - def test_configure_blocktracking_enable_metadata_initialisation_fail(self, context, mock_cbt, mock_bt_vdi): - context.setup_error_codes() - + def test_configure_blocktracking_enable_metadata_initialisation_fail( + self, mock_cbt, mock_bt_vdi): # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, False) @@ -266,12 +239,10 @@ def test_configure_blocktracking_enable_metadata_initialisation_fail(self, conte self._check_tapdisk_not_modified(mock_bt_vdi) self._check_setting_state(self.vdi, False) - @testlib.with_context @mock.patch('blktap2.VDI') @mock.patch('lock.LockImplementation') - def test_configure_blocktracking_disable_metadata_deletion_fail(self, context, mock_lock, mock_bt_vdi): - context.setup_error_codes() - + def test_configure_blocktracking_disable_metadata_deletion_fail( + self, mock_lock, mock_bt_vdi): # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) @@ -286,8 +257,6 @@ def test_configure_blocktracking_disable_metadata_deletion_fail(self, context, m @testlib.with_context @mock.patch('VDI.cbtutil', autospec=True) def test_activate_no_tracking_success(self, context, mock_cbt): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, False) @@ -303,8 +272,6 @@ def test_activate_no_tracking_success(self, context, mock_cbt): @mock.patch('VDI.VDI._cbt_op', autospec=True) @mock.patch('lock.LockImplementation', autospec=True) def test_activate_consistent_success(self, context, mock_lock, mock_cbt): - context.setup_error_codes() - expected_log_path = '/mock/sr_path/{0}.log'.format(self.vdi_uuid) logname = '%s.cbtlog' % self.vdi_uuid @@ -330,8 +297,6 @@ def test_activate_consistent_success(self, context, mock_lock, mock_cbt): @mock.patch('VDI.cbtutil', autospec=True) @mock.patch('lock.LockImplementation', autospec=True) def test_activate_consistency_check_fail(self, context, mock_lock, mock_cbt): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) @@ -347,8 +312,6 @@ def test_activate_consistency_check_fail(self, context, mock_lock, mock_cbt): @testlib.with_context @mock.patch('VDI.cbtutil', autospec=True) def test_deactivate_no_tracking_success(self, context, mock_cbt): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, False) @@ -364,8 +327,6 @@ def test_deactivate_no_tracking_success(self, context, mock_cbt): @mock.patch('VDI.VDI._cbt_op', autospec=True) @mock.patch('lock.LockImplementation', autospec=True) def test_deactivate_success(self, context, mock_lock, mock_cbt): - context.setup_error_codes() - expected_log_path = '/mock/sr_path/{0}.log'.format(self.vdi_uuid) logname = '%s.cbtlog' % self.vdi_uuid @@ -381,8 +342,6 @@ def test_deactivate_success(self, context, mock_lock, mock_cbt): @testlib.with_context def test_snapshot_success_with_CBT_disable(self, context): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, False) @@ -402,8 +361,6 @@ def test_snapshot_success_with_CBT_disable(self, context): @mock.patch('lock.LockImplementation') def test_snapshot_success_no_parent(self, context, mock_lock, mock_logchecker, mock_cbt): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) @@ -437,15 +394,12 @@ def test_snapshot_success_with_parent(self, context, mock_lock, self._check_CBT_chain_created(self.vdi, mock_cbt, self.vdi_uuid, snap_uuid, parent_uuid, child_uuid) - @testlib.with_context @mock.patch('VDI.VDI._cbt_op', autospec=True) @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) @mock.patch('VDI.VDI._ensure_cbt_space', autospec=True) @mock.patch('util.SMlog', autospec=True) - def test_snapshot_out_of_space_failure(self, context, mock_smlog, + def test_snapshot_out_of_space_failure(self, mock_smlog, mock_ensure_space, mock_logcheck, mock_cbt): - context.setup_error_codes() - # Create the test object self.vdi = TestVDI(self.sr, self.vdi_uuid) # Set initial state @@ -594,11 +548,10 @@ def test_vdi_delete_cbt_enabled_with_child(self, context, mock_lock, mock_cbt.coalesce_bitmap.assert_called_with(logpath, child_log) self.vdi.state_mock._delete_cbt_log.assert_called_with() - @testlib.with_context @mock.patch('VDI.cbtutil', autospec=True) @mock.patch('VDI.VDI._cbt_log_exists') @mock.patch('lock.LockImplementation') - def test_vdi_delete_bitmap_coalesce_exc(self, context, mock_lock, + def test_vdi_delete_bitmap_coalesce_exc(self, mock_lock, mock_logcheck, mock_cbt): # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) @@ -618,9 +571,7 @@ def test_vdi_delete_bitmap_coalesce_exc(self, context, mock_lock, mock_cbt.set_cbt_parent.assert_called_with(child_log, self.vdi_uuid) self.assertEqual(0, self.vdi.state_mock._delete_cbt_log.call_count) - @testlib.with_context - def test_list_changed_blocks_same_vdi(self, context): - context.setup_error_codes() + def test_list_changed_blocks_same_vdi(self): # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) self.xenapi.VDI.get_uuid.return_value = self.vdi_uuid @@ -630,10 +581,8 @@ def test_list_changed_blocks_same_vdi(self, context): # Test CBTChangedBlocksError is raised self.assertEqual(exc.exception.errno, 460) - @testlib.with_context @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) - def test_list_changed_blocks_not_related(self, context, mock_log): - context.setup_error_codes() + def test_list_changed_blocks_not_related(self, mock_log): # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) self.xenapi.VDI.get_uuid.return_value = "target_uuid" @@ -645,9 +594,7 @@ def test_list_changed_blocks_not_related(self, context, mock_log): # Test CBTChangedBlocksError is raised self.assertEqual(exc.exception.errno, 460) - @testlib.with_context - def test_list_changed_blocks_cbt_disabled(self, context): - context.setup_error_codes() + def test_list_changed_blocks_cbt_disabled(self): # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, False) @@ -664,7 +611,6 @@ def test_list_changed_blocks_cbt_disabled(self, context): @mock.patch('lock.LockImplementation', autospec=True) def test_list_changed_blocks_success(self, context, mock_lock, mock_log, mock_cbt): - context.setup_error_codes() # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) @@ -696,7 +642,6 @@ def test_list_changed_blocks_success(self, context, mock_lock, def test_list_changed_blocks_vdi_resized_success(self, context, mock_lock, mock_log, mock_cbt): - context.setup_error_codes() # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) @@ -723,13 +668,11 @@ def test_list_changed_blocks_vdi_resized_success(self, context, result = self.vdi.list_changed_blocks() self.assertEqual(result, expected_result) - @testlib.with_context @mock.patch('VDI.cbtutil', autospec=True) @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) @mock.patch('lock.LockImplementation', autospec=True) - def test_list_changed_blocks_vdi_shrunk(self, context, mock_lock, + def test_list_changed_blocks_vdi_shrunk(self, mock_lock, mock_log, mock_cbt): - context.setup_error_codes() # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) @@ -748,14 +691,12 @@ def test_list_changed_blocks_vdi_shrunk(self, context, mock_lock, # Test CBTChangedBlocksError is raised self.assertEqual(exc.exception.errno, 459) - @testlib.with_context @mock.patch('VDI.cbtutil', autospec=True) @mock.patch('VDI.VDI._cbt_log_exists', autospec=True) @mock.patch('lock.LockImplementation', autospec=True) - def test_list_changed_blocks_smaller_bitmap(self, context, + def test_list_changed_blocks_smaller_bitmap(self, mock_lock, mock_log, mock_cbt): - context.setup_error_codes() # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) @@ -779,7 +720,6 @@ def test_list_changed_blocks_smaller_bitmap(self, context, def test_list_changed_blocks_larger_bitmap(self, context, mock_lock, mock_log, mock_cbt): - context.setup_error_codes() # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) @@ -815,7 +755,6 @@ def test_list_changed_blocks_larger_bitmap(self, context, def test_list_changed_blocks_strip_sensitive_bitmap(self, context, mock_lock, mock_log, mock_call, mock_child, mock_size): - context.setup_error_codes() # Create the test object and initialise self.vdi = TestVDI(self.sr, self.vdi_uuid) self._set_initial_state(self.vdi, True) diff --git a/tests/test_mpath_dmp.py b/tests/test_mpath_dmp.py index 032625ab5..134b40e0c 100644 --- a/tests/test_mpath_dmp.py +++ b/tests/test_mpath_dmp.py @@ -34,17 +34,12 @@ def setUp(self): self.addCleanup(mock.patch.stopall) - @testlib.with_context @mock.patch('mpath_dmp.util', autospec=True) @mock.patch('mpath_dmp.os', autospec=True) - def test_is_valid_multipath_device(self, context, mock_os, util_mod): + def test_is_valid_multipath_device(self, mock_os, util_mod): """ Tests for checking validity of multipath device """ - - # Setup errors codes - context.setup_error_codes() - # Test 'multipath -ll' success util_mod.doexec.side_effect = [(0, "out", "err")] self.assertTrue(mpath_dmp._is_valid_multipath_device("fake_dev")) @@ -145,16 +140,12 @@ def test_refresh_dmp_success(self, context, mock_exists, mock_util, mock_valid): msg='wait_for_path not called with expected mapper path') mock_activate.assert_called_with(test_id, mock.ANY) - @testlib.with_context @mock.patch('mpath_dmp._is_valid_multipath_device', autospec=True) @mock.patch('mpath_dmp.util', autospec=True) - def test_refresh_dmp_device_not_found(self, context, mock_util, mock_valid): + def test_refresh_dmp_device_not_found(self, mock_util, mock_valid): """ Test refresh DMP device not found """ - # Setup error codes - context.setup_error_codes() - mock_valid.return_value = True test_id = '360871234' @@ -231,17 +222,13 @@ def test_activate_noiscsi_start_mpath( mock_util.pread2.assert_called_once_with( ['/usr/bin/systemctl', 'start', 'multipathd.service']) - @testlib.with_context @mock.patch('mpath_dmp.iscsilib', autospec=True) @mock.patch('mpath_dmp.util', autospec=True) def test_activate_noiscsi_mpath_not_working( - self, context, mock_util, mock_iscsilib): + self, mock_util, mock_iscsilib): """ MPATH activate, mpath not running """ - # Setup error codes - context.setup_error_codes() - mock_iscsilib.is_iscsi_daemon_running.return_value = False mock_util.doexec.return_value = (0, "", "") self.mock_mpath_cli.is_working.side_effect = [False] * 120 @@ -368,11 +355,7 @@ def test_deactivate_mpath_no_iscsi_targets( self.assertEqual(1, mock_iscsilib.restart_daemon.call_count) - @testlib.with_context - def test_refresh_no_sid(self, context): - # Setup error codes - context.setup_error_codes() - + def test_refresh_no_sid(self): with self.assertRaises(SROSError): mpath_dmp.refresh("", 0) @@ -408,16 +391,11 @@ def test_refresh_refresh_scsi( mock_exists.assert_called_once_with( '/dev/disk/by-id/scsi-360a98000534b4f4e46704f5270674d70') - @testlib.with_context @mock.patch('mpath_dmp.util.wait_for_path', autospec=True) @mock.patch('mpath_dmp.scsiutil', autospec=True) @mock.patch('mpath_dmp.os.path.exists', autospec=True) def test_refresh_refresh_error( - self, context, mock_exists, mock_scsiutil, mock_wait): - - # Setup error codes - context.setup_error_codes() - + self, mock_exists, mock_scsiutil, mock_wait): def exists(path): print('Exists %s' % path) if path.startswith('/dev/'): diff --git a/tests/test_srmetadata.py b/tests/test_srmetadata.py index ed91915a4..720f12ff4 100644 --- a/tests/test_srmetadata.py +++ b/tests/test_srmetadata.py @@ -451,7 +451,6 @@ class LVMMetadataTestContext(testlib.TestContext): def __init__(self): super().__init__() - self.setup_error_codes() self._metadata_file_content = b'\x00' * 4 * 1024 * 1024 def start(self): @@ -459,8 +458,6 @@ def start(self): self.patch("util.gen_uuid", new=genuuid) def generate_device_paths(self): - for path in super().generate_device_paths(): - yield path yield self.METADATA_PATH def fake_open(self, fname, mode='r'): diff --git a/tests/test_testlib.py b/tests/test_testlib.py index d006a6540..ebca322fc 100644 --- a/tests/test_testlib.py +++ b/tests/test_testlib.py @@ -147,15 +147,6 @@ def test_stat_does_not_fail_with_existing_file(self, context): os.stat('/existingstuff') - @testlib.with_context - def test_error_codes_read(self, context): - context.setup_error_codes() - errorcodes_file = open('/opt/xensource/sm/XE_SR_ERRORCODES.xml', 'rb') - errorcodes = errorcodes_file.read() - errorcodes_file.close() - - self.assertTrue("" in errorcodes.decode()) - @testlib.with_context def test_executable_shows_up_on_filesystem(self, context): context.add_executable('/something', None) @@ -218,13 +209,6 @@ def test_makedirs_raises_if_exists(self, context): self.assertRaises(OSError, os.makedirs, '/blah/subdir') - @testlib.with_context - def test_setup_error_codes(self, context): - context.setup_error_codes() - - self.assertTrue( - os.path.exists('/opt/xensource/sm/XE_SR_ERRORCODES.xml')) - @testlib.with_context def test_write_a_file(self, context): import os @@ -363,18 +347,6 @@ def test_rmdir_raises_exception_if_dir_is_not_empty(self): context.fake_rmdir('/existing_dir') self.assertEqual(errno.ENOTEMPTY, cm.exception.errno) - def test_get_error_code(self): - context = testlib.TestContext() - self.assertEqual(context.get_error_code("SMBMount"), 111) - - def test_get_error_code_not_found(self): - """ - When error code can't be found then None is returned. - This test is to keep 100% coverage on tests. - """ - context = testlib.TestContext() - self.assertEqual(context.get_error_code("PANCAKES"), None) - class TestFilesystemFor(unittest.TestCase): def test_returns_single_item_for_root(self): diff --git a/tests/test_trim_util.py b/tests/test_trim_util.py index c5a40d2b5..cfdd8e896 100644 --- a/tests/test_trim_util.py +++ b/tests/test_trim_util.py @@ -35,7 +35,6 @@ def test_do_trim_error_code_trim_not_supported(self, context, sr_get_capability): sr_get_capability.return_value = [] - context.setup_error_codes() result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -64,7 +63,6 @@ def test_do_trim_unable_to_obtain_lock_on_sr(self, sleep): MockLock.return_value = AlwaysBusyLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -93,7 +91,6 @@ def test_do_trim_sleeps_a_sec_and_retries_three_times(self, sleep): MockLock.return_value = AlwaysBusyLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -120,7 +117,6 @@ def test_do_trim_creates_an_lv(self, 'freespace': EMPTY_VG_SPACE} MockLock.return_value = AlwaysFreeLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -147,7 +143,6 @@ def test_do_trim_removes_lv_no_leftover_trim_vol(self, lvutil.exists.return_value = False MockLock.return_value = AlwaysFreeLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -167,7 +162,6 @@ def test_do_trim_releases_lock(self, lvutil.exists.return_value = False sr_lock = MockLock.return_value = AlwaysFreeLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -185,7 +179,6 @@ def test_do_trim_removes_lv_with_leftover_trim_vol(self, lvutil.exists.return_value = True MockLock.return_value = AlwaysFreeLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -208,7 +201,6 @@ def test_do_trim_lock_released_even_if_exception_raised(self, srlock = AlwaysFreeLock() MockLock.return_value = srlock sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -231,7 +223,6 @@ def test_do_trim_when_exception_then_returns_generic_err(self, srlock = AlwaysFreeLock() MockLock.return_value = srlock sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -266,7 +257,6 @@ def test_do_trim_when_trim_succeeded_returns_true(self, 'freespace': EMPTY_VG_SPACE} MockLock.return_value = AlwaysFreeLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -328,7 +318,6 @@ def test_do_trim_returns_exception_when_sr_full(self, 'freespace': 0} MockLock.return_value = AlwaysFreeLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -366,7 +355,6 @@ def pread2(cmd): 'freespace': EMPTY_VG_SPACE} mock_lock.return_value = AlwaysFreeLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() # Act result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) @@ -394,7 +382,6 @@ def pread2(cmd): 'freespace': EMPTY_VG_SPACE} mock_lock.return_value = AlwaysFreeLock() sr_get_capability.return_value = [trim_util.TRIM_CAP] - context.setup_error_codes() # Act result = trim_util.do_trim(None, {'sr_uuid': 'some-uuid'}) diff --git a/tests/test_util.py b/tests/test_util.py index 449b95b3b..965eec150 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -578,7 +578,6 @@ def test_host_success(self): open_socket.connect.assert_called_with(("192.168.1.2", 3260)) open_socket.send.assert_called_once_with(b"\n") - @mock.patch('SR.xs_errors.XML_DEFS', "drivers/XE_SR_ERRORCODES.xml") def test_host_dns_lookup_failure(self): # Arrange self.mock_socket.getaddrinfo.side_effect = socket.gaierror(errno.ENOENT) @@ -589,7 +588,6 @@ def test_host_dns_lookup_failure(self): self.assertEqual(140, sroe.exception.errno) - @mock.patch('SR.xs_errors.XML_DEFS', "drivers/XE_SR_ERRORCODES.xml") def test_host_connect_failure(self): # Arrange sock_addr = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, diff --git a/tests/test_vhdutil.py b/tests/test_vhdutil.py index ec0fac17a..331bafd42 100644 --- a/tests/test_vhdutil.py +++ b/tests/test_vhdutil.py @@ -32,15 +32,11 @@ def test_validate_and_round_odd_size_up_to_next_boundary(self): self.assertTrue(size == vhdutil.MAX_VHD_SIZE) - @testlib.with_context - def test_validate_and_round_negative(self, context): - context.setup_error_codes() + def test_validate_and_round_negative(self): with self.assertRaises(xs_errors.SROSError): vhdutil.validate_and_round_vhd_size(-1) - @testlib.with_context - def test_validate_and_round_too_large(self, context): - context.setup_error_codes() + def test_validate_and_round_too_large(self): with self.assertRaises(xs_errors.SROSError): vhdutil.validate_and_round_vhd_size(vhdutil.MAX_VHD_SIZE + 1) diff --git a/tests/test_xs_errors.py b/tests/test_xs_errors.py index 0f486db79..5ebfbbd7f 100644 --- a/tests/test_xs_errors.py +++ b/tests/test_xs_errors.py @@ -1,22 +1,20 @@ import unittest - -import testlib +from unittest import mock import xs_errors class TestXenError(unittest.TestCase): - @testlib.with_context - def test_without_xml_defs(self, context): + @mock.patch('xs_errors.os.path.exists', autospec=True) + def test_without_xml_defs(self, mock_exists): + mock_exists.return_value = False + with self.assertRaises(Exception) as e: xs_errors.XenError('blah') self.assertTrue("No XML def file found" in str(e.exception)) - @testlib.with_context - def test_xml_defs(self, context): - context.setup_error_codes() - + def test_xml_defs(self): with self.assertRaises(Exception) as e: raise xs_errors.XenError('SRInUse') diff --git a/tests/testlib.py b/tests/testlib.py index fe03779fa..167bb172f 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -17,16 +17,6 @@ class ContextSetupError(Exception): pass -def get_error_codes(): - this_dir = os.path.dirname(__file__) - drivers_dir = os.path.join(this_dir, '..', 'drivers') - error_codes_path = os.path.join(drivers_dir, 'XE_SR_ERRORCODES.xml') - error_code_catalog = open(error_codes_path, 'r') - contents = error_code_catalog.read() - error_code_catalog.close() - return contents.encode('utf-8') - - class SCSIDisk(object): def __init__(self, adapter): self.adapter = adapter @@ -93,7 +83,6 @@ def communicate(self, data): class TestContext(object): def __init__(self): self.patchers = [] - self.error_codes = get_error_codes() self.inventory = { 'PRIMARY_DISK': '/dev/disk/by-id/primary' } @@ -169,11 +158,6 @@ def fake_makedirs(self, path): def setup_modinfo(self): self.add_executable('/sbin/modinfo', self.fake_modinfo) - def setup_error_codes(self): - self._path_content['/opt/xensource/sm/XE_SR_ERRORCODES.xml'] = ( - self.error_codes - ) - def fake_modinfo(self, args, stdin_data): assert len(args) == 3 assert args[1] == '-d' @@ -330,14 +314,6 @@ def add_adapter(self, adapter): self.scsi_adapters.append(adapter) return adapter - def get_error_code(self, error_name): - xml = parseString(self.error_codes) - for code in xml.getElementsByTagName('code'): - name = code.getElementsByTagName('name')[0].firstChild.nodeValue - if name == error_name: - return int(code.getElementsByTagName('value')[0].firstChild.nodeValue) - return None - @staticmethod def is_binary(mode): return 'b' in mode