Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: build/android/pylib/device/device_utils_test.py

Issue 700983006: Add NeedsSu for RunShellCommand to check whether it needs SU (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved. 2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """ 6 """
7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils).
8 """ 8 """
9 9
10 # pylint: disable=C0321 10 # pylint: disable=C0321
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 def assertShellCallSequence(self, calls): 306 def assertShellCallSequence(self, calls):
307 '''Assert that we expect a sequence of calls to adb.Shell. 307 '''Assert that we expect a sequence of calls to adb.Shell.
308 308
309 Args: 309 Args:
310 calls: a sequence of (cmd, return_value) pairs, where |cmd| is the 310 calls: a sequence of (cmd, return_value) pairs, where |cmd| is the
311 expected shell command to run on the device (with any quoting already 311 expected shell command to run on the device (with any quoting already
312 applied), and |return_value| is either a string to give as mock output 312 applied), and |return_value| is either a string to give as mock output
313 or a _ShellError object to raise an AdbShellCommandFailedError. 313 or a _ShellError object to raise an AdbShellCommandFailedError.
314 ''' 314 '''
315 def mk_expected_call(cmd, return_value): 315 def mk_expected_call(cmd, return_value):
316 expected_args = Args(cmd, expect_rc=0, timeout=10, retries=0) 316 expected_args = Args(cmd, expect_rc=0)
317 if isinstance(return_value, _ShellError): 317 if isinstance(return_value, _ShellError):
318 return_value = device_errors.AdbShellCommandFailedError(cmd, 318 return_value = device_errors.AdbShellCommandFailedError(cmd,
319 return_value.return_code, return_value.output, str(self.device)) 319 return_value.return_code, return_value.output, str(self.device))
320 elif isinstance(return_value, _CmdTimeout): 320 elif isinstance(return_value, _CmdTimeout):
321 return_value = device_errors.CommandTimeoutError(return_value.msg, 321 return_value = device_errors.CommandTimeoutError(return_value.msg,
322 str(self.device)) 322 str(self.device))
323 return (expected_args, return_value) 323 return (expected_args, return_value)
324 324
325 expected_calls = (mk_expected_call(a, r) for a, r in calls) 325 expected_calls = (mk_expected_call(a, r) for a, r in calls)
326 return MockCallSequence(self, self.adb, 'Shell', expected_calls) 326 return MockCallSequence(self, self.adb, 'Shell', expected_calls)
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 def testNewRunShellImpl_withCwd(self): 683 def testNewRunShellImpl_withCwd(self):
684 with self.assertShellCall('cd /some/test/path && ls'): 684 with self.assertShellCall('cd /some/test/path && ls'):
685 self.device.RunShellCommand('ls', cwd='/some/test/path') 685 self.device.RunShellCommand('ls', cwd='/some/test/path')
686 686
687 def testNewRunShellImpl_withCwdQuoted(self): 687 def testNewRunShellImpl_withCwdQuoted(self):
688 with self.assertShellCall("cd '/some test/path with/spaces' && ls"): 688 with self.assertShellCall("cd '/some test/path with/spaces' && ls"):
689 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') 689 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces')
690 690
691 def testRunShellCommand_withSu(self): 691 def testRunShellCommand_withSu(self):
692 with self.assertShellCallSequence([ 692 with self.assertShellCallSequence([
693 ('ls /root', _ShellError()), 693 ('su -c ls /root && ! ls /root', ''),
694 ('su -c setprop service.adb.root 0', '')]): 694 ('su -c setprop service.adb.root 0', '')]):
695 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) 695 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True)
696 696
697 def testRunShellCommand_withRoot(self): 697 def testRunShellCommand_withRoot(self):
698 with self.assertShellCallSequence([ 698 with self.assertShellCallSequence([
699 ('ls /root', '\r\n'), 699 ('su -c ls /root && ! ls /root', _ShellError()),
700 ('setprop service.adb.root 0', '')]): 700 ('setprop service.adb.root 0', '')]):
701 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) 701 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True)
702 702
703 def testRunShellCommand_manyLines(self): 703 def testRunShellCommand_manyLines(self):
704 cmd = 'ls /some/path' 704 cmd = 'ls /some/path'
705 with self.assertShellCall(cmd, 'file1\r\nfile2\r\nfile3\r\n'): 705 with self.assertShellCall(cmd, 'file1\r\nfile2\r\nfile3\r\n'):
706 self.assertEquals(['file1', 'file2', 'file3'], 706 self.assertEquals(['file1', 'file2', 'file3'],
707 self.device.RunShellCommand(cmd)) 707 self.device.RunShellCommand(cmd))
708 708
709 def testRunShellCommand_singleLine_success(self): 709 def testRunShellCommand_singleLine_success(self):
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 'this.is.a.test.process\r\n'), 787 'this.is.a.test.process\r\n'),
788 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n')]): 788 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n')]):
789 self.assertEquals(1, 789 self.assertEquals(1,
790 self.device.KillAll('this.is.a.test.process', blocking=True)) 790 self.device.KillAll('this.is.a.test.process', blocking=True))
791 791
792 def testKillAll_root(self): 792 def testKillAll_root(self):
793 with self.assertShellCallSequence([ 793 with self.assertShellCallSequence([
794 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' 794 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n'
795 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' 795 'u0_a1 1234 174 123456 54321 ffffffff 456789ab '
796 'this.is.a.test.process\r\n'), 796 'this.is.a.test.process\r\n'),
797 ('ls /root', _ShellError()), 797 ('su -c ls /root && ! ls /root', ''),
798 ('su -c kill -9 1234', '')]): 798 ('su -c kill -9 1234', '')]):
799 self.assertEquals(1, 799 self.assertEquals(1,
800 self.device.KillAll('this.is.a.test.process', as_root=True)) 800 self.device.KillAll('this.is.a.test.process', as_root=True))
801 801
802 def testKillAll_sigterm(self): 802 def testKillAll_sigterm(self):
803 with self.assertShellCallSequence([ 803 with self.assertShellCallSequence([
804 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' 804 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n'
805 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' 805 'u0_a1 1234 174 123456 54321 ffffffff 456789ab '
806 'this.is.a.test.process\r\n'), 806 'this.is.a.test.process\r\n'),
807 ('kill -15 1234', '')]): 807 ('kill -15 1234', '')]):
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
1088 test_files = [] 1088 test_files = []
1089 self.device._PushChangedFilesZipped(test_files) 1089 self.device._PushChangedFilesZipped(test_files)
1090 self.assertEqual(0, self.adb.Push.call_count) 1090 self.assertEqual(0, self.adb.Push.call_count)
1091 1091
1092 def testPushChangedFilesZipped_single(self): 1092 def testPushChangedFilesZipped_single(self):
1093 test_files = [('/test/host/path/file1', '/test/device/path/file1')] 1093 test_files = [('/test/host/path/file1', '/test/device/path/file1')]
1094 1094
1095 self.device._GetExternalStoragePathImpl = mock.Mock( 1095 self.device._GetExternalStoragePathImpl = mock.Mock(
1096 return_value='/test/device/external_dir') 1096 return_value='/test/device/external_dir')
1097 self.device.IsOnline = mock.Mock(return_value=True) 1097 self.device.IsOnline = mock.Mock(return_value=True)
1098 self.device._RunShellCommandImpl = mock.Mock() 1098 self.device.RunShellCommand = mock.Mock()
1099 mock_zip_temp = mock.mock_open() 1099 mock_zip_temp = mock.mock_open()
1100 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' 1100 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip'
1101 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( 1101 with mock.patch('multiprocessing.Process') as mock_zip_proc, (
1102 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): 1102 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)):
1103 self.device._PushChangedFilesZipped(test_files) 1103 self.device._PushChangedFilesZipped(test_files)
1104 1104
1105 mock_zip_proc.assert_called_once_with( 1105 mock_zip_proc.assert_called_once_with(
1106 target=device_utils.DeviceUtils._CreateDeviceZip, 1106 target=device_utils.DeviceUtils._CreateDeviceZip,
1107 args=('/test/temp/file/tmp.zip', test_files)) 1107 args=('/test/temp/file/tmp.zip', test_files))
1108 self.adb.Push.assert_called_once_with( 1108 self.adb.Push.assert_called_once_with(
1109 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') 1109 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip')
1110 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) 1110 self.assertEqual(2, self.device.RunShellCommand.call_count)
1111 self.device._RunShellCommandImpl.assert_any_call( 1111 self.device.RunShellCommand.assert_any_call(
1112 ['unzip', '/test/device/external_dir/tmp.zip'], 1112 ['unzip', '/test/device/external_dir/tmp.zip'],
1113 as_root=True, 1113 as_root=True,
1114 env={'PATH': '$PATH:/data/local/tmp/bin'}, 1114 env={'PATH': '$PATH:/data/local/tmp/bin'},
1115 check_return=True) 1115 check_return=True)
1116 self.device._RunShellCommandImpl.assert_any_call( 1116 self.device.RunShellCommand.assert_any_call(
1117 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) 1117 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True)
1118 1118
1119 def testPushChangedFilesZipped_multiple(self): 1119 def testPushChangedFilesZipped_multiple(self):
1120 test_files = [('/test/host/path/file1', '/test/device/path/file1'), 1120 test_files = [('/test/host/path/file1', '/test/device/path/file1'),
1121 ('/test/host/path/file2', '/test/device/path/file2')] 1121 ('/test/host/path/file2', '/test/device/path/file2')]
1122 1122
1123 self.device._GetExternalStoragePathImpl = mock.Mock( 1123 self.device._GetExternalStoragePathImpl = mock.Mock(
1124 return_value='/test/device/external_dir') 1124 return_value='/test/device/external_dir')
1125 self.device.IsOnline = mock.Mock(return_value=True) 1125 self.device.IsOnline = mock.Mock(return_value=True)
1126 self.device._RunShellCommandImpl = mock.Mock() 1126 self.device.RunShellCommand = mock.Mock()
1127 mock_zip_temp = mock.mock_open() 1127 mock_zip_temp = mock.mock_open()
1128 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' 1128 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip'
1129 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( 1129 with mock.patch('multiprocessing.Process') as mock_zip_proc, (
1130 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): 1130 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)):
1131 self.device._PushChangedFilesZipped(test_files) 1131 self.device._PushChangedFilesZipped(test_files)
1132 1132
1133 mock_zip_proc.assert_called_once_with( 1133 mock_zip_proc.assert_called_once_with(
1134 target=device_utils.DeviceUtils._CreateDeviceZip, 1134 target=device_utils.DeviceUtils._CreateDeviceZip,
1135 args=('/test/temp/file/tmp.zip', test_files)) 1135 args=('/test/temp/file/tmp.zip', test_files))
1136 self.adb.Push.assert_called_once_with( 1136 self.adb.Push.assert_called_once_with(
1137 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') 1137 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip')
1138 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) 1138 self.assertEqual(2, self.device.RunShellCommand.call_count)
1139 self.device._RunShellCommandImpl.assert_any_call( 1139 self.device.RunShellCommand.assert_any_call(
1140 ['unzip', '/test/device/external_dir/tmp.zip'], 1140 ['unzip', '/test/device/external_dir/tmp.zip'],
1141 as_root=True, 1141 as_root=True,
1142 env={'PATH': '$PATH:/data/local/tmp/bin'}, 1142 env={'PATH': '$PATH:/data/local/tmp/bin'},
1143 check_return=True) 1143 check_return=True)
1144 self.device._RunShellCommandImpl.assert_any_call( 1144 self.device.RunShellCommand.assert_any_call(
1145 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) 1145 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True)
1146 1146
1147 1147
1148 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): 1148 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest):
1149 1149
1150 def testFileExists_usingTest_fileExists(self): 1150 def testFileExists_usingTest_fileExists(self):
1151 with self.assertCalls( 1151 with self.assertCalls(
1152 "adb -s 0123456789abcdef shell " 1152 "adb -s 0123456789abcdef shell "
1153 "'test -e \"/data/app/test.file.exists\"; echo $?'", 1153 "'test -e \"/data/app/test.file.exists\"; echo $?'",
1154 '0\r\n'): 1154 '0\r\n'):
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
1376 with self.assertShellCall('echo some.string > /test/file/to.write'): 1376 with self.assertShellCall('echo some.string > /test/file/to.write'):
1377 self.device.WriteTextFile('/test/file/to.write', 'some.string') 1377 self.device.WriteTextFile('/test/file/to.write', 'some.string')
1378 1378
1379 def testWriteTextFileTest_quoted(self): 1379 def testWriteTextFileTest_quoted(self):
1380 with self.assertShellCall( 1380 with self.assertShellCall(
1381 "echo 'some other string' > '/test/file/to write'"): 1381 "echo 'some other string' > '/test/file/to write'"):
1382 self.device.WriteTextFile('/test/file/to write', 'some other string') 1382 self.device.WriteTextFile('/test/file/to write', 'some other string')
1383 1383
1384 def testWriteTextFileTest_asRoot(self): 1384 def testWriteTextFileTest_asRoot(self):
1385 with self.assertShellCallSequence([ 1385 with self.assertShellCallSequence([
1386 ('ls /root', _ShellError()), 1386 ('su -c ls /root && ! ls /root', ''),
1387 ('su -c echo string > /test/file', '')]): 1387 ('su -c echo string > /test/file', '')]):
1388 self.device.WriteTextFile('/test/file', 'string', as_root=True) 1388 self.device.WriteTextFile('/test/file', 'string', as_root=True)
1389 1389
1390 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): 1390 class DeviceUtilsLsTest(DeviceUtilsOldImplTest):
1391 1391
1392 def testLs_nothing(self): 1392 def testLs_nothing(self):
1393 with self.assertCallsSequence([ 1393 with self.assertCallsSequence([
1394 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", 1394 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'",
1395 '/this/file/does.not.exist: No such file or directory\r\n'), 1395 '/this/file/does.not.exist: No such file or directory\r\n'),
1396 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): 1396 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]):
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
1681 self.device = device_utils.DeviceUtils(None) 1681 self.device = device_utils.DeviceUtils(None)
1682 with self.assertCalls('adb get-serialno', 'unknown'), ( 1682 with self.assertCalls('adb get-serialno', 'unknown'), (
1683 self.assertRaises(device_errors.NoDevicesError)): 1683 self.assertRaises(device_errors.NoDevicesError)):
1684 str(self.device) 1684 str(self.device)
1685 1685
1686 1686
1687 if __name__ == '__main__': 1687 if __name__ == '__main__':
1688 logging.getLogger().setLevel(logging.DEBUG) 1688 logging.getLogger().setLevel(logging.DEBUG)
1689 unittest.main(verbosity=2) 1689 unittest.main(verbosity=2)
1690 1690
OLDNEW
« build/android/pylib/device/device_utils.py ('K') | « build/android/pylib/device/device_utils.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698