How to use launch_remote_followfiles method in autotest

Best Python code snippet using autotest_python

logfile_monitor.py

Source:logfile_monitor.py Github

copy

Full Screen

...31 """Copy over monitordir to a tmpdir on the remote host."""32 tmp_dir = host.get_tmp_dir()33 host.send_file(MONITORDIR, tmp_dir)34 return os.path.join(tmp_dir, 'monitors')35def launch_remote_followfiles(host, lastlines_dirpath, follow_paths):36 """Launch followfiles.py remotely on follow_paths."""37 logging.info('Launching followfiles on target: %s, %s, %s',38 host.hostname, lastlines_dirpath, str(follow_paths))39 # First make sure a supported Python is on host40 installed_pythons = list_remote_pythons(host)41 supported_python = select_supported_python(installed_pythons)42 if not supported_python:43 if DEFAULT_PYTHON in installed_pythons:44 logging.info('No versioned Python binary found, '45 'defaulting to: %s', DEFAULT_PYTHON)46 supported_python = DEFAULT_PYTHON47 else:48 raise FollowFilesLaunchError('No supported Python on host.')49 remote_monitordir = copy_monitordir(host)50 remote_script_path = os.path.join(remote_monitordir, 'followfiles.py')51 followfiles_cmd = '%s %s --lastlines_dirpath=%s %s' % (52 supported_python, remote_script_path,53 lastlines_dirpath, ' '.join(follow_paths))54 remote_ff_proc = subprocess.Popen(host._make_ssh_cmd(followfiles_cmd),55 stdin=open(os.devnull, 'r'),56 stdout=subprocess.PIPE, shell=True)57 def wait_for_crash():58 """59 Warning: this is not threadsafe due to the call to60 host.job.record()61 """62 # Give it enough time to crash if it's going to (it shouldn't).63 time.sleep(5)64 doa = remote_ff_proc.poll()65 if doa:66 # We're hosed, there is no point in proceeding.67 logging.fatal('Failed to launch followfiles on target,'68 ' aborting logfile monitoring: %s', host.hostname)69 if host.job:70 # Put a warning in the status.log71 host.job.record(72 'WARN', None, 'logfile.monitor',73 'followfiles launch failed')74 crash_waiter = threading.Thread(target=wait_for_crash)75 crash_waiter.start()76 return remote_ff_proc77def resolve_patterns_path(patterns_path):78 """Resolve patterns_path to existing absolute local path or raise.79 As a convenience we allow users to specify a non-absolute patterns_path.80 However these need to be resolved before allowing them to be passed down81 to console.py.82 For now we expect non-absolute ones to be in self.monitordir.83 """84 if os.path.isabs(patterns_path):85 if os.path.exists(patterns_path):86 return patterns_path87 else:88 raise InvalidPatternsPathError('Absolute path does not exist.')89 else:90 patterns_path = os.path.join(MONITORDIR, patterns_path)91 if os.path.exists(patterns_path):92 return patterns_path93 else:94 raise InvalidPatternsPathError('Relative path does not exist.')95def launch_local_console(96 input_stream, console_log_path, pattern_paths=None):97 """Launch console.py locally.98 This will process the output from followfiles and99 fire warning messages per configuration in pattern_paths.100 """101 r, w = os.pipe()102 local_script_path = os.path.join(MONITORDIR, 'console.py')103 console_cmd = [sys.executable, local_script_path]104 if pattern_paths:105 console_cmd.append('--pattern_paths=%s' % ','.join(pattern_paths))106 console_cmd += [console_log_path, str(w)]107 # Setup warning stream before we actually launch108 warning_stream = os.fdopen(r, 'r', 0)109 devnull_w = open(os.devnull, 'w')110 # Launch console.py locally111 console_proc = subprocess.Popen(112 console_cmd, stdin=input_stream,113 stdout=devnull_w, stderr=devnull_w)114 os.close(w)115 return console_proc, warning_stream116def _log_and_ignore_exceptions(f):117 """Decorator: automatically log exception during a method call.118 """119 def wrapped(self, *args, **dargs):120 try:121 return f(self, *args, **dargs)122 except Exception as e:123 print "LogfileMonitor.%s failed with exception %s" % (f.__name__, e)124 print "Exception ignored:"125 traceback.print_exc(file=sys.stdout)126 wrapped.__name__ = f.__name__127 wrapped.__doc__ = f.__doc__128 wrapped.__dict__.update(f.__dict__)129 return wrapped130class LogfileMonitorMixin(abstract_ssh.AbstractSSHHost):131 """This can monitor one or more remote files using tail.132 This class and its counterpart script, monitors/followfiles.py,133 add most functionality one would need to launch and monitor134 remote tail processes on self.hostname.135 This can be used by subclassing normally or by calling136 NewLogfileMonitorMixin (below)137 It is configured via two class attributes:138 follow_paths: Remote paths to monitor139 pattern_paths: Local paths to alert pattern definition files.140 """141 follow_paths = ()142 pattern_paths = ()143 def _initialize(self, console_log=None, *args, **dargs):144 super(LogfileMonitorMixin, self)._initialize(*args, **dargs)145 self._lastlines_dirpath = None146 self._console_proc = None147 self._console_log = console_log or 'logfile_monitor.log'148 def reboot_followup(self, *args, **dargs):149 super(LogfileMonitorMixin, self).reboot_followup(*args, **dargs)150 self.__stop_loggers()151 self.__start_loggers()152 def start_loggers(self):153 super(LogfileMonitorMixin, self).start_loggers()154 self.__start_loggers()155 def remote_path_exists(self, remote_path):156 """Return True if remote_path exists, False otherwise."""157 return not self.run(158 'ls %s' % remote_path, ignore_status=True).exit_status159 def check_remote_paths(self, remote_paths):160 """Return list of remote_paths that currently exist."""161 return [162 path for path in remote_paths if self.remote_path_exists(path)]163 @_log_and_ignore_exceptions164 def __start_loggers(self):165 """Start multifile monitoring logger.166 Launch monitors/followfiles.py on the target and hook its output167 to monitors/console.py locally.168 """169 # Check if follow_paths exist, in the case that one doesn't170 # emit a warning and proceed.171 follow_paths_set = set(self.follow_paths)172 existing = self.check_remote_paths(follow_paths_set)173 missing = follow_paths_set.difference(existing)174 if missing:175 # Log warning that we are missing expected remote paths.176 logging.warn('Target %s is missing expected remote paths: %s',177 self.hostname, ', '.join(missing))178 # If none of them exist just return (for now).179 if not existing:180 return181 # Create a new lastlines_dirpath on the remote host if not already set.182 if not self._lastlines_dirpath:183 self._lastlines_dirpath = self.get_tmp_dir(parent='/var/tmp')184 # Launch followfiles on target185 self._followfiles_proc = launch_remote_followfiles(186 self, self._lastlines_dirpath, existing)187 # Ensure we have sane pattern_paths before launching console.py188 sane_pattern_paths = []189 for patterns_path in set(self.pattern_paths):190 try:191 patterns_path = resolve_patterns_path(patterns_path)192 except InvalidPatternsPathError as e:193 logging.warn('Specified patterns_path is invalid: %s, %s',194 patterns_path, str(e))195 else:196 sane_pattern_paths.append(patterns_path)197 # Launch console.py locally, pass in output stream from followfiles.198 self._console_proc, self._logfile_warning_stream = \199 launch_local_console(...

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

Helpful

NotHelpful