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

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

Issue 646523002: [Android] Add zip pushing and refine push mode selection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix findbugs issues. Created 6 years, 2 months 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
« no previous file with comments | « build/android/pylib/device/device_utils.py ('k') | build/android/pylib/forwarder.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 def osStatResult( 78 def osStatResult(
79 st_mode=None, st_ino=None, st_dev=None, st_nlink=None, st_uid=None, 79 st_mode=None, st_ino=None, st_dev=None, st_nlink=None, st_uid=None,
80 st_gid=None, st_size=None, st_atime=None, st_mtime=None, st_ctime=None): 80 st_gid=None, st_size=None, st_atime=None, st_mtime=None, st_ctime=None):
81 MockOSStatResult = collections.namedtuple('MockOSStatResult', [ 81 MockOSStatResult = collections.namedtuple('MockOSStatResult', [
82 'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', 82 'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid',
83 'st_size', 'st_atime', 'st_mtime', 'st_ctime']) 83 'st_size', 'st_atime', 'st_mtime', 'st_ctime'])
84 return MockOSStatResult(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, 84 return MockOSStatResult(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid,
85 st_size, st_atime, st_mtime, st_ctime) 85 st_size, st_atime, st_mtime, st_ctime)
86 86
87 MOCKED_FUNCTIONS = [ 87 MOCKED_FUNCTIONS = [
88 ('os.listdir', []),
88 ('os.path.abspath', ''), 89 ('os.path.abspath', ''),
89 ('os.path.dirname', ''), 90 ('os.path.dirname', ''),
90 ('os.path.exists', False), 91 ('os.path.exists', False),
91 ('os.path.getsize', 0), 92 ('os.path.getsize', 0),
92 ('os.path.isdir', False), 93 ('os.path.isdir', False),
93 ('os.stat', osStatResult.__func__()), 94 ('os.stat', osStatResult.__func__()),
94 ('os.walk', []), 95 ('os.walk', []),
95 ] 96 ]
96 97
97 def _get(self, mocked, path, default_val): 98 def _get(self, mocked, path, default_val):
(...skipping 12 matching lines...) Expand all
110 self._patched_functions = [ 111 self._patched_functions = [
111 self._patched(m, d) for m, d in type(self).MOCKED_FUNCTIONS] 112 self._patched(m, d) for m, d in type(self).MOCKED_FUNCTIONS]
112 self._verbose = verbose 113 self._verbose = verbose
113 114
114 def addMockFile(self, path, **kw): 115 def addMockFile(self, path, **kw):
115 self._addMockThing(path, False, **kw) 116 self._addMockThing(path, False, **kw)
116 117
117 def addMockDirectory(self, path, **kw): 118 def addMockDirectory(self, path, **kw):
118 self._addMockThing(path, True, **kw) 119 self._addMockThing(path, True, **kw)
119 120
120 def _addMockThing(self, path, is_dir, size=0, stat=None, walk=None): 121 def _addMockThing(self, path, is_dir, listdir=None, size=0, stat=None,
122 walk=None):
123 if listdir is None:
124 listdir = []
121 if stat is None: 125 if stat is None:
122 stat = self.osStatResult() 126 stat = self.osStatResult()
123 if walk is None: 127 if walk is None:
124 walk = [] 128 walk = []
129
130 dirname = os.sep.join(path.rstrip(os.sep).split(os.sep)[:-1])
131 if dirname and not dirname in self.mock_file_info:
132 self._addMockThing(dirname, True)
133
125 self.mock_file_info[path] = { 134 self.mock_file_info[path] = {
135 'os.listdir': listdir,
126 'os.path.abspath': path, 136 'os.path.abspath': path,
127 'os.path.dirname': '/' + '/'.join(path.strip('/').split('/')[:-1]), 137 'os.path.dirname': dirname,
128 'os.path.exists': True, 138 'os.path.exists': True,
129 'os.path.isdir': is_dir, 139 'os.path.isdir': is_dir,
130 'os.path.getsize': size, 140 'os.path.getsize': size,
131 'os.stat': stat, 141 'os.stat': stat,
132 'os.walk': walk, 142 'os.walk': walk,
133 } 143 }
134 144
135 def __enter__(self): 145 def __enter__(self):
136 for p in self._patched_functions: 146 for p in self._patched_functions:
137 p.mocked = p.patched.__enter__() 147 p.mocked = p.patched.__enter__()
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) 211 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp)
202 212
203 def assertCallsSequence(self, cmd_ret, comp=str.__eq__): 213 def assertCallsSequence(self, cmd_ret, comp=str.__eq__):
204 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) 214 return type(self).AndroidCommandsCalls(self, cmd_ret, comp)
205 215
206 def setUp(self): 216 def setUp(self):
207 self.device = device_utils.DeviceUtils( 217 self.device = device_utils.DeviceUtils(
208 '0123456789abcdef', default_timeout=1, default_retries=0) 218 '0123456789abcdef', default_timeout=1, default_retries=0)
209 219
210 220
221 class DeviceUtilsNewImplTest(unittest.TestCase):
222
223 def setUp(self):
224 test_serial = '0123456789abcdef'
225 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper)
226 self.adb.__str__ = mock.Mock(return_value=test_serial)
227 self.adb.GetDeviceSerial.return_value = test_serial
228 self.device = device_utils.DeviceUtils(
229 self.adb, default_timeout=1, default_retries=0)
230
231
232 class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest):
233
234 def setUp(self):
235 super(DeviceUtilsHybridImplTest, self).setUp()
236 self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper)
237
238
211 class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest): 239 class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest):
212 240
213 def testIsOnline_true(self): 241 def testIsOnline_true(self):
214 with self.assertCalls('adb -s 0123456789abcdef devices', 242 with self.assertCalls('adb -s 0123456789abcdef devices',
215 '00123456789abcdef device\r\n'): 243 '00123456789abcdef device\r\n'):
216 self.assertTrue(self.device.IsOnline()) 244 self.assertTrue(self.device.IsOnline())
217 245
218 def testIsOnline_false(self): 246 def testIsOnline_false(self):
219 with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'): 247 with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'):
220 self.assertFalse(self.device.IsOnline()) 248 self.assertFalse(self.device.IsOnline())
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 864
837 class DeviceUtilsSendKeyEventTest(DeviceUtilsOldImplTest): 865 class DeviceUtilsSendKeyEventTest(DeviceUtilsOldImplTest):
838 866
839 def testSendKeyEvent(self): 867 def testSendKeyEvent(self):
840 with self.assertCalls( 868 with self.assertCalls(
841 "adb -s 0123456789abcdef shell 'input keyevent 66'", 869 "adb -s 0123456789abcdef shell 'input keyevent 66'",
842 ''): 870 ''):
843 self.device.SendKeyEvent(66) 871 self.device.SendKeyEvent(66)
844 872
845 873
846 class DeviceUtilsPushChangedFilesTest(DeviceUtilsOldImplTest): 874 class DeviceUtilsPushChangedFilesIndividuallyTest(DeviceUtilsNewImplTest):
875
876 def testPushChangedFilesIndividually_empty(self):
877 test_files = []
878 self.device._PushChangedFilesIndividually(test_files)
879 self.assertEqual(0, self.adb.Push.call_count)
880
881 def testPushChangedFilesIndividually_single(self):
882 test_files = [('/test/host/path', '/test/device/path')]
883 self.device._PushChangedFilesIndividually(test_files)
884 self.adb.Push.assert_called_once_with(
885 '/test/host/path', '/test/device/path')
886
887 def testPushChangedFilesIndividually_multiple(self):
888 test_files = [
889 ('/test/host/path/file1', '/test/device/path/file1'),
890 ('/test/host/path/file2', '/test/device/path/file2')]
891 self.device._PushChangedFilesIndividually(test_files)
892 self.assertEqual(2, self.adb.Push.call_count)
893 self.adb.Push.assert_any_call(
894 '/test/host/path/file1', '/test/device/path/file1')
895 self.adb.Push.assert_any_call(
896 '/test/host/path/file2', '/test/device/path/file2')
847 897
848 898
849 def testPushChangedFiles_noHostPath(self): 899 class DeviceUtilsPushChangedFilesZippedTest(DeviceUtilsHybridImplTest):
850 with mock.patch('os.path.exists', return_value=False):
851 with self.assertRaises(device_errors.CommandFailedError):
852 self.device.PushChangedFiles('/test/host/path', '/test/device/path')
853 900
854 def testPushChangedFiles_file_noChange(self): 901 def setUp(self):
855 self.device.old_interface._push_if_needed_cache = {} 902 super(DeviceUtilsPushChangedFilesZippedTest, self).setUp()
903 self.original_install_commands = self.device._InstallCommands
904 self.device._InstallCommands = mock.Mock()
856 905
857 host_file_path = '/test/host/path' 906 def testPushChangedFilesZipped_empty(self):
858 device_file_path = '/test/device/path' 907 test_files = []
908 self.device._PushChangedFilesZipped(test_files)
909 self.assertEqual(0, self.adb.Push.call_count)
859 910
860 mock_fs = MockFileSystem() 911 def testPushChangedFilesZipped_single(self):
861 mock_fs.addMockFile(host_file_path, size=100) 912 test_files = [('/test/host/path/file1', '/test/device/path/file1')]
862 913
863 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) 914 self.device._GetExternalStoragePathImpl = mock.Mock(
915 return_value='/test/device/external_dir')
916 self.device._IsOnlineImpl = mock.Mock(return_value=True)
917 self.device._RunShellCommandImpl = mock.Mock()
918 mock_zip_temp = mock.mock_open()
919 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip'
920 with mock.patch('multiprocessing.Process') as mock_zip_proc, (
921 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)):
922 self.device._PushChangedFilesZipped(test_files)
864 923
865 with mock_fs: 924 mock_zip_proc.assert_called_once_with(
866 # GetFilesChanged is mocked, so its adb calls are omitted. 925 target=device_utils.DeviceUtils._CreateDeviceZip,
867 with self.assertNoAdbCalls(): 926 args=('/test/temp/file/tmp.zip', test_files))
868 self.device.PushChangedFiles(host_file_path, device_file_path) 927 self.adb.Push.assert_called_once_with(
928 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip')
929 self.assertEqual(2, self.device._RunShellCommandImpl.call_count)
930 self.device._RunShellCommandImpl.assert_any_call(
931 ['unzip', '/test/device/external_dir/tmp.zip'],
932 as_root=True, check_return=True,
933 env={'PATH': '$PATH:/data/local/tmp/bin'})
934 self.device._RunShellCommandImpl.assert_any_call(
935 ['rm', '/test/device/external_dir/tmp.zip'])
869 936
870 def testPushChangedFiles_file_changed(self): 937 def testPushChangedFilesZipped_multiple(self):
871 self.device.old_interface._push_if_needed_cache = {} 938 test_files = [('/test/host/path/file1', '/test/device/path/file1'),
939 ('/test/host/path/file2', '/test/device/path/file2')]
872 940
873 host_file_path = '/test/host/path' 941 self.device._GetExternalStoragePathImpl = mock.Mock(
874 device_file_path = '/test/device/path' 942 return_value='/test/device/external_dir')
943 self.device._IsOnlineImpl = mock.Mock(return_value=True)
944 self.device._RunShellCommandImpl = mock.Mock()
945 mock_zip_temp = mock.mock_open()
946 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip'
947 with mock.patch('multiprocessing.Process') as mock_zip_proc, (
948 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)):
949 self.device._PushChangedFilesZipped(test_files)
875 950
876 mock_fs = MockFileSystem() 951 mock_zip_proc.assert_called_once_with(
877 mock_fs.addMockFile( 952 target=device_utils.DeviceUtils._CreateDeviceZip,
878 host_file_path, size=100, 953 args=('/test/temp/file/tmp.zip', test_files))
879 stat=MockFileSystem.osStatResult(st_mtime=1000000000)) 954 self.adb.Push.assert_called_once_with(
880 955 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip')
881 self.device.old_interface.GetFilesChanged = mock.Mock( 956 self.assertEqual(2, self.device._RunShellCommandImpl.call_count)
882 return_value=[('/test/host/path', '/test/device/path')]) 957 self.device._RunShellCommandImpl.assert_any_call(
883 958 ['unzip', '/test/device/external_dir/tmp.zip'],
884 with mock_fs: 959 as_root=True, check_return=True,
885 with self.assertCalls('adb -s 0123456789abcdef push ' 960 env={'PATH': '$PATH:/data/local/tmp/bin'})
886 '/test/host/path /test/device/path', '100 B/s (100 B in 1.000s)\r\n'): 961 self.device._RunShellCommandImpl.assert_any_call(
887 self.device.PushChangedFiles(host_file_path, device_file_path) 962 ['rm', '/test/device/external_dir/tmp.zip'])
888
889 def testPushChangedFiles_directory_nothingChanged(self):
890 self.device.old_interface._push_if_needed_cache = {}
891
892 host_file_path = '/test/host/path'
893 device_file_path = '/test/device/path'
894
895 mock_fs = MockFileSystem()
896 mock_fs.addMockDirectory(
897 host_file_path, size=256,
898 stat=MockFileSystem.osStatResult(st_mtime=1000000000))
899 mock_fs.addMockFile(
900 host_file_path + '/file1', size=251,
901 stat=MockFileSystem.osStatResult(st_mtime=1000000001))
902 mock_fs.addMockFile(
903 host_file_path + '/file2', size=252,
904 stat=MockFileSystem.osStatResult(st_mtime=1000000002))
905
906 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[])
907
908 with mock_fs:
909 with self.assertCallsSequence([
910 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'",
911 '')]):
912 self.device.PushChangedFiles(host_file_path, device_file_path)
913
914 def testPushChangedFiles_directory_somethingChanged(self):
915 self.device.old_interface._push_if_needed_cache = {}
916
917 host_file_path = '/test/host/path'
918 device_file_path = '/test/device/path'
919
920 mock_fs = MockFileSystem()
921 mock_fs.addMockDirectory(
922 host_file_path, size=256,
923 stat=MockFileSystem.osStatResult(st_mtime=1000000000),
924 walk=[('/test/host/path', [], ['file1', 'file2'])])
925 mock_fs.addMockFile(
926 host_file_path + '/file1', size=256,
927 stat=MockFileSystem.osStatResult(st_mtime=1000000001))
928 mock_fs.addMockFile(
929 host_file_path + '/file2', size=256,
930 stat=MockFileSystem.osStatResult(st_mtime=1000000002))
931
932 self.device.old_interface.GetFilesChanged = mock.Mock(
933 return_value=[('/test/host/path/file1', '/test/device/path/file1')])
934
935 with mock_fs:
936 with self.assertCallsSequence([
937 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'",
938 ''),
939 ('adb -s 0123456789abcdef push '
940 '/test/host/path/file1 /test/device/path/file1',
941 '256 B/s (256 B in 1.000s)\r\n')]):
942 self.device.PushChangedFiles(host_file_path, device_file_path)
943
944 def testPushChangedFiles_directory_everythingChanged(self):
945 self.device.old_interface._push_if_needed_cache = {}
946
947 host_file_path = '/test/host/path'
948 device_file_path = '/test/device/path'
949
950 mock_fs = MockFileSystem()
951 mock_fs.addMockDirectory(
952 host_file_path, size=256,
953 stat=MockFileSystem.osStatResult(st_mtime=1000000000))
954 mock_fs.addMockFile(
955 host_file_path + '/file1', size=256,
956 stat=MockFileSystem.osStatResult(st_mtime=1000000001))
957 mock_fs.addMockFile(
958 host_file_path + '/file2', size=256,
959 stat=MockFileSystem.osStatResult(st_mtime=1000000002))
960
961 self.device.old_interface.GetFilesChanged = mock.Mock(
962 return_value=[('/test/host/path/file1', '/test/device/path/file1'),
963 ('/test/host/path/file2', '/test/device/path/file2')])
964
965 with mock_fs:
966 with self.assertCallsSequence([
967 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'",
968 ''),
969 ('adb -s 0123456789abcdef push /test/host/path /test/device/path',
970 '768 B/s (768 B in 1.000s)\r\n')]):
971 self.device.PushChangedFiles(host_file_path, device_file_path)
972 963
973 964
974 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): 965 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest):
975 966
976 def testFileExists_usingTest_fileExists(self): 967 def testFileExists_usingTest_fileExists(self):
977 with self.assertCalls( 968 with self.assertCalls(
978 "adb -s 0123456789abcdef shell " 969 "adb -s 0123456789abcdef shell "
979 "'test -e \"/data/app/test.file.exists\"; echo $?'", 970 "'test -e \"/data/app/test.file.exists\"; echo $?'",
980 '0\r\n'): 971 '0\r\n'):
981 self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) 972 self.assertTrue(self.device.FileExists('/data/app/test.file.exists'))
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after
1520 self.device = device_utils.DeviceUtils(None) 1511 self.device = device_utils.DeviceUtils(None)
1521 with self.assertCalls('adb get-serialno', 'unknown'), ( 1512 with self.assertCalls('adb get-serialno', 'unknown'), (
1522 self.assertRaises(device_errors.NoDevicesError)): 1513 self.assertRaises(device_errors.NoDevicesError)):
1523 str(self.device) 1514 str(self.device)
1524 1515
1525 1516
1526 if __name__ == '__main__': 1517 if __name__ == '__main__':
1527 logging.getLogger().setLevel(logging.DEBUG) 1518 logging.getLogger().setLevel(logging.DEBUG)
1528 unittest.main(verbosity=2) 1519 unittest.main(verbosity=2)
1529 1520
OLDNEW
« no previous file with comments | « build/android/pylib/device/device_utils.py ('k') | build/android/pylib/forwarder.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698