Best Python code snippet using autotest_python
iscsi_unittest.py
Source:iscsi_unittest.py
...9from autotest.client.shared.test_utils import mock10from autotest.client import os_dep11from autotest.client.shared import utils12class iscsi_test(unittest.TestCase):13 def setup_stubs_init(self):14 os_dep.command.expect_call("iscsiadm")15 utils.system_output.expect_call("hostname").and_return("localhost")16 os_dep.command.expect_call("tgtadm")17 def setup_stubs_login(self, iscsi_obj):18 c_cmd = "dd if=/dev/zero of=/tmp/iscsitest count=1024 bs=1K"19 lg_cmd = "iscsiadm --mode node --login --targetname "20 lg_cmd += "%s" % iscsi_obj.target21 self.setup_stubs_portal_visible(iscsi_obj)22 os.path.isfile.expect_call(iscsi_obj.emulated_image).and_return(False)23 utils.system.expect_call(c_cmd)24 self.setup_stubs_export_target(iscsi_obj)25 utils.system.expect_call("service iscsid restart")26 self.setup_stubs_portal_visible(iscsi_obj, "127.0.0.1:3260,1 %s"27 % iscsi_obj.target)28 lg_msg = "successful"29 utils.system_output.expect_call(lg_cmd).and_return(lg_msg)30 def setup_stubs_get_device_name(self, iscsi_obj):31 s_msg = "tcp [15] 127.0.0.1:3260,1 %s" % iscsi_obj.target32 utils.system_output.expect_call("iscsiadm --mode session",33 ignore_status=True34 ).and_return(s_msg)35 detail = "Target: iqn.iscsitest\n Attached scsi disk "36 detail += "sdb State running"37 utils.system_output.expect_call("iscsiadm -m session -P 3"38 ).and_return(detail)39 def setup_stubs_cleanup(self, iscsi_obj, fname=""):40 s_msg = "tcp [15] 127.0.0.1:3260,1 %s" % iscsi_obj.target41 utils.system_output.expect_call("iscsiadm --mode session",42 ignore_status=True43 ).and_return(s_msg)44 out_cmd = "iscsiadm --mode node --logout -T %s" % iscsi_obj.target45 utils.system_output.expect_call(out_cmd).and_return("successful")46 os.path.isfile.expect_call(fname).and_return(False)47 s_cmd = "tgtadm --lld iscsi --mode target --op show"48 utils.system_output.expect_call(s_cmd49 ).and_return("Target 1: iqn.iscsitest")50 d_cmd = "tgtadm --lld iscsi --mode target --op delete"51 d_cmd += " --tid %s" % iscsi_obj.emulated_id52 utils.system.expect_call(d_cmd)53 def setup_stubs_logged_in(self, result=""):54 utils.system_output.expect_call("iscsiadm --mode session",55 ignore_status=True56 ).and_return(result)57 def setup_stubs_portal_visible(self, iscsi_obj, result=""):58 host_name = iscsi_obj.portal_ip59 v_cmd = "iscsiadm -m discovery -t sendtargets -p %s" % host_name60 utils.system_output.expect_call(v_cmd,61 ignore_status=True).and_return(result)62 def setup_stubs_export_target(self, iscsi_obj):63 s_cmd = "tgtadm --lld iscsi --mode target --op show"64 utils.system_output.expect_call(s_cmd).and_return("")65 utils.system_output.expect_call(s_cmd).and_return("")66 t_cmd = "tgtadm --mode target --op new --tid"67 t_cmd += " %s --lld iscsi " % iscsi_obj.emulated_id68 t_cmd += "--targetname %s" % iscsi_obj.target69 utils.system.expect_call(t_cmd)70 l_cmd = "tgtadm --lld iscsi --op bind --mode target"71 l_cmd += " --tid %s -I ALL" % iscsi_obj.emulated_id72 utils.system.expect_call(l_cmd)73 utils.system_output.expect_call(s_cmd).and_return("")74 t_cmd = "tgtadm --mode logicalunit --op new "75 t_cmd += "--tid %s --lld iscsi " % iscsi_obj.emulated_id76 t_cmd += "--lun %s " % 077 t_cmd += "--backing-store %s" % iscsi_obj.emulated_image78 utils.system.expect_call(t_cmd)79 def setup_stubs_get_target_id(self):80 s_cmd = "tgtadm --lld iscsi --mode target --op show"81 s_msg = "Target 1: iqn.iscsitest\nBacking store path: /tmp/iscsitest"82 utils.system_output.expect_call(s_cmd).and_return(s_msg)83 def setUp(self):84 # The normal iscsi with iscsi server should configure following85 # parameters. As this will need env support only test emulated86 # iscsi in local host.87 # self.iscsi_params = {"target": "",88 # "portal_ip": "",89 # "initiator": ""}90 self.iscsi_emulated_params = {"emulated_image": "/tmp/iscsitest",91 "target": "iqn.iscsitest",92 "image_size": "1024K"}93 self.god = mock.mock_god()94 self.god.stub_function(os_dep, "command")95 self.god.stub_function(utils, "system")96 self.god.stub_function(utils, "system_output")97 self.god.stub_function(os.path, "isfile")98 def tearDown(self):99 self.god.unstub_all()100 def test_iscsi_get_device_name(self):101 self.setup_stubs_init()102 iscsi_emulated = iscsi.Iscsi(self.iscsi_emulated_params)103 iscsi_emulated.emulated_id = "1"104 self.setup_stubs_login(iscsi_emulated)105 iscsi_emulated.login()106 self.setup_stubs_get_device_name(iscsi_emulated)107 self.assertNotEqual(iscsi_emulated.get_device_name(), "")108 fname = "/etc/iscsi/initiatorname.iscsi-%s" % iscsi_emulated.id109 self.setup_stubs_cleanup(iscsi_emulated, fname=fname)110 iscsi_emulated.cleanup()111 self.god.check_playback()112 def test_iscsi_login(self):113 self.setup_stubs_init()114 iscsi_emulated = iscsi.Iscsi(self.iscsi_emulated_params)115 self.setup_stubs_logged_in()116 self.assertFalse(iscsi_emulated.logged_in())117 result = "tcp [15] 127.0.0.1:3260,1 %s" % iscsi_emulated.target118 self.setup_stubs_logged_in(result)119 self.assertTrue(iscsi_emulated.logged_in())120 def test_iscsi_visible(self):121 self.setup_stubs_init()122 iscsi_emulated = iscsi.Iscsi(self.iscsi_emulated_params)123 self.setup_stubs_portal_visible(iscsi_emulated)124 self.assertFalse(iscsi_emulated.portal_visible())125 self.setup_stubs_portal_visible(iscsi_emulated, "127.0.0.1:3260,1 %s"126 % iscsi_emulated.target)127 def test_iscsi_target_id(self):128 self.setup_stubs_init()129 iscsi_emulated = iscsi.Iscsi(self.iscsi_emulated_params)130 self.setup_stubs_get_target_id()131 self.assertNotEqual(iscsi_emulated.get_target_id(), "")132if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!