How to use collect_log_file method in autotest

Best Python code snippet using autotest_python Github


Full Screen

...32 collect_uncollected_logs(host)33 # Collect everything in /var/log.34 log_path = os.path.join(crashinfo_dir, 'var')35 os.makedirs(log_path)36 collect_log_file(host, constants.LOG_DIR, log_path)37 # Collect console-ramoops38 log_path = os.path.join(39 crashinfo_dir, os.path.basename(constants.LOG_CONSOLE_RAMOOPS))40 collect_log_file(host, constants.LOG_CONSOLE_RAMOOPS, log_path)41 # Collect i915_error_state, only available on intel systems.42 # i915 contains the Intel graphics state. It might contain useful data43 # when a DUT hangs, times out or crashes.44 log_path = os.path.join(45 crashinfo_dir, os.path.basename(constants.LOG_I915_ERROR_STATE))46 collect_log_file(host, constants.LOG_I915_ERROR_STATE,47 log_path, use_tmp=True)48# Load default for number of hours to wait before giving up on crash collection.49HOURS_TO_WAIT = global_config.global_config.get_config_value(50 'SERVER', 'crash_collection_hours_to_wait', type=float, default=4.0)51def wait_for_machine_to_recover(host, hours_to_wait=HOURS_TO_WAIT):52 """Wait for a machine (possibly down) to become accessible again.53 @param host: A RemoteHost instance to wait on54 @param hours_to_wait: Number of hours to wait before giving up55 @returns: True if the machine comes back up, False otherwise56 """57 current_time = time.strftime("%b %d %H:%M:%S", time.localtime())58 if host.is_up():59"%s already up, collecting crash info", host.hostname)60 return True61"Waiting %s hours for %s to come up (%s)",62 hours_to_wait, host.hostname, current_time)63 if not host.wait_up(timeout=hours_to_wait * 3600):64 autotest_stats.Counter('collect_crashinfo_timeout').increment()65 logging.warning("%s down, unable to collect crash info",66 host.hostname)67 return False68 else:69"%s is back up, collecting crash info", host.hostname)70 return True71def get_crashinfo_dir(host, dir_prefix):72 """Find and if necessary create a directory to store crashinfo in.73 @param host: The RemoteHost object that crashinfo will be collected from74 @param dir_prefix: Prefix of directory name.75 @returns: The path to an existing directory for writing crashinfo into76 """77 host_resultdir = getattr(getattr(host, "job", None), "resultdir", None)78 if host_resultdir:79 infodir = host_resultdir80 else:81 infodir = os.path.abspath(os.getcwd())82 infodir = os.path.join(infodir, "%s.%s" % (dir_prefix, host.hostname))83 if not os.path.exists(infodir):84 os.mkdir(infodir)85 return infodir86def collect_log_file(host, log_path, dest_path, use_tmp=False):87 """Collects a log file from the remote machine.88 Log files are collected from the remote machine and written into the89 destination path. If dest_path is a directory, the log file will be named90 using the basename of the remote log path.91 @param host: The RemoteHost to collect logs from92 @param log_path: The remote path to collect the log file from93 @param dest_path: A path (file or directory) to write the copies logs into94 @param use_tmp: If True, will first copy the logs to a temporary directory95 on the host and download logs from there.96 """97'Collecting %s...', log_path)98 try:99 source_path = log_path100 if use_tmp:101 devnull = open('/dev/null', 'w')102 tmpdir ='mktemp -d', stdout_tee=devnull).stdout.strip()103'cp -rp %s %s' % (log_path, tmpdir))104 source_path = os.path.join(tmpdir, os.path.basename(log_path))105 host.get_file(source_path, dest_path, preserve_perm=False)106 if use_tmp:107'rm -rf %s' % tmpdir)108 except Exception, e:109 logging.warning('Collection of %s failed: %s', log_path, e)110def collect_command(host, command, dest_path):111 """Collects the result of a command on the remote machine.112 The standard output of the command will be collected and written into the113 desitionation path. The destination path is assumed to be filename and114 not a directory.115 @param host: The RemoteHost to collect from116 @param command: A shell command to run on the remote machine and capture117 the output from.118 @param dest_path: A file path to write the results of the log into119 """120"Collecting '%s' ...", command)121 devnull = open("/dev/null", "w")122 try:123 try:124 result =, stdout_tee=devnull).stdout125 utils.open_write_close(dest_path, result)126 except Exception, e:127 logging.warning("Collection of '%s' failed:\n%s", command, e)128 finally:129 devnull.close()130def collect_uncollected_logs(host):131 """Collects any leftover uncollected logs from the client.132 @param host: The RemoteHost to collect from133 """134 if host.job:135 try:136 logs = host.job.get_client_logs()137 for hostname, remote_path, local_path in logs:138 if hostname == host.hostname:139"Retrieving logs from %s:%s into %s",140 hostname, remote_path, local_path)141 host.get_file(remote_path + "/", local_path + "/")142 except Exception, e:143 logging.warning("Error while trying to collect stranded "144 "Autotest client logs: %s", e)145def collect_messages(host):146 """Collects the 'new' contents of /var/log/messages.147 If host.VAR_LOG_MESSAGE_COPY_PATH is on the remote machine, collects148 the contents of /var/log/messages excluding whatever initial contents149 are already present in host.VAR_LOG_MESSAGE_COPY_PATH. If it is not150 present, simply collects the entire contents of /var/log/messages.151 @param host: The RemoteHost to collect from152 """153 crashinfo_dir = get_crashinfo_dir(host, 'crashinfo')154 try:155 # paths to the messages files156 messages = os.path.join(crashinfo_dir, "messages")157 messages_raw = os.path.join(crashinfo_dir, "messages.raw")158 messages_at_start = os.path.join(crashinfo_dir, "messages.at_start")159 # grab the files from the remote host160 collect_log_file(host, host.VAR_LOG_MESSAGES_COPY_PATH,161 messages_at_start)162 collect_log_file(host, "/var/log/messages", messages_raw)163 # figure out how much of messages.raw to skip164 if os.path.exists(messages_at_start):165 # if the first lines of the messages at start should match the166 # first lines of the current messages; if they don't then messages167 # has been erase or rotated and we just grab all of it168 first_line_at_start = utils.read_one_line(messages_at_start)169 first_line_now = utils.read_one_line(messages_raw)170 if first_line_at_start != first_line_now:171 size_at_start = 0172 else:173 size_at_start = os.path.getsize(messages_at_start)174 else:175 size_at_start = 0176 raw_messages_file = open(messages_raw)...

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:


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

Run autotest 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?