def _test_connectivity(self, dut_handler: "ResourceHandler"): for handler in (self.handler, dut_handler): if ( not handler.autoload_finished.is_set() or not handler.is_autoload_success ): raise BaseAutomationException( f"Autoload doesn't finish for the {handler.name} resource," f" so skip testing connectivity" ) res_info = self.handler.get_details() dut_info = dut_handler.get_details() res_port1, res_port2 = get_port_names_for_connectivity(res_info) dut_port1, dut_port2 = get_port_names_for_connectivity(dut_info) # adding physical connections self.handler.sandbox_handler.add_physical_connection(res_port1, dut_port1) self.handler.sandbox_handler.add_physical_connection(res_port2, dut_port2) # add VLAN self.handler.sandbox_handler.connect_ports_with_connector( dut_port1, dut_port2, "connector" ) # remove VLAN self.handler.sandbox_handler.remove_connector(dut_port1, dut_port2)
def _find_topology_name_for_cloudshell(self) -> str: cs_names = sorted(self._do_handler.get_topologies_by_category("")) for topology_name in cs_names: # 'Environments/CloudShell - Latest 8.3' if topology_name.split("/", 1)[-1] == self._cs_on_do_conf.cs_version: return topology_name emsg = f"CloudShell version {self._cs_on_do_conf.cs_version} isn't exists" raise BaseAutomationException(emsg)
def _find_topology_name_for_app(self, app_name: str) -> str: names = sorted( self._do_handler.get_topologies_by_category("Networking Apps")) for topology_name in names: # 'Environments/Cisco IOSv Switch' if topology_name.split("/", 1)[-1] == app_name: return topology_name raise BaseAutomationException( f"Networking App {app_name} isn't exists")
def get_port_names_for_connectivity(resource_info: ResourceInfo) -> tuple[str, str]: ports = find_ports(resource_info) try: port1_name, port2_name = ports[0].Name, ports[1].Name except IndexError: raise BaseAutomationException( f"Resource {resource_info.Name} has too few ports for " f"connectivity {len(ports)}" ) return port1_name, port2_name
def test_load_config(self): try: params = self.handler.tests_conf.params["load_config"] config_path = params.pop("config_file_location") except KeyError: raise BaseAutomationException( "You have to specify params for load_config command") # just check that command runs without errors self.handler.load_config(config_path, params)
def _wait_for_futures(futures: set[ft.Future], stop_flag: Event): done, undone = ft.wait(futures, return_when=ft.FIRST_EXCEPTION) for f in done: if f.exception() is not None: stop_flag.set() ft.wait(futures) exceptions = set(filter(None, map(ft.Future.exception, futures))) if exceptions: if len(exceptions) == 1 and isinstance(next(iter(exceptions)), KeyboardInterrupt): raise KeyboardInterrupt emsg = f"Sandbox threads finished with exceptions: {exceptions}" raise BaseAutomationException(emsg)
def end_reservation(self, reservation_id: ReservationId, name: str, wait: bool = True): logger.info(f"Ending a reservation for {name} {reservation_id}") self._api.EndReservation(reservation_id) if wait: for _ in range(30): status = self.get_reservation_status(reservation_id).Status if status == "Completed": break time.sleep(30) else: raise BaseAutomationException("Can't end reservation") logger.info("Reservation ended")
def _get_port_info_from_vcenter(self, vm_uuid, adapter_name): vm = self.sandbox_handler.vcenter_handler.get_vm_by_uuid(vm_uuid) for device in vm.config.hardware.device: if device.deviceInfo.label == adapter_name: break else: raise BaseAutomationException( f"Cannot find adapter {adapter_name} on vCenter", ) port_group_key = device.backing.port.portgroupKey for network in vm.network: if getattr(network, "key", "") == port_group_key: break else: raise BaseAutomationException( 'Cannot find network on the vCenter by portgroupKey "{}"'.format( port_group_key ), ) return PortInfo(device.macAddress, adapter_name, network.name)
def get_other_device_for_connectivity(self): sandbox_resources_handlers = [ handler for handler in self.handler_storage.resource_handlers if handler.conf.name in self.handler.sandbox_handler.conf.resource_names ] for resource_handler in sandbox_resources_handlers: if self.handler != resource_handler: other_resource = resource_handler return other_resource raise BaseAutomationException( f"You have to add an additional resource to the sandbox " f"{self.handler.sandbox_handler.conf.name} for connectivity tests" )
def _get_port_info_from_vm_details_network_data(network_data): attrs = {attr.Name: attr.Value for attr in network_data.AdditionalData} try: port_info = PortInfo( attrs["mac address"], attrs["network adapter"], attrs["port group name"], ) except KeyError: raise BaseAutomationException( "Cannot get all needed information about port from cs. " 'We need: "mac address", "network adapter", "port group name". ' "We have: {}".format(attrs) ) return port_info
def _update_resource_conf_from_deployment_resource( self, handler: DeploymentResourceHandler): try: conf = next(c for c in self._conf.resources_conf if c.networking_app_name == handler.conf.name) except StopIteration: emsg = f"Resource {handler.conf.name} is not present in resources config" raise BaseAutomationException(emsg) conf.device_ip = handler.device_ip if not conf.attributes.get("User"): attrs = { "User": "******", "Password": "******", "Enable Password": "******", } conf.attributes.update(attrs)
def test_get_test_file(self): test_name = self.handler.tests_conf.params["get_test_file"][ "test_name"] test_path = self.handler.get_test_file(test_name) dir_path, test_file_name = re.search(r"^(.*)[\\/](.*?)$", test_path).groups() if not dir_path.lower().startswith("c:\\"): raise BaseAutomationException( "Test file have to locate under C:\\") dir_path = re.sub(r"^[Cc]:\\", "", dir_path) cs = self.sandbox_handler._cs_handler files = cs.smb.ls(cs.CS_SHARE, dir_path) file_names = map(lambda f: f.filename, files) self.assertIn(test_file_name, file_names)
def sandbox_handler(self) -> "SandboxHandler": if self._sandbox_handler is None: raise BaseAutomationException("You have to add Sandbox Handler") return self._sandbox_handler