aboutsummaryrefslogtreecommitdiff
path: root/tests/mobly
diff options
context:
space:
mode:
authorAng Li <angli@google.com>2021-05-05 22:15:37 -0700
committerGitHub <noreply@github.com>2021-05-05 22:15:37 -0700
commit8624f722e95b1e6858c535a7abf74243f2535b7b (patch)
tree6ca97552bfd9fc9d193047d4d306dd94c24e727f /tests/mobly
parent509f7178a111cadf09c48e0355036eda2fa2725d (diff)
downloadmobly-8624f722e95b1e6858c535a7abf74243f2535b7b.tar.gz
Check Android devices exist before calling commands on them. (#743)
`AndroidDevice` instantiation calls `adb root` on devices if the device is rootable. And today, we instantiate the device objects before we check if the device exists at all, which is logically incorrect.
Diffstat (limited to 'tests/mobly')
-rwxr-xr-xtests/mobly/controllers/android_device_test.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/mobly/controllers/android_device_test.py b/tests/mobly/controllers/android_device_test.py
index c64c826..1c4be14 100755
--- a/tests/mobly/controllers/android_device_test.py
+++ b/tests/mobly/controllers/android_device_test.py
@@ -27,6 +27,7 @@ import yaml
from mobly import runtime_test_info
from mobly.controllers import android_device
from mobly.controllers.android_device_lib import adb
+from mobly.controllers.android_device_lib import errors
from mobly.controllers.android_device_lib import snippet_client
from mobly.controllers.android_device_lib.services import base_service
from mobly.controllers.android_device_lib.services import logcat
@@ -136,6 +137,61 @@ class AndroidDeviceTest(unittest.TestCase):
with self.assertRaisesRegex(android_device.Error, expected_msg):
android_device.create([1])
+ @mock.patch('mobly.controllers.android_device.list_adb_devices')
+ @mock.patch('mobly.controllers.android_device.list_adb_devices_by_usb_id')
+ @mock.patch('mobly.controllers.android_device.AndroidDevice')
+ def test_get_instances(self, mock_ad_class, mock_list_adb_usb, mock_list_adb):
+ mock_list_adb.return_value = ['1']
+ mock_list_adb_usb.return_value = []
+ android_device.get_instances(['1'])
+ mock_ad_class.assert_called_with('1')
+
+ @mock.patch('mobly.controllers.android_device.list_adb_devices')
+ @mock.patch('mobly.controllers.android_device.list_adb_devices_by_usb_id')
+ @mock.patch('mobly.controllers.android_device.AndroidDevice')
+ def test_get_instances_do_not_exist(self, mock_ad_class, mock_list_adb_usb,
+ mock_list_adb):
+ mock_list_adb.return_value = []
+ mock_list_adb_usb.return_value = []
+ with self.assertRaisesRegex(
+ errors.Error,
+ 'Android device serial "1" is specified in config but is not reachable'
+ ):
+ android_device.get_instances(['1'])
+
+ @mock.patch('mobly.controllers.android_device.list_adb_devices')
+ @mock.patch('mobly.controllers.android_device.list_adb_devices_by_usb_id')
+ @mock.patch('mobly.controllers.android_device.AndroidDevice')
+ def test_get_instances_with_configs(self, mock_ad_class, mock_list_adb_usb,
+ mock_list_adb):
+ mock_list_adb.return_value = ['1']
+ mock_list_adb_usb.return_value = []
+ config = {'serial': '1'}
+ android_device.get_instances_with_configs([config])
+ mock_ad_class.assert_called_with('1')
+
+ def test_get_instances_with_configs_invalid_config(self):
+ config = {'something': 'random'}
+ with self.assertRaisesRegex(
+ errors.Error,
+ f'Required value "serial" is missing in AndroidDevice config {config}'):
+ android_device.get_instances_with_configs([config])
+
+ @mock.patch('mobly.controllers.android_device.list_adb_devices')
+ @mock.patch('mobly.controllers.android_device.list_adb_devices_by_usb_id')
+ @mock.patch('mobly.controllers.android_device.AndroidDevice')
+ def test_get_instances_with_configsdo_not_exist(self, mock_ad_class,
+ mock_list_adb_usb,
+ mock_list_adb):
+ mock_list_adb.return_value = []
+ mock_list_adb_usb.return_value = []
+ config = {'serial': '1'}
+ with self.assertRaisesRegex(
+ errors.Error,
+ 'Android device serial "1" is specified in config but is not reachable'
+ ):
+ android_device.get_instances_with_configs([config])
+
def test_get_devices_success_with_extra_field(self):
ads = mock_android_device.get_mock_ads(5)
expected_label = 'selected'