def merge_bundles(bundles=[]): config = get_config() merged_bundles = {"format": config.bundle_format, 'test_runs': []} for bundle in bundles: if bundle['test_runs']: merged_bundles['test_runs'].append(bundle['test_runs'][0]) return merged_bundles
def _savetestdata(self, analyzer_assigned_uuid, run_options=""): config = get_config() TIMEFORMAT = '%Y-%m-%dT%H:%M:%SZ' bundle = { 'format': config.bundle_format, 'test_runs': [ { 'analyzer_assigned_uuid': analyzer_assigned_uuid, 'analyzer_assigned_date': self.runner.starttime.strftime(TIMEFORMAT), 'time_check_performed': False, 'attributes':{}, 'test_id': self.testname, 'test_results':[], 'attachments':[], 'hardware_context': hwprofile.get_hardware_context(self.adb), 'software_context': swprofile.get_software_context(self.adb) } ] } if run_options: bundle['test_runs'][0]['attributes']['run_options'] = run_options self._add_install_options(bundle, config) filename_host = os.path.join(config.tempdir_host, 'testdata.json') write_file(DocumentIO.dumps(bundle), filename_host) filename_target = os.path.join(self.resultsdir, 'testdata.json') self.adb.push(filename_host, filename_target)
def run(self, quiet=False, run_options=None): if not self.runner: raise RuntimeError("no test runner defined for '%s'" % self.testname) if not run_options: run_options = self.default_options self.runner.setadb(self.adb) config = get_config() if not os.path.exists(config.tempdir_host): os.mkdir(config.tempdir_host) os.chdir(config.tempdir_host) resultname = (self.testname + str(time.mktime(datetime.utcnow().timetuple()))) self.resultsdir = os.path.join(config.resultsdir_android, resultname) self.adb.makedirs(self.resultsdir) self.runner.run(self.resultsdir, run_options=run_options) self._gather_tombstones(self.resultsdir) self._copyattachments(self.resultsdir) self._screencap(self.resultsdir) self._savetestdata(str(uuid4()), run_options=run_options) result_id = os.path.basename(self.resultsdir) print("ANDROID TEST RUN COMPLETE: Result id is '%s'" % result_id) os.chdir(self.origdir) return result_id
def generate_bundle(serial=None, result_id=None, test=None, test_id=None, attachments=[]): if result_id is None: return {} config = get_config() adb = ADB(serial) resultdir = os.path.join(config.resultsdir_android, result_id) if not adb.exists(resultdir): raise Exception("The result (%s) is not existed." % result_id) bundle_text = adb.read_file(os.path.join(resultdir, "testdata.json")) bundle = DocumentIO.loads(bundle_text)[1] test_tmp = None if test: test_tmp = test else: test_tmp = TestProvider().load_test(bundle['test_runs'][0]['test_id'], serial) if test_id: bundle['test_runs'][0]['test_id'] = test_id else: attrs = bundle['test_runs'][0].get('attributes') if attrs: run_options = attrs.get('run_options') if run_options: test_id = '%s(%s)' % (bundle['test_runs'][0]['test_id'], run_options) bundle['test_runs'][0]['test_id'] = test_id test_tmp.parse(result_id) stdout_text = adb.read_file(os.path.join(resultdir, os.path.basename(test_tmp.org_ouput_file))) if stdout_text is None: stdout_text = '' stderr_text = adb.read_file(os.path.join(resultdir, 'stderr.log')) if stderr_text is None: stderr_text = '' bundle['test_runs'][0]["test_results"] = test_tmp.parser.results[ "test_results"] ## following part is used for generating the attachment for normal test attachment_bundles = [] for attachment in test_tmp.attachments: data_bundle = attachment.generate_bundle(adb=adb, resultsdir=resultdir) if data_bundle: attachment_bundles.append(data_bundle) bundle['test_runs'][0]["attachments"] = attachment_bundles ##following used for the attachment for monkeyrunner test for attach in attachments: if os.path.exists(attach): with open(attach, 'rb') as stream: data = stream.read() if data: bundle['test_runs'][0]["attachments"].append({ "pathname": os.path.basename(attach), "mime_type": 'image/png', "content": base64.standard_b64encode(data)}) return bundle
def test_run(self): config = get_config() test_name = 'foo' clear_fake() testrunner = makerunner(steps_host_pre=["echo foo"]) test = maketest(name=test_name, runner=testrunner) real_installed_path = '%s/%s/%s' % (test_tmp, config.installdir_android, test_name) test.setadb(FakeAdb()) self.assertFalse(os.path.exists(real_installed_path)) test.install() self.assertTrue(os.path.exists(real_installed_path)) fake_adb(output_str=fake_output_str) result_id = test.run() self.assertTrue("LAVA: (stdout) foo" in self.out.getvalue()) result_id_pattern = "foo\d+\.\d+" self.assertTrue(re.match(result_id_pattern, result_id)) self.assertTrue("LAVA: (stdout) foo" in self.out.getvalue()) test.uninstall() self.assertFalse(os.path.exists(real_installed_path))
def _fake_install_path(self): """ The path that we create on the android system to indicate that the black box test is installed. This is used by uninstall() and install() """ config = get_config() return os.path.join(config.installdir_android, self.testname)
def uninstall(self): """Uninstall the test suite. Uninstalling just recursively removes the test specific directory under the user's XDG_DATA_HOME directory. This will both mark the test as removed, and clean up any files that were downloaded or installed under that directory. Dependencies are intentionally not removed by this. And others installed files won't be removed too. """ config = get_config() path = os.path.join(config.installdir_android, self.testname) if self.adb.exists(path): self.adb.rmtree(path)
def _get_combined_bundle(self, result_id): """ Compute the combined bundle of a past run and return it """ config = get_config() temp_dir = tempfile.mkdtemp() remote_bundle_dir = os.path.join(config.resultsdir_android, result_id) try: self._copy_all_bundles(remote_bundle_dir, temp_dir) bundle = self._combine_bundles(temp_dir) finally: shutil.rmtree(temp_dir) return bundle
class ShellTestProvider(TestProvider): test_prefix = 'shell' config = get_config() dotext = '.sh' def list_test(self): dotext = '.sh' mod = self.import_mod("lava_android_test.test_definitions.shells") sh_files = find_files(mod.curdir, dotext) test_list = [] for f in sh_files: ##Assume that the file name only has one '.sh' f_name_no_dotext = os.path.basename(f).replace(dotext, '') test_list.append('%s-%s' % (self.test_prefix, f_name_no_dotext)) return test_list def load_test(self, test_name=None, serial=None): if not test_name.startswith('%s-' % self.test_prefix): raise UnfoundTest('The test(%s) is not found!' % test_name) f_name_no_dotext = test_name.replace('%s-' % self.test_prefix, '', 1) mod = self.import_mod("lava_android_test.test_definitions.%ss" % self.test_prefix) f_name = '%s%s' % (f_name_no_dotext, self.dotext) sh_file = '%s/%s' % (mod.curdir, f_name) test_sh_android_path = os.path.join(self.config.installdir_android, test_name, f_name) INSTALL_STEPS_ADB_PRE = [ 'push %s %s ' % (sh_file, test_sh_android_path), 'shell chmod 777 %s' % test_sh_android_path ] ADB_SHELL_STEPS = ['%s $(OPTIONS)' % test_sh_android_path] testobj = self.gen_testobj( testname=test_name, installer=testdef.AndroidTestInstaller( steps_adb_pre=INSTALL_STEPS_ADB_PRE), runner=testdef.AndroidTestRunner(adbshell_steps=ADB_SHELL_STEPS), parser=testdef.AndroidSimpleTestParser(), adb=ADB(serial)) return testobj
def parse(self, resultname): if not self.parser: raise RuntimeError("no test parser defined for '%s'" % self.testname) output_filename = os.path.basename(self.org_ouput_file) config = get_config() os.chdir(config.tempdir_host) resultsdir_android = os.path.join(config.resultsdir_android, resultname) result_filename_android = os.path.join(resultsdir_android, output_filename) result_filename_host_temp = tempfile.mkstemp(prefix=output_filename, dir=config.tempdir_host)[1] self.adb.pull(result_filename_android, result_filename_host_temp) self.parser.parse(output_filename, output_filename=result_filename_host_temp, test_name=self.testname) os.remove(result_filename_host_temp) os.chdir(self.origdir)
def generate_bundle(self, adb=None, resultsdir=None): data_bundle = {} if not self.pathname: return data_bundle if not adb: adb = ADB() config = get_config() basename = os.path.basename(self.pathname) android_path = os.path.join(resultsdir, basename) if adb.exists(android_path): tmp_path = os.path.join(config.tempdir_host, basename) adb.pull(android_path, tmp_path) with open(tmp_path, 'rb') as stream: data = stream.read() if data: data_bundle = {"pathname": basename, "mime_type": self.mime_type, "content": base64.standard_b64encode(data)} os.unlink(tmp_path) return data_bundle
def _gather_tombstones(self, resultsdir): """ Extension of the generate bundle function. Grabs the tombstones and appends them to the bundle. """ config = get_config() tombstone_path = '/data/tombstones' tombstone_zip = os.path.join(config.tempdir_host, 'tombstones.zip') if self.adb.exists(tombstone_path): tmp_path = os.path.join(config.tempdir_host, 'tombstones') self.adb.pull(tombstone_path, tmp_path) self.adb.shell("rm -R " + tombstone_path) zipf = zipfile.ZipFile(tombstone_zip, mode='w') for rootdir, dirs, files in os.walk(tmp_path): for f in files: zipf.write(os.path.join(rootdir, f), arcname=f) zipf.close() self.adb.push(tombstone_zip, os.path.join(resultsdir, 'tombstones.zip')) os.unlink(tombstone_zip)
def main(): if not check_adb_installed(): print >> sys.stderr, "Can't find the command adb." print >> sys.stderr, ("Please add the path of adb" " command to PATH environment.") sys.exit(1) config = get_config() try: if not os.path.exists(config.tempdir_host): os.makedirs(config.tempdir_host) #make every user can write/read this directory os.chmod(config.tempdir_host, 0777) config.tempdir_host = mkdtemp(dir=config.tempdir_host) set_config(config) os.chmod(config.tempdir_host, 0755) run_with_dispatcher_class(LAVAAndroidTestDispatcher) finally: #can't remove the parent directory, because there may be other #instance using the parent directory shutil.rmtree(config.tempdir_host)
def _download(self): """Download the file specified by the url and check the md5. Returns the path and filename if successful, otherwise return None """ if not self.url: return 0 config = get_config() filename = geturl(self.url, config.tempdir_host) #If the file does not exist, then the download was not successful if not os.path.exists(filename): return None if self.md5: checkmd5 = hashlib.md5() with open(filename, 'rb') as fd: data = fd.read(0x10000) while data: checkmd5.update(data) data = fd.read(0x10000) if checkmd5.hexdigest() != self.md5: raise RuntimeError("Unexpected md5sum downloading %s" % filename) return None return filename
def run(self, quiet=False, run_options=None): """ Run the black-box test on the target device. Use ADB to run the black-box executable on the device. Keep the results in the place that lava-android-test expects us to use. ..note:: This method is part of the lava-android-test framework API. """ # The blackbox test runner will create a directory each time it is # started. All of those directories will be created relative to a so # called spool directory. Instead of using the default spool directory # (which can also change) we will use the directory where # lava-android-test keeps all of the results. spool_dir = get_config().resultsdir_android logging.debug("Using spool directory for black-box testing: %r", spool_dir) stuff_before = frozenset(self.super_adb.listdir(spool_dir)) blackbox_command = [ 'shell', self._blackbox_pathname, '--spool', spool_dir, '--run-all-tests' ] # Let's run the blackbox executable via ADB logging.debug("Starting black-box tests...") self.super_adb(blackbox_command, stdout=None) logging.debug("Black-box tests have finished!") stuff_after = frozenset(self.super_adb.listdir(spool_dir)) # Check what got added to the spool directory new_entries = stuff_after - stuff_before if len(new_entries) == 0: raise RuntimeError("Nothing got added to the spool directory") elif len(new_entries) > 1: raise RuntimeError("Multiple items added to the spool directory") result_id = list(new_entries)[0] print "The blackbox test have finished running, the result id is %r" % result_id return result_id
def test_run(self): config = get_config() test_name = 'foo' clear_fake() testrunner = makerunner(steps_host_pre=["echo foo"]) test = maketest(name=test_name, runner=testrunner) real_installed_path = '%s/%s/%s' % ( test_tmp, config.installdir_android, test_name) test.setadb(FakeAdb()) self.assertFalse(os.path.exists(real_installed_path)) test.install() self.assertTrue(os.path.exists(real_installed_path)) fake_adb(output_str=fake_output_str) result_id = test.run() self.assertTrue("LAVA: (stdout) foo" in self.out.getvalue()) result_id_pattern = "foo\d+\.\d+" self.assertTrue(re.match(result_id_pattern, result_id)) self.assertTrue("LAVA: (stdout) foo" in self.out.getvalue()) test.uninstall() self.assertFalse(os.path.exists(real_installed_path))
class HostShellTestProvider(TestProvider): test_prefix = 'hostshell' config = get_config() dotext = '.sh' def list_test(self): dotext = '.sh' mod = self.import_mod("lava_android_test.test_definitions.hostshells") sh_files = find_files(mod.curdir, dotext) test_list = [] for f in sh_files: ##Assume that the file name only has one '.sh' f_name_no_dotext = os.path.basename(f).replace(dotext, '') test_list.append('%s-%s' % (self.test_prefix, f_name_no_dotext)) return test_list def load_test(self, test_name=None, serial=None): if not test_name.startswith('%s-' % self.test_prefix): raise UnfoundTest('The test(%s) is not found!' % test_name) f_name_no_prefix = test_name.replace('%s-' % self.test_prefix, '', 1) mod = self.import_mod("lava_android_test.test_definitions.%ss" % self.test_prefix) f_name = '%s%s' % (f_name_no_prefix, self.dotext) test_sh_path = '%s/%s' % (mod.curdir, f_name) HOST_SHELL_STEPS = ['bash %s -s $(SERIAL) $(OPTIONS)' % test_sh_path] testobj = self.gen_testobj( testname=test_name, installer=testdef.AndroidTestInstaller(), runner=testdef.AndroidTestRunner(steps_host_pre=HOST_SHELL_STEPS), parser=testdef.AndroidSimpleTestParser(), adb=ADB(serial)) return testobj
def install(self, install_options=None): """Install the test suite. This creates an install directory under the user's XDG_DATA_HOME directory to mark that the test is installed. The installer's install() method is then called from this directory to complete any test specific install that may be needed. """ if not self.installer: raise RuntimeError("no installer defined for '%s'" % self.testname) self.installer.setadb(self.adb) config = get_config() if not os.path.exists(config.tempdir_host): os.makedirs(config.tempdir_host) os.chdir(config.tempdir_host) installdir = os.path.join(config.installdir_android, self.testname) if self.adb.exists(installdir): raise RuntimeError("%s is already installed" % self.testname) ret_code = self.adb.makedirs(installdir) if ret_code != 0: raise RuntimeError( "Failed to create directory(%s) for test(%s)" % (installdir, self.testname)) if install_options is not None: self.adb.shell('echo "%s" > %s/install_options' % (install_options, installdir)) try: self.installer.install(install_options) except Exception as e: self.uninstall() raise RuntimeError( "Failed to install test(%s):%s" % (self.testname, e)) finally: os.chdir(self.origdir)
def run(self, quiet=False, run_options=None): """ Run the black-box test on the target device. Use ADB to run the black-box executable on the device. Keep the results in the place that lava-android-test expects us to use. ..note:: This method is part of the lava-android-test framework API. """ # The blackbox test runner will create a directory each time it is # started. All of those directories will be created relative to a so # called spool directory. Instead of using the default spool directory # (which can also change) we will use the directory where # lava-android-test keeps all of the results. spool_dir = get_config().resultsdir_android logging.debug("Using spool directory for black-box testing: %r", spool_dir) stuff_before = frozenset(self.super_adb.listdir(spool_dir)) blackbox_command = [ 'shell', self._blackbox_pathname, '--spool', spool_dir, '--run-all-tests'] # Let's run the blackbox executable via ADB logging.debug("Starting black-box tests...") self.super_adb(blackbox_command, stdout=None) logging.debug("Black-box tests have finished!") stuff_after = frozenset(self.super_adb.listdir(spool_dir)) # Check what got added to the spool directory new_entries = stuff_after - stuff_before if len(new_entries) == 0: raise RuntimeError("Nothing got added to the spool directory") elif len(new_entries) > 1: raise RuntimeError("Multiple items added to the spool directory") result_id = list(new_entries)[0] print "The blackbox test have finished running, the result id is %r" % result_id return result_id
for n in range(0, len(reclen_split)): results = {'test_case_id': "iozone_%sKB_%s_rclen_%s" % (report_name, str(size), str(reclen_split[n])), 'result': 'pass', 'measurement': int(results_split[n + 1]), 'units': units} self.results['test_results'].append(results) elif match_units: units = match_units.groupdict()['units'] test_name = 'iozone' config = get_config() curdir = os.path.realpath(os.path.dirname(__file__)) iozone_sh_name = 'iozone.sh' iozone_dir_path = os.path.join(curdir, 'iozone') #copy the whole directory over #this will alow users to place the crosscompiled izone binary #in the folder and modify iozone.sh to remount the root fs. iozone_dir_android_path = os.path.join(config.installdir_android, test_name) iozone_sh_android_path = os.path.join(iozone_dir_android_path, iozone_sh_name) INSTALL_STEPS_ADB_PRE = ['push %s %s ' % (iozone_dir_path, iozone_dir_android_path), 'shell chmod -R 777 %s' % iozone_dir_android_path]
""" Performs a test of multimedia functionality in Android by playing a variety of different multimedia formats on Android. **URL:** http://android.git.linaro.org/gitweb?p=platform/frameworks/base.git;a=tree;f=media/tests/MediaFrameworkTest **Default options:** None """ import os import lava_android_test.testdef from lava_android_test.utils import get_local_name from lava_android_test.config import get_config test_name = 'mmtest' config = get_config() site = 'http://samplemedia.linaro.org/' local_name = get_local_name(site) RUN_STEPS_HOST_PRE = [ 'wget --progress=dot -e dotbytes=1M -r -np -l 10 -R csv,txt,css,html,gif,pdf %s -P %s' % (site, local_name), r'find %s -type f -name "index*" -exec rm -f \{\} \;' % local_name, r'find %s -type f -name "README" -exec rm -f \{\} \;' % local_name] test_files_target_path = os.path.join(config.installdir_android, test_name, local_name) RUN_STEPS_ADB_PRE = ['push %s %s' % (local_name, test_files_target_path)] RUN_ADB_SHELL_STEPS = ['am instrument -r -e targetDir %s \ -w com.android.mediaframeworktest/.MediaFrameworkTestRunner' % test_files_target_path,
def generate_bundle(serial=None, result_id=None, test=None, test_id=None, attachments=[]): if result_id is None: return {} config = get_config() adb = ADB(serial) resultdir = os.path.join(config.resultsdir_android, result_id) if not adb.exists(resultdir): raise Exception("The result (%s) is not existed." % result_id) bundle_text = adb.read_file(os.path.join(resultdir, "testdata.json")) bundle = DocumentIO.loads(bundle_text)[1] test_tmp = None if test: test_tmp = test else: test_tmp = TestProvider().load_test(bundle['test_runs'][0]['test_id'], serial) if test_id: bundle['test_runs'][0]['test_id'] = test_id else: attrs = bundle['test_runs'][0].get('attributes') if attrs: run_options = attrs.get('run_options') if run_options: test_id = '%s(%s)' % (bundle['test_runs'][0]['test_id'], run_options) bundle['test_runs'][0]['test_id'] = test_id test_tmp.parse(result_id) stdout_text = adb.read_file( os.path.join(resultdir, os.path.basename(test_tmp.org_ouput_file))) if stdout_text is None: stdout_text = '' stderr_text = adb.read_file(os.path.join(resultdir, 'stderr.log')) if stderr_text is None: stderr_text = '' bundle['test_runs'][0]["test_results"] = test_tmp.parser.results[ "test_results"] ## following part is used for generating the attachment for normal test attachment_bundles = [] for attachment in test_tmp.attachments: data_bundle = attachment.generate_bundle(adb=adb, resultsdir=resultdir) if data_bundle: attachment_bundles.append(data_bundle) bundle['test_runs'][0]["attachments"] = attachment_bundles ##following used for the attachment for monkeyrunner test for attach in attachments: if os.path.exists(attach): with open(attach, 'rb') as stream: data = stream.read() if data: bundle['test_runs'][0]["attachments"].append({ "pathname": os.path.basename(attach), "mime_type": 'image/png', "content": base64.standard_b64encode(data) }) return bundle
def __init__(self, parser, args): super(Command, self).__init__(parser, args) self.config = get_config()