Best Python code snippet using autotest_python
iscsi.py
Source:iscsi.py  
...9import os10import re11from autotest.client import os_dep12from autotest.client.shared import utils, error13def iscsi_get_sessions():14    """15    Get the iscsi sessions activated16    """17    cmd = "iscsiadm --mode session"18    output = utils.system_output(cmd, ignore_status=True)19    sessions = []20    if "No active sessions" not in output:21        for session in output.splitlines():22            ip_addr = session.split()[2].split(',')[0]23            target = session.split()[3]24            sessions.append((ip_addr, target))25    return sessions26def iscsi_get_nodes():27    """28    Get the iscsi nodes29    """30    cmd = "iscsiadm --mode node"31    output = utils.system_output(cmd)32    pattern = r"(\d+\.\d+\.\d+\.\d+|\W:{2}\d\W):\d+,\d+\s+([\w\.\-:\d]+)"33    nodes = []34    if "No records found" not in output:35        nodes = re.findall(pattern, output)36    return nodes37def iscsi_login(target_name):38    """39    Login to a target with the target name40    :param target_name: Name of the target41    """42    cmd = "iscsiadm --mode node --login --targetname %s" % target_name43    output = utils.system_output(cmd)44    target_login = ""45    if "successful" in output:46        target_login = target_name47    return target_login48def iscsi_logout(target_name=None):49    """50    Logout from a target. If the target name is not set then logout all51    targets.52    :params target_name: Name of the target.53    """54    if target_name:55        cmd = "iscsiadm --mode node --logout -T %s" % target_name56    else:57        cmd = "iscsiadm --mode node --logout all"58    output = utils.system_output(cmd)59    target_logout = ""60    if "successful" in output:61        target_logout = target_name62    return target_logout63def iscsi_discover(portal_ip):64    """65    Query from iscsi server for available targets66    :param portal_ip: Ip for iscsi server67    """68    cmd = "iscsiadm -m discovery -t sendtargets -p %s" % portal_ip69    output = utils.system_output(cmd, ignore_status=True)70    session = ""71    if "Invalid" in output:72        logging.debug(output)73    else:74        session = output75    return session76class Iscsi(object):77    """78    Basic iscsi support class. Will handle the emulated iscsi export and79    access to both real iscsi and emulated iscsi device.80    """81    def __init__(self, params, root_dir="/tmp"):82        os_dep.command("iscsiadm")83        self.target = params.get("target")84        self.export_flag = False85        if params.get("portal_ip"):86            self.portal_ip = params.get("portal_ip")87        else:88            self.portal_ip = utils.system_output("hostname")89        if params.get("iscsi_thread_id"):90            self.id = params.get("iscsi_thread_id")91        else:92            self.id = utils.generate_random_string(4)93        self.initiator = params.get("initiator")94        if params.get("emulated_image"):95            self.initiator = None96            os_dep.command("tgtadm")97            emulated_image = params.get("emulated_image")98            self.emulated_image = os.path.join(root_dir, emulated_image)99            self.emulated_id = ""100            self.emulated_size = params.get("image_size")101            self.unit = self.emulated_size[-1].upper()102            self.emulated_size = self.emulated_size[:-1]103            # maps K,M,G,T => (count, bs)104            emulated_size = {'K': (1, 1),105                             'M': (1, 1024),106                             'G': (1024, 1024),107                             'T': (1024, 1048576),108                             }109            if self.unit in emulated_size:110                block_size = emulated_size[self.unit][1]111                size = int(self.emulated_size) * emulated_size[self.unit][0]112                self.create_cmd = ("dd if=/dev/zero of=%s count=%s bs=%sK"113                                   % (self.emulated_image, size, block_size))114    def logged_in(self):115        """116        Check if the session is login or not.117        """118        sessions = iscsi_get_sessions()119        login = False120        if self.target in map(lambda x: x[1], sessions):121            login = True122        return login123    def portal_visible(self):124        """125        Check if the portal can be found or not.126        """127        return bool(re.findall("%s$" % self.target,128                               iscsi_discover(self.portal_ip), re.M))129    def login(self):130        """131        Login session for both real iscsi device and emulated iscsi. Include132        env check and setup....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!!
