Skip to content

Commit

Permalink
verify return code for adb executes (#27)
Browse files Browse the repository at this point in the history
* verify adb execute (timeout and retcode==0)
* increase default timeout to 10s
  • Loading branch information
jupe authored Dec 4, 2022
1 parent 5fca4ba commit de68a8a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
13 changes: 9 additions & 4 deletions stf_appium_client/AdbServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ def port(self) -> int:
""" Get local adb server port """
return self._port

def execute(self, command: str, timeout: int = 2) -> EasyProcess:
def execute(self, command: str, timeout: int = 10, verify: bool = True) -> EasyProcess:
"""
Internal execute function
:param command: adb command to be executed
:param timeout: command timeout
:param timeout: command timeout [s]
:param verify: verify return code is 0 of executed adb command, Also timeout causes assert raise.
:return: EasyProcess instance which contains stdout, stderr, return_code, timeout_happened
:raise AssertionError: if non 0 is returned or timeout happens and verify=True.
"""
port = f"-P {self.port} " if self.port else ""
cmd = f"adb {port} {command}"
port = f" -P {self.port}" if self.port else ""
cmd = f"adb{port} {command}"
self.logger.debug(f"adb: {cmd}")
my_env = os.environ.copy()
if "ADB_VENDOR_KEYS" not in my_env:
Expand All @@ -75,6 +77,9 @@ def execute(self, command: str, timeout: int = 2) -> EasyProcess:
self.logger.debug(f'adb retcode: {response.return_code}, '
f'stdout: {response.stdout}, '
f'stderr: {response.stderr}')
if verify:
assert response.timeout_happened is False, f'adb command "{cmd}" timeout ({timeout}s)'
assert response.return_code == 0, f'adb command "{cmd}" fails with code: {response.return_code}'
return response

def connect(self) -> None:
Expand Down
26 changes: 19 additions & 7 deletions test/test_AdbServer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import logging
import sys
from shutil import which
from time import sleep
from unittest.mock import MagicMock, patch
from unittest.mock import patch

import pytest
from easyprocess import EasyProcess
Expand Down Expand Up @@ -31,33 +30,46 @@ def test_context_e2e(self):
def test_context(self, mock_easy_process, mock_which):
mock_easy_process.return_value.call.return_value.stdout = ''
mock_easy_process.return_value.call.return_value.return_code = 0
mock_easy_process.return_value.call.return_value.timeout_happened = False
with AdbServer('localhost') as adb:
assert isinstance(adb.port, int)

@patch('stf_appium_client.AdbServer.EasyProcess')
def test_execute_success(self, mock_easy_process):
mock_easy_process.return_value.call.return_value.stdout = '123'
mock_easy_process.return_value.call.return_value.return_code = 0
adb_server = AdbServer('locvalhost', port=1000)
mock_easy_process.return_value.call.return_value.timeout_happened = False
adb_server = AdbServer('localhost', port=1000)
resp = adb_server.execute('hello', 10)
assert resp.return_code == 0
assert resp.stdout == '123'
mock_easy_process.assert_called_once()
mock_easy_process.return_value.call.assert_called_once_with(timeout=10)

@patch('stf_appium_client.AdbServer.EasyProcess')
def test_execute_fail(self, mock_easy_process):
def test_execute_fail_no_verify(self, mock_easy_process):
mock_easy_process.return_value.call.return_value.stderr = 'abc'
mock_easy_process.return_value.call.return_value.stdout = '123'
mock_easy_process.return_value.call.return_value.return_code = 1
adb_server = AdbServer('locvalhost', port=1000)
resp = adb_server.execute('hello', 10)
adb_server = AdbServer('localhost', port=1000)
resp = adb_server.execute('hello', 10, verify=False)
assert resp.return_code == 1
assert resp.stdout == '123'
assert resp.stderr == 'abc'
mock_easy_process.assert_called_once()
mock_easy_process.return_value.call.assert_called_once_with(timeout=10)

@patch('stf_appium_client.AdbServer.EasyProcess')
def test_execute_fail_verify(self, mock_easy_process):
mock_easy_process.return_value.call.return_value.stderr = 'abc'
mock_easy_process.return_value.call.return_value.stdout = '123'
mock_easy_process.return_value.call.return_value.return_code = 1
adb_server = AdbServer('locvalhost', port=1000)
with pytest.raises(AssertionError):
adb_server.execute('hello', 10)
mock_easy_process.assert_called_once()
mock_easy_process.return_value.call.assert_called_once_with(timeout=10)

@patch('stf_appium_client.AdbServer.EasyProcess')
def test_execute_timeout(self, mock_easy_process):

Expand All @@ -66,6 +78,6 @@ def call(timeout):
return EasyProcess([python, "-c", 'import time\ntime.sleep(10)']).call(timeout=timeout)
mock_easy_process.return_value.call.side_effect = call
adb_server = AdbServer('localhost', port=1000)
resp = adb_server.execute('hello', timeout=0.1)
resp = adb_server.execute('hello', timeout=0.01, verify=False)
assert resp.timeout_happened
mock_easy_process.assert_called_once()

0 comments on commit de68a8a

Please sign in to comment.