How to use get_pci_addresses method in avocado

Best Python code snippet using avocado_python

pci.py

Source:pci.py Github

copy

Full Screen

...30 for line in output.splitlines():31 domains.append(line.split(":")[0])32 return list(set(domains))33 return []34def get_pci_addresses():35 """36 Gets list of PCI addresses in the system.37 Does not return the PCI Bridges/Switches.38 :return: list of full PCI addresses including domain (0000:00:14.0)39 """40 addresses = []41 for line in runcmd("lspci -D")[1].splitlines():42 if not get_pci_prop(line.split()[0], 'Class').startswith('06'):43 addresses.append(line.split()[0])44 return addresses45def get_num_interfaces_in_pci(dom_pci_address):46 """47 Gets number of interfaces of a given partial PCI address starting with48 full domain address.49 :param dom_pci_address: Partial PCI address including domain50 address (0000, 0000:00:1f, 0000:00:1f.2, etc)51 :return: number of devices in a PCI domain.52 """53 count = 054 output = runcmd("ls -l /sys/class/*/ -1")[1]55 if output:56 filt = '/%s' % dom_pci_address57 for line in output.splitlines():58 if filt in line:59 count += 160 return count61def get_disks_in_pci_address(pci_address):62 """63 Gets disks in a PCI address.64 :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)65 :return: list of disks in a PCI address.66 """67 disks_path = "/dev/disk/by-path/"68 disk_list = []69 for dev in os.listdir(disks_path):70 if pci_address in dev:71 link = os.readlink(os.path.join(disks_path, dev))72 disk_list.append(os.path.abspath(os.path.join(disks_path, link)))73 return disk_list74def get_disks_in_interface(interface):75 """76 Gets disks in a PCI interface.77 :param interface: interface (host1, nvme0, etc)78 :return: list of disks in a PCI interface79 """80 disks_path = "/sys/block/"81 disk_list = []82 for dev in os.listdir(disks_path):83 link = os.readlink(os.path.join(disks_path, dev))84 if "/%s/" % interface in link:85 disk_list.append('/dev/%s' % dev)86 return disk_list87def get_multipath_wwids(disks_list):88 """89 Get mpath wwid for given scsi disks90 :param disks_list: list of disks(/dev/sda, /dev/sdaa, ...)91 :return: list of mpath wwids92 """93 wwid_list = []94 for line in runcmd('lsscsi -i')[1].splitlines():95 for disk in disks_list:96 if disk == line.split()[-2] and line.split()[-1] != '-':97 wwid_list.append(line.split()[-1])98 existing_wwids = []99 if not os.path.isfile("/etc/multipath/wwids"):100 return []101 with open('/etc/multipath/wwids', 'r') as wwid_file:102 for line in wwid_file:103 if '#' not in line:104 existing_wwids.append(line.split('/')[1])105 return [mpath for mpath in list(set(wwid_list)) if mpath in existing_wwids]106def get_multipath_disks(wwids_list):107 """108 Get mpath disk names for given wwids109 :param disks_list: list of disks(360050768028383d7f000000000000022, ...)110 :return: list of mpath disks111 """112 mpath_list = []113 for wwid in wwids_list:114 disk = runcmd("multipath -l %s" % wwid)[1].split()[0]115 mpath_list.append("/dev/mapper/%s" % disk)116 return mpath_list117def get_root_disks():118 """119 Gets the PCI address of the root disk.120 :return: list of root disk.121 """122 root_disk = []123 root_part = runcmd('df -h /')[1].splitlines()[-1].split()[0]124 for line in runcmd('lsblk -sl %s' % root_part)[1].splitlines():125 if 'disk' in line:126 root_disk.append('/dev/%s' % line.split()[0])127 return root_disk128def get_nics_in_pci_address(pci_address):129 """130 Gets network interface(nic) in a PCI address.131 :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)132 :return: list of network interfaces in a PCI address.133 """134 return get_interfaces_in_pci_address(pci_address, "net")135def get_interfaces_in_pci_address(pci_address, pci_class):136 """137 Gets interface in a PCI address.138 e.g: host = pci.get_interfaces_in_pci_address("0001:01:00.0", "net")139 ['enP1p1s0f0']140 host = pci.get_interfaces_in_pci_address("0004:01:00.0", "scsi_host")141 ['host6']142 :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)143 :param class: Adapter class (FC(fc_host), FCoE(net), NIC(net), SCSI(scsi)..)144 :return: list of generic interfaces in a PCI address.145 """146 pci_class_path = "/sys/class/%s/" % pci_class147 if not pci_class or not os.path.isdir(pci_class_path):148 return ""149 return [interface for interface in os.listdir(pci_class_path)150 if pci_address in os.readlink(os.path.join(pci_class_path,151 interface))]152def get_pci_class_name(pci_address):153 """154 Gets PCI class name for given PCI bus address155 e.g: >>> pci.get_pci_class_name("0000:01:00.0")156 'scsi_host'157 :param pci_address: Any segment of a PCI address(1f, 0000:00:if, ...)158 :return: class name for corresponding PCI bus address159 """160 pci_class_dic = {'0104': 'scsi_host', '0c04': 'scsi_host', '0280': 'net',161 '0c03': 'scsi_host', '0200': 'net', '0108': 'nvme',162 '0106': 'ata_port', '0207': 'net'}163 pci_class_id = get_pci_prop(pci_address, "Class")164 if pci_class_id not in pci_class_dic:165 return ""166 return pci_class_dic.get(pci_class_id)167def get_pci_type(pci_address):168 """169 Gets PCI type for given PCI bus address170 e.g: >>> pci.get_pci_class_name("0000:01:00.0")171 'fc'172 :param pci_address: Any segment of a PCI address(1f, 0000:00:if, ...)173 :return: type for corresponding PCI bus address174 """175 pci_class_dic = {'0104': 'raid', '0c04': 'fc', '0280': 'infiniband',176 '0c03': 'usb', '0200': 'network', '0108': 'nvme',177 '0207': 'infiniband'}178 pci_class_id = get_pci_prop(pci_address, "Class")179 if pci_class_id not in pci_class_dic:180 return ""181 return pci_class_dic.get(pci_class_id)182def get_firmware(pci_address):183 """184 Gets firmware of a pci_address185 :param pci_address: PCI address(0000:00:if.0, ...)186 :return: firmware for its interface187 """188 class_name = get_pci_class_name(pci_address)189 interface = get_interfaces_in_pci_address(pci_address, class_name)190 firmware = ''191 if not interface:192 return firmware193 interface = interface[0]194 if class_name == 'net':195 for line in runcmd('ethtool -i %s' % interface)[1].splitlines():196 if 'firmware-version' in line:197 firmware = line.split()[1]198 break199 else:200 for name in ['firmware_rev', 'fwrev', 'fw_version']:201 filename = "/sys/class/%s/%s/%s" % (class_name, interface, name)202 if os.path.isfile(filename):203 with open(filename, 'r') as fw_file:204 firmware = fw_file.read().strip('\t\r\n\0').split()[0]205 firmware = firmware.strip(',')206 return firmware207def get_pci_fun_list(pci_address):208 """209 Gets list of functions in the given PCI address.210 Example: in address 0000:03:00, functions are 0000:03:00.0 and 0000:03:00.1211 :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)212 :return: list of functions in a PCI address.213 """214 return list(dev for dev in get_pci_addresses() if pci_address in dev)215def get_slot_from_sysfs(full_pci_address):216 """217 Gets the PCI slot of given address.218 :note: Specific for ppc64 processor.219 :param full_pci_address: Full PCI address including domain (0000:03:00.0)220 :return: Removed port related details using re, only returns till221 physical slot of the adapter.222 """223 if 'ppc64' not in platform.processor():224 return ""225 if not os.path.isfile('/sys/bus/pci/devices/%s/devspec' % full_pci_address):226 return227 filename = "/sys/bus/pci/devices/%s/devspec" % full_pci_address228 with open(filename, 'r') as file_obj:229 devspec = file_obj.read()230 if not os.path.isfile("/proc/device-tree/%s/ibm,loc-code" % devspec):231 return232 filename = "/proc/device-tree/%s/ibm,loc-code" % devspec233 with open(filename, 'r') as file_obj:234 slot = file_obj.read()235 slot_ibm = re.match(r'((\w+)[.])+(\w+)-[P(\d+)-]*C(\d+)', slot)236 if slot_ibm:237 return slot_ibm.group()238 slot_openpower = re.match(r'(\w+)[\s]*(\w+)(\d*)', slot)239 if slot_openpower:240 return slot_openpower.group()241 return ""242def get_slot_list():243 """244 Gets list of PCI slots in the system.245 :note: Specific for ppc64 processor.246 :return: list of slots in the system.247 """248 return list(set(get_slot_from_sysfs(dev) for dev in get_pci_addresses()))249def get_pci_id_from_sysfs(full_pci_address):250 """251 Gets the PCI ID from sysfs of given PCI address.252 :param full_pci_address: Full PCI address including domain (0000:03:00.0)253 :return: PCI ID of a PCI address from sysfs.254 """255 path = "/sys/bus/pci/devices/%s" % full_pci_address256 if os.path.isdir(path):257 path = "%s/%%s" % path258 return ":".join(["%04x" % int(open(path % param).read(), 16)259 for param in ['vendor', 'device', 'subsystem_vendor',260 'subsystem_device']])261 return ""262def get_pci_prop(pci_address, prop):263 """264 Gets specific PCI ID of given PCI address. (first match only)265 :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)266 :param part: prop of PCI ID.267 :return: specific PCI ID of a PCI address.268 """269 output = runcmd("lspci -Dnvmm -s %s" % pci_address)[1]270 if output:271 for line in output.splitlines():272 if prop == line.split(':')[0]:273 return line.split()[-1]274 return ""275def get_pci_id(pci_address):276 """277 Gets PCI id of given address. (first match only)278 :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)279 :return: PCI ID of a PCI address.280 """281 pci_id = []282 for params in ['Vendor', 'Device', 'SVendor', 'SDevice']:283 output = get_pci_prop(pci_address, params)284 if not output:285 return ""286 pci_id.append(output)287 if pci_id:288 return ":".join(pci_id)289def get_pci_prop_name(pci_address, prop):290 """291 Gets specific PCI ID of given PCI address. (first match only)292 :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)293 :param prop: prop of PCI ID.294 :return: specific PCI ID of a PCI address.295 """296 output = runcmd("lspci -Dvmm -s %s" % pci_address)[1]297 if output:298 for line in output.splitlines():299 if prop == line.split(':')[0]:300 return " ".join(line.split()[1:])301 return ""302def get_pci_name(pci_address):303 """304 Gets PCI id of given address. (first match only)305 :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)306 :return: PCI ID of a PCI address.307 """308 pci_name = []309 for params in ['Vendor', 'Device']:310 output = get_pci_prop_name(pci_address, params)311 if not output:312 return313 pci_name.append(output)314 if pci_name:315 return " ".join(pci_name)316 return ""317def get_driver(pci_address):318 """319 Gets the kernel driver in use of given PCI address. (first match only)320 :param pci_address: Any segment of a PCI address (1f, 0000:00:1f, ...)321 :return: driver of a PCI address.322 """323 output = runcmd("lspci -ks %s" % pci_address)[1]324 if output:325 for line in output.splitlines():326 if 'Kernel driver in use:' in line:327 return line.rsplit(None, 1)[-1]328 return ""329def ioa_details():330 """331 Gets the IPR IOA details and returns332 return: list of dics, with keys ioa, serial, remote serial, pci, status333 """334 show_ioas = runcmd("iprconfig -c show-ioas")[1]335 ioas = []336 if show_ioas:337 for line in show_ioas.splitlines():338 if 'Operational' in line:339 ioa = line.split()[0]340 serial = r_serial = pci = status = ''341 ioa_details = runcmd('iprconfig -c show-details %s' % ioa)[1]342 for line in ioa_details.splitlines():343 if line.startswith('PCI Address'):344 pci = line.split()[-1]345 if line.startswith('Serial Number'):346 serial = line.split()[-1]347 if line.startswith('Remote Adapter Serial Number'):348 r_serial = line.split()[-1]349 if line.startswith('Current Dual Adapter State'):350 status = line.split()[-1]351 ioas.append({'ioa': ioa, 'pci': pci, 'serial': serial, 'r_serial': r_serial, 'status': status})352 return ioas353def get_primary_ioa(pci_address):354 """355 Gets the Primary IPR IOA in the given PCI address356 :param pci_address: PCI Address (0000:00:1f, 0000:00:1f.1, ...)357 :return: primary IOA358 """359 for ioa_detail in ioa_details():360 if pci_address in ioa_detail['pci'] and 'Primary' in ioa_detail['status']:361 return ioa_detail['ioa']362 return ''363def get_secondary_ioa(primary_ioa):364 """365 Gets the Secondary IPR IOA in the given Primary IPR IOA366 :param primary_ioa: Primary IPR IOA (sg1, sg22, ...)367 :return: secondary IOA368 """369 details = ioa_details()370 serial = ''371 for ioa_detail in details:372 if primary_ioa == ioa_detail['ioa']:373 serial = ioa_detail['r_serial']374 if not serial:375 return ''376 for ioa_detail in details:377 if serial == ioa_detail['serial']:378 return ioa_detail['ioa']379 return ''380def pci_info(pci_addrs, pci_type='', pci_blocklist='', type_blocklist=''):381 """382 Get all the information for given PCI addresses (comma separated).383 :param pci_addrs: PCI addresses384 :return: list of dictionaries of PCI information385 """386 if not pci_addrs:387 return []388 pci_addrs = pci_addrs.split(',')389 pci_addrs = [pci_addr.split('.')[0] for pci_addr in pci_addrs]390 pci_addrs = list(set(pci_addrs))391 if pci_blocklist:392 pci_blocklist = pci_blocklist.split(',')393 pci_blocklist = [pci_addr.split('.')[0] for pci_addr in pci_blocklist]394 pci_addrs = list(set(pci_addrs) - set(pci_blocklist))395 pci_addrs.sort()396 pci_list = []397 if pci_type:398 pci_type = list(set(pci_type.split(',')))399 else:400 pci_type = []401 if type_blocklist:402 type_blocklist = type_blocklist.split(',')403 for pci_addr in pci_addrs:404 pci_dic = {}405 root_disks = get_root_disks()406 pci_dic['functions'] = get_pci_fun_list(pci_addr)407 pci_dic['pci_root'] = pci_addr408 pci_dic['adapter_description'] = get_pci_name(pci_dic['functions'][0])409 pci_dic['adapter_id'] = get_pci_id(pci_dic['functions'][0])410 pci_dic['adapter_type'] = get_pci_type(pci_dic['functions'][0])411 pci_dic['driver'] = get_driver(pci_dic['functions'][0])412 pci_dic['slot'] = get_slot_from_sysfs(pci_dic['functions'][0])413 if pci_dic['adapter_type'] in type_blocklist:414 continue415 elif "All" not in pci_type and pci_dic['adapter_type'] not in pci_type:416 continue417 pci_dic['interfaces'] = []418 pci_dic['class'] = get_pci_class_name(pci_dic['functions'][0])419 for fun in pci_dic['functions']:420 pci_dic['interfaces'].extend(get_interfaces_in_pci_address(fun, pci_dic['class']))421 pci_dic['firmware'] = get_firmware(pci_dic['functions'][0])422 pci_dic['disks'] = []423 for interface in pci_dic['interfaces']:424 pci_dic['disks'].extend(get_disks_in_interface(interface))425 pci_dic['disks'] = list(set(pci_dic['disks']))426 pci_dic['mpath_wwids'] = []427 pci_dic['mpath_disks'] = []428 if pci_dic['class'] == 'scsi_host':429 pci_dic['mpath_wwids'] = get_multipath_wwids(pci_dic['disks'])430 pci_dic['mpath_disks'] = get_multipath_disks(pci_dic['mpath_wwids'])431 pci_dic['infiniband_interfaces'] = []432 if pci_dic['adapter_type'] == 'infiniband':433 for fun in pci_dic['functions']:434 pci_dic['infiniband_interfaces'].extend(get_interfaces_in_pci_address(fun, 'infiniband'))435 if pci_dic['adapter_type'] == 'raid':436 for fun in pci_dic['functions']:437 pci_dic['primary_ioa'] = get_primary_ioa(fun)438 pci_dic['secondary_ioa'] = get_secondary_ioa(pci_dic['primary_ioa'])439 pci_dic['is_root_disk'] = False440 for disk in pci_dic['disks']:441 for root_disk in root_disks:442 if root_disk in disk:443 pci_dic['is_root_disk'] = True444 break445 pci_list.append(pci_dic)446 return pci_list447def all_pci_info(pci_type='', pci_blocklist='', type_blocklist=''):448 """449 Get all the information for all PCI addresses in the system.450 :return: list of dictionaries of PCI information451 """452 pci_addrs = get_pci_addresses()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful