| Index: build/android/pylib/device/device_utils_test.py
|
| diff --git a/build/android/pylib/device/device_utils_test.py b/build/android/pylib/device/device_utils_test.py
|
| index bc50e6dc3fef3c09af83e3139172f24b6934d670..22a44559b8c4e2b3597b5a72ba552302288ae5af 100755
|
| --- a/build/android/pylib/device/device_utils_test.py
|
| +++ b/build/android/pylib/device/device_utils_test.py
|
| @@ -620,6 +620,44 @@ class DeviceUtilsRunShellCommandTest(DeviceUtilsTest):
|
| self.device.RunShellCommand(cmd, check_return=True))
|
|
|
|
|
| +class DeviceUtilsRunPipedShellCommandTest(DeviceUtilsTest):
|
| +
|
| + def testRunPipedShellCommand_success(self):
|
| + with self.assertCall(
|
| + self.call.device.RunShellCommand(
|
| + 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"'),
|
| + ['This line contains foo', 'PIPESTATUS: 0 0']):
|
| + self.assertEquals(['This line contains foo'],
|
| + self.device._RunPipedShellCommand('ps | grep foo'))
|
| +
|
| + def testRunPipedShellCommand_firstCommandFails(self):
|
| + with self.assertCall(
|
| + self.call.device.RunShellCommand(
|
| + 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"'),
|
| + ['PIPESTATUS: 1 0']):
|
| + with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec:
|
| + self.device._RunPipedShellCommand('ps | grep foo')
|
| + self.assertEquals([1, 0], ec.exception.status)
|
| +
|
| + def testRunPipedShellCommand_secondCommandFails(self):
|
| + with self.assertCall(
|
| + self.call.device.RunShellCommand(
|
| + 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"'),
|
| + ['PIPESTATUS: 0 1']):
|
| + with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec:
|
| + self.device._RunPipedShellCommand('ps | grep foo')
|
| + self.assertEquals([0, 1], ec.exception.status)
|
| +
|
| + def testRunPipedShellCommand_outputCutOff(self):
|
| + with self.assertCall(
|
| + self.call.device.RunShellCommand(
|
| + 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"'),
|
| + ['foo.bar'] * 256 + ['foo.ba']):
|
| + with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec:
|
| + self.device._RunPipedShellCommand('ps | grep foo')
|
| + self.assertIs(None, ec.exception.status)
|
| +
|
| +
|
| class DeviceUtilsGetDevicePieWrapper(DeviceUtilsTest):
|
|
|
| def testGetDevicePieWrapper_jb(self):
|
| @@ -642,52 +680,40 @@ class DeviceUtilsGetDevicePieWrapper(DeviceUtilsTest):
|
| class DeviceUtilsKillAllTest(DeviceUtilsTest):
|
|
|
| def testKillAll_noMatchingProcesses(self):
|
| - with self.assertCall(self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'):
|
| + with self.assertCall(self.call.device.GetPids('test_process'), []):
|
| with self.assertRaises(device_errors.CommandFailedError):
|
| self.device.KillAll('test_process')
|
|
|
| def testKillAll_nonblocking(self):
|
| with self.assertCalls(
|
| - (self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
|
| - 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'),
|
| + (self.call.device.GetPids('some.process'), {'some.process': '1234'}),
|
| (self.call.adb.Shell('kill -9 1234'), '')):
|
| - self.assertEquals(1,
|
| - self.device.KillAll('some.process', blocking=False))
|
| + self.assertEquals(
|
| + 1, self.device.KillAll('some.process', blocking=False))
|
|
|
| def testKillAll_blocking(self):
|
| with self.assertCalls(
|
| - (self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
|
| - 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'),
|
| + (self.call.device.GetPids('some.process'), {'some.process': '1234'}),
|
| (self.call.adb.Shell('kill -9 1234'), ''),
|
| - (self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
|
| - 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'),
|
| - (self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')):
|
| - self.assertEquals(1,
|
| - self.device.KillAll('some.process', blocking=True))
|
| + (self.call.device.GetPids('some.process'), {'some.process': '1234'}),
|
| + (self.call.device.GetPids('some.process'), [])):
|
| + self.assertEquals(
|
| + 1, self.device.KillAll('some.process', blocking=True))
|
|
|
| def testKillAll_root(self):
|
| with self.assertCalls(
|
| - (self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
|
| - 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'),
|
| + (self.call.device.GetPids('some.process'), {'some.process': '1234'}),
|
| (self.call.device.NeedsSU(), True),
|
| (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')):
|
| - self.assertEquals(1,
|
| - self.device.KillAll('some.process', as_root=True))
|
| + self.assertEquals(
|
| + 1, self.device.KillAll('some.process', as_root=True))
|
|
|
| def testKillAll_sigterm(self):
|
| with self.assertCalls(
|
| - (self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
|
| - 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'),
|
| + (self.call.device.GetPids('some.process'), {'some.process': '1234'}),
|
| (self.call.adb.Shell('kill -15 1234'), '')):
|
| - self.assertEquals(1,
|
| - self.device.KillAll('some.process', signum=signal.SIGTERM))
|
| + self.assertEquals(
|
| + 1, self.device.KillAll('some.process', signum=signal.SIGTERM))
|
|
|
|
|
| class DeviceUtilsStartActivityTest(DeviceUtilsTest):
|
| @@ -1115,7 +1141,8 @@ class DeviceUtilsReadFileTest(DeviceUtilsTest):
|
| as_root=False, check_return=True),
|
| ['-rw-rw---- root foo 256 1970-01-01 00:00 file']),
|
| (self.call.device.RunShellCommand(
|
| - ['cat', '/read/this/test/file'], as_root=False, check_return=True),
|
| + ['cat', '/read/this/test/file'],
|
| + as_root=False, check_return=True),
|
| ['this is a test file'])):
|
| self.assertEqual('this is a test file\n',
|
| self.device.ReadFile('/read/this/test/file'))
|
| @@ -1129,6 +1156,17 @@ class DeviceUtilsReadFileTest(DeviceUtilsTest):
|
| with self.assertRaises(device_errors.CommandFailedError):
|
| self.device.ReadFile('/this/file/does.not.exist')
|
|
|
| + def testReadFile_zeroSize(self):
|
| + with self.assertCalls(
|
| + (self.call.device.RunShellCommand(
|
| + ['ls', '-l', '/this/file/has/zero/size'],
|
| + as_root=False, check_return=True),
|
| + ['-r--r--r-- root foo 0 1970-01-01 00:00 zero_size_file']),
|
| + (self.call.device._ReadFileWithPull('/this/file/has/zero/size'),
|
| + 'but it has contents\n')):
|
| + self.assertEqual('but it has contents\n',
|
| + self.device.ReadFile('/this/file/has/zero/size'))
|
| +
|
| def testReadFile_withSU(self):
|
| with self.assertCalls(
|
| (self.call.device.RunShellCommand(
|
| @@ -1387,34 +1425,36 @@ class DeviceUtilsSetPropTest(DeviceUtilsTest):
|
| class DeviceUtilsGetPidsTest(DeviceUtilsTest):
|
|
|
| def testGetPids_noMatches(self):
|
| - with self.assertCall(self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
|
| - 'user 1000 100 1024 1024 ffffffff 00000000 no.match\n'):
|
| + with self.assertCall(
|
| + self.call.device._RunPipedShellCommand(
|
| + "'ps | grep -F does.not.match'", check_return=False),
|
| + []):
|
| self.assertEqual({}, self.device.GetPids('does.not.match'))
|
|
|
| def testGetPids_oneMatch(self):
|
| - with self.assertCall(self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
|
| - 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match\n'
|
| - 'user 1001 100 1024 1024 ffffffff 00000000 one.match\n'):
|
| + with self.assertCall(
|
| + self.call.device._RunPipedShellCommand(
|
| + "'ps | grep -F one.match'", check_return=False),
|
| + ['user 1001 100 1024 1024 ffffffff 00000000 one.match']):
|
| self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match'))
|
|
|
| def testGetPids_mutlipleMatches(self):
|
| - with self.assertCall(self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
|
| - 'user 1000 100 1024 1024 ffffffff 00000000 not\n'
|
| - 'user 1001 100 1024 1024 ffffffff 00000000 one.match\n'
|
| - 'user 1002 100 1024 1024 ffffffff 00000000 two.match\n'
|
| - 'user 1003 100 1024 1024 ffffffff 00000000 three.match\n'):
|
| + with self.assertCall(
|
| + self.call.device._RunPipedShellCommand(
|
| + "'ps | grep -F match'", check_return=False),
|
| + ['user 1001 100 1024 1024 ffffffff 00000000 one.match',
|
| + 'user 1002 100 1024 1024 ffffffff 00000000 two.match',
|
| + 'user 1003 100 1024 1024 ffffffff 00000000 three.match']):
|
| self.assertEqual(
|
| {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'},
|
| self.device.GetPids('match'))
|
|
|
| def testGetPids_exactMatch(self):
|
| - with self.assertCall(self.call.adb.Shell('ps'),
|
| - 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
|
| - 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match\n'
|
| - 'user 1234 100 1024 1024 ffffffff 00000000 exact.match\n'):
|
| + with self.assertCall(
|
| + self.call.device._RunPipedShellCommand(
|
| + "'ps | grep -F exact.match'", check_return=False),
|
| + ['user 1000 100 1024 1024 ffffffff 00000000 not.exact.match',
|
| + 'user 1234 100 1024 1024 ffffffff 00000000 exact.match']):
|
| self.assertEqual(
|
| {'not.exact.match': '1000', 'exact.match': '1234'},
|
| self.device.GetPids('exact.match'))
|
| @@ -1441,8 +1481,8 @@ class DeviceUtilsGetMemoryUsageForPidTest(DeviceUtilsTest):
|
|
|
| def testGetMemoryUsageForPid_validPid(self):
|
| with self.assertCalls(
|
| - (self.call.device.RunShellCommand(
|
| - ['showmap', '1234'], as_root=True, check_return=True),
|
| + (self.call.device._RunPipedShellCommand(
|
| + 'showmap 1234 | grep TOTAL', as_root=True, check_return=False),
|
| ['100 101 102 103 104 105 106 107 TOTAL']),
|
| (self.call.device.ReadFile('/proc/1234/status', as_root=True),
|
| 'VmHWM: 1024 kB\n')):
|
| @@ -1461,8 +1501,8 @@ class DeviceUtilsGetMemoryUsageForPidTest(DeviceUtilsTest):
|
|
|
| def testGetMemoryUsageForPid_noSmaps(self):
|
| with self.assertCalls(
|
| - (self.call.device.RunShellCommand(
|
| - ['showmap', '4321'], as_root=True, check_return=True),
|
| + (self.call.device._RunPipedShellCommand(
|
| + 'showmap 4321 | grep TOTAL', as_root=True, check_return=False),
|
| ['cannot open /proc/4321/smaps: No such file or directory']),
|
| (self.call.device.ReadFile('/proc/4321/status', as_root=True),
|
| 'VmHWM: 1024 kb\n')):
|
| @@ -1470,8 +1510,8 @@ class DeviceUtilsGetMemoryUsageForPidTest(DeviceUtilsTest):
|
|
|
| def testGetMemoryUsageForPid_noStatus(self):
|
| with self.assertCalls(
|
| - (self.call.device.RunShellCommand(
|
| - ['showmap', '4321'], as_root=True, check_return=True),
|
| + (self.call.device._RunPipedShellCommand(
|
| + 'showmap 4321 | grep TOTAL', as_root=True, check_return=False),
|
| ['100 101 102 103 104 105 106 107 TOTAL']),
|
| (self.call.device.ReadFile('/proc/4321/status', as_root=True),
|
| self.CommandError())):
|
|
|