def invoke(self): uri = normalize_xmlrpc_url(self.args.HOST) parsed_host = urlparse.urlparse(uri) if parsed_host.username: username = parsed_host.username else: username = getpass.getuser() host = parsed_host.hostname if parsed_host.port: host += ':' + str(parsed_host.port) uri = '%s://%s@%s%s' % (parsed_host.scheme, username, host, parsed_host.path) if self.args.token_file: if parsed_host.password: raise LavaCommandError( "Token specified in url but --token-file also passed.") else: try: token_file = open(self.args.token_file) except IOError as ex: raise LavaCommandError("opening %r failed: %s" % (self.args.token_file, ex)) token = token_file.read().strip() else: if parsed_host.password: token = parsed_host.password else: token = getpass.getpass("Paste token for %s: " % uri) userless_uri = '%s://%s%s' % (parsed_host.scheme, host, parsed_host.path) if not self.args.no_check: sp = AuthenticatingServerProxy(uri, auth_backend=MemoryAuthBackend([ (username, userless_uri, token) ])) try: token_user = sp.system.whoami() except xmlrpclib.ProtocolError as ex: if ex.errcode == 401: raise LavaCommandError( "Token rejected by server for user %s." % username) else: raise except xmlrpclib.Fault as ex: raise LavaCommandError( "Server reported error during check: %s." % ex) if token_user != username: raise LavaCommandError( "whoami() returned %s rather than expected %s -- this is " "a bug." % (token_user, username)) self.auth_backend.add_token(username, userless_uri, token) print 'Token added successfully for user %s.' % username
def invoke_sub(self): tip_msg = self.get_tip_msg("Run test") self.say_begin(tip_msg) if not self.test_installed(self.args.test_id): raise LavaCommandError( "The test (%s) has not been installed yet." % self.args.test_id) test = TestProvider().load_test(self.args.test_id, self.args.serial) if not self.test_installed(test.testname): raise LavaCommandError( "The test (%s) has not been installed yet." % self.args.test_id) try: result_id = test.run(quiet=self.args.quiet, run_options=self.args.run_option) if self.args.output: output_dir = os.path.dirname(self.args.output) if output_dir and (not os.path.exists(output_dir)): os.makedirs(output_dir) bundle = generate_bundle(self.args.serial, result_id) with open(self.args.output, "wt") as stream: DocumentIO.dump(stream, bundle) except Exception as strerror: raise LavaCommandError("Test execution error: %s" % strerror) self.say_end(tip_msg)
def invoke(self): serial = self.get_device_serial() if not serial: raise LavaCommandError("No device attached") self.serial = serial self.adb = ADB(self.serial) try: self.assertDeviceIsConnected() except Exception as err: raise LavaCommandError(err) self.invoke_sub()
def invoke_sub(self): tip_msg = self.get_tip_msg("Install test") self.say_begin(tip_msg) if self.test_installed(self.args.test_id): raise LavaCommandError("The test (%s) has already installed." % self.args.test_id) test = TestProvider().load_test(self.args.test_id, self.args.serial) try: test.install(self.args.install_option) except Exception as strerror: raise LavaCommandError("Test installation error: %s" % strerror) self.say_end(tip_msg)
def get_token_for_endpoint(self, username, endpoint_url): try: token = keyring.core.get_password( "lava-tool-%s" % endpoint_url, username) except ValueError as exc: raise LavaCommandError(exc) return token
def invoke_sub(self): resultsdir = os.path.join(self.config.resultsdir_android, self.args.result_id) if not self.adb.exists(resultsdir): raise LavaCommandError("The result (%s) is not existed." % self.args.result_id) stdout = os.path.join(resultsdir, "stdout.log") if not self.adb.exists(stdout): self.say("No result found for '%s'" % self.args.result_id) return try: output = self.adb.get_shellcmdoutput('cat %s' % stdout)[1] if output is not None: for line in output: self.display_subprocess_output('stdout', line) except IOError: pass stderr = os.path.join(resultsdir, "stderr.log") if not self.adb.exists(stderr): return try: output = self.adb.get_shellcmdoutput('cat %s' % stderr)[1] if output is not None: for line in output: self.display_subprocess_output('stderr', line) except IOError: pass
def invoke_sub(self): tip_msg = self.get_tip_msg("Uninstall test") self.say_begin(tip_msg) test = TestProvider().load_test(self.args.test_id, self.args.serial) try: test.uninstall() except Exception as strerror: raise LavaCommandError("Test uninstall error: %s" % strerror) self.say_end(tip_msg)
def invoke(self): if not os.path.exists(self.args.result_file): raise LavaCommandError("The specified result file(%s) " "does not exist." % self.args.result_file) msg = "extract attachment file from result bundle file(%s)" % ( self.args.result_file) self.say_begin(msg) badchars = "[^a-zA-Z0-9\._-]" with open(self.args.result_file) as stream: jobdata = stream.read() result_data = DocumentIO.loads(jobdata)[1] test_runs = result_data.get('test_runs') if not self.args.directory: attachment_dir = mkdtemp(prefix='attachments-', dir=os.path.curdir) elif not os.path.exists(self.args.directory): os.makedirs(self.args.directory) attachment_dir = self.args.directory elif not os.path.isdir(self.args.directory): raise LavaCommandError( "The specified path(%s) is not a directory." % self.args.directory) else: attachment_dir = self.args.directory for test in test_runs: test_id = test.get('test_id').replace(" ", "_") test_id = re.sub(badchars, "_", test_id) target_dir = mkdtemp(prefix='%s' % test_id, dir=attachment_dir) print "The test id is: %s" % test_id attachments = test.get('attachments', []) for attach in attachments: pathname = attach.get('pathname') file_name = os.path.basename(pathname) content_decoded = base64.standard_b64decode( attach.get("content")) with open(os.path.join(target_dir, file_name), 'w') as fd: fd.write(content_decoded) self.say("All attachment files are put under directory(%s)" % (attachment_dir)) self.say_end(msg)
def build_http_request(self, host, handler, request_body): token = None user = None auth, host = urllib.splituser(host) if auth: user, token = urllib.splitpasswd(auth) url = self._scheme + "://" + host + handler if user is not None and token is None: token = self.auth_backend.get_token_for_endpoint(user, url) if token is None: raise LavaCommandError( "Username provided but no token found.") request = urllib2.Request(url, request_body) request.add_header("Content-Type", "text/xml") if token: auth = base64.b64encode(urllib.unquote(user + ':' + token)) request.add_header("Authorization", "Basic " + auth) return request
def invoke_sub(self): if not utils.check_command_exist('monkeyrunner'): raise LavaCommandError('The command monkeyrunner can not be found') if self.args.repo_type == 'git': target_dir = mkdtemp(prefix='git_repo', dir=self.config.tempdir_host) os.chmod(target_dir, 0755) GitRepository(self.args.url).checkout(target_dir) else: raise LavaCommandError("The repository type(%s) is not supported" % self.args.repo_type) script_list = utils.find_files(target_dir, '.py') test_id = self.args.url if len(test_id) > 40: test_id = '%s...' % (test_id[:40]) test_id = 'monkeyrunner_%s' % test_id tip_msg = ("Run monkeyrunner scripts in following url on device(%s):" "\n\turl=%s") % (self.serial, self.args.url) self.say_begin(tip_msg) bundles = [] for script in script_list: if "monkeycommon.py" == os.path.basename(script): continue sub_bundle = {} from datetime import datetime starttime = datetime.utcnow() test_case_id = script.replace('%s/' % target_dir, '') if len(test_case_id) > 50: test_case_id = '%s...' % (test_case_id[:50]) try: sub_bundle = self.run_monkeyrunner_test( script, self.serial, test_case_id) test_result = {"test_case_id": test_case_id, "result": 'pass'} if sub_bundle: sub_bundle['test_runs'][0]['test_results'].append( test_result) except Exception as strerror: self.say('Failed to run script(%s) with error:\n%s' % (script, strerror)) test_result = {"test_case_id": test_case_id, "result": 'fail'} TIMEFORMAT = '%Y-%m-%dT%H:%M:%SZ' sub_bundle['test_runs'] = [{ 'test_results': [test_result], 'test_id': 'monkeyrunner(%s)' % test_case_id, 'time_check_performed': False, 'analyzer_assigned_uuid': str(uuid4()), 'analyzer_assigned_date': starttime.strftime(TIMEFORMAT) }] if sub_bundle: bundles.append(sub_bundle) if self.args.output: output_dir = os.path.dirname(self.args.output) if output_dir and (not os.path.exists(output_dir)): os.makedirs(output_dir) with open(self.args.output, "wt") as stream: DocumentIO.dump(stream, merge_bundles(bundles)) self.say_end(tip_msg)
def invoke_sub(self): test_name = 'custom' ADB_SHELL_STEPS = [] STEPS_HOST_PRE = [] STEPS_ADB_PRE = [] file_name = None if self.args.android_command: ADB_SHELL_STEPS = self.args.android_command cmds_str = ','.join(ADB_SHELL_STEPS) if len(cmds_str) > 40: cmds_str = '%s...' % (cmds_str[:40]) test_name_suffix = 'command=[%s]' % (cmds_str) elif self.args.command_file: file_url = self.args.command_file urlpath = urlparse.urlsplit(file_url).path file_name = os.path.basename(urlpath) target_path = os.path.join(self.config.installdir_android, test_name, file_name) STEPS_HOST_PRE = ["wget %s -O %s" % (file_url, file_name)] STEPS_ADB_PRE = ["push %s %s" % (file_name, target_path)] ADB_SHELL_STEPS = ["chmod 777 %s" % target_path, target_path] file_name_str = file_name if len(file_name_str) > 40: file_name_str = '%s...' % (cmds_str[:40]) test_name_suffix = 'command_file=%s' % (file_name_str) PATTERN = None if self.args.parse_regex: PATTERN = self.args.parse_regex tip_msg = '' if self.args.serial: tip_msg = ("Run following custom test(s) on device(%s):" "\n\tcommands=%s" "\n\tcommand-file=%s\n") % ( self.args.serial, '\n\t\t'.join(ADB_SHELL_STEPS), file_name) else: tip_msg = ("Run following custom test(s):" "\n\t\tcommands=%s" "\n\tcommand-file=%s\n") % ( '\n\t\t'.join(ADB_SHELL_STEPS), file_name) self.say_begin(tip_msg) inst = AndroidTestInstaller() run = AndroidTestRunner(steps_host_pre=STEPS_HOST_PRE, steps_adb_pre=STEPS_ADB_PRE, adbshell_steps=ADB_SHELL_STEPS) parser = AndroidTestParser(pattern=PATTERN) test = AndroidTest(testname=test_name, installer=inst, runner=run, parser=parser) test.parser.results = {'test_results': []} test.setadb(self.adb) if not self.test_installed(test.testname): test.install() try: result_id = test.run(quiet=self.args.quiet) if self.args.output: output_dir = os.path.dirname(self.args.output) if output_dir and (not os.path.exists(output_dir)): os.makedirs(output_dir) bundle = generate_bundle(self.args.serial, result_id, test=test, test_id='%s(%s)' % (test_name, test_name_suffix)) with open(self.args.output, "wt") as stream: DocumentIO.dump(stream, bundle) except Exception as strerror: raise LavaCommandError("Test execution error: %s" % strerror) self.say_end(tip_msg)
def invoke(self): raise LavaCommandError("error message")