def wait_for_done(self, m_id, host, timeout=60): """ wait action finished, m_id: the mission id for action host: which host the task is running timeout: wait timeout """ query = databases.DBQuery(host) while True: try: msg = LISTENING_QUEUE.get(True, timeout) #logging.debug(msg) except Queue.Empty: raise error.OperationTimeoutException(timeout) else: if ((msg.has_key('action') or msg.has_key('option_result')) and msg.has_key('id')): if m_id == msg['id'] and 'actionResult' == msg['message']: logging.debug(msg) if 'failed' == query.get_mission_stat(m_id): err_msg = query.get_mission_message(m_id) raise error.TestError("Action Failed: %s" % err_msg) if msg.has_key('option_result'): return msg['option_result'] return True #Just put it back if mission id is not match, someone must need it else: #Every mission start from code has a negative id if msg['id'] < 0 and msg[ 'message'] != 'missionProgress': LISTENING_QUEUE.put(msg, timeout=timeout)
def check_clone(self, vm): img = os.path.basename(vm.get_baseimg()) storage_path = vm.get_storage_path() result = vm.host.run("find %s -name %s" % (storage_path, img)).stdout.strip() if vm.get_baseimg() not in result: raise error.TestError("VERIFY clone: Cannot find image file")
def start_server(session): if c_type == "ssh": start_cmd = "sendfile %s &" % d_port else: drive_path = get_abs_path(session, "sendfile", "exe") start_cmd = "start /b %ssendfile.exe %s" % (drive_path, d_port) session.cmd_output_safe(start_cmd) if not server_alive(session): raise error.TestError("Start udt server failed")
def server_alive(session): if c_type == "ssh": check_cmd = "ps aux" else: check_cmd = "tasklist" o = session.cmd_output(check_cmd) if not o: raise error.TestError("Can not get the server status") if "sendfile" in o.lower(): return True return False
def get_abs_path(session, filename, extension): """ return file path drive+path """ cmd_tmp = "wmic datafile where \"Filename='%s' and " cmd_tmp += "extension='%s'\" get drive^,path" cmd = cmd_tmp % (filename, extension) info = session.cmd_output(cmd, timeout=360).strip() drive_path = re.search(r'(\w):\s+(\S+)', info, re.M) if not drive_path: raise error.TestError("Not found file %s.%s in your guest" % (filename, extension)) return ":".join(drive_path.groups())
def __init__(self, client="ssh", host=None, port="22", username="******", password=None, prompt=r"[\#\$]\s*$", linesep="\n", log_filename=None, timeout=240, internal_timeout=10, session=None): """ Initialization of RemoteRunner. Init a session login to remote host or guest. :param client: The client to use ('ssh', 'telnet' or 'nc') :param host: Hostname or IP address :param port: Port to connect to :param username: Username (if required) :param password: Password (if required) :param prompt: Shell prompt (regular expression) :param linesep: The line separator to use when sending lines (e.g. '\\n' or '\\r\\n') :param log_filename: If specified, log all output to this file :param timeout: Total time duration to wait for a successful login :param internal_timeout: The maximal time duration (in seconds) to wait for each step of the login procedure (e.g. the "Are you sure" prompt or the password prompt) :param session: An existing session :see: wait_for_login() :raise: Whatever wait_for_login() raises """ if session is None: if host is None: raise error.TestError("Neither host, nor session was defined!") self.session = wait_for_login(client, host, port, username, password, prompt, linesep, log_filename, timeout, internal_timeout) else: self.session = session # Init stdout pipe and stderr pipe. self.stdout_pipe = tempfile.mktemp() self.stderr_pipe = tempfile.mktemp()
def get_file_md5(session, file_path): """ Get files md5sums """ if c_type == "ssh": md5_cmd = "md5sum %s" % file_path md5_reg = r"(\w+)\s+%s.*" % file_path else: drive_path = get_abs_path(session, "md5sums", "exe") filename = file_path.split("\\")[-1] md5_reg = r"%s\s+(\w+)" % filename md5_cmd = '%smd5sums.exe %s | find "%s"' % (drive_path, file_path, filename) o = session.cmd_output(md5_cmd) file_md5 = re.findall(md5_reg, o) if not o: raise error.TestError("Get file %s md5sum error" % file_path) return file_md5
def start_record_thread(self): """Start recording power play data. Get a list of connected USB devices and try to establish a serial connection. Once the connection is established, open a text file and start reading serial data and write it to the text file after some formatting. """ devices = [x for x in os.listdir('/dev/') if x.startswith('ttyUSB')] for device in devices: device_link = '/dev/' + device try: if self.ser == None: logging.info('Trying ... %s', device_link) self.ser = serial.Serial(device_link, 115200) logging.info('Successfully connected to %s', device_link) break except serial.SerialException, e: raise error.TestError('Failed to connect to %s becuase of %s' % (device_link, str(e)))
def verify_vm_ip(self): if self.ip: temp_list = str(self.ip).split('.') if '169' == temp_list[0]: raise error.TestError("VM IP not available")
def udp_copy_between_remotes(src, dst, s_port, s_passwd, d_passwd, s_name, d_name, s_path, d_path, c_type="ssh", c_prompt="\n", d_port="9000", timeout=600): """ Copy files from a remote host (guest) to another remote host (guest) by udp. :param src/dst: Hostname or IP address of src and dst :param s_name/d_name: Username (if required) :param s_passwd/d_passwd: Password (if required) :param s_path/d_path: Path on the remote machine where we are copying :param c_type: Login method to remote host(guest). :param c_prompt : command line prompt of remote host(guest) :param d_port: the port data transfer :param timeout: data transfer timeout """ s_session = remote_login(c_type, src, s_port, s_name, s_passwd, c_prompt) d_session = remote_login(c_type, dst, s_port, d_name, d_passwd, c_prompt) def get_abs_path(session, filename, extension): """ return file path drive+path """ cmd_tmp = "wmic datafile where \"Filename='%s' and " cmd_tmp += "extension='%s'\" get drive^,path" cmd = cmd_tmp % (filename, extension) info = session.cmd_output(cmd, timeout=360).strip() drive_path = re.search(r'(\w):\s+(\S+)', info, re.M) if not drive_path: raise error.TestError("Not found file %s.%s in your guest" % (filename, extension)) return ":".join(drive_path.groups()) def get_file_md5(session, file_path): """ Get files md5sums """ if c_type == "ssh": md5_cmd = "md5sum %s" % file_path md5_reg = r"(\w+)\s+%s.*" % file_path else: drive_path = get_abs_path(session, "md5sums", "exe") filename = file_path.split("\\")[-1] md5_reg = r"%s\s+(\w+)" % filename md5_cmd = '%smd5sums.exe %s | find "%s"' % (drive_path, file_path, filename) o = session.cmd_output(md5_cmd) file_md5 = re.findall(md5_reg, o) if not o: raise error.TestError("Get file %s md5sum error" % file_path) return file_md5 def server_alive(session): if c_type == "ssh": check_cmd = "ps aux" else: check_cmd = "tasklist" o = session.cmd_output(check_cmd) if not o: raise error.TestError("Can not get the server status") if "sendfile" in o.lower(): return True return False def start_server(session): if c_type == "ssh": start_cmd = "sendfile %s &" % d_port else: drive_path = get_abs_path(session, "sendfile", "exe") start_cmd = "start /b %ssendfile.exe %s" % (drive_path, d_port) session.cmd_output_safe(start_cmd) if not server_alive(session): raise error.TestError("Start udt server failed") def start_client(session): if c_type == "ssh": client_cmd = "recvfile %s %s %s %s" % (src, d_port, s_path, d_path) else: drive_path = get_abs_path(session, "recvfile", "exe") client_cmd_tmp = "%srecvfile.exe %s %s %s %s" client_cmd = client_cmd_tmp % (drive_path, src, d_port, s_path.split("\\")[-1], d_path.split("\\")[-1]) session.cmd_output_safe(client_cmd, timeout) def stop_server(session): if c_type == "ssh": stop_cmd = "killall sendfile" else: stop_cmd = "taskkill /F /IM sendfile.exe" if server_alive(session): session.cmd_output_safe(stop_cmd) try: src_md5 = get_file_md5(s_session, s_path) if not server_alive(s_session): start_server(s_session) start_client(d_session) dst_md5 = get_file_md5(d_session, d_path) if src_md5 != dst_md5: err_msg = "Files md5sum mismatch, file %s md5sum is '%s', " err_msg = "but the file %s md5sum is %s" raise error.TestError(err_msg % (s_path, src_md5, d_path, dst_md5)) finally: stop_server(s_session) s_session.close() d_session.close()