blob: fd8d0973abc1e91d874ca55ab11b5ed5b8a4c703 [file] [log] [blame]
Alex Kleinc05f3d12019-05-29 14:16:21 -06001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS 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"""field_handler module tests."""
7
8from __future__ import print_function
9
10import os
Mike Frysingeref94e4c2020-02-10 23:59:54 -050011import sys
Alex Kleinc05f3d12019-05-29 14:16:21 -060012
13from chromite.api import field_handler
14from chromite.api.gen.chromite.api import build_api_test_pb2
15from chromite.api.gen.chromiumos import common_pb2
16from chromite.lib import chroot_lib
17from chromite.lib import cros_test_lib
18from chromite.lib import osutils
19
20
Mike Frysingeref94e4c2020-02-10 23:59:54 -050021assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
22
23
Alex Kleinc05f3d12019-05-29 14:16:21 -060024class ChrootHandlerTest(cros_test_lib.TestCase):
25 """ChrootHandler tests."""
26
27 def setUp(self):
28 self.path = '/chroot/dir'
29 self.cache_dir = '/cache/dir'
Alex Klein5e4b1bc2019-07-02 12:27:06 -060030 self.chrome_dir = '/chrome/dir'
Alex Kleinb7485bb2019-09-19 13:23:37 -060031 self.env = {'FEATURES': 'thing', 'CHROME_ORIGIN': 'LOCAL_SOURCE'}
Alex Klein5e4b1bc2019-07-02 12:27:06 -060032 self.expected_chroot = chroot_lib.Chroot(path=self.path,
33 cache_dir=self.cache_dir,
34 chrome_root=self.chrome_dir,
35 env=self.env)
Alex Kleinc05f3d12019-05-29 14:16:21 -060036
37 def test_parse_chroot_success(self):
38 """Test successful Chroot message parse."""
39 chroot_msg = common_pb2.Chroot()
40 chroot_msg.path = self.path
41 chroot_msg.cache_dir = self.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060042 chroot_msg.chrome_dir = self.chrome_dir
Alex Kleinc05f3d12019-05-29 14:16:21 -060043 chroot_msg.env.features.add().feature = 'thing'
44
Alex Kleinc7d647f2020-01-06 12:00:48 -070045 chroot_handler = field_handler.ChrootHandler(clear_field=False)
Alex Kleinc05f3d12019-05-29 14:16:21 -060046 parsed_chroot = chroot_handler.parse_chroot(chroot_msg)
47
48 self.assertEqual(self.expected_chroot, parsed_chroot)
49
50 def test_handle_success(self):
51 """Test a successful Chroot message parse from a parent message."""
52 message = build_api_test_pb2.TestRequestMessage()
53 message.chroot.path = self.path
54 message.chroot.cache_dir = self.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060055 message.chroot.chrome_dir = self.chrome_dir
Alex Kleinc05f3d12019-05-29 14:16:21 -060056 message.chroot.env.features.add().feature = 'thing'
57
58 # First a no-clear parse.
Alex Kleinc7d647f2020-01-06 12:00:48 -070059 chroot_handler = field_handler.ChrootHandler(clear_field=False)
Alex Kleinc05f3d12019-05-29 14:16:21 -060060 chroot = chroot_handler.handle(message)
61
62 self.assertEqual(self.expected_chroot, chroot)
63 self.assertEqual(message.chroot.path, self.path)
64
65 # A clear field parse.
Alex Kleinc7d647f2020-01-06 12:00:48 -070066 clear_chroot_handler = field_handler.ChrootHandler(clear_field=True)
Alex Kleinc05f3d12019-05-29 14:16:21 -060067 chroot = clear_chroot_handler.handle(message)
68
69 self.assertEqual(self.expected_chroot, chroot)
70 self.assertFalse(message.chroot.path)
71
72 def test_handle_empty_chroot_message(self):
73 """Test handling of an empty chroot message."""
74 message = build_api_test_pb2.TestRequestMessage()
Alex Kleinb49be8a2019-12-20 10:23:03 -070075 empty_chroot = chroot_lib.Chroot()
Alex Kleinc05f3d12019-05-29 14:16:21 -060076
Alex Kleinc7d647f2020-01-06 12:00:48 -070077 chroot_handler = field_handler.ChrootHandler(clear_field=False)
Alex Kleinc05f3d12019-05-29 14:16:21 -060078 chroot = chroot_handler.handle(message)
79
80 self.assertEqual(empty_chroot, chroot)
81
82
Alex Kleinaae49772019-07-26 10:20:50 -060083class CopyPathInTest(cros_test_lib.TempDirTestCase):
Alex Kleinc05f3d12019-05-29 14:16:21 -060084 """PathHandler tests."""
85
86 def setUp(self):
87 self.source_dir = os.path.join(self.tempdir, 'source')
88 self.dest_dir = os.path.join(self.tempdir, 'destination')
89 osutils.SafeMakedirs(self.source_dir)
90 osutils.SafeMakedirs(self.dest_dir)
91
92 self.source_file1 = os.path.join(self.source_dir, 'file1')
93 self.file1_contents = 'file 1'
94 osutils.WriteFile(self.source_file1, self.file1_contents)
95
96 self.file2_contents = 'some data'
97 self.source_file2 = os.path.join(self.source_dir, 'file2')
98 osutils.WriteFile(self.source_file2, self.file2_contents)
99
100 def _path_checks(self, source_file, dest_file, contents=None):
101 """Set of common checks for the copied files/directories."""
102 # Message should now reflect the new path.
103 self.assertNotEqual(source_file, dest_file)
104 # The new path should be in the destination directory.
105 self.assertStartsWith(dest_file, self.dest_dir)
106 # The new file should exist.
107 self.assertExists(dest_file)
108
109 if contents:
110 # The contents should be the same as the source file.
111 self.assertFileContents(dest_file, contents)
112
113 def test_handle_file(self):
114 """Test handling of a single file."""
115 message = build_api_test_pb2.TestRequestMessage()
116 message.path.path = self.source_file1
117 message.path.location = common_pb2.Path.OUTSIDE
118
Alex Kleinaae49772019-07-26 10:20:50 -0600119 with field_handler.copy_paths_in(message, self.dest_dir, delete=True):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600120 new_path = message.path.path
121 self._path_checks(self.source_file1, new_path, self.file1_contents)
122
123 # The file should have been deleted on exit with delete=True.
124 self.assertNotExists(new_path)
Alex Kleinaae49772019-07-26 10:20:50 -0600125 # The original should still exist.
126 self.assertExists(self.source_file1)
127 # The path should get reset.
Alex Kleinaa705412019-06-04 15:00:30 -0600128 self.assertEqual(message.path.path, self.source_file1)
Alex Kleinc05f3d12019-05-29 14:16:21 -0600129
130 def test_handle_files(self):
131 """Test handling of multiple files."""
132 message = build_api_test_pb2.TestRequestMessage()
133 message.path.path = self.source_file1
134 message.path.location = common_pb2.Path.OUTSIDE
135 message.another_path.path = self.source_file2
136 message.another_path.location = common_pb2.Path.OUTSIDE
137
Alex Kleinaae49772019-07-26 10:20:50 -0600138 with field_handler.copy_paths_in(message, self.dest_dir, delete=False):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600139 new_path1 = message.path.path
140 new_path2 = message.another_path.path
141
142 self._path_checks(self.source_file1, new_path1, self.file1_contents)
143 self._path_checks(self.source_file2, new_path2, self.file2_contents)
144
145 # The files should still exist with delete=False.
146 self.assertExists(new_path1)
147 self.assertExists(new_path2)
148
Alex Kleinaa705412019-06-04 15:00:30 -0600149 def test_handle_nested_file(self):
150 """Test the nested path handling."""
151 message = build_api_test_pb2.TestRequestMessage()
152 message.nested_path.path.path = self.source_file1
153 message.nested_path.path.location = common_pb2.Path.OUTSIDE
154
Alex Kleinaae49772019-07-26 10:20:50 -0600155 with field_handler.copy_paths_in(message, self.dest_dir):
Alex Kleinaa705412019-06-04 15:00:30 -0600156 new_path = message.nested_path.path.path
157 self._path_checks(self.source_file1, new_path, self.file1_contents)
158
Alex Kleinc05f3d12019-05-29 14:16:21 -0600159 def test_handle_directory(self):
160 """Test handling of a directory."""
161 message = build_api_test_pb2.TestRequestMessage()
162 message.path.path = self.source_dir
163 message.path.location = common_pb2.Path.OUTSIDE
164
Alex Kleinaae49772019-07-26 10:20:50 -0600165 with field_handler.copy_paths_in(message, self.dest_dir):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600166 new_path = message.path.path
167
168 self._path_checks(self.source_dir, self.dest_dir)
169 # Make sure both directories have the same files.
Mike Frysinger678735c2019-09-28 18:23:28 -0400170 self.assertCountEqual(os.listdir(self.source_dir), os.listdir(new_path))
Alex Kleinc05f3d12019-05-29 14:16:21 -0600171
172 def test_direction(self):
173 """Test the direction argument preventing copies."""
174 message = build_api_test_pb2.TestRequestMessage()
175 message.path.path = self.source_file1
176 message.path.location = common_pb2.Path.INSIDE
177
Alex Kleinaae49772019-07-26 10:20:50 -0600178 with field_handler.copy_paths_in(message, self.dest_dir, delete=True):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600179 self.assertEqual(self.source_file1, message.path.path)
180
181 # It should not be deleting the file when it doesn't need to copy it even
182 # with delete=True.
183 self.assertExists(self.source_file1)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600184
185 def test_prefix_inside(self):
186 """Test the transfer inside prefix handling."""
187 message = build_api_test_pb2.TestRequestMessage()
188 message.path.path = self.source_dir
189 message.path.location = common_pb2.Path.OUTSIDE
190
Alex Kleinaae49772019-07-26 10:20:50 -0600191 with field_handler.copy_paths_in(message, self.dest_dir,
192 prefix=self.tempdir):
Alex Kleinbd6edf82019-07-18 10:30:49 -0600193 new_path = message.path.path
194 # The prefix should be removed.
195 self.assertFalse(new_path.startswith(self.tempdir))
196
Alex Kleinbd6edf82019-07-18 10:30:49 -0600197
Alex Kleinf0717a62019-12-06 09:45:00 -0700198class SyncDirsTest(cros_test_lib.TempDirTestCase):
199 """Tests for sync_dirs."""
200
201 def setUp(self):
202 D = cros_test_lib.Directory
203 filesystem = (
204 D('chroot', (
205 D('tmp', (
206 D('tempdir', ()),
207 )),
208 )),
209 D('sources', (
210 D('single_file', ('single_file.txt',)),
211 D('nested_directories', (
212 'basedir_file.log',
213 D('nested1', (
214 'nested1.txt',
215 D('nested2', ('nested2.txt',)),
216 )),
217 )),
218 )),
219 )
220 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem)
221
222 self.chroot = os.path.join(self.tempdir, 'chroot')
223 self.chroot_tmp = os.path.join(self.chroot, 'tmp')
224 self.destination = os.path.join(self.chroot_tmp, 'tempdir')
225 self.inside_path = '/tmp/tempdir'
226
227 self.single_file_src = os.path.join(self.tempdir, 'sources', 'single_file')
228 self.sf_src_file = os.path.join(self.single_file_src, 'single_file.txt')
229 self.sf_dest_file = os.path.join(self.destination, 'single_file.txt')
230
231 self.nested_dirs_src = (
232 os.path.join(self.tempdir, 'sources', 'nested_directories'))
233 self.nested_src_files = (
234 os.path.join(self.nested_dirs_src, 'basedir_file.log'),
235 os.path.join(self.nested_dirs_src, 'nested1', 'nested1.txt'),
236 os.path.join(self.nested_dirs_src, 'nested1', 'nested2', 'nested2.txt'),
237 )
238 self.nested_dest_files = (
239 os.path.join(self.destination, 'basedir_file.log'),
240 os.path.join(self.destination, 'nested1', 'nested1.txt'),
241 os.path.join(self.destination, 'nested1', 'nested2', 'nested2.txt'),
242 )
243
244 self.message = build_api_test_pb2.TestRequestMessage()
245
246 def _assertExist(self, files):
247 for f in files:
248 self.assertExists(f)
249
250 def _assertNotExist(self, files):
251 for f in files:
252 self.assertNotExists(f)
253
254 def testSingleFileTransfer(self):
255 """Single source file syncs."""
256 self.message.synced_dir.dir = self.single_file_src
257
258 # Verify source files exist and destination files do not.
259 self.assertExists(self.sf_src_file)
260 self.assertNotExists(self.sf_dest_file)
261
262 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
263 # Verify the prefix is getting correctly stripped.
264 self.assertEqual(self.message.synced_dir.dir, self.inside_path)
265 # Verify the files have all been correctly copied in.
266 self.assertExists(self.sf_dest_file)
267
268 self.assertEqual(self.message.synced_dir.dir, self.single_file_src)
269 # Verify the files have all been copied out.
270 self.assertExists(self.sf_src_file)
271
272 def testNestedFileSync(self):
273 """Nested directories and files sync."""
274 self.message.synced_dir.dir = self.nested_dirs_src
275
276 self._assertExist(self.nested_src_files)
277 self._assertNotExist(self.nested_dest_files)
278
279 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
280 self.assertEqual(self.message.synced_dir.dir, self.inside_path)
281 self._assertExist(self.nested_dest_files)
282
283 self.assertEqual(self.message.synced_dir.dir, self.nested_dirs_src)
284 self._assertExist(self.nested_src_files)
285
286 def testDeletion(self):
287 """Test file deletions are exported correctly."""
288 self.message.synced_dir.dir = self.nested_dirs_src
289
290 deleted_src = os.path.join(self.nested_dirs_src, 'nested1', 'nested1.txt')
291 deleted_dest = os.path.join(self.destination, 'nested1', 'nested1.txt')
292
293 self._assertExist(self.nested_src_files)
294 self._assertNotExist(self.nested_dest_files)
295
296 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
297 self._assertExist(self.nested_dest_files)
298 osutils.SafeUnlink(deleted_dest)
299
300 self._assertExist(set(self.nested_src_files) - {deleted_src})
301 self.assertNotExists(deleted_src)
302
303 def testCreation(self):
304 """Test file creations are exported correctly."""
305 self.message.synced_dir.dir = self.nested_dirs_src
306
307 new_src = os.path.join(self.nested_dirs_src, 'new_dir', 'new_file')
308 new_dest = os.path.join(self.destination, 'new_dir', 'new_file')
309
310 self._assertExist(self.nested_src_files)
311 self._assertNotExist(self.nested_dest_files)
312
313 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
314 self._assertExist(self.nested_dest_files)
315 osutils.Touch(new_dest, makedirs=True)
316
317 self._assertExist(self.nested_src_files)
318 self.assertExists(new_src)
319
320 def testModification(self):
321 """Test file modifications are exported correctly."""
322 self.message.synced_dir.dir = self.single_file_src
323
324 self.assertExists(self.sf_src_file)
325 self.assertNotExists(self.sf_dest_file)
326
327 self.assertEqual('', osutils.ReadFile(self.sf_src_file))
328 file_content = 'Content!'
329
330 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
331 self.assertExists(self.sf_dest_file)
332 osutils.WriteFile(self.sf_dest_file, file_content)
333
334 self.assertExists(self.sf_src_file)
335 self.assertEqual(file_content, osutils.ReadFile(self.sf_src_file))
336
337
Alex Kleinaae49772019-07-26 10:20:50 -0600338class ExtractResultsTest(cros_test_lib.TempDirTestCase):
339 """Tests for extract_results."""
Alex Kleinbd6edf82019-07-18 10:30:49 -0600340
341 def setUp(self):
342 # Setup the directories.
343 self.chroot_dir = os.path.join(self.tempdir, 'chroot')
344 self.source_dir = '/source'
345 self.chroot_source = os.path.join(self.chroot_dir,
346 self.source_dir.lstrip(os.sep))
347 self.source_dir2 = '/source2'
348 self.chroot_source2 = os.path.join(self.chroot_dir,
349 self.source_dir2.lstrip(os.sep))
350 self.dest_dir = os.path.join(self.tempdir, 'destination')
351 osutils.SafeMakedirs(self.chroot_source)
352 osutils.SafeMakedirs(self.chroot_source2)
353 osutils.SafeMakedirs(self.dest_dir)
354
355 # Two files in the same directory inside the chroot.
356 self.source_file1 = os.path.join(self.chroot_source, 'file1')
357 self.source_file1_inside = os.path.join(self.source_dir, 'file1')
358 self.file1_contents = 'file 1'
359 osutils.WriteFile(self.source_file1, self.file1_contents)
360
361 self.file2_contents = 'some data'
362 self.source_file2 = os.path.join(self.chroot_source, 'file2')
363 self.source_file2_inside = os.path.join(self.source_dir, 'file2')
364 osutils.WriteFile(self.source_file2, self.file2_contents)
365
366 # Third file in a different location.
367 self.file3_contents = 'another file'
368 self.source_file3 = os.path.join(self.chroot_source2, 'file3')
369 self.source_file3_inside = os.path.join(self.source_dir2, 'file3')
370 osutils.WriteFile(self.source_file3, self.file3_contents)
371
372 self.request = build_api_test_pb2.TestRequestMessage()
373 self.request.result_path.path.path = self.dest_dir
374 self.request.result_path.path.location = common_pb2.Path.OUTSIDE
375 self.response = build_api_test_pb2.TestResultMessage()
376 self.chroot = chroot_lib.Chroot(path=self.chroot_dir)
377
378 def _path_checks(self, path, destination, contents=None):
379 self.assertTrue(path)
380 self.assertStartsWith(path, destination)
381 self.assertExists(path)
382 if contents:
383 self.assertFileContents(path, contents)
384
385 def test_single_file(self):
386 """Test a single file."""
387 self.response.artifact.path = self.source_file1_inside
388 self.response.artifact.location = common_pb2.Path.INSIDE
389
Alex Kleinaae49772019-07-26 10:20:50 -0600390 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600391
392 self._path_checks(self.response.artifact.path, self.dest_dir,
393 contents=self.file1_contents)
394
395 def test_single_directory(self):
396 """Test a single directory."""
397 self.response.artifact.path = self.source_dir
398 self.response.artifact.location = common_pb2.Path.INSIDE
399
Alex Kleinaae49772019-07-26 10:20:50 -0600400 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600401
402 self._path_checks(self.response.artifact.path, self.dest_dir)
Mike Frysinger678735c2019-09-28 18:23:28 -0400403 self.assertCountEqual(os.listdir(self.chroot_source),
Alex Kleinbd6edf82019-07-18 10:30:49 -0600404 os.listdir(self.response.artifact.path))
405
406 def test_multiple_files(self):
407 """Test multiple files."""
408 self.response.artifact.path = self.source_file1_inside
409 self.response.artifact.location = common_pb2.Path.INSIDE
410 self.response.nested_artifact.path.path = self.source_file2_inside
411 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
412
413 artifact3 = self.response.artifacts.add()
414 artifact3.path = self.source_file3_inside
415 artifact3.location = common_pb2.Path.INSIDE
416
Alex Kleinaae49772019-07-26 10:20:50 -0600417 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600418
419 self._path_checks(self.response.artifact.path, self.dest_dir,
420 contents=self.file1_contents)
421 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir,
422 contents=self.file2_contents)
423
424 self.assertEqual(1, len(self.response.artifacts))
425 for artifact in self.response.artifacts:
426 self._path_checks(artifact.path, self.dest_dir,
427 contents=self.file3_contents)
428
429 def test_multiple_directories(self):
430 """Test multiple directories."""
431 self.response.artifact.path = self.source_dir
432 self.response.artifact.location = common_pb2.Path.INSIDE
433 self.response.nested_artifact.path.path = self.source_dir2
434 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
435
Alex Kleinaae49772019-07-26 10:20:50 -0600436 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600437
438 self._path_checks(self.response.artifact.path, self.dest_dir)
439 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir)
440
441 expected = os.listdir(self.chroot_source)
442 expected.extend(os.listdir(self.chroot_source2))
Mike Frysinger678735c2019-09-28 18:23:28 -0400443 self.assertCountEqual(expected, os.listdir(self.response.artifact.path))