def pytest_runtest_teardown(): for con_ssh_ in ControllerClient.get_active_controllers( current_thread_only=True): con_ssh_.flush() con_ssh_.connect(retry=True, retry_interval=3, retry_timeout=300) if natbox_ssh: natbox_ssh.flush() natbox_ssh.connect(retry=False)
def copy_test_apps(): cons_ssh = ControllerClient.get_active_controllers() home_dir = HostLinuxUser.get_home() app_dir = '{}/custom_apps/'.format(home_dir) common.scp_from_test_server_to_active_controllers( source_path=TestServerPath.CUSTOM_APPS, dest_dir=home_dir, dest_name='custom_apps/', cons_ssh=cons_ssh, timeout=60, is_dir=True) return app_dir
def scp_from_test_server_to_active_controllers(source_path, dest_dir, dest_name=None, timeout=900, cons_ssh=None, is_dir=False, force_ipv4=False): """ SCP file or files under a directory from test server to TiS server Args: source_path (str): test server file path or directory path dest_dir (str): destination directory. should end with '/' dest_name (str): destination file name if not dir timeout (int): con_ssh: is_dir (bool) Returns (str|None): destination file/dir path if scp successful else None """ if not cons_ssh: cons_ssh = ControllerClient.get_active_controllers() if not force_ipv4: ipv6 = ProjVar.get_var('IPV6_OAM') else: ipv6 = None source_server = TestFileServer.get_server(ipv6=ipv6) source_user = TestFileServer.get_user() source_password = TestFileServer.get_password() return _scp_from_remote_to_active_controllers( source_server=source_server, source_path=source_path, dest_dir=dest_dir, dest_name=dest_name, source_user=source_user, source_password=source_password, timeout=timeout, cons_ssh=cons_ssh, is_dir=is_dir, ipv6=ipv6)
def pytest_unconfigure(config): # collect all if needed if config.getoption('help'): return try: natbox_ssh = ProjVar.get_var('NATBOX_SSH') natbox_ssh.close() except: pass version_and_patch = '' try: version_and_patch = setups.get_version_and_patch_info() except Exception as e: LOG.debug(e) pass log_dir = ProjVar.get_var('LOG_DIR') if not log_dir: try: from utils.clients.ssh import ControllerClient ssh_list = ControllerClient.get_active_controllers(fail_ok=True) for con_ssh_ in ssh_list: con_ssh_.close() except: pass return log_dir = ProjVar.get_var('LOG_DIR') if not log_dir: try: from utils.clients.ssh import ControllerClient ssh_list = ControllerClient.get_active_controllers(fail_ok=True) for con_ssh_ in ssh_list: con_ssh_.close() except: pass return try: tc_res_path = log_dir + '/test_results.log' build_info = ProjVar.get_var('BUILD_INFO') build_id = build_info.get('BUILD_ID', '') build_job = build_info.get('JOB', '') build_server = build_info.get('BUILD_HOST', '') system_config = ProjVar.get_var('SYS_TYPE') session_str = '' total_exec = TestRes.PASSNUM + TestRes.FAILNUM # pass_rate = fail_rate = '0' if total_exec > 0: pass_rate = "{}%".format( round(TestRes.PASSNUM * 100 / total_exec, 2)) fail_rate = "{}%".format( round(TestRes.FAILNUM * 100 / total_exec, 2)) with open(tc_res_path, mode='a') as f: # Append general info to result log f.write('\n\nLab: {}\n' 'Build ID: {}\n' 'Job: {}\n' 'Build Server: {}\n' 'System Type: {}\n' 'Automation LOGs DIR: {}\n' 'Ends at: {}\n' '{}' # test session id and tag '{}'.format(ProjVar.get_var('LAB_NAME'), build_id, build_job, build_server, system_config, ProjVar.get_var('LOG_DIR'), tc_end_time, session_str, version_and_patch)) # Add result summary to beginning of the file f.write( '\nSummary:\nPassed: {} ({})\nFailed: {} ({})\nTotal ' 'Executed: {}\n'. format(TestRes.PASSNUM, pass_rate, TestRes.FAILNUM, fail_rate, total_exec)) if TestRes.SKIPNUM > 0: f.write('------------\nSkipped: {}'.format(TestRes.SKIPNUM)) LOG.info("Test Results saved to: {}".format(tc_res_path)) with open(tc_res_path, 'r') as fin: print(fin.read()) except Exception as e: LOG.exception( "Failed to add session summary to test_results.py. " "\nDetails: {}".format(e.__str__())) # Below needs con_ssh to be initialized try: from utils.clients.ssh import ControllerClient con_ssh = ControllerClient.get_active_controller() except: LOG.warning("No con_ssh found") return try: parse_log.parse_test_steps(ProjVar.get_var('LOG_DIR')) except Exception as e: LOG.warning( "Unable to parse test steps. \nDetails: {}".format(e.__str__())) if test_count > 0 and (ProjVar.get_var('ALWAYS_COLLECT') or ( has_fail and ProjVar.get_var('COLLECT_ALL'))): # Collect tis logs if collect all required upon test(s) failure # Failure on collect all would not change the result of the last test # case. try: setups.collect_tis_logs(con_ssh) except Exception as e: LOG.warning("'collect all' failed. {}".format(e.__str__())) ssh_list = ControllerClient.get_active_controllers(fail_ok=True, current_thread_only=True) for con_ssh_ in ssh_list: try: con_ssh_.close() except: pass
def run(self): """ Do not run this command. Start threads from start_thread functions Returns: """ LOG.info("Starting {}".format(self.name)) # run the function try: MThread.running_threads.append(self) LOG.info("Connecting to lab fip in new thread...") lab = ProjVar.get_var('lab') from keywords import common con_ssh = common.ssh_to_stx(set_client=True) if ProjVar.get_var('IS_DC'): LOG.info("Connecting to subclouds fip in new thread...") ControllerClient.set_active_controller(con_ssh, 'RegionOne') con_ssh_dict = ControllerClient.get_active_controllers_map() for name in con_ssh_dict: if name in lab: subcloud_fip = lab[name]['floating ip'] subcloud_ssh = SSHClient(subcloud_fip) try: subcloud_ssh.connect(use_current=False) ControllerClient.set_active_controller( subcloud_ssh, name=name) except: if name == ProjVar.get_var('PRIMARY_SUBCLOUD'): raise LOG.warning('Cannot connect to {}'.format(name)) LOG.info("Connecting to NatBox in new thread...") NATBoxClient.set_natbox_client() if ProjVar.get_var('REMOTE_CLI'): RemoteCLIClient.get_remote_cli_client() LOG.info("Execute function {}({}, {})".format( self.func.__name__, self.args, self.kwargs)) self._output = self.func(*self.args, **self.kwargs) LOG.info("{} returned: {}".format(self.func.__name__, self._output.__str__())) self._output_returned.set() except: err = traceback.format_exc() # LOG.error("Error found in thread call {}".format(err)) self._err = err raise finally: LOG.info("Terminating thread: {}".format(self.thread_id)) if ProjVar.get_var('IS_DC'): ssh_clients = ControllerClient.get_active_controllers( current_thread_only=True) for con_ssh in ssh_clients: con_ssh.close() else: ControllerClient.get_active_controller().close() natbox_ssh = NATBoxClient.get_natbox_client() if natbox_ssh: natbox_ssh.close() if ProjVar.get_var('REMOTE_CLI'): RemoteCLIClient.get_remote_cli_client().close() LOG.debug("{} has finished".format(self.name)) MThread.running_threads.remove(self)
def _scp_from_remote_to_active_controllers(source_server, source_path, dest_dir, dest_name=None, source_user=None, source_password=None, timeout=900, cons_ssh=None, is_dir=False, ipv6=None): """ SCP file or files under a directory from remote server to TiS server Args: source_path (str): remote server file path or directory path dest_dir (str): destination directory. should end with '/' dest_name (str): destination file name if not dir timeout (int): con_ssh: is_dir Returns (str|None): destination file/dir path if scp successful else None """ if cons_ssh is None: cons_ssh = ControllerClient.get_active_controllers() if not source_user: source_user = TestFileServer.get_user() if not source_password: source_password = TestFileServer.get_password() if dest_name is None and not is_dir: dest_name = source_path.split(sep='/')[-1] dest_path = dest_dir if not dest_name else os.path.join( dest_dir, dest_name) LOG.info( 'Create destination directory on tis server if not already exists') cmd = 'mkdir -p {}'.format(dest_dir) for con_ssh in cons_ssh: if not con_ssh.file_exists(dest_path): con_ssh.exec_cmd(cmd, fail_ok=False) nat_name = ProjVar.get_var('NATBOX') if nat_name: nat_name = nat_name.get('name') if nat_name and (nat_name == 'localhost' or nat_name.startswith('128.224.')): LOG.info('VBox detected, performing intermediate scp') nat_dest_path = '/tmp/{}'.format(dest_name) nat_ssh = NATBoxClient.get_natbox_client() if not nat_ssh.file_exists(nat_dest_path): LOG.info("scp file from {} to NatBox: {}".format( nat_name, source_server)) nat_ssh.scp_on_dest(source_user=source_user, source_ip=source_server, source_path=source_path, dest_path=nat_dest_path, source_pswd=source_password, timeout=timeout, is_dir=is_dir) LOG.info('scp file from natbox {} to active controller'.format( nat_name)) dest_user = HostLinuxUser.get_user() dest_pswd = HostLinuxUser.get_password() dest_ip = con_ssh.host nat_ssh.scp_on_source(source_path=nat_dest_path, dest_user=dest_user, dest_ip=dest_ip, dest_path=dest_path, dest_password=dest_pswd, timeout=timeout, is_dir=is_dir) else: # if not a VBox lab, scp from remote server directly to TiS server LOG.info("scp file(s) from {} to tis".format(source_server)) con_ssh.scp_on_dest(source_user=source_user, source_ip=source_server, source_path=source_path, dest_path=dest_path, source_pswd=source_password, timeout=timeout, is_dir=is_dir, ipv6=ipv6) return dest_path