Best Python code snippet using autotest_python
test.py
Source:test.py
...48def install_autotest_and_run(func):49 def wrapper(self, mytest):50 host, at, outputdir = self._install()51 try:52 host.erase_dir_contents(outputdir)53 func(self, mytest, host, at, outputdir)54 finally:55 # the test class can define this flag to make us remove the56 # sysinfo install files and outputdir contents after each run57 if mytest.disable_sysinfo_install_cache:58 self.cleanup(host_close=False)59 return wrapper60class _sysinfo_logger(object):61 def __init__(self, job):62 self.job = job63 self.pickle = None64 # for now support a single host65 self.host = None66 self.autotest = None67 self.outputdir = None68 self.disable_hooks = False69 if len(job.machines) != 1:70 # disable logging on multi-machine tests71 self.disable_hooks = True72 def _install(self):73 if not self.host:74 from autotest.server import hosts, autotest_remote75 self.host = hosts.create_host(self.job.machines[0],76 auto_monitor=False)77 try:78 tmp_dir = self.host.get_tmp_dir(parent="/tmp/sysinfo")79 self.autotest = autotest_remote.Autotest(self.host)80 self.autotest.install(autodir=tmp_dir)81 self.outputdir = self.host.get_tmp_dir()82 except Exception:83 # if installation fails roll back the host84 try:85 self.host.close()86 except Exception:87 logging.exception("Unable to close host %s",88 self.host.hostname)89 self.host = None90 self.autotest = None91 raise92 else:93 host = self.host94 # if autotest client dir does not exist, reinstall (it may have95 # been removed by the test code)96 autodir = host.get_autodir()97 if not autodir or not host.path_exists(autodir):98 self.autotest.install(autodir=autodir)99 # if the output dir does not exist, recreate it100 if not host.path_exists(self.outputdir):101 host.run('mkdir -p %s' % self.outputdir)102 return self.host, self.autotest, self.outputdir103 def _pull_pickle(self, host, outputdir):104 """Pulls from the client the pickle file with the saved sysinfo state.105 """106 fd, path = tempfile.mkstemp(dir=self.job.tmpdir)107 os.close(fd)108 host.get_file(os.path.join(outputdir, "sysinfo.pickle"), path)109 self.pickle = path110 def _push_pickle(self, host, outputdir):111 """Pushes the server saved sysinfo pickle file to the client.112 """113 if self.pickle:114 host.send_file(self.pickle,115 os.path.join(outputdir, "sysinfo.pickle"))116 os.remove(self.pickle)117 self.pickle = None118 def _pull_sysinfo_keyval(self, host, outputdir, mytest):119 """Pulls sysinfo and keyval data from the client.120 """121 # pull the sysinfo data back on to the server122 host.get_file(os.path.join(outputdir, "sysinfo"), mytest.outputdir)123 # pull the keyval data back into the local one124 fd, path = tempfile.mkstemp(dir=self.job.tmpdir)125 os.close(fd)126 host.get_file(os.path.join(outputdir, "keyval"), path)127 keyval = utils.read_keyval(path)128 os.remove(path)129 mytest.write_test_keyval(keyval)130 @log.log_and_ignore_errors("pre-test server sysinfo error:")131 @install_autotest_and_run132 def before_hook(self, mytest, host, at, outputdir):133 if not self.disable_hooks:134 # run the pre-test sysinfo script135 at.run(_sysinfo_before_test_script % outputdir,136 results_dir=self.job.resultdir)137 self._pull_pickle(host, outputdir)138 @log.log_and_ignore_errors("pre-test iteration server sysinfo error:")139 @install_autotest_and_run140 def before_iteration_hook(self, mytest, host, at, outputdir):141 if not self.disable_hooks:142 # this function is called after before_hook() se we have sysinfo state143 # to push to the server144 self._push_pickle(host, outputdir)145 # run the pre-test iteration sysinfo script146 at.run(_sysinfo_iteration_script %147 (outputdir, 'log_before_each_iteration', mytest.iteration,148 'before'),149 results_dir=self.job.resultdir)150 # get the new sysinfo state from the client151 self._pull_pickle(host, outputdir)152 @log.log_and_ignore_errors("post-test iteration server sysinfo error:")153 @install_autotest_and_run154 def after_iteration_hook(self, mytest, host, at, outputdir):155 if not self.disable_hooks:156 # push latest sysinfo state to the client157 self._push_pickle(host, outputdir)158 # run the post-test iteration sysinfo script159 at.run(_sysinfo_iteration_script %160 (outputdir, 'log_after_each_iteration', mytest.iteration,161 'after'),162 results_dir=self.job.resultdir)163 # get the new sysinfo state from the client164 self._pull_pickle(host, outputdir)165 @log.log_and_ignore_errors("post-test server sysinfo error:")166 @install_autotest_and_run167 def after_hook(self, mytest, host, at, outputdir):168 if not self.disable_hooks:169 self._push_pickle(host, outputdir)170 # run the post-test sysinfo script171 at.run(_sysinfo_after_test_script % outputdir,172 results_dir=self.job.resultdir)173 self._pull_sysinfo_keyval(host, outputdir, mytest)174 def cleanup(self, host_close=True):175 if self.host and self.autotest:176 try:177 try:178 self.autotest.uninstall()179 finally:180 if host_close:181 self.host.close()182 else:183 self.host.erase_dir_contents(self.outputdir)184 except Exception:185 # ignoring exceptions here so that we don't hide the true186 # reason of failure from runtest187 logging.exception('Error cleaning up the sysinfo autotest/host '188 'objects, ignoring it')189def runtest(job, url, tag, args, dargs):190 if not dargs.pop('disable_sysinfo', False):191 logger = _sysinfo_logger(job)192 logging_args = [logger.before_hook, logger.after_hook,193 logger.before_iteration_hook,194 logger.after_iteration_hook]195 else:196 logger = None197 logging_args = [None, None, None, None]...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!