def test_has_shell_command_with_missing_command_on_newer_devices(self): with mock.patch.object(adb.AdbProxy, '_exec_cmd') as mock_exec_cmd: mock_exec_cmd.return_value = MOCK_DEFAULT_COMMAND_OUTPUT mock_exec_cmd.side_effect = adb.AdbError( MOCK_ADB_SHELL_COMMAND_CHECK, '', '', 1) self.assertFalse( adb.AdbProxy().has_shell_command(MOCK_SHELL_COMMAND))
def test_clear_adb_log(self, MockFastboot, MockAdbProxy): mock_serial = '1' ad = android_device.AndroidDevice(serial=mock_serial) ad.adb.logcat = mock.MagicMock() ad.adb.logcat.side_effect = adb.AdbError( cmd='cmd', stdout=b'', stderr=b'failed to clear "main" log', ret_code=1) logcat_service = logcat.Logcat(ad) logcat_service.clear_adb_log()
def test_snippet_start_app_and_connect_persistent_session( self, mock_get_port, mock_connect, mock_read_protocol_line, mock_check_app_installed, mock_do_start_app): def _mocked_shell(arg): if 'setsid' in arg: raise adb.AdbError('cmd', 'stdout', 'stderr', 'ret_code') else: return b'nohup' mock_get_port.return_value = 123 mock_read_protocol_line.side_effect = [ 'SNIPPET START, PROTOCOL 1 234', 'SNIPPET SERVING, PORT 1234', 'SNIPPET START, PROTOCOL 1 234', 'SNIPPET SERVING, PORT 1234', 'SNIPPET START, PROTOCOL 1 234', 'SNIPPET SERVING, PORT 1234', ] # Test 'setsid' exists client = self._make_client() client._adb = mock.MagicMock() client._adb.shell.return_value = b'setsid' client._adb.current_user_id = MOCK_USER_ID client.start_app_and_connect() cmd_setsid = '%s am instrument --user %s -w -e action start %s/%s' % ( snippet_client._SETSID_COMMAND, MOCK_USER_ID, MOCK_PACKAGE_NAME, snippet_client._INSTRUMENTATION_RUNNER_PACKAGE) mock_do_start_app.assert_has_calls([mock.call(cmd_setsid)]) # Test 'setsid' does not exist, but 'nohup' exsits client = self._make_client() client._adb.shell = _mocked_shell client.start_app_and_connect() cmd_nohup = '%s am instrument --user %s -w -e action start %s/%s' % ( snippet_client._NOHUP_COMMAND, MOCK_USER_ID, MOCK_PACKAGE_NAME, snippet_client._INSTRUMENTATION_RUNNER_PACKAGE) mock_do_start_app.assert_has_calls( [mock.call(cmd_setsid), mock.call(cmd_nohup)]) # Test both 'setsid' and 'nohup' do not exist client._adb.shell = mock.Mock( side_effect=adb.AdbError('cmd', 'stdout', 'stderr', 'ret_code')) client = self._make_client() client.start_app_and_connect() cmd_not_persist = ' am instrument --user %s -w -e action start %s/%s' % ( MOCK_USER_ID, MOCK_PACKAGE_NAME, snippet_client._INSTRUMENTATION_RUNNER_PACKAGE) mock_do_start_app.assert_has_calls([ mock.call(cmd_setsid), mock.call(cmd_nohup), mock.call(cmd_not_persist) ])
def test_root_success_with_retry(self, mock_exec_cmd, mock_sleep): mock_exec_cmd.side_effect = [ adb.AdbError('adb root', '', MOCK_ROOT_ERROR_OUTPUT, 1), MOCK_ROOT_SUCCESS_OUTPUT ] output = adb.AdbProxy().root() mock_exec_cmd.assert_called_with(['adb', 'root'], shell=False, timeout=None, stderr=None) self.assertEqual(output, MOCK_ROOT_SUCCESS_OUTPUT) self.assertEqual(mock_sleep.call_count, 1) mock_sleep.assert_called_with(10)
def test_root_raises_adb_error_when_all_retries_failed( self, mock_exec_cmd, mock_sleep): mock_exec_cmd.side_effect = adb.AdbError('adb root', '', MOCK_ROOT_ERROR_OUTPUT, 1) expected_msg = ('Error executing adb cmd "adb root". ' 'ret: 1, stdout: , stderr: %s' % MOCK_ROOT_ERROR_OUTPUT) with self.assertRaisesRegex(adb.AdbError, expected_msg): adb.AdbProxy().root() mock_exec_cmd.assert_called_with(['adb', 'root'], shell=False, timeout=None, stderr=None) self.assertEqual(mock_sleep.call_count, 2) mock_sleep.assert_called_with(10)
'02-29 14:02:21.789 4454 Something again\n' ] # A mocked piece of adb logcat output. MOCK_ADB_UNICODE_LOGCAT = ( u'02-29 14:02:19.123 4454 Nothing\n' u'%s' u'02-29 14:02:22.123 4454 Something again and again\n' ) % u''.join(MOCK_ADB_UNICODE_LOGCAT_CAT_RESULT) # Mock start and end time of the adb cat. MOCK_ADB_LOGCAT_BEGIN_TIME = '02-29 14:02:20.123' MOCK_ADB_LOGCAT_END_TIME = '02-29 14:02:22.000' # Mock AdbError for missing logpersist scripts MOCK_LOGPERSIST_STOP_MISSING_ADB_ERROR = adb.AdbError( 'logpersist.stop --clear', b'', '/system/bin/sh: logpersist.stop: not found', 0) MOCK_LOGPERSIST_START_MISSING_ADB_ERROR = adb.AdbError( 'logpersist.start --clear', b'', b'/system/bin/sh: logpersist.stop: not found', 0) class LogcatTest(unittest.TestCase): """Tests for Logcat service and its integration with AndroidDevice.""" def setUp(self): # Set log_path to logging since mobly logger setup is not called. if not hasattr(logging, 'log_path'): setattr(logging, 'log_path', '/tmp/logs') # Creates a temp dir to be used by tests in this test class. self.tmp_dir = tempfile.mkdtemp()
def _mocked_shell(arg): if 'setsid' in arg: raise adb.AdbError('cmd', 'stdout', 'stderr', 'ret_code') else: return b'nohup'
def test_has_shell_command_with_missing_command_on_older_devices(self): with mock.patch.object(adb.AdbProxy, '_exec_cmd') as mock_exec_cmd: mock_exec_cmd.side_effect = adb.AdbError( MOCK_ADB_SHELL_COMMAND_CHECK, '', '', 0) self.assertFalse( adb.AdbProxy().has_shell_command(MOCK_SHELL_COMMAND))