Example #1
0
 def test_run_command_with_custom_params(self, mock_Popen, mock_Timer):
     mock_command = mock.MagicMock(spec=dict)
     mock_stdout = mock.MagicMock(spec=int)
     mock_stderr = mock.MagicMock(spec=int)
     mock_shell = mock.MagicMock(spec=bool)
     mock_timeout = 1234
     mock_env = mock.MagicMock(spec=dict)
     mock_proc = mock_Popen.return_value
     mock_proc.communicate.return_value = ('fake_out', 'fake_err')
     mock_proc.returncode = 127
     out = utils.run_command(mock_command,
                             stdout=mock_stdout,
                             stderr=mock_stderr,
                             shell=mock_shell,
                             timeout=mock_timeout,
                             env=mock_env)
     self.assertEqual(out, (127, 'fake_out', 'fake_err'))
     mock_Popen.assert_called_with(
         mock_command,
         stdout=mock_stdout,
         stderr=mock_stderr,
         shell=mock_shell,
         cwd=None,
         env=mock_env,
     )
     mock_Timer.assert_called_with(1234, mock.ANY)
Example #2
0
def is_adb_available():
    """Checks if adb is available as a command line tool.

  Returns:
    True if adb binary is available in console, False otherwise.
  """
    ret, out, err = utils.run_command('which adb', shell=True)
    clean_out = out.decode('utf-8').strip()
    if clean_out:
        return True
    return False
Example #3
0
 def test_run_command_with_default_params(self, mock_Popen, mock_Timer):
     mock_command = mock.MagicMock(spec=dict)
     mock_proc = mock_Popen.return_value
     mock_proc.communicate.return_value = ('fake_out', 'fake_err')
     mock_proc.returncode = 0
     out = utils.run_command(mock_command)
     self.assertEqual(out, (0, 'fake_out', 'fake_err'))
     mock_Popen.assert_called_with(
         mock_command,
         stdout=subprocess.PIPE,
         stderr=subprocess.PIPE,
         shell=False,
         env=None,
     )
     mock_Timer.assert_not_called()
Example #4
0
    def _exec_cmd(self, args, shell, timeout, stderr):
        """Executes adb commands.

    Args:
      args: string or list of strings, program arguments.
        See subprocess.Popen() documentation.
      shell: bool, True to run this command through the system shell,
        False to invoke it directly. See subprocess.Popen() docs.
      timeout: float, the number of seconds to wait before timing out.
        If not specified, no timeout takes effect.
      stderr: a Byte stream, like io.BytesIO, stderr of the command will
        be written to this object if provided.

    Returns:
      The output of the adb command run if exit code is 0.

    Raises:
      ValueError: timeout value is invalid.
      AdbError: The adb command exit code is not 0.
      AdbTimeoutError: The adb command timed out.
    """
        if timeout and timeout <= 0:
            raise ValueError('Timeout is not a positive value: %s' % timeout)
        try:
            (ret, out, err) = utils.run_command(args,
                                                shell=shell,
                                                timeout=timeout)
        except psutil.TimeoutExpired:
            raise AdbTimeoutError(cmd=args,
                                  timeout=timeout,
                                  serial=self.serial)

        if stderr:
            stderr.write(err)
        logging.debug('cmd: %s, stdout: %s, stderr: %s, ret: %s',
                      utils.cli_cmd_to_string(args), out, err, ret)
        if ret == 0:
            return out
        else:
            raise AdbError(cmd=args,
                           stdout=out,
                           stderr=err,
                           ret_code=ret,
                           serial=self.serial)
Example #5
0
 def test_run_command_with_timeout_expired(self):
     with self.assertRaises(psutil.TimeoutExpired):
         _ = utils.run_command(self.sleep_cmd(4), timeout=0.01)
Example #6
0
 def test_run_command_with_timeout(self):
     (ret, out, err) = utils.run_command(self.sleep_cmd(0.01), timeout=4)
     self.assertEqual(ret, 0)
Example #7
0
 def test_run_command(self):
     (ret, out, err) = utils.run_command(self.sleep_cmd(0.01))
     self.assertEqual(ret, 0)
Example #8
0
    def test_run_command_with_universal_newlines_true(self):
        _, out, _ = utils.run_command(self.sleep_cmd(0.01),
                                      universal_newlines=True)

        self.assertIsInstance(out, str)
Example #9
0
    def test_run_command_with_universal_newlines_false(self):
        _, out, _ = utils.run_command(self.sleep_cmd(0.01),
                                      universal_newlines=False)

        self.assertIsInstance(out, bytes)
Example #10
0
    def test_run_command(self):
        ret, _, _ = utils.run_command(self.sleep_cmd(0.01))

        self.assertEqual(ret, 0)
Example #11
0
 def test_run_command_with_timeout_expired(self):
   with self.assertRaisesRegex(subprocess.TimeoutExpired, 'sleep'):
     _ = utils.run_command(self.sleep_cmd(4), timeout=0.01)