Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """ | |
| 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | |
| 8 The test will invoke real devices | |
|
jbudorick
2015/06/08 18:34:29
Maybe "device_utils_device_test.py"
?
Menglin
2015/06/09 22:11:45
Done.
| |
| 9 """ | |
| 10 | |
| 11 import os | |
| 12 import tempfile | |
| 13 import unittest | |
| 14 | |
| 15 from pylib import cmd_helper | |
| 16 from pylib import constants | |
| 17 from pylib.device import adb_wrapper | |
| 18 from pylib.device import device_utils | |
| 19 from pylib.utils import md5sum | |
| 20 | |
| 21 _old_contents = "foo" | |
| 22 _new_contents = "bar" | |
| 23 _device_dir = "/data/local/tmp/device_utils_test" | |
| 24 _sub_dir = "sub" | |
| 25 _sub_dir1 = "sub1" | |
| 26 _sub_dir2 = "sub2" | |
| 27 | |
| 28 class DeviceUtilsPushDeleteFilesTest(unittest.TestCase): | |
| 29 | |
| 30 def setUp(self): | |
| 31 devices = adb_wrapper.AdbWrapper.Devices() | |
| 32 assert devices, 'A device must be attached' | |
| 33 self.adb = devices[0] | |
| 34 self.adb.WaitForDevice() | |
| 35 self.device = device_utils.DeviceUtils( | |
| 36 self.adb, default_timeout=10, default_retries=0) | |
| 37 default_build_type = os.environ.get('BUILDTYPE', 'Debug') | |
| 38 constants.SetBuildType(default_build_type) | |
| 39 | |
| 40 @staticmethod | |
| 41 def _MakeTempFile(contents): | |
| 42 """Make a temporary file with the given contents. | |
| 43 | |
| 44 Args: | |
| 45 contents: string to write to the temporary file. | |
| 46 | |
| 47 Returns: | |
| 48 the tuple contains the absolute path to the file and the file name | |
| 49 """ | |
| 50 fi, path = tempfile.mkstemp(text=True) | |
| 51 with os.fdopen(fi, 'w') as f: | |
| 52 f.write(contents) | |
| 53 file_name = os.path.basename(path) | |
| 54 return (path, file_name) | |
| 55 | |
| 56 @staticmethod | |
| 57 def _MakeTempFileGivenDir(directory, contents): | |
| 58 """Make a temporary file under the given directory | |
| 59 with the given contents | |
| 60 | |
| 61 Args: | |
| 62 directory: the temp directory to create the file | |
| 63 contents: string to write to the temp file | |
| 64 | |
| 65 Returns: | |
| 66 the list contains the absolute path to the file and the file name | |
| 67 """ | |
| 68 fi, path = tempfile.mkstemp(dir=directory, text=True) | |
| 69 with os.fdopen(fi, 'w') as f: | |
| 70 f.write(contents) | |
| 71 file_name = os.path.basename(path) | |
| 72 return (path, file_name) | |
| 73 | |
| 74 @staticmethod | |
| 75 def _ChangeTempFile(path, contents): | |
| 76 with os.open(path, 'w') as f: | |
| 77 f.write(contents) | |
| 78 | |
| 79 @staticmethod | |
| 80 def _DeleteTempFile(path): | |
| 81 os.remove(path) | |
| 82 | |
| 83 def testPushChangedFiles_noChange(self): | |
| 84 (host_file_path, file_name) = self._MakeTempFile(_old_contents) | |
| 85 device_file_path = "%s/%s" % (_device_dir, file_name) | |
| 86 self.adb.Push(host_file_path, device_file_path) | |
| 87 self.device.PushChangedFiles([(host_file_path, device_file_path)]) | |
| 88 result = self.device.RunShellCommand(['cat', device_file_path], | |
| 89 single_line=True) | |
| 90 self.assertEqual(_old_contents, result) | |
| 91 | |
| 92 cmd_helper.RunCmd(['rm', host_file_path]) | |
| 93 self.device.RunShellCommand(['rm', '-rf', _device_dir]) | |
| 94 | |
| 95 def testPushChangedFiles_singleFileChange(self): | |
| 96 (host_file_path, file_name) = self._MakeTempFile(_old_contents) | |
| 97 device_file_path = "%s/%s" % (_device_dir, file_name) | |
| 98 self.adb.Push(host_file_path, device_file_path) | |
| 99 | |
| 100 with open(host_file_path, 'w') as f: | |
| 101 f.write(_new_contents) | |
| 102 self.device.PushChangedFiles([(host_file_path, device_file_path)]) | |
| 103 result = self.device.RunShellCommand(['cat', device_file_path], | |
| 104 single_line=True) | |
| 105 self.assertEqual(_new_contents, result) | |
| 106 | |
| 107 cmd_helper.RunCmd(['rm', host_file_path]) | |
| 108 self.device.RunShellCommand(['rm', '-rf', _device_dir]) | |
| 109 | |
| 110 def testPushAndDeleteFiles_noPush(self): | |
| 111 (host_file_path, file_name) = self._MakeTempFile(_old_contents) | |
| 112 | |
| 113 device_file_path = "%s/%s" % (_device_dir, file_name) | |
| 114 self.adb.Push(host_file_path, device_file_path) | |
| 115 | |
| 116 cmd_helper.RunCmd(['rm', host_file_path]) | |
| 117 self.device.PushAndDeleteFiles(_device_dir, []) | |
| 118 result = self.device.RunShellCommand(['ls', _device_dir], single_line=True) | |
| 119 self.assertEqual('', result) | |
| 120 self.device.RunShellCommand(['rm', '-rf', _device_dir]) | |
| 121 | |
| 122 def testPushAndDeleteFiles_noSubDir(self): | |
| 123 host_tmp_dir = tempfile.mkdtemp() | |
| 124 (host_file_path1, file_name1) = self._MakeTempFileGivenDir( | |
| 125 host_tmp_dir, _old_contents) | |
| 126 (host_file_path2, file_name2) = self._MakeTempFileGivenDir( | |
| 127 host_tmp_dir, _old_contents) | |
| 128 | |
| 129 device_file_path1 = "%s/%s" % (_device_dir, file_name1) | |
| 130 device_file_path2 = "%s/%s" % (_device_dir, file_name2) | |
| 131 self.adb.Push(host_file_path1, device_file_path1) | |
| 132 self.adb.Push(host_file_path2, device_file_path2) | |
| 133 | |
| 134 with open(host_file_path1, 'w') as f: | |
| 135 f.write(_new_contents) | |
| 136 cmd_helper.RunCmd(['rm', host_file_path2]) | |
| 137 | |
| 138 host_device_tuples = [(os.path.join(host_tmp_dir, p), | |
| 139 '%s/%s' % (_device_dir, p)) | |
| 140 for p in os.listdir(host_tmp_dir)] | |
| 141 self.device.PushAndDeleteFiles(_device_dir, host_device_tuples) | |
| 142 result = self.device.RunShellCommand(['cat', device_file_path1], | |
| 143 single_line=True) | |
| 144 self.assertEqual(_new_contents, result) | |
| 145 result = self.device.RunShellCommand(['ls', _device_dir], single_line=True) | |
| 146 self.assertEqual(file_name1, result) | |
| 147 | |
| 148 self.device.RunShellCommand(['rm', '-rf', _device_dir]) | |
| 149 cmd_helper.RunCmd(['rm', '-rf', host_tmp_dir]) | |
| 150 | |
| 151 def testPushAndDeleteFiles_SubDir(self): | |
| 152 host_tmp_dir = tempfile.mkdtemp() | |
| 153 host_sub_dir1 = "%s/%s" % (host_tmp_dir, _sub_dir1) | |
| 154 host_sub_dir2 = "%s/%s/%s" % (host_tmp_dir, _sub_dir, _sub_dir2) | |
| 155 cmd_helper.RunCmd(['mkdir', '-p', host_sub_dir1]) | |
| 156 cmd_helper.RunCmd(['mkdir', '-p', host_sub_dir2]) | |
| 157 | |
| 158 (host_file_path1, file_name1) = self._MakeTempFileGivenDir( | |
| 159 host_tmp_dir, _old_contents) | |
| 160 (host_file_path2, _) = self._MakeTempFileGivenDir( | |
| 161 host_tmp_dir, _old_contents) | |
| 162 (_, file_name3) = self._MakeTempFileGivenDir(host_sub_dir1, _old_contents) | |
| 163 (host_file_path4, _) = self._MakeTempFileGivenDir( | |
| 164 host_sub_dir2, _old_contents) | |
| 165 | |
| 166 host_device_tuples = [(os.path.join(host_tmp_dir, p), | |
| 167 '%s/%s' % (_device_dir, p)) | |
| 168 for p in os.listdir(host_tmp_dir)] | |
| 169 self.adb.Push(host_device_tuples[0][0], host_device_tuples[0][1]) | |
| 170 self.adb.Push(host_device_tuples[1][0], host_device_tuples[1][1]) | |
| 171 self.adb.Push(host_device_tuples[2][0], host_device_tuples[2][1]) | |
| 172 self.adb.Push(host_device_tuples[3][0], host_device_tuples[3][1]) | |
| 173 | |
| 174 with open(host_file_path1, 'w') as f: | |
| 175 f.write(_new_contents) | |
| 176 cmd_helper.RunCmd(['rm', host_file_path2]) | |
| 177 cmd_helper.RunCmd(['rm', host_file_path4]) | |
| 178 | |
| 179 host_device_tuples = [(os.path.join(host_tmp_dir, p), | |
| 180 '%s/%s' % (_device_dir, p)) | |
| 181 for p in os.listdir(host_tmp_dir)] | |
| 182 self.device.PushAndDeleteFiles(_device_dir, host_device_tuples) | |
| 183 result = self.device.RunShellCommand(['cat', "%s/%s" | |
| 184 % (_device_dir, file_name1)], | |
| 185 single_line=True) | |
| 186 self.assertEqual(_new_contents, result) | |
| 187 | |
| 188 result = self.device.RunShellCommand(['ls', _device_dir]) | |
| 189 self.assertIn(file_name1, result) | |
| 190 self.assertIn(_sub_dir1, result) | |
| 191 self.assertIn(_sub_dir, result) | |
| 192 self.assertEqual(3, len(result)) | |
| 193 | |
| 194 result = self.device.RunShellCommand(['cat', "%s/%s/%s" | |
| 195 % (_device_dir, _sub_dir1, file_name3)], | |
| 196 single_line=True) | |
| 197 self.assertEqual(_old_contents, result) | |
| 198 | |
| 199 result = self.device.RunShellCommand(["ls", "%s/%s/%s" | |
| 200 % (_device_dir, _sub_dir, _sub_dir2)], | |
| 201 single_line=True) | |
| 202 self.assertEqual('', result) | |
| 203 | |
| 204 self.device.RunShellCommand(['rm', '-rf', _device_dir]) | |
| 205 cmd_helper.RunCmd(['rm', '-rf', host_tmp_dir]) | |
| 206 | |
| 207 if __name__ == '__main__': | |
| 208 unittest.main() | |
| OLD | NEW |