blob: b91aaaee41a688f9169b73f70a80c0a2470e5e9a [file] [log] [blame]
Alex Kleinc05f3d12019-05-29 14:16:21 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""field_handler module tests."""
6
Alex Kleinc05f3d12019-05-29 14:16:21 -06007import os
8
9from chromite.api import field_handler
10from chromite.api.gen.chromite.api import build_api_test_pb2
11from chromite.api.gen.chromiumos import common_pb2
12from chromite.lib import chroot_lib
13from chromite.lib import cros_test_lib
14from chromite.lib import osutils
15
16
17class ChrootHandlerTest(cros_test_lib.TestCase):
18 """ChrootHandler tests."""
19
20 def setUp(self):
21 self.path = '/chroot/dir'
22 self.cache_dir = '/cache/dir'
Alex Klein5e4b1bc2019-07-02 12:27:06 -060023 self.chrome_dir = '/chrome/dir'
Alex Kleinb7485bb2019-09-19 13:23:37 -060024 self.env = {'FEATURES': 'thing', 'CHROME_ORIGIN': 'LOCAL_SOURCE'}
Alex Klein5e4b1bc2019-07-02 12:27:06 -060025 self.expected_chroot = chroot_lib.Chroot(path=self.path,
26 cache_dir=self.cache_dir,
27 chrome_root=self.chrome_dir,
28 env=self.env)
Alex Kleinc05f3d12019-05-29 14:16:21 -060029
30 def test_parse_chroot_success(self):
31 """Test successful Chroot message parse."""
32 chroot_msg = common_pb2.Chroot()
33 chroot_msg.path = self.path
34 chroot_msg.cache_dir = self.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060035 chroot_msg.chrome_dir = self.chrome_dir
Alex Kleinc05f3d12019-05-29 14:16:21 -060036 chroot_msg.env.features.add().feature = 'thing'
37
Alex Kleinc7d647f2020-01-06 12:00:48 -070038 chroot_handler = field_handler.ChrootHandler(clear_field=False)
Alex Kleinc05f3d12019-05-29 14:16:21 -060039 parsed_chroot = chroot_handler.parse_chroot(chroot_msg)
40
41 self.assertEqual(self.expected_chroot, parsed_chroot)
42
43 def test_handle_success(self):
44 """Test a successful Chroot message parse from a parent message."""
45 message = build_api_test_pb2.TestRequestMessage()
46 message.chroot.path = self.path
47 message.chroot.cache_dir = self.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060048 message.chroot.chrome_dir = self.chrome_dir
Alex Kleinc05f3d12019-05-29 14:16:21 -060049 message.chroot.env.features.add().feature = 'thing'
50
51 # First a no-clear parse.
Alex Kleinc7d647f2020-01-06 12:00:48 -070052 chroot_handler = field_handler.ChrootHandler(clear_field=False)
Alex Kleinc05f3d12019-05-29 14:16:21 -060053 chroot = chroot_handler.handle(message)
54
55 self.assertEqual(self.expected_chroot, chroot)
56 self.assertEqual(message.chroot.path, self.path)
57
58 # A clear field parse.
Alex Kleinc7d647f2020-01-06 12:00:48 -070059 clear_chroot_handler = field_handler.ChrootHandler(clear_field=True)
Alex Kleinc05f3d12019-05-29 14:16:21 -060060 chroot = clear_chroot_handler.handle(message)
61
62 self.assertEqual(self.expected_chroot, chroot)
63 self.assertFalse(message.chroot.path)
64
65 def test_handle_empty_chroot_message(self):
66 """Test handling of an empty chroot message."""
67 message = build_api_test_pb2.TestRequestMessage()
Alex Kleinb49be8a2019-12-20 10:23:03 -070068 empty_chroot = chroot_lib.Chroot()
Alex Kleinc05f3d12019-05-29 14:16:21 -060069
Alex Kleinc7d647f2020-01-06 12:00:48 -070070 chroot_handler = field_handler.ChrootHandler(clear_field=False)
Alex Kleinc05f3d12019-05-29 14:16:21 -060071 chroot = chroot_handler.handle(message)
72
73 self.assertEqual(empty_chroot, chroot)
74
75
Alex Kleinaae49772019-07-26 10:20:50 -060076class CopyPathInTest(cros_test_lib.TempDirTestCase):
Alex Kleinc05f3d12019-05-29 14:16:21 -060077 """PathHandler tests."""
78
79 def setUp(self):
80 self.source_dir = os.path.join(self.tempdir, 'source')
81 self.dest_dir = os.path.join(self.tempdir, 'destination')
82 osutils.SafeMakedirs(self.source_dir)
83 osutils.SafeMakedirs(self.dest_dir)
84
85 self.source_file1 = os.path.join(self.source_dir, 'file1')
86 self.file1_contents = 'file 1'
87 osutils.WriteFile(self.source_file1, self.file1_contents)
88
89 self.file2_contents = 'some data'
90 self.source_file2 = os.path.join(self.source_dir, 'file2')
91 osutils.WriteFile(self.source_file2, self.file2_contents)
92
93 def _path_checks(self, source_file, dest_file, contents=None):
94 """Set of common checks for the copied files/directories."""
95 # Message should now reflect the new path.
96 self.assertNotEqual(source_file, dest_file)
97 # The new path should be in the destination directory.
98 self.assertStartsWith(dest_file, self.dest_dir)
99 # The new file should exist.
100 self.assertExists(dest_file)
101
102 if contents:
103 # The contents should be the same as the source file.
104 self.assertFileContents(dest_file, contents)
105
106 def test_handle_file(self):
107 """Test handling of a single file."""
108 message = build_api_test_pb2.TestRequestMessage()
109 message.path.path = self.source_file1
110 message.path.location = common_pb2.Path.OUTSIDE
111
Alex Kleinaae49772019-07-26 10:20:50 -0600112 with field_handler.copy_paths_in(message, self.dest_dir, delete=True):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600113 new_path = message.path.path
114 self._path_checks(self.source_file1, new_path, self.file1_contents)
115
116 # The file should have been deleted on exit with delete=True.
117 self.assertNotExists(new_path)
Alex Kleinaae49772019-07-26 10:20:50 -0600118 # The original should still exist.
119 self.assertExists(self.source_file1)
120 # The path should get reset.
Alex Kleinaa705412019-06-04 15:00:30 -0600121 self.assertEqual(message.path.path, self.source_file1)
Alex Kleinc05f3d12019-05-29 14:16:21 -0600122
123 def test_handle_files(self):
124 """Test handling of multiple files."""
125 message = build_api_test_pb2.TestRequestMessage()
126 message.path.path = self.source_file1
127 message.path.location = common_pb2.Path.OUTSIDE
128 message.another_path.path = self.source_file2
129 message.another_path.location = common_pb2.Path.OUTSIDE
130
Alex Kleinaae49772019-07-26 10:20:50 -0600131 with field_handler.copy_paths_in(message, self.dest_dir, delete=False):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600132 new_path1 = message.path.path
133 new_path2 = message.another_path.path
134
135 self._path_checks(self.source_file1, new_path1, self.file1_contents)
136 self._path_checks(self.source_file2, new_path2, self.file2_contents)
137
138 # The files should still exist with delete=False.
139 self.assertExists(new_path1)
140 self.assertExists(new_path2)
141
Alex Kleinaa705412019-06-04 15:00:30 -0600142 def test_handle_nested_file(self):
143 """Test the nested path handling."""
144 message = build_api_test_pb2.TestRequestMessage()
145 message.nested_path.path.path = self.source_file1
146 message.nested_path.path.location = common_pb2.Path.OUTSIDE
147
Alex Kleinaae49772019-07-26 10:20:50 -0600148 with field_handler.copy_paths_in(message, self.dest_dir):
Alex Kleinaa705412019-06-04 15:00:30 -0600149 new_path = message.nested_path.path.path
150 self._path_checks(self.source_file1, new_path, self.file1_contents)
151
Alex Kleinc05f3d12019-05-29 14:16:21 -0600152 def test_handle_directory(self):
153 """Test handling of a directory."""
154 message = build_api_test_pb2.TestRequestMessage()
155 message.path.path = self.source_dir
156 message.path.location = common_pb2.Path.OUTSIDE
157
Alex Kleinaae49772019-07-26 10:20:50 -0600158 with field_handler.copy_paths_in(message, self.dest_dir):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600159 new_path = message.path.path
160
161 self._path_checks(self.source_dir, self.dest_dir)
162 # Make sure both directories have the same files.
Mike Frysinger678735c2019-09-28 18:23:28 -0400163 self.assertCountEqual(os.listdir(self.source_dir), os.listdir(new_path))
Alex Kleinc05f3d12019-05-29 14:16:21 -0600164
165 def test_direction(self):
166 """Test the direction argument preventing copies."""
167 message = build_api_test_pb2.TestRequestMessage()
168 message.path.path = self.source_file1
169 message.path.location = common_pb2.Path.INSIDE
170
Alex Kleinaae49772019-07-26 10:20:50 -0600171 with field_handler.copy_paths_in(message, self.dest_dir, delete=True):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600172 self.assertEqual(self.source_file1, message.path.path)
173
174 # It should not be deleting the file when it doesn't need to copy it even
175 # with delete=True.
176 self.assertExists(self.source_file1)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600177
178 def test_prefix_inside(self):
179 """Test the transfer inside prefix handling."""
180 message = build_api_test_pb2.TestRequestMessage()
181 message.path.path = self.source_dir
182 message.path.location = common_pb2.Path.OUTSIDE
183
Alex Kleinaae49772019-07-26 10:20:50 -0600184 with field_handler.copy_paths_in(message, self.dest_dir,
185 prefix=self.tempdir):
Alex Kleinbd6edf82019-07-18 10:30:49 -0600186 new_path = message.path.path
187 # The prefix should be removed.
188 self.assertFalse(new_path.startswith(self.tempdir))
189
Alex Kleinbd6edf82019-07-18 10:30:49 -0600190
Alex Kleinf0717a62019-12-06 09:45:00 -0700191class SyncDirsTest(cros_test_lib.TempDirTestCase):
192 """Tests for sync_dirs."""
193
194 def setUp(self):
195 D = cros_test_lib.Directory
196 filesystem = (
197 D('chroot', (
198 D('tmp', (
199 D('tempdir', ()),
200 )),
201 )),
202 D('sources', (
203 D('single_file', ('single_file.txt',)),
204 D('nested_directories', (
205 'basedir_file.log',
206 D('nested1', (
207 'nested1.txt',
208 D('nested2', ('nested2.txt',)),
209 )),
210 )),
211 )),
212 )
213 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem)
214
215 self.chroot = os.path.join(self.tempdir, 'chroot')
216 self.chroot_tmp = os.path.join(self.chroot, 'tmp')
217 self.destination = os.path.join(self.chroot_tmp, 'tempdir')
218 self.inside_path = '/tmp/tempdir'
219
220 self.single_file_src = os.path.join(self.tempdir, 'sources', 'single_file')
221 self.sf_src_file = os.path.join(self.single_file_src, 'single_file.txt')
222 self.sf_dest_file = os.path.join(self.destination, 'single_file.txt')
223
224 self.nested_dirs_src = (
225 os.path.join(self.tempdir, 'sources', 'nested_directories'))
226 self.nested_src_files = (
227 os.path.join(self.nested_dirs_src, 'basedir_file.log'),
228 os.path.join(self.nested_dirs_src, 'nested1', 'nested1.txt'),
229 os.path.join(self.nested_dirs_src, 'nested1', 'nested2', 'nested2.txt'),
230 )
231 self.nested_dest_files = (
232 os.path.join(self.destination, 'basedir_file.log'),
233 os.path.join(self.destination, 'nested1', 'nested1.txt'),
234 os.path.join(self.destination, 'nested1', 'nested2', 'nested2.txt'),
235 )
236
237 self.message = build_api_test_pb2.TestRequestMessage()
238
239 def _assertExist(self, files):
240 for f in files:
241 self.assertExists(f)
242
243 def _assertNotExist(self, files):
244 for f in files:
245 self.assertNotExists(f)
246
247 def testSingleFileTransfer(self):
248 """Single source file syncs."""
249 self.message.synced_dir.dir = self.single_file_src
250
251 # Verify source files exist and destination files do not.
252 self.assertExists(self.sf_src_file)
253 self.assertNotExists(self.sf_dest_file)
254
255 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
256 # Verify the prefix is getting correctly stripped.
257 self.assertEqual(self.message.synced_dir.dir, self.inside_path)
258 # Verify the files have all been correctly copied in.
259 self.assertExists(self.sf_dest_file)
260
261 self.assertEqual(self.message.synced_dir.dir, self.single_file_src)
262 # Verify the files have all been copied out.
263 self.assertExists(self.sf_src_file)
264
265 def testNestedFileSync(self):
266 """Nested directories and files sync."""
267 self.message.synced_dir.dir = self.nested_dirs_src
268
269 self._assertExist(self.nested_src_files)
270 self._assertNotExist(self.nested_dest_files)
271
272 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
273 self.assertEqual(self.message.synced_dir.dir, self.inside_path)
274 self._assertExist(self.nested_dest_files)
275
276 self.assertEqual(self.message.synced_dir.dir, self.nested_dirs_src)
277 self._assertExist(self.nested_src_files)
278
279 def testDeletion(self):
280 """Test file deletions are exported correctly."""
281 self.message.synced_dir.dir = self.nested_dirs_src
282
283 deleted_src = os.path.join(self.nested_dirs_src, 'nested1', 'nested1.txt')
284 deleted_dest = os.path.join(self.destination, 'nested1', 'nested1.txt')
285
286 self._assertExist(self.nested_src_files)
287 self._assertNotExist(self.nested_dest_files)
288
289 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
290 self._assertExist(self.nested_dest_files)
291 osutils.SafeUnlink(deleted_dest)
292
293 self._assertExist(set(self.nested_src_files) - {deleted_src})
294 self.assertNotExists(deleted_src)
295
296 def testCreation(self):
297 """Test file creations are exported correctly."""
298 self.message.synced_dir.dir = self.nested_dirs_src
299
300 new_src = os.path.join(self.nested_dirs_src, 'new_dir', 'new_file')
301 new_dest = os.path.join(self.destination, 'new_dir', 'new_file')
302
303 self._assertExist(self.nested_src_files)
304 self._assertNotExist(self.nested_dest_files)
305
306 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
307 self._assertExist(self.nested_dest_files)
308 osutils.Touch(new_dest, makedirs=True)
309
310 self._assertExist(self.nested_src_files)
311 self.assertExists(new_src)
312
313 def testModification(self):
314 """Test file modifications are exported correctly."""
315 self.message.synced_dir.dir = self.single_file_src
316
317 self.assertExists(self.sf_src_file)
318 self.assertNotExists(self.sf_dest_file)
319
320 self.assertEqual('', osutils.ReadFile(self.sf_src_file))
321 file_content = 'Content!'
322
323 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
324 self.assertExists(self.sf_dest_file)
325 osutils.WriteFile(self.sf_dest_file, file_content)
326
327 self.assertExists(self.sf_src_file)
328 self.assertEqual(file_content, osutils.ReadFile(self.sf_src_file))
329
330
Alex Kleinaae49772019-07-26 10:20:50 -0600331class ExtractResultsTest(cros_test_lib.TempDirTestCase):
332 """Tests for extract_results."""
Alex Kleinbd6edf82019-07-18 10:30:49 -0600333
334 def setUp(self):
335 # Setup the directories.
336 self.chroot_dir = os.path.join(self.tempdir, 'chroot')
337 self.source_dir = '/source'
338 self.chroot_source = os.path.join(self.chroot_dir,
339 self.source_dir.lstrip(os.sep))
340 self.source_dir2 = '/source2'
341 self.chroot_source2 = os.path.join(self.chroot_dir,
342 self.source_dir2.lstrip(os.sep))
343 self.dest_dir = os.path.join(self.tempdir, 'destination')
344 osutils.SafeMakedirs(self.chroot_source)
345 osutils.SafeMakedirs(self.chroot_source2)
346 osutils.SafeMakedirs(self.dest_dir)
347
348 # Two files in the same directory inside the chroot.
349 self.source_file1 = os.path.join(self.chroot_source, 'file1')
350 self.source_file1_inside = os.path.join(self.source_dir, 'file1')
351 self.file1_contents = 'file 1'
352 osutils.WriteFile(self.source_file1, self.file1_contents)
353
354 self.file2_contents = 'some data'
355 self.source_file2 = os.path.join(self.chroot_source, 'file2')
356 self.source_file2_inside = os.path.join(self.source_dir, 'file2')
357 osutils.WriteFile(self.source_file2, self.file2_contents)
358
359 # Third file in a different location.
360 self.file3_contents = 'another file'
361 self.source_file3 = os.path.join(self.chroot_source2, 'file3')
362 self.source_file3_inside = os.path.join(self.source_dir2, 'file3')
363 osutils.WriteFile(self.source_file3, self.file3_contents)
364
365 self.request = build_api_test_pb2.TestRequestMessage()
366 self.request.result_path.path.path = self.dest_dir
367 self.request.result_path.path.location = common_pb2.Path.OUTSIDE
368 self.response = build_api_test_pb2.TestResultMessage()
369 self.chroot = chroot_lib.Chroot(path=self.chroot_dir)
370
371 def _path_checks(self, path, destination, contents=None):
372 self.assertTrue(path)
373 self.assertStartsWith(path, destination)
374 self.assertExists(path)
375 if contents:
376 self.assertFileContents(path, contents)
377
378 def test_single_file(self):
Alex Klein3164d132020-11-19 15:11:31 -0700379 """Test a single file.
380
381 Verify:
382 /path/to/chroot/file -> /path/to/destination/file
383 """
Alex Kleinbd6edf82019-07-18 10:30:49 -0600384 self.response.artifact.path = self.source_file1_inside
385 self.response.artifact.location = common_pb2.Path.INSIDE
386
Alex Kleinaae49772019-07-26 10:20:50 -0600387 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600388
389 self._path_checks(self.response.artifact.path, self.dest_dir,
390 contents=self.file1_contents)
391
392 def test_single_directory(self):
Alex Klein3164d132020-11-19 15:11:31 -0700393 """Test a single directory.
394
395 Verify:
396 /path/to/chroot/directory/* -> /path/to/destination/directory/*
397 """
Alex Kleinbd6edf82019-07-18 10:30:49 -0600398 self.response.artifact.path = self.source_dir
399 self.response.artifact.location = common_pb2.Path.INSIDE
400
Alex Kleinaae49772019-07-26 10:20:50 -0600401 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600402
403 self._path_checks(self.response.artifact.path, self.dest_dir)
Mike Frysinger678735c2019-09-28 18:23:28 -0400404 self.assertCountEqual(os.listdir(self.chroot_source),
Alex Kleinbd6edf82019-07-18 10:30:49 -0600405 os.listdir(self.response.artifact.path))
406
407 def test_multiple_files(self):
Alex Klein3164d132020-11-19 15:11:31 -0700408 """Test multiple files.
409
410 Verify:
411 /path/to/chroot/some/path/file1 -> /path/to/destination/file1
412 /path/to/chroot/different/path/file2 -> /path/to/destination/file2
413 etc.
414 """
Alex Kleinbd6edf82019-07-18 10:30:49 -0600415 self.response.artifact.path = self.source_file1_inside
416 self.response.artifact.location = common_pb2.Path.INSIDE
417 self.response.nested_artifact.path.path = self.source_file2_inside
418 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
419
420 artifact3 = self.response.artifacts.add()
421 artifact3.path = self.source_file3_inside
422 artifact3.location = common_pb2.Path.INSIDE
423
Alex Kleinaae49772019-07-26 10:20:50 -0600424 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600425
426 self._path_checks(self.response.artifact.path, self.dest_dir,
427 contents=self.file1_contents)
428 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir,
429 contents=self.file2_contents)
430
431 self.assertEqual(1, len(self.response.artifacts))
432 for artifact in self.response.artifacts:
433 self._path_checks(artifact.path, self.dest_dir,
434 contents=self.file3_contents)
435
436 def test_multiple_directories(self):
Alex Klein3164d132020-11-19 15:11:31 -0700437 """Test multiple directories.
438
439 Verify:
440 /path/to/chroot/some/directory -> /path/to/destination/directory
441 /path/to/chroot/another/directory2 -> /path/to/destination/directory2
442 etc.
443 """
Alex Kleinbd6edf82019-07-18 10:30:49 -0600444 self.response.artifact.path = self.source_dir
445 self.response.artifact.location = common_pb2.Path.INSIDE
446 self.response.nested_artifact.path.path = self.source_dir2
447 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
448
Alex Kleinaae49772019-07-26 10:20:50 -0600449 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600450
451 self._path_checks(self.response.artifact.path, self.dest_dir)
452 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir)
453
454 expected = os.listdir(self.chroot_source)
455 expected.extend(os.listdir(self.chroot_source2))
Mike Frysinger678735c2019-09-28 18:23:28 -0400456 self.assertCountEqual(expected, os.listdir(self.response.artifact.path))