Skip to content

Commit

Permalink
CP-49689: load error defs from sibling file
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Syms <[email protected]>
  • Loading branch information
MarkSymsCtx committed May 30, 2024
1 parent f6ec9f0 commit 8a1428f
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 284 deletions.
9 changes: 6 additions & 3 deletions drivers/xs_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/test_BaseISCSI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 5 additions & 13 deletions tests/test_FileSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions tests/test_HBASR.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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()
Expand All @@ -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):
Expand Down
42 changes: 10 additions & 32 deletions tests/test_ISOSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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]
Expand All @@ -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,
Expand All @@ -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)])
Expand All @@ -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), " "])

Expand All @@ -493,31 +473,29 @@ 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)])
_checkmount.side_effect = [False, True]
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):
Expand Down
7 changes: 3 additions & 4 deletions tests/test_LVHDoFCoESR.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions tests/test_LVHDoHBASR.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
Loading

0 comments on commit 8a1428f

Please sign in to comment.