Beispiel #1
0
    def test_stop_sets_stopped_to_true(self):
        """Tests that stop() sets the _stopped attribute to True."""
        process = Process('cmd')
        process._process = mock.Mock()

        process.stop()

        self.assertTrue(process._stopped)
Beispiel #2
0
    def test_set_on_terminate_callback(self):
        """Tests that set_on_terminate_callback sets _on_terminate_callback."""
        callback = mock.Mock()

        process = Process('cmd').set_on_terminate_callback(callback)
        process._on_terminate_callback()

        self.assertTrue(callback.called)
Beispiel #3
0
    def test_exec_loop_waits_for_process(self):
        """Tests that the _exec_loop waits for the process to complete before
        returning."""
        process = Process('cmd')
        Process._Process__start_process = mock.Mock()

        with self.patch('Thread', FakeThread):
            process._exec_loop()

        self.assertEqual(process._process.wait.called, True)
Beispiel #4
0
    def test_sends_signal(self, mock_os, *_):
        """Tests that signal is sent to process.."""
        process = Process('cmd')
        mock_process = mock.Mock()
        mock_process.pid = -1
        process._process = mock_process

        process.signal(51641)

        mock_os.assert_called_with(-1, 51641)
Beispiel #5
0
    def test_wait_kills_after_timeout(self, *_):
        """Tests that if a TimeoutExpired error is thrown during wait, the
        process is killed."""
        process = Process('cmd')
        process._process = mock.Mock()
        process._process.wait.side_effect = subprocess.TimeoutExpired('', '')

        process.wait(0)

        self.assertEqual(process._kill_process.called, True)
Beispiel #6
0
    def test_signal_raises_error_on_windows(self, *_):
        """Tests that signaling is unsupported in windows with appropriate
        error msg."""
        process = Process('cmd')
        mock_inner_process = mock.Mock()
        mock_inner_process.pid = -1
        process._process = mock_inner_process

        with mock.patch('acts.libs.proc.process._on_windows', True):
            with self.assertRaises(ProcessError):
                process.signal(51641)
Beispiel #7
0
    def test_exec_loop_redirections_output(self):
        """Tests that the _exec_loop function calls to redirect the output."""
        process = Process('cmd')
        Process._Process__start_process = mock.Mock()

        with self.patch('Thread', FakeThread):
            process._exec_loop()

        self.assertEqual(process._redirection_thread.target,
                         process._redirect_output)
        self.assertEqual(process._redirection_thread.alive, True)
Beispiel #8
0
    def test_exec_loop_loops_if_not_stopped(self):
        process = Process('1st')
        Process._Process__start_process = mock.Mock()
        process._on_terminate_callback = mock.Mock(side_effect=[['2nd'], None])

        with self.patch('Thread', FakeThread):
            process._exec_loop()

        self.assertEqual(Process._Process__start_process.call_count, 2)
        self.assertEqual(Process._Process__start_process.call_args_list[0][0],
                         (['1st'], ))
        self.assertEqual(Process._Process__start_process.call_args_list[1][0],
                         (['2nd'], ))
Beispiel #9
0
    def test_start_starts_listening_thread(self):
        """Tests that start starts the _exec_popen_loop function."""
        process = Process('cmd')

        # Here we need the thread to start the process object.
        class FakeThreadImpl(FakeThread):
            def _on_start(self):
                process._process = mock.Mock()

        with self.patch('Thread', FakeThreadImpl):
            process.start()

        self.assertTrue(process._listening_thread.alive)
        self.assertEqual(process._listening_thread.target, process._exec_loop)
Beispiel #10
0
    def test_wait_raises_if_called_back_to_back(self):
        """Tests that wait raises an exception if it has already been called
        prior."""
        process = Process('cmd')
        process._process = mock.Mock()

        process.wait(0)
        expected_msg = 'Process is already being stopped.'
        with self.assertRaisesRegex(ProcessError, expected_msg):
            process.wait(0)
Beispiel #11
0
    def start_packet_capture(self, band, log_path, pcap_fname):
        """Start packet capture for band.

        band = 2G starts tcpdump on 'mon0' interface.
        band = 5G starts tcpdump on 'mon1' interface.

        Args:
            band: '2g' or '2G' and '5g' or '5G'.
            log_path: test log path to save the pcap file.
            pcap_fname: name of the pcap file.

        Returns:
            pcap_proc: Process object of the tcpdump.
        """
        band = band.upper()
        if band not in BAND_IFACE.keys() or band in self.pcap_properties:
            self.log.error("Invalid band or packet capture already running")
            return None

        pcap_name = '%s_%s.pcap' % (pcap_fname, band)
        pcap_fname = os.path.join(log_path, pcap_name)
        pcap_file = open(pcap_fname, 'w+b')

        tcpdump_cmd = 'tcpdump -i %s -w - -U 2>/dev/null' % (BAND_IFACE[band])
        cmd = formatter.SshFormatter().format_command(
            tcpdump_cmd, None, self.ssh_settings, extra_flags={'-q': None})
        pcap_proc = Process(cmd)
        pcap_proc.set_on_output_callback(
            lambda msg: pcap_file.write(msg), binary=True)
        pcap_proc.start()

        self.pcap_properties[band] = PcapProperties(pcap_proc, pcap_fname,
                                                    pcap_file)
        return pcap_proc
Beispiel #12
0
    def test_stop_calls_wait(self):
        """Tests that stop() also has the functionality of wait()."""
        process = Process('cmd')
        process._process = mock.Mock()
        process.wait = mock.Mock()

        process.stop()

        self.assertEqual(process.wait.called, True)
Beispiel #13
0
    def test_wait_joins_redirection_thread_if_it_exists(self):
        """Tests wait() joins _listening_thread if it exists."""
        process = Process('cmd')
        process._process = mock.Mock()
        mocked_thread = mock.Mock()
        process._redirection_thread = mocked_thread

        process.wait(0)

        self.assertEqual(mocked_thread.join.called, True)
Beispiel #14
0
    def test_wait_clears_redirection_thread_if_it_exists(self):
        """Tests wait() joins _listening_thread if it exists.

        Threads can only be started once, so after wait has been called, we
        want to make sure we clear the listening thread.
        """
        process = Process('cmd')
        process._process = mock.Mock()
        process._redirection_thread = mock.Mock()

        process.wait(0)

        self.assertEqual(process._redirection_thread, None)
Beispiel #15
0
    def test_redirect_output_feeds_all_lines_to_on_output_callback(self):
        """Tests that _redirect_output loops until all lines are parsed."""
        received_list = []

        def appender(line):
            received_list.append(line)

        process = Process('cmd')
        process.set_on_output_callback(appender)
        process._process = mock.Mock()
        process._process.stdout.readline.side_effect = [b'a\n', b'b\n', b'']

        process._redirect_output()

        self.assertEqual(received_list[0], 'a')
        self.assertEqual(received_list[1], 'b')
        self.assertEqual(len(received_list), 2)
Beispiel #16
0
    def test_start_raises_if_called_back_to_back(self):
        """Tests that start raises an exception if it has already been called
        prior.

        This is required to prevent references to processes and threads from
        being overwritten, potentially causing ACTS to hang."""
        process = Process('cmd')

        # Here we need the thread to start the process object.
        class FakeThreadImpl(FakeThread):
            def _on_start(self):
                process._process = mock.Mock()

        with self.patch('Thread', FakeThreadImpl):
            process.start()
            expected_msg = 'Process has already started.'
            with self.assertRaisesRegex(ProcessError, expected_msg):
                process.start()
Beispiel #17
0
def create_logcat_keepalive_process(serial, logcat_dir, extra_params=''):
    """Creates a Logcat Process that automatically attempts to reconnect.

    Args:
        serial: The serial of the device to read the logcat of.
        logcat_dir: The directory used for logcat file output.
        extra_params: Any additional params to be added to the logcat cmdline.

    Returns:
        A acts.libs.proc.process.Process object.
    """
    logger = log_stream.create_logger(
        'adblog_%s' % serial, log_name=serial, subcontext=logcat_dir,
        log_styles=(LogStyles.LOG_DEBUG | LogStyles.TESTCASE_LOG))
    process = Process('adb -s %s logcat -T 1 -v year %s' %
                      (serial, extra_params))
    timestamp_tracker = TimestampTracker()
    process.set_on_output_callback(_log_line_func(logger, timestamp_tracker))
    process.set_on_terminate_callback(
        _on_retry(serial, extra_params, timestamp_tracker))
    return process
Beispiel #18
0
    def test_stop_sets_stopped_to_true_before_process_kill(self):
        """Tests that stop() sets the _stopped attribute to True.

        This order is required to prevent the _exec_loop from calling
        _on_terminate_callback when the user has killed the process.
        """
        verifier = mock.Mock()
        verifier.passed = False

        def test_call_order():
            self.assertTrue(process._stopped)
            verifier.passed = True

        process = Process('cmd')
        process._process = mock.Mock()
        process._process.poll.return_value = None
        process._kill_process = test_call_order
        process._process.wait.side_effect = subprocess.TimeoutExpired('', '')

        process.stop()

        self.assertEqual(verifier.passed, True)
Beispiel #19
0
 def test_start_process_returns_a_popen_object(self):
     """Tests that a Popen object is returned by __start_process."""
     with self.patch('subprocess.Popen', return_value='verification'):
         self.assertEqual(Process._Process__start_process('cmd'),
                          'verification')