def test_start_standing_subproc_without_env(self, mock_popen): utils.start_standing_subprocess(self.sleep_cmd(0.01)) mock_popen.assert_called_with( self.sleep_cmd(0.01), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, env=None, )
def test_start_standing_subproc_with_custom_env(self, mock_popen): mock_env = mock.MagicMock(spec=dict) utils.start_standing_subprocess(self.sleep_cmd(0.01), env=mock_env) mock_popen.assert_called_with( self.sleep_cmd(0.01), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, env=mock_env, )
def start_adb_logcat(self, clear_log=True): """Starts a standing adb logcat collection in separate subprocesses and save the logcat in a file. This clears the previous cached logcat content on device. Args: clear: If True, clear device log before starting logcat. """ if self._adb_logcat_process: raise DeviceError( self, 'Logcat thread is already running, cannot start another one.') if clear_log: self._clear_adb_log() # Disable adb log spam filter for rootable devices. Have to stop and # clear settings first because 'start' doesn't support --clear option # before Android N. if self.is_rootable: self.adb.shell('logpersist.stop --clear') self.adb.shell('logpersist.start') f_name = 'adblog,%s,%s.txt' % (self.model, self.serial) utils.create_dir(self.log_path) logcat_file_path = os.path.join(self.log_path, f_name) try: extra_params = self.adb_logcat_param except AttributeError: extra_params = '' cmd = '"%s" -s %s logcat -v threadtime %s >> "%s"' % (adb.ADB, self.serial, extra_params, logcat_file_path) process = utils.start_standing_subprocess(cmd, shell=True) self._adb_logcat_process = process self.adb_logcat_file_path = logcat_file_path
def start_capture(self, override_configs=None, additional_args=None, duration=None, packet_count=None): """See base class documentation """ if self._process is not None: raise sniffer.InvalidOperationError( "Trying to start a sniff while another is still running!") capture_dir = os.path.join(self._logger.log_path, "Sniffer-{}".format(self._interface)) os.makedirs(capture_dir, exist_ok=True) self._capture_file_path = os.path.join( capture_dir, "capture_{}.pcap".format(logger.get_log_file_timestamp())) self._pre_capture_config(override_configs) _, self._temp_capture_file_path = tempfile.mkstemp(suffix=".pcap") cmd = self._get_command_line(additional_args=additional_args, duration=duration, packet_count=packet_count) self._process = utils.start_standing_subprocess(cmd) return sniffer.ActiveCaptureContext(self, duration)
def start_adb_logcat(self, clear_log=True): """Starts a standing adb logcat collection in separate subprocesses and save the logcat in a file. This clears the previous cached logcat content on device. Args: clear: If True, clear device log before starting logcat. """ if self._adb_logcat_process: raise DeviceError( self, 'Logcat thread is already running, cannot start another one.') if clear_log: self._clear_adb_log() self._enable_logpersist() f_name = 'adblog,%s,%s.txt' % (self.model, self._normalized_serial) utils.create_dir(self.log_path) logcat_file_path = os.path.join(self.log_path, f_name) try: extra_params = self.adb_logcat_param except AttributeError: extra_params = '' cmd = '"%s" -s %s logcat -v threadtime %s >> "%s"' % ( adb.ADB, self.serial, extra_params, logcat_file_path) process = utils.start_standing_subprocess(cmd, shell=True) self._adb_logcat_process = process self.adb_logcat_file_path = logcat_file_path
def start_adb_logcat(self): """Starts a standing adb logcat collection in separate subprocesses and save the logcat in a file. """ if self._adb_logcat_process: raise DeviceError( self, 'Logcat thread is already running, cannot start another one.') # Disable adb log spam filter for rootable. Have to stop and clear # settings first because 'start' doesn't support --clear option before # Android N. if self.is_rootable: self.adb.shell('logpersist.stop --clear') self.adb.shell('logpersist.start') f_name = 'adblog,%s,%s.txt' % (self.model, self.serial) utils.create_dir(self.log_path) logcat_file_path = os.path.join(self.log_path, f_name) try: extra_params = self.adb_logcat_param except AttributeError: extra_params = '-b all' cmd = 'adb -s %s logcat -v threadtime %s >> %s' % ( self.serial, extra_params, logcat_file_path) self._adb_logcat_process = utils.start_standing_subprocess(cmd) self.adb_logcat_file_path = logcat_file_path
def _run_adb_cmd(self, cmd): """Starts a long-running adb subprocess and returns it immediately.""" adb_cmd = [adb.ADB] if self._adb.serial: adb_cmd += ['-s', self._adb.serial] adb_cmd += ['shell', cmd] return utils.start_standing_subprocess(adb_cmd, shell=False)
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess([self.sleep_cmd, '4']) p1 = psutil.Process(p.pid) start_time = time.time() utils.stop_standing_subprocess(p) self.assertFalse(p1.is_running()) stop_time = time.time() self.assertTrue(stop_time - start_time < 0.1)
def test_start_standing_subproc(self): try: p = utils.start_standing_subprocess(self.sleep_cmd(0.01)) p1 = psutil.Process(p.pid) self.assertTrue(p1.is_running()) finally: p.stdout.close() p.stderr.close() p.wait()
def test_start_standing_subproc(self): try: p = utils.start_standing_subprocess(self.sleep_cmd(4)) self.assertTrue(_is_process_running(p.pid)) os.kill(p.pid, signal.SIGTERM) finally: p.stdout.close() p.stderr.close() p.wait()
def _do_start_app(self): """Overrides superclass.""" cmd = _LAUNCH_CMD % (self.device_port, self.package) # Use info here so people know exactly what's happening here, which is # helpful since they need to create their own instrumentations and # manifest. self.log.info('Launching snippet apk %s', self.package) adb_cmd = ['adb', '-s', self._adb.serial, 'shell', cmd] self._proc = utils.start_standing_subprocess(adb_cmd, shell=False)
def _start(self): """The actual logic of starting logcat.""" self._enable_logpersist() logcat_file_path = self._configs.output_file_path if not logcat_file_path: f_name = 'adblog,%s,%s.txt' % (self._ad.model, self._ad._normalized_serial) logcat_file_path = os.path.join(self._ad.log_path, f_name) utils.create_dir(os.path.dirname(logcat_file_path)) cmd = '"%s" -s %s logcat -v threadtime %s >> "%s"' % ( adb.ADB, self._ad.serial, self._configs.logcat_params, logcat_file_path) process = utils.start_standing_subprocess(cmd, shell=True) self._adb_logcat_process = process self.adb_logcat_file_path = logcat_file_path
def _start(self): """The actual logic of starting logcat.""" self._enable_logpersist() logcat_file_path = self._config.output_file_path if not logcat_file_path: f_name = self._ad.generate_filename(self.OUTPUT_FILE_TYPE, extension_name='txt') logcat_file_path = os.path.join(self._ad.log_path, f_name) utils.create_dir(os.path.dirname(logcat_file_path)) cmd = '"%s" -s %s logcat -v threadtime %s >> "%s"' % ( adb.ADB, self._ad.serial, self._config.logcat_params, logcat_file_path) process = utils.start_standing_subprocess(cmd, shell=True) self._adb_logcat_process = process self.adb_logcat_file_path = logcat_file_path
def _start(self): """The actual logic of starting logcat.""" self._enable_logpersist() if self._config.output_file_path: self._close_logcat_file() self.adb_logcat_file_path = self._config.output_file_path if not self.adb_logcat_file_path: f_name = self._ad.generate_filename(self.OUTPUT_FILE_TYPE, extension_name='txt') logcat_file_path = os.path.join(self._ad.log_path, f_name) self.adb_logcat_file_path = logcat_file_path utils.create_dir(os.path.dirname(self.adb_logcat_file_path)) # In debugging mode of IntelijIDEA, "patch_args" remove # double quotes in args if starting and ending with it. # Add spaces at beginning and at last to fix this issue. cmd = ' "%s" -s %s logcat -v threadtime -T 1 %s >> "%s" ' % ( adb.ADB, self._ad.serial, self._config.logcat_params, self.adb_logcat_file_path) process = utils.start_standing_subprocess(cmd, shell=True) self._adb_logcat_process = process
def start(self, extra_args="", tag=""): """Starts iperf server on specified port. Args: extra_args: A string representing extra arguments to start iperf server with. tag: Appended to log file name to identify logs from different iperf runs. """ if self.started: return utils.create_dir(self.log_path) if tag: tag = tag + ',' out_file_name = "IPerfServer,{},{}{}.log".format( self.port, tag, len(self.log_files)) full_out_path = os.path.join(self.log_path, out_file_name) cmd = '%s %s > %s' % (self.iperf_str, extra_args, full_out_path) self.iperf_process = utils.start_standing_subprocess(cmd, shell=True) self.log_files.append(full_out_path) self.started = True
def start_adb_logcat(self): """Starts a standing adb logcat collection in separate subprocesses and save the logcat in a file. """ if self.is_adb_logcat_on: raise Error(("Android device {} already has an adb " "logcat thread going on. Cannot start " "another one.").format(self.serial)) # Disable adb log spam filter. Have to stop and clear settings first # because 'start' doesn't support --clear option before Android N. self.adb.shell("logpersist.stop --clear") self.adb.shell("logpersist.start") f_name = "adblog,{},{}.txt".format(self.model, self.serial) utils.create_dir(self.log_path) logcat_file_path = os.path.join(self.log_path, f_name) try: extra_params = self.adb_logcat_param except AttributeError: extra_params = "-b all" cmd = "adb -s {} logcat -v threadtime {} >> {}".format( self.serial, extra_params, logcat_file_path) self.adb_logcat_process = utils.start_standing_subprocess(cmd) self.adb_logcat_file_path = logcat_file_path
def test_stop_standing_subproc_already_dead(self): p = utils.start_standing_subprocess(['sleep', '0']) time.sleep(0.5) with self.assertRaisesRegexp(utils.Error, 'Process .* has terminated'): utils.stop_standing_subprocess(p)
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess(['sleep', '5']) utils.stop_standing_subprocess(p) self.assertIsNotNone(p.poll())
def test_start_standing_subproc(self): with self.assertRaisesRegexp(utils.Error, 'Process .* has terminated'): utils.start_standing_subprocess( ['sleep', '0'], check_health_delay=0.5)
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess(self.sleep_cmd(4)) utils.stop_standing_subprocess(p) self.assertFalse(_is_process_running(p.pid))
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess("sleep 0") time.sleep(0.1) with self.assertRaisesRegexp(utils.Error, "Process .* has terminated"): utils.stop_standing_subprocess(p)
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess(self.sleep_cmd(4)) p1 = psutil.Process(p.pid) utils.stop_standing_subprocess(p) self.assertFalse(p1.is_running())
def test_start_standing_subproc(self): p = utils.start_standing_subprocess(['sleep', '1']) p1 = psutil.Process(p.pid) self.assertTrue(p1.is_running())
def _do_start_app(self, launch_cmd): adb_cmd = [adb.ADB] if self._adb.serial: adb_cmd += ['-s', self._adb.serial] adb_cmd += ['shell', launch_cmd] return utils.start_standing_subprocess(adb_cmd, shell=False)
def test_start_standing_subproc(self): with self.assertRaisesRegexp(utils.Error, "Process .* has terminated"): utils.start_standing_subprocess("sleep 0", check_health_delay=0.1)