def _set_status(self, action, status): """Set outlets to on or off. Args: action: "on" or "off" status: 8 bits of 0 or 1. e.g., "11111111" """ cmd = "curl http://{}:{}@{}/{}s.cgi?led={}".format(self.config['username'], self.config['password'], self.config['host'], action, status) self.log.info("PDU cmd: {}".format(cmd)) utils.start_standing_subprocess(cmd) time.sleep(10)
def start(self, extra_args="", tag=""): """Starts iperf server on specified machine and 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 if tag: tag = tag + ',' out_file_name = "IPerfServer,{},{}{}.log".format( self.port, tag, len(self.log_files)) self.full_out_path = os.path.join(self.log_path, out_file_name) if self.server_type == "local": cmd = "{} {} > {}".format(self.iperf_str, extra_args, self.full_out_path) print(cmd) self.iperf_process = utils.start_standing_subprocess(cmd) else: cmd = "{} {} > {}".format( self.iperf_str, extra_args, "iperf_server_port{}.log".format(self.port)) job_result = self.ssh_session.run_async(cmd) self.iperf_process = job_result.stdout self.log_files.append(self.full_out_path) self.started = True
def start_tcpdump(ad, test_name): """Start tcpdump on all interfaces Args: ad: android device object. test_name: tcpdump file name will have this """ ad.log.info("Starting tcpdump on all interfaces") try: ad.adb.shell("killall -9 tcpdump") except AdbError: ad.log.warn("Killing existing tcpdump processes failed") out = ad.adb.shell("ls -l %s" % TCPDUMP_PATH) if "No such file" in out or not out: ad.adb.shell("mkdir %s" % TCPDUMP_PATH) else: ad.adb.shell("rm -rf %s/*" % TCPDUMP_PATH, ignore_status=True) begin_time = epoch_to_log_line_timestamp(get_current_epoch_time()) begin_time = normalize_log_line_timestamp(begin_time) file_name = "%s/tcpdump_%s_%s.pcap" % (TCPDUMP_PATH, ad.serial, test_name) ad.log.info("tcpdump file is %s", file_name) cmd = "adb -s {} shell tcpdump -i any -s0 -w {}".format( ad.serial, file_name) try: return start_standing_subprocess(cmd, 5) except Exception: ad.log.exception('Could not start standing process %s' % repr(cmd)) return None
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 _send_command(self, command): """ Send command to b29 using apollo debug bridge Args: command: The command for apollo debug to execute Returns: subprocess object """ return utils.start_standing_subprocess( '{} {} {}'.format(DEBUG_BRIDGE, '--rpc_port=-1', command), shell=True)
def pdu_func(self): """control Power Distribution Units on local machine. Logic steps are 1. Turn off PDU for all port. 2. Turn on PDU for specified port. """ out_file_name = "PDU.log" self.full_out_path = os.path.join(self.log_path, out_file_name) cmd = "curl http://snmp:1234@{}/offs.cgi?led=11111111> {}".format( self.pdu_address, self.full_out_path) self.pdu_process = utils.start_standing_subprocess(cmd) wait_time = 10 self.log.info("Starting set PDU to OFF") time.sleep(wait_time) self.full_out_path = os.path.join(self.log_path, out_file_name) cmd = "curl http://snmp:1234@{}/ons.cgi?led={}> {}".format( self.pdu_address, self.pduon_address, self.full_out_path) self.pdu_process = utils.start_standing_subprocess(cmd) wait_time = int("{}".format(self.pduon_wait_time)) self.log.info("Starting set PDU to ON for port1," "wait for {}s".format(self.pduon_wait_time)) time.sleep(wait_time) self.log.info("PDU setup complete")
def start_iperf_server_on_shell(self, server_port): """Starts iperf server on android device with specified. Args: server_port: Port in which server should be started. """ log_path = os.path.join(self.pri_ad.log_path, "iPerf{}".format(server_port)) iperf_server = "iperf3 -s -p {} -J".format(server_port) create_dir(log_path) out_file_name = "IPerfServer,{},{},{}.log".format( server_port, self.tag, len(self.log_files)) self.tag = self.tag + 1 self.iperf_server_path = os.path.join(log_path, out_file_name) cmd = "adb -s {} shell {} > {}".format(self.pri_ad.serial, iperf_server, self.iperf_server_path) self.iperf_process.append(start_standing_subprocess(cmd)) self.log_files.append(self.iperf_server_path) time.sleep(IPERF_SERVER_WAIT_TIME)
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 create_dir(self.log_path) self.exec_count += 1 if tag: tag = tag + ',' out_file_name = "IPerfServer,{},{}{}.log".format( self.port, tag, self.exec_count) full_out_path = os.path.join(self.log_path, out_file_name) cmd = "{} {} > {}".format(self.iperf_str, extra_args, full_out_path) self.iperf_process = start_standing_subprocess(cmd) 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 AndroidDeviceError(("Android device {} already has an adb " "logcat thread going on. Cannot start " "another one.").format(self.serial)) # Disable adb log spam filter. self.adb.shell("logpersist.start --clear") 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 start(self, extra_args="", tag=""): """Starts iperf server on local machine. 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 if tag: tag = tag + ',' out_file_name = "IPerfServer,{},{}{}.log".format( self.port, tag, len(self.log_files)) self.full_out_path = os.path.join(self.log_path, out_file_name) cmd = "{} {} > {}".format(self.iperf_str, extra_args, self.full_out_path) self.iperf_process = utils.start_standing_subprocess(cmd) self.log_files.append(self.full_out_path) self.started = True
def get_status(self): """Get outlets status Returns: A tuple of (outlets_list, outlets_str) outlets_list: A List indicates the status of the outlets. e.g., outlet 1 is ON, returns: ['1', '0', '0', '0', '0', '0', '0', '0',] e.g., outlets 1 & 8 are ON, returns: ['1', '0', '0', '0', '0', '0', '0', '1'] outlets_str: A string indicates the status of the outlets. e.g., outlet 1 is ON: returns: '1' e.g., outlet 1 & 3 $ 8 are ON: returns: '138' """ outlets_str = "" cmd = "curl http://{}:{}@{}/status.xml".format(self.config['username'], self.config['password'], self.config['host']) proc = utils.start_standing_subprocess(cmd) time.sleep(1) try: outlets_list = proc.communicate()[0].decode().split(",")[10:18] """Translate a list of strings to a sequence of strings. e.g. ['1', '0', '0', '0', '0', '0', '0', '0',] turns into '1' ['1', '1', '1', '1', '1', '1', '1', '1'] turns into '12345678' """ for i in range(len(outlets_list)): if outlets_list[i] == '1': outlets_str = outlets_str + str(i + 1) except: raise KeyError("Fail to get status from PDU.") return outlets_list, outlets_str
def start_tcpdump(ad, test_name): """Start tcpdump on all interfaces Args: ad: android device object. test_name: tcpdump file name will have this """ ad.log.info("Starting tcpdump on all interfaces") ad.adb.shell("killall -9 tcpdump", ignore_status=True) ad.adb.shell("mkdir %s" % TCPDUMP_PATH, ignore_status=True) ad.adb.shell("rm -rf %s/*" % TCPDUMP_PATH, ignore_status=True) file_name = "%s/tcpdump_%s_%s.pcap" % (TCPDUMP_PATH, ad.serial, test_name) ad.log.info("tcpdump file is %s", file_name) cmd = "adb -s {} shell tcpdump -i any -s0 -w {}".format(ad.serial, file_name) try: return start_standing_subprocess(cmd, 5) except Exception: ad.log.exception('Could not start standing process %s' % repr(cmd)) return None
def start_adb_logcat(self, cont_logcat_file=False): """Starts a standing adb logcat collection in separate subprocesses and save the logcat in a file. Args: cont_logcat_file: Specifies whether to continue the previous logcat file. This allows for start_adb_logcat to act as a restart logcat function if it is noticed logcat is no longer running. """ if self.is_adb_logcat_on: raise AndroidDeviceError(("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") if cont_logcat_file: if self.droid: self.droid.logI('Restarting logcat') self.log.info('Restarting logcat on file %s' % self.adb_logcat_file_path) logcat_file_path = self.adb_logcat_file_path else: 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(self): p = utils.start_standing_subprocess('sleep 0') time.sleep(0.1) with self.assertRaisesRegex(utils.ActsUtilsError, 'Process .* has terminated'): utils.stop_standing_subprocess(p)
def test_start_standing_subproc(self): with self.assertRaisesRegex(utils.ActsUtilsError, 'Process .* has terminated'): utils.start_standing_subprocess('sleep 0', check_health_delay=0.1)