Commit 6a02a618 authored by aberent's avatar aberent Committed by Commit bot

Revert of [Android] Remove more uses of android_commands from...

Revert of [Android] Remove more uses of android_commands from build/android/pylib. (patchset #3 id:40001 of https://codereview.chromium.org/1105323002/)

Reason for revert:
Causes HostDriven_SigninTest.testManagedSignin to fail, and hence prevents rolls into Android downstream repository

BUG=482406

Original issue's description:
> [Android] Remove more uses of android_commands from build/android/pylib.
>
> BUG=267773
>
> Committed: https://crrev.com/166b709fc6c8fefad72520473850ce15ffe45898
> Cr-Commit-Position: refs/heads/master@{#327402}

TBR=perezju@chromium.org,mikecase@chromium.org,rnephew@chromium.org,jbudorick@chromium.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=267773

Review URL: https://codereview.chromium.org/1116493003

Cr-Commit-Position: refs/heads/master@{#327469}
parent 356fb212
......@@ -42,7 +42,6 @@ def CommonChecks(input_api, output_api):
input_api,
output_api,
unit_tests=[
J('pylib', 'base', 'test_dispatcher_unittest.py'),
J('pylib', 'device', 'battery_utils_test.py'),
J('pylib', 'device', 'device_utils_test.py'),
J('pylib', 'device', 'logcat_monitor_test.py'),
......
......@@ -26,15 +26,14 @@ NET_TEST_SERVER_PORT_INFO_FILE = 'net-test-server-ports'
class BaseTestRunner(object):
"""Base class for running tests on a single device."""
def __init__(self, device, tool):
def __init__(self, device_serial, tool):
"""
Args:
device: An instance of DeviceUtils that the tests will run on.
device: Tests will run on the device of this ID.
tool: Name of the Valgrind tool.
"""
assert isinstance(device, device_utils.DeviceUtils)
self.device = device
self.device_serial = self.device.adb.GetDeviceSerial()
self.device_serial = device_serial
self.device = device_utils.DeviceUtils(device_serial)
self.tool = CreateTool(tool, self.device)
self._http_server = None
self._forwarder_device_port = 8000
......
......@@ -21,6 +21,7 @@ Performs the following steps:
import logging
import threading
from pylib import android_commands
from pylib import constants
from pylib.base import base_test_result
from pylib.base import test_collection
......@@ -101,7 +102,7 @@ def _RunTestsFromQueue(runner, collection, out_results, watcher,
for test in collection:
watcher.Reset()
try:
if not runner.device.IsOnline():
if runner.device_serial not in android_commands.GetAttachedDevices():
# Device is unresponsive, stop handling tests on this device.
msg = 'Device %s is unresponsive.' % runner.device_serial
logging.warning(msg)
......@@ -149,7 +150,10 @@ def _SetUp(runner_factory, device, out_runners, threadsafe_counter):
runner = runner_factory(device, index)
runner.SetUp()
out_runners.append(runner)
except device_errors.DeviceUnreachableError as e:
except (device_errors.DeviceUnreachableError,
# TODO(jbudorick) Remove this once the underlying implementations
# for the above are switched or wrapped.
android_commands.errors.DeviceUnresponsiveError) as e:
logging.warning('Failed to create shard for %s: [%s]', device, e)
......@@ -191,7 +195,10 @@ def _RunAllTests(runners, test_collection_factory, num_retries, timeout=None,
# Catch DeviceUnreachableErrors and set a warning exit code
try:
workers.JoinAll(watcher)
except device_errors.DeviceUnreachableError as e:
except (device_errors.DeviceUnreachableError,
# TODO(jbudorick) Remove this once the underlying implementations
# for the above are switched or wrapped.
android_commands.errors.DeviceUnresponsiveError) as e:
logging.error(e)
if not all((len(tc) == 0 for tc in test_collections)):
......@@ -229,7 +236,7 @@ def _CreateRunners(runner_factory, devices, timeout=None):
threads = reraiser_thread.ReraiserThreadGroup(
[reraiser_thread.ReraiserThread(_SetUp,
[runner_factory, d, runners, counter],
name=str(d)[-4:])
name=d[-4:])
for d in devices])
threads.StartAll()
threads.JoinAll(watchdog_timer.WatchdogTimer(timeout))
......@@ -326,7 +333,10 @@ def RunTests(tests, runner_factory, devices, shard=True,
finally:
try:
_TearDownRunners(runners, setup_timeout)
except device_errors.DeviceUnreachableError as e:
except (device_errors.DeviceUnreachableError,
# TODO(jbudorick) Remove this once the underlying implementations
# for the above are switched or wrapped.
android_commands.errors.DeviceUnresponsiveError) as e:
logging.warning('Device unresponsive during TearDown: [%s]', e)
except Exception as e:
logging.error('Unexpected exception caught during TearDown: %s' % str(e))
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
......@@ -11,38 +10,27 @@ import os
import sys
import unittest
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),
os.pardir, os.pardir))
# Mock out android_commands.GetAttachedDevices().
from pylib import android_commands
android_commands.GetAttachedDevices = lambda: ['0', '1']
from pylib import constants
from pylib.base import base_test_result
from pylib.base import test_collection
from pylib.base import test_dispatcher
from pylib.device import adb_wrapper
from pylib.device import device_utils
from pylib.utils import watchdog_timer
sys.path.append(
os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pymock'))
import mock
class TestException(Exception):
pass
def _MockDevice(serial):
d = mock.MagicMock(spec=device_utils.DeviceUtils)
d.__str__.return_value = serial
d.adb = mock.MagicMock(spec=adb_wrapper.AdbWrapper)
d.adb.GetDeviceSerial = mock.MagicMock(return_value=serial)
d.IsOnline = mock.MagicMock(return_value=True)
return d
class MockRunner(object):
"""A mock TestRunner."""
def __init__(self, device=None, shard_index=0):
self.device = device or _MockDevice('0')
self.device_serial = self.device.adb.GetDeviceSerial()
def __init__(self, device='0', shard_index=0):
self.device_serial = device
self.shard_index = shard_index
self.setups = 0
self.teardowns = 0
......@@ -69,7 +57,7 @@ class MockRunnerFail(MockRunner):
class MockRunnerFailTwice(MockRunner):
def __init__(self, device=None, shard_index=0):
def __init__(self, device='0', shard_index=0):
super(MockRunnerFailTwice, self).__init__(device, shard_index)
self._fails = 0
......@@ -123,7 +111,7 @@ class TestFunctions(unittest.TestCase):
def testSetUp(self):
runners = []
counter = test_dispatcher._ThreadSafeCounter()
test_dispatcher._SetUp(MockRunner, _MockDevice('0'), runners, counter)
test_dispatcher._SetUp(MockRunner, '0', runners, counter)
self.assertEqual(len(runners), 1)
self.assertEqual(runners[0].setups, 1)
......@@ -147,8 +135,7 @@ class TestThreadGroupFunctions(unittest.TestCase):
self.test_collection_factory = lambda: shared_test_collection
def testCreate(self):
runners = test_dispatcher._CreateRunners(
MockRunner, [_MockDevice('0'), _MockDevice('1')])
runners = test_dispatcher._CreateRunners(MockRunner, ['0', '1'])
for runner in runners:
self.assertEqual(runner.setups, 1)
self.assertEqual(set([r.device_serial for r in runners]),
......@@ -157,29 +144,27 @@ class TestThreadGroupFunctions(unittest.TestCase):
set([0, 1]))
def testRun(self):
runners = [MockRunner(_MockDevice('0')), MockRunner(_MockDevice('1'))]
runners = [MockRunner('0'), MockRunner('1')]
results, exit_code = test_dispatcher._RunAllTests(
runners, self.test_collection_factory, 0)
self.assertEqual(len(results.GetPass()), len(self.tests))
self.assertEqual(exit_code, 0)
def testTearDown(self):
runners = [MockRunner(_MockDevice('0')), MockRunner(_MockDevice('1'))]
runners = [MockRunner('0'), MockRunner('1')]
test_dispatcher._TearDownRunners(runners)
for runner in runners:
self.assertEqual(runner.teardowns, 1)
def testRetry(self):
runners = test_dispatcher._CreateRunners(
MockRunnerFail, [_MockDevice('0'), _MockDevice('1')])
runners = test_dispatcher._CreateRunners(MockRunnerFail, ['0', '1'])
results, exit_code = test_dispatcher._RunAllTests(
runners, self.test_collection_factory, 0)
self.assertEqual(len(results.GetFail()), len(self.tests))
self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
def testReraise(self):
runners = test_dispatcher._CreateRunners(
MockRunnerException, [_MockDevice('0'), _MockDevice('1')])
runners = test_dispatcher._CreateRunners(MockRunnerException, ['0', '1'])
with self.assertRaises(TestException):
test_dispatcher._RunAllTests(runners, self.test_collection_factory, 0)
......@@ -189,8 +174,7 @@ class TestShard(unittest.TestCase):
@staticmethod
def _RunShard(runner_factory):
return test_dispatcher.RunTests(
['a', 'b', 'c'], runner_factory, [_MockDevice('0'), _MockDevice('1')],
shard=True)
['a', 'b', 'c'], runner_factory, ['0', '1'], shard=True)
def testShard(self):
results, exit_code = TestShard._RunShard(MockRunner)
......@@ -205,7 +189,7 @@ class TestShard(unittest.TestCase):
def testNoTests(self):
results, exit_code = test_dispatcher.RunTests(
[], MockRunner, [_MockDevice('0'), _MockDevice('1')], shard=True)
[], MockRunner, ['0', '1'], shard=True)
self.assertEqual(len(results.GetAll()), 0)
self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
......@@ -215,8 +199,7 @@ class TestReplicate(unittest.TestCase):
@staticmethod
def _RunReplicate(runner_factory):
return test_dispatcher.RunTests(
['a', 'b', 'c'], runner_factory, [_MockDevice('0'), _MockDevice('1')],
shard=False)
['a', 'b', 'c'], runner_factory, ['0', '1'], shard=False)
def testReplicate(self):
results, exit_code = TestReplicate._RunReplicate(MockRunner)
......@@ -232,7 +215,7 @@ class TestReplicate(unittest.TestCase):
def testNoTests(self):
results, exit_code = test_dispatcher.RunTests(
[], MockRunner, [_MockDevice('0'), _MockDevice('1')], shard=False)
[], MockRunner, ['0', '1'], shard=False)
self.assertEqual(len(results.GetAll()), 0)
self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
......
......@@ -542,24 +542,6 @@ class AdbWrapper(object):
raise device_errors.AdbCommandFailedError(
['root'], output, device_serial=self._device_serial)
def Emu(self, cmd, timeout=_DEFAULT_TIMEOUT,
retries=_DEFAULT_RETRIES):
"""Runs an emulator console command.
See http://developer.android.com/tools/devices/emulator.html#console
Args:
cmd: The command to run on the emulator console.
timeout: (optional) Timeout per try in seconds.
retries: (optional) Number of retries to attempt.
Returns:
The output of the emulator console command.
"""
if isinstance(cmd, basestring):
cmd = [cmd]
return self._RunDeviceAdbCmd(['emu'] + cmd, timeout, retries)
@property
def is_emulator(self):
return _EMULATOR_RE.match(self._device_serial)
......
......@@ -14,6 +14,7 @@ import os
import sys
import unittest
from pylib import android_commands
from pylib import constants
from pylib.device import battery_utils
from pylib.device import device_errors
......@@ -61,7 +62,8 @@ class BatteryUtilsInitTest(unittest.TestCase):
def testInitWithDeviceUtil(self):
serial = '0fedcba987654321'
d = device_utils.DeviceUtils(serial)
a = android_commands.AndroidCommands(device=serial)
d = device_utils.DeviceUtils(a)
b = battery_utils.BatteryUtils(d)
self.assertEqual(d, b._device)
......
......@@ -175,29 +175,6 @@ class DeviceUtils(object):
assert hasattr(self, decorators.DEFAULT_TIMEOUT_ATTR)
assert hasattr(self, decorators.DEFAULT_RETRIES_ATTR)
def __eq__(self, other):
"""Checks whether |other| refers to the same device as |self|.
Args:
other: The object to compare to. This can be a basestring, an instance
of adb_wrapper.AdbWrapper, or an instance of DeviceUtils.
Returns:
Whether |other| refers to the same device as |self|.
"""
return self.adb.GetDeviceSerial() == str(other)
def __lt__(self, other):
"""Compares two instances of DeviceUtils.
This merely compares their serial numbers.
Args:
other: The instance of DeviceUtils to compare to.
Returns:
Whether |self| is less than |other|.
"""
return self.adb.GetDeviceSerial() < other.adb.GetDeviceSerial()
def __str__(self):
"""Returns the device serial."""
return self.adb.GetDeviceSerial()
......
......@@ -161,73 +161,6 @@ class DeviceUtilsTest(mock_calls.TestCase):
msg, str(self.device)))
class DeviceUtilsEqTest(DeviceUtilsTest):
def testEq_equal_deviceUtils(self):
other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdef'))
self.assertTrue(self.device == other)
self.assertTrue(other == self.device)
def testEq_equal_adbWrapper(self):
other = adb_wrapper.AdbWrapper('0123456789abcdef')
self.assertTrue(self.device == other)
self.assertTrue(other == self.device)
def testEq_equal_string(self):
other = '0123456789abcdef'
self.assertTrue(self.device == other)
self.assertTrue(other == self.device)
def testEq_devicesNotEqual(self):
other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdee'))
self.assertFalse(self.device == other)
self.assertFalse(other == self.device)
def testEq_identity(self):
self.assertTrue(self.device == self.device)
def testEq_serialInList(self):
devices = [self.device]
self.assertTrue('0123456789abcdef' in devices)
class DeviceUtilsLtTest(DeviceUtilsTest):
def testLt_lessThan(self):
other = device_utils.DeviceUtils(_AdbWrapperMock('ffffffffffffffff'))
self.assertTrue(self.device < other)
self.assertTrue(other > self.device)
def testLt_greaterThan_lhs(self):
other = device_utils.DeviceUtils(_AdbWrapperMock('0000000000000000'))
self.assertFalse(self.device < other)
self.assertFalse(other > self.device)
def testLt_equal(self):
other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdef'))
self.assertFalse(self.device < other)
self.assertFalse(other > self.device)
def testLt_sorted(self):
devices = [
device_utils.DeviceUtils(_AdbWrapperMock('ffffffffffffffff')),
device_utils.DeviceUtils(_AdbWrapperMock('0000000000000000')),
]
sorted_devices = sorted(devices)
self.assertEquals('0000000000000000',
sorted_devices[0].adb.GetDeviceSerial())
self.assertEquals('ffffffffffffffff',
sorted_devices[1].adb.GetDeviceSerial())
class DeviceUtilsStrTest(DeviceUtilsTest):
def testStr_returnsSerial(self):
with self.assertCalls(
(self.call.adb.GetDeviceSerial(), '0123456789abcdef')):
self.assertEqual('0123456789abcdef', str(self.device))
class DeviceUtilsIsOnlineTest(DeviceUtilsTest):
def testIsOnline_true(self):
......@@ -1606,6 +1539,14 @@ class DeviceUtilsGetMemoryUsageForPidTest(DeviceUtilsTest):
self.device.GetMemoryUsageForPid(4321))
class DeviceUtilsStrTest(DeviceUtilsTest):
def testStr_returnsSerial(self):
with self.assertCalls(
(self.call.adb.GetDeviceSerial(), '0123456789abcdef')):
self.assertEqual('0123456789abcdef', str(self.device))
class DeviceUtilsClientCache(DeviceUtilsTest):
def testClientCache_twoCaches(self):
......
......@@ -9,6 +9,7 @@ import unittest
sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
from pylib import android_commands
from pylib.device import device_utils
from pylib.perf import perf_control
......@@ -17,9 +18,10 @@ class TestPerfControl(unittest.TestCase):
if not os.getenv('BUILDTYPE'):
os.environ['BUILDTYPE'] = 'Debug'
devices = device_utils.DeviceUtils.HealthyDevices()
devices = android_commands.GetAttachedDevices()
self.assertGreater(len(devices), 0, 'No device attached!')
self._device = devices[0]
self._device = device_utils.DeviceUtils(
android_commands.AndroidCommands(device=devices[0]))
def testHighPerfMode(self):
perf = perf_control.PerfControl(self._device)
......
......@@ -10,10 +10,10 @@ import logging
import os
import shutil
from pylib import android_commands
from pylib import constants
from pylib import forwarder
from pylib.device import device_list
from pylib.device import device_utils
from pylib.perf import test_runner
from pylib.utils import test_environment
......@@ -22,11 +22,10 @@ def _GetAllDevices():
devices_path = os.path.join(os.environ.get('CHROMIUM_OUT_DIR', 'out'),
device_list.LAST_DEVICES_FILENAME)
try:
devices = [device_utils.DeviceUtils(s)
for s in device_list.GetPersistentDeviceList(devices_path)]
devices = device_list.GetPersistentDeviceList(devices_path)
except IOError as e:
logging.error('Unable to find %s [%s]', devices_path, e)
devices = device_utils.DeviceUtils.HealthyDevices()
devices = android_commands.GetAttachedDevices()
return sorted(devices)
......
......@@ -15,6 +15,7 @@ import subprocess
import time
# TODO(craigdh): Move these pylib dependencies to pylib/utils/.
from pylib import android_commands
from pylib import cmd_helper
from pylib import constants
from pylib import pexpect
......@@ -89,14 +90,14 @@ def _KillAllEmulators():
running but a device slot is taken. A little bot trouble and and
we're out of room forever.
"""
emulators = [d for d in device_utils.HealthyDevices() if d.adb.is_emulator]
emulators = android_commands.GetAttachedDevices(hardware=False)
if not emulators:
return
for e in emulators:
e.adb.Emu(['kill'])
for emu_name in emulators:
cmd_helper.RunCmd(['adb', '-s', emu_name, 'emu', 'kill'])
logging.info('Emulator killing is async; give a few seconds for all to die.')
for _ in range(5):
if not any(d.adb.is_emulator for d in device_utils.HealthyDevices()):
if not android_commands.GetAttachedDevices(hardware=False):
return
time.sleep(1)
......@@ -140,9 +141,9 @@ class PortPool(object):
def _GetAvailablePort():
"""Returns an available TCP port for the console."""
used_ports = []
emulators = [d for d in device_utils.HealthyDevices() if d.adb.is_emulator]
emulators = android_commands.GetAttachedDevices(hardware=False)
for emulator in emulators:
used_ports.append(emulator.adb.GetDeviceSerial().split('-')[1])
used_ports.append(emulator.split('-')[1])
for port in PortPool.port_range():
if str(port) not in used_ports:
return port
......
......@@ -16,6 +16,7 @@ import sys
import threading
import unittest
from pylib import android_commands
from pylib import constants
from pylib import forwarder
from pylib import ports
......@@ -24,8 +25,6 @@ from pylib.base import environment_factory
from pylib.base import test_dispatcher
from pylib.base import test_instance_factory
from pylib.base import test_run_factory
from pylib.device import device_errors
from pylib.device import device_utils
from pylib.gtest import gtest_config
from pylib.gtest import setup as gtest_setup
from pylib.gtest import test_options as gtest_test_options
......@@ -868,16 +867,16 @@ def _GetAttachedDevices(test_device=None):
Returns:
A list of attached devices.
"""
attached_devices = device_utils.DeviceUtils.HealthyDevices()
attached_devices = []
attached_devices = android_commands.GetAttachedDevices()
if test_device:
test_device = [d for d in attached_devices if d == test_device]
if not test_device:
raise device_errors.DeviceUnreachableError(
'Did not find device %s among attached device. Attached devices: %s'
% (test_device, ', '.join(attached_devices)))
if not attached_devices:
raise device_errors.NoDevicesError()
assert test_device in attached_devices, (
'Did not find device %s among attached device. Attached devices: %s'
% (test_device, ', '.join(attached_devices)))
attached_devices = [test_device]
assert attached_devices, 'No devices attached.'
return sorted(attached_devices)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment