Skip to content

Commit

Permalink
CA-128095: [UnitTest] Unhandled exception in LVMoISCSISR.load()
Browse files Browse the repository at this point in the history
Signed-off-by: Kostas Ladopoulos <konstantinos.ladopoulos@citrix.com>
  • Loading branch information
kostaslambda committed Dec 19, 2014
1 parent d26108d commit 27a7502
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions tests/test_LVHDoISCSISR.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import unittest
import mock
import LVHDoISCSISR


class RandomException(Exception):
pass


class NonInitingLVHDoISCSISR(LVHDoISCSISR.LVHDoISCSISR):

""" Helper class; Creates dummy LVHDoISCSISR object.
Add attributes/methods as appropriate.
"""

def __init__(self, extra_dconf=None, extra_params=None):

from SRCommand import SRCommand
from DummySR import DRIVER_INFO

self.mpath = "false"
self.dconf = {
'target': 'target',
'localIQN': 'localIQN',
'targetIQN': 'targetIQN',
'SCSIid': 'SCSIid'
}

self.srcmd = mock.Mock(spec=SRCommand(DRIVER_INFO))
self.srcmd.dconf = self.dconf

self.original_srcmd = self.srcmd

self.srcmd.params = {'command': 'command'}

self.srcmd.dconf.update(extra_dconf or {})
self.srcmd.params.update(extra_params or {})


class TestLVHDoISCSISR(unittest.TestCase):

@mock.patch('iscsilib.ensure_daemon_running_ok')
@mock.patch('util._convertDNS')
@mock.patch('SR.driver')
@mock.patch('util.gen_uuid')
def test_load_reraise_unhandled_exceptions(
self,
mock_util_gen_uuid,
mock_SR_driver,
mock_util__convertDNS,
mock_iscsilib_ensure_daemon_running_ok):

""" Asserts that all unhandled exceptions are reraised.
In LVHDoISCSISR.load() there is a big try/except block that
doesn't handle all exceptions. Specifically, it logs the
exception and tries to continue after the block, almost certainly
resulting in an "IndexError" exception because the "self.iscsiSRs"
list is empty. This test asserts that all such exceptions are
reraised.
"""

mock_util_gen_uuid.return_value = 'deadbeef'
mock_util__convertDNS.return_value = '127.0.0.1'
mock_iscsilib_ensure_daemon_running_ok.side_effect = \
RandomException('test_load_reraise_unhandled_exceptions')

lvhd_o_iscsi_sr = NonInitingLVHDoISCSISR(
{'targetIQN': '*'},
{'command': 'sr_create'}
)

try:
lvhd_o_iscsi_sr.load(None)
except Exception, e:
self.assertTrue(isinstance(e, RandomException))
self.assertEqual(str(e), 'test_load_reraise_unhandled_exceptions')

0 comments on commit 27a7502

Please sign in to comment.