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

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

Issue 751063002: Allow RunShellCommand to work even with very large commands (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment implementation of WriteFile Created 6 years 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
11 # pylint: disable=W0212 11 # pylint: disable=W0212
12 # pylint: disable=W0613 12 # pylint: disable=W0613
13 13
14 import collections 14 import collections
15 import datetime 15 import datetime
16 import logging 16 import logging
17 import os 17 import os
18 import re 18 import re
19 import signal 19 import signal
20 import sys 20 import sys
21 import unittest 21 import unittest
22 22
23 from pylib import android_commands 23 from pylib import android_commands
24 from pylib import cmd_helper
24 from pylib import constants 25 from pylib import constants
25 from pylib.device import adb_wrapper 26 from pylib.device import adb_wrapper
26 from pylib.device import device_errors 27 from pylib.device import device_errors
27 from pylib.device import device_utils 28 from pylib.device import device_utils
28 from pylib.device import intent 29 from pylib.device import intent
29 from pylib.utils import mock_calls 30 from pylib.utils import mock_calls
30 31
31 # RunCommand from third_party/android_testrunner/run_command.py is mocked 32 # RunCommand from third_party/android_testrunner/run_command.py is mocked
32 # below, so its path needs to be in sys.path. 33 # below, so its path needs to be in sys.path.
33 sys.path.append(os.path.join( 34 sys.path.append(os.path.join(
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 device_utils.DeviceUtils(None) 68 device_utils.DeviceUtils(None)
68 with self.assertRaises(ValueError): 69 with self.assertRaises(ValueError):
69 device_utils.DeviceUtils('') 70 device_utils.DeviceUtils('')
70 71
71 72
72 class MockTempFile(object): 73 class MockTempFile(object):
73 74
74 def __init__(self, name='/tmp/some/file'): 75 def __init__(self, name='/tmp/some/file'):
75 self.file = mock.MagicMock(spec=file) 76 self.file = mock.MagicMock(spec=file)
76 self.file.name = name 77 self.file.name = name
78 self.file.name_quoted = cmd_helper.SingleQuote(name)
77 79
78 def __enter__(self): 80 def __enter__(self):
79 return self.file 81 return self.file
80 82
81 def __exit__(self, exc_type, exc_val, exc_tb): 83 def __exit__(self, exc_type, exc_val, exc_tb):
82 pass 84 pass
83 85
84 86
85 class _PatchedFunction(object): 87 class _PatchedFunction(object):
86 def __init__(self, patched=None, mocked=None): 88 def __init__(self, patched=None, mocked=None):
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 614
613 def testNewRunShellImpl_withCwd(self): 615 def testNewRunShellImpl_withCwd(self):
614 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): 616 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''):
615 self.device.RunShellCommand('ls', cwd='/some/test/path') 617 self.device.RunShellCommand('ls', cwd='/some/test/path')
616 618
617 def testNewRunShellImpl_withCwdQuoted(self): 619 def testNewRunShellImpl_withCwdQuoted(self):
618 with self.assertCall( 620 with self.assertCall(
619 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): 621 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''):
620 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') 622 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces')
621 623
624 def testRunShellCommand_withHugeCmd(self):
625 payload = 'hi! ' * 1024
626 expected_cmd = "echo '%s'" % payload
627 with self.assertCalls(
628 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(
629 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')),
630 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd),
631 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')):
632 self.assertEquals([payload],
633 self.device.RunShellCommand(['echo', payload]))
634
635 def testRunShellCommand_withHugeCmdAmdSU(self):
636 payload = 'hi! ' * 1024
637 expected_cmd = """su -c sh -c 'echo '"'"'%s'"'"''""" % payload
638 with self.assertCalls(
639 (self.call.device.NeedsSU(), True),
640 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(
641 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')),
642 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd),
643 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')):
644 self.assertEquals(
645 [payload],
646 self.device.RunShellCommand(['echo', payload], as_root=True))
647
622 def testRunShellCommand_withSu(self): 648 def testRunShellCommand_withSu(self):
623 with self.assertCalls( 649 with self.assertCalls(
624 (self.call.device.NeedsSU(), True), 650 (self.call.device.NeedsSU(), True),
625 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): 651 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')):
626 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) 652 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True)
627 653
628 def testRunShellCommand_manyLines(self): 654 def testRunShellCommand_manyLines(self):
629 cmd = 'ls /some/path' 655 cmd = 'ls /some/path'
630 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): 656 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'):
631 self.assertEquals(['file1', 'file2', 'file3'], 657 self.assertEquals(['file1', 'file2', 'file3'],
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 def testReadFile_asRoot_rejected(self): 1190 def testReadFile_asRoot_rejected(self):
1165 self.device.old_interface._privileged_command_runner = None 1191 self.device.old_interface._privileged_command_runner = None
1166 self.device.old_interface._protected_file_access_method_initialized = True 1192 self.device.old_interface._protected_file_access_method_initialized = True
1167 with self.assertRaises(device_errors.CommandFailedError): 1193 with self.assertRaises(device_errors.CommandFailedError):
1168 self.device.ReadFile('/this/file/cannot.be.read.by.user', 1194 self.device.ReadFile('/this/file/cannot.be.read.by.user',
1169 as_root=True) 1195 as_root=True)
1170 1196
1171 1197
1172 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): 1198 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest):
1173 1199
1174 def testWriteFile_withPush(self): 1200 def testWriteFileWithPush_success(self):
1175 tmp_host = MockTempFile('/tmp/file/on.host') 1201 tmp_host = MockTempFile('/tmp/file/on.host')
1176 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars 1202 contents = 'some interesting contents'
1177 with self.assertCalls( 1203 with self.assertCalls(
1178 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), 1204 (mock.call.tempfile.NamedTemporaryFile(), tmp_host),
1179 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): 1205 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')):
1180 self.device.WriteFile('/path/to/device/file', contents) 1206 self.device._WriteFileWithPush('/path/to/device/file', contents)
1181 tmp_host.file.write.assert_called_once_with(contents) 1207 tmp_host.file.write.assert_called_once_with(contents)
1182 1208
1183 def testWriteFile_withPushForced(self): 1209 def testWriteFileWithPush_rejected(self):
1184 tmp_host = MockTempFile('/tmp/file/on.host') 1210 tmp_host = MockTempFile('/tmp/file/on.host')
1185 contents = 'tiny contents' 1211 contents = 'some interesting contents'
1186 with self.assertCalls(
1187 (mock.call.tempfile.NamedTemporaryFile(), tmp_host),
1188 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')):
1189 self.device.WriteFile('/path/to/device/file', contents, force_push=True)
1190 tmp_host.file.write.assert_called_once_with(contents)
1191
1192 def testWriteFile_withPushAndSU(self):
1193 tmp_host = MockTempFile('/tmp/file/on.host')
1194 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars
1195 with self.assertCalls(
1196 (mock.call.tempfile.NamedTemporaryFile(), tmp_host),
1197 (self.call.device.NeedsSU(), True),
1198 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device),
1199 MockTempFile('/external/path/tmp/on.device')),
1200 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'),
1201 self.call.device.RunShellCommand(
1202 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'],
1203 as_root=True, check_return=True)):
1204 self.device.WriteFile('/path/to/device/file', contents, as_root=True)
1205 tmp_host.file.write.assert_called_once_with(contents)
1206
1207 def testWriteFile_withPush_rejected(self):
1208 tmp_host = MockTempFile('/tmp/file/on.host')
1209 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars
1210 with self.assertCalls( 1212 with self.assertCalls(
1211 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), 1213 (mock.call.tempfile.NamedTemporaryFile(), tmp_host),
1212 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), 1214 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'),
1213 self.CommandError())): 1215 self.CommandError())):
1214 with self.assertRaises(device_errors.CommandFailedError): 1216 with self.assertRaises(device_errors.CommandFailedError):
1215 self.device.WriteFile('/path/to/device/file', contents) 1217 self.device._WriteFileWithPush('/path/to/device/file', contents)
1218
1219 def testWriteFile_withPush(self):
1220 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars
1221 with self.assertCalls(
1222 self.call.device._WriteFileWithPush('/path/to/device/file', contents)):
1223 self.device.WriteFile('/path/to/device/file', contents)
1224
1225 def testWriteFile_withPushForced(self):
1226 contents = 'tiny contents'
1227 with self.assertCalls(
1228 self.call.device._WriteFileWithPush('/path/to/device/file', contents)):
1229 self.device.WriteFile('/path/to/device/file', contents, force_push=True)
1230
1231 def testWriteFile_withPushAndSU(self):
1232 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars
1233 with self.assertCalls(
1234 (self.call.device.NeedsSU(), True),
1235 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb),
1236 MockTempFile('/sdcard/tmp/on.device')),
1237 self.call.device._WriteFileWithPush('/sdcard/tmp/on.device', contents),
1238 self.call.device.RunShellCommand(
1239 ['cp', '/sdcard/tmp/on.device', '/path/to/device/file'],
1240 as_root=True, check_return=True)):
1241 self.device.WriteFile('/path/to/device/file', contents, as_root=True)
1216 1242
1217 def testWriteFile_withEcho(self): 1243 def testWriteFile_withEcho(self):
1218 with self.assertCall(self.call.adb.Shell( 1244 with self.assertCall(self.call.adb.Shell(
1219 "echo -n the.contents > /test/file/to.write"), ''): 1245 "echo -n the.contents > /test/file/to.write"), ''):
1220 self.device.WriteFile('/test/file/to.write', 'the.contents') 1246 self.device.WriteFile('/test/file/to.write', 'the.contents')
1221 1247
1222 def testWriteFile_withEchoAndQuotes(self): 1248 def testWriteFile_withEchoAndQuotes(self):
1223 with self.assertCall(self.call.adb.Shell( 1249 with self.assertCall(self.call.adb.Shell(
1224 "echo -n 'the contents' > '/test/file/to write'"), ''): 1250 "echo -n 'the contents' > '/test/file/to write'"), ''):
1225 self.device.WriteFile('/test/file/to write', 'the contents') 1251 self.device.WriteFile('/test/file/to write', 'the contents')
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
1529 def testStr_returnsSerial(self): 1555 def testStr_returnsSerial(self):
1530 with self.assertCalls( 1556 with self.assertCalls(
1531 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): 1557 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')):
1532 self.assertEqual('0123456789abcdef', str(self.device)) 1558 self.assertEqual('0123456789abcdef', str(self.device))
1533 1559
1534 1560
1535 if __name__ == '__main__': 1561 if __name__ == '__main__':
1536 logging.getLogger().setLevel(logging.DEBUG) 1562 logging.getLogger().setLevel(logging.DEBUG)
1537 unittest.main(verbosity=2) 1563 unittest.main(verbosity=2)
1538 1564
OLDNEW
« no previous file with comments | « build/android/pylib/device/device_utils.py ('k') | build/android/pylib/utils/device_temp_file.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698