blob: 465a039b8539b2eb4cc560e465ad6271403ebf89 [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
11
12from chromite.api import field_handler
13from chromite.api.gen.chromite.api import build_api_test_pb2
14from chromite.api.gen.chromiumos import common_pb2
15from chromite.lib import chroot_lib
16from chromite.lib import cros_test_lib
17from chromite.lib import osutils
18
19
20class ChrootHandlerTest(cros_test_lib.TestCase):
21 """ChrootHandler tests."""
22
23 def setUp(self):
24 self.path = '/chroot/dir'
25 self.cache_dir = '/cache/dir'
Alex Klein5e4b1bc2019-07-02 12:27:06 -060026 self.chrome_dir = '/chrome/dir'
Alex Kleinb7485bb2019-09-19 13:23:37 -060027 self.env = {'FEATURES': 'thing', 'CHROME_ORIGIN': 'LOCAL_SOURCE'}
Alex Klein5e4b1bc2019-07-02 12:27:06 -060028 self.expected_chroot = chroot_lib.Chroot(path=self.path,
29 cache_dir=self.cache_dir,
30 chrome_root=self.chrome_dir,
31 env=self.env)
Alex Kleinc05f3d12019-05-29 14:16:21 -060032
33 def test_parse_chroot_success(self):
34 """Test successful Chroot message parse."""
35 chroot_msg = common_pb2.Chroot()
36 chroot_msg.path = self.path
37 chroot_msg.cache_dir = self.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060038 chroot_msg.chrome_dir = self.chrome_dir
Alex Kleinc05f3d12019-05-29 14:16:21 -060039 chroot_msg.env.features.add().feature = 'thing'
40
Alex Kleinc7d647f2020-01-06 12:00:48 -070041 chroot_handler = field_handler.ChrootHandler(clear_field=False)
Alex Kleinc05f3d12019-05-29 14:16:21 -060042 parsed_chroot = chroot_handler.parse_chroot(chroot_msg)
43
44 self.assertEqual(self.expected_chroot, parsed_chroot)
45
46 def test_handle_success(self):
47 """Test a successful Chroot message parse from a parent message."""
48 message = build_api_test_pb2.TestRequestMessage()
49 message.chroot.path = self.path
50 message.chroot.cache_dir = self.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060051 message.chroot.chrome_dir = self.chrome_dir
Alex Kleinc05f3d12019-05-29 14:16:21 -060052 message.chroot.env.features.add().feature = 'thing'
53
54 # First a no-clear parse.
Alex Kleinc7d647f2020-01-06 12:00:48 -070055 chroot_handler = field_handler.ChrootHandler(clear_field=False)
Alex Kleinc05f3d12019-05-29 14:16:21 -060056 chroot = chroot_handler.handle(message)
57
58 self.assertEqual(self.expected_chroot, chroot)
59 self.assertEqual(message.chroot.path, self.path)
60
61 # A clear field parse.
Alex Kleinc7d647f2020-01-06 12:00:48 -070062 clear_chroot_handler = field_handler.ChrootHandler(clear_field=True)
Alex Kleinc05f3d12019-05-29 14:16:21 -060063 chroot = clear_chroot_handler.handle(message)
64
65 self.assertEqual(self.expected_chroot, chroot)
66 self.assertFalse(message.chroot.path)
67
68 def test_handle_empty_chroot_message(self):
69 """Test handling of an empty chroot message."""
70 message = build_api_test_pb2.TestRequestMessage()
Alex Kleinb49be8a2019-12-20 10:23:03 -070071 empty_chroot = chroot_lib.Chroot()
Alex Kleinc05f3d12019-05-29 14:16:21 -060072
Alex Kleinc7d647f2020-01-06 12:00:48 -070073 chroot_handler = field_handler.ChrootHandler(clear_field=False)
Alex Kleinc05f3d12019-05-29 14:16:21 -060074 chroot = chroot_handler.handle(message)
75
76 self.assertEqual(empty_chroot, chroot)
77
78
Alex Kleinaae49772019-07-26 10:20:50 -060079class CopyPathInTest(cros_test_lib.TempDirTestCase):
Alex Kleinc05f3d12019-05-29 14:16:21 -060080 """PathHandler tests."""
81
82 def setUp(self):
83 self.source_dir = os.path.join(self.tempdir, 'source')
84 self.dest_dir = os.path.join(self.tempdir, 'destination')
85 osutils.SafeMakedirs(self.source_dir)
86 osutils.SafeMakedirs(self.dest_dir)
87
88 self.source_file1 = os.path.join(self.source_dir, 'file1')
89 self.file1_contents = 'file 1'
90 osutils.WriteFile(self.source_file1, self.file1_contents)
91
92 self.file2_contents = 'some data'
93 self.source_file2 = os.path.join(self.source_dir, 'file2')
94 osutils.WriteFile(self.source_file2, self.file2_contents)
95
96 def _path_checks(self, source_file, dest_file, contents=None):
97 """Set of common checks for the copied files/directories."""
98 # Message should now reflect the new path.
99 self.assertNotEqual(source_file, dest_file)
100 # The new path should be in the destination directory.
101 self.assertStartsWith(dest_file, self.dest_dir)
102 # The new file should exist.
103 self.assertExists(dest_file)
104
105 if contents:
106 # The contents should be the same as the source file.
107 self.assertFileContents(dest_file, contents)
108
109 def test_handle_file(self):
110 """Test handling of a single file."""
111 message = build_api_test_pb2.TestRequestMessage()
112 message.path.path = self.source_file1
113 message.path.location = common_pb2.Path.OUTSIDE
114
Alex Kleinaae49772019-07-26 10:20:50 -0600115 with field_handler.copy_paths_in(message, self.dest_dir, delete=True):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600116 new_path = message.path.path
117 self._path_checks(self.source_file1, new_path, self.file1_contents)
118
119 # The file should have been deleted on exit with delete=True.
120 self.assertNotExists(new_path)
Alex Kleinaae49772019-07-26 10:20:50 -0600121 # The original should still exist.
122 self.assertExists(self.source_file1)
123 # The path should get reset.
Alex Kleinaa705412019-06-04 15:00:30 -0600124 self.assertEqual(message.path.path, self.source_file1)
Alex Kleinc05f3d12019-05-29 14:16:21 -0600125
126 def test_handle_files(self):
127 """Test handling of multiple files."""
128 message = build_api_test_pb2.TestRequestMessage()
129 message.path.path = self.source_file1
130 message.path.location = common_pb2.Path.OUTSIDE
131 message.another_path.path = self.source_file2
132 message.another_path.location = common_pb2.Path.OUTSIDE
133
Alex Kleinaae49772019-07-26 10:20:50 -0600134 with field_handler.copy_paths_in(message, self.dest_dir, delete=False):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600135 new_path1 = message.path.path
136 new_path2 = message.another_path.path
137
138 self._path_checks(self.source_file1, new_path1, self.file1_contents)
139 self._path_checks(self.source_file2, new_path2, self.file2_contents)
140
141 # The files should still exist with delete=False.
142 self.assertExists(new_path1)
143 self.assertExists(new_path2)
144
Alex Kleinaa705412019-06-04 15:00:30 -0600145 def test_handle_nested_file(self):
146 """Test the nested path handling."""
147 message = build_api_test_pb2.TestRequestMessage()
148 message.nested_path.path.path = self.source_file1
149 message.nested_path.path.location = common_pb2.Path.OUTSIDE
150
Alex Kleinaae49772019-07-26 10:20:50 -0600151 with field_handler.copy_paths_in(message, self.dest_dir):
Alex Kleinaa705412019-06-04 15:00:30 -0600152 new_path = message.nested_path.path.path
153 self._path_checks(self.source_file1, new_path, self.file1_contents)
154
Alex Kleinc05f3d12019-05-29 14:16:21 -0600155 def test_handle_directory(self):
156 """Test handling of a directory."""
157 message = build_api_test_pb2.TestRequestMessage()
158 message.path.path = self.source_dir
159 message.path.location = common_pb2.Path.OUTSIDE
160
Alex Kleinaae49772019-07-26 10:20:50 -0600161 with field_handler.copy_paths_in(message, self.dest_dir):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600162 new_path = message.path.path
163
164 self._path_checks(self.source_dir, self.dest_dir)
165 # Make sure both directories have the same files.
Mike Frysinger678735c2019-09-28 18:23:28 -0400166 self.assertCountEqual(os.listdir(self.source_dir), os.listdir(new_path))
Alex Kleinc05f3d12019-05-29 14:16:21 -0600167
168 def test_direction(self):
169 """Test the direction argument preventing copies."""
170 message = build_api_test_pb2.TestRequestMessage()
171 message.path.path = self.source_file1
172 message.path.location = common_pb2.Path.INSIDE
173
Alex Kleinaae49772019-07-26 10:20:50 -0600174 with field_handler.copy_paths_in(message, self.dest_dir, delete=True):
Alex Kleinc05f3d12019-05-29 14:16:21 -0600175 self.assertEqual(self.source_file1, message.path.path)
176
177 # It should not be deleting the file when it doesn't need to copy it even
178 # with delete=True.
179 self.assertExists(self.source_file1)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600180
181 def test_prefix_inside(self):
182 """Test the transfer inside prefix handling."""
183 message = build_api_test_pb2.TestRequestMessage()
184 message.path.path = self.source_dir
185 message.path.location = common_pb2.Path.OUTSIDE
186
Alex Kleinaae49772019-07-26 10:20:50 -0600187 with field_handler.copy_paths_in(message, self.dest_dir,
188 prefix=self.tempdir):
Alex Kleinbd6edf82019-07-18 10:30:49 -0600189 new_path = message.path.path
190 # The prefix should be removed.
191 self.assertFalse(new_path.startswith(self.tempdir))
192
Alex Kleinbd6edf82019-07-18 10:30:49 -0600193
Alex Kleinf0717a62019-12-06 09:45:00 -0700194class SyncDirsTest(cros_test_lib.TempDirTestCase):
195 """Tests for sync_dirs."""
196
197 def setUp(self):
198 D = cros_test_lib.Directory
199 filesystem = (
200 D('chroot', (
201 D('tmp', (
202 D('tempdir', ()),
203 )),
204 )),
205 D('sources', (
206 D('single_file', ('single_file.txt',)),
207 D('nested_directories', (
208 'basedir_file.log',
209 D('nested1', (
210 'nested1.txt',
211 D('nested2', ('nested2.txt',)),
212 )),
213 )),
214 )),
215 )
216 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem)
217
218 self.chroot = os.path.join(self.tempdir, 'chroot')
219 self.chroot_tmp = os.path.join(self.chroot, 'tmp')
220 self.destination = os.path.join(self.chroot_tmp, 'tempdir')
221 self.inside_path = '/tmp/tempdir'
222
223 self.single_file_src = os.path.join(self.tempdir, 'sources', 'single_file')
224 self.sf_src_file = os.path.join(self.single_file_src, 'single_file.txt')
225 self.sf_dest_file = os.path.join(self.destination, 'single_file.txt')
226
227 self.nested_dirs_src = (
228 os.path.join(self.tempdir, 'sources', 'nested_directories'))
229 self.nested_src_files = (
230 os.path.join(self.nested_dirs_src, 'basedir_file.log'),
231 os.path.join(self.nested_dirs_src, 'nested1', 'nested1.txt'),
232 os.path.join(self.nested_dirs_src, 'nested1', 'nested2', 'nested2.txt'),
233 )
234 self.nested_dest_files = (
235 os.path.join(self.destination, 'basedir_file.log'),
236 os.path.join(self.destination, 'nested1', 'nested1.txt'),
237 os.path.join(self.destination, 'nested1', 'nested2', 'nested2.txt'),
238 )
239
240 self.message = build_api_test_pb2.TestRequestMessage()
241
242 def _assertExist(self, files):
243 for f in files:
244 self.assertExists(f)
245
246 def _assertNotExist(self, files):
247 for f in files:
248 self.assertNotExists(f)
249
250 def testSingleFileTransfer(self):
251 """Single source file syncs."""
252 self.message.synced_dir.dir = self.single_file_src
253
254 # Verify source files exist and destination files do not.
255 self.assertExists(self.sf_src_file)
256 self.assertNotExists(self.sf_dest_file)
257
258 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
259 # Verify the prefix is getting correctly stripped.
260 self.assertEqual(self.message.synced_dir.dir, self.inside_path)
261 # Verify the files have all been correctly copied in.
262 self.assertExists(self.sf_dest_file)
263
264 self.assertEqual(self.message.synced_dir.dir, self.single_file_src)
265 # Verify the files have all been copied out.
266 self.assertExists(self.sf_src_file)
267
268 def testNestedFileSync(self):
269 """Nested directories and files sync."""
270 self.message.synced_dir.dir = self.nested_dirs_src
271
272 self._assertExist(self.nested_src_files)
273 self._assertNotExist(self.nested_dest_files)
274
275 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
276 self.assertEqual(self.message.synced_dir.dir, self.inside_path)
277 self._assertExist(self.nested_dest_files)
278
279 self.assertEqual(self.message.synced_dir.dir, self.nested_dirs_src)
280 self._assertExist(self.nested_src_files)
281
282 def testDeletion(self):
283 """Test file deletions are exported correctly."""
284 self.message.synced_dir.dir = self.nested_dirs_src
285
286 deleted_src = os.path.join(self.nested_dirs_src, 'nested1', 'nested1.txt')
287 deleted_dest = os.path.join(self.destination, 'nested1', 'nested1.txt')
288
289 self._assertExist(self.nested_src_files)
290 self._assertNotExist(self.nested_dest_files)
291
292 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
293 self._assertExist(self.nested_dest_files)
294 osutils.SafeUnlink(deleted_dest)
295
296 self._assertExist(set(self.nested_src_files) - {deleted_src})
297 self.assertNotExists(deleted_src)
298
299 def testCreation(self):
300 """Test file creations are exported correctly."""
301 self.message.synced_dir.dir = self.nested_dirs_src
302
303 new_src = os.path.join(self.nested_dirs_src, 'new_dir', 'new_file')
304 new_dest = os.path.join(self.destination, 'new_dir', 'new_file')
305
306 self._assertExist(self.nested_src_files)
307 self._assertNotExist(self.nested_dest_files)
308
309 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
310 self._assertExist(self.nested_dest_files)
311 osutils.Touch(new_dest, makedirs=True)
312
313 self._assertExist(self.nested_src_files)
314 self.assertExists(new_src)
315
316 def testModification(self):
317 """Test file modifications are exported correctly."""
318 self.message.synced_dir.dir = self.single_file_src
319
320 self.assertExists(self.sf_src_file)
321 self.assertNotExists(self.sf_dest_file)
322
323 self.assertEqual('', osutils.ReadFile(self.sf_src_file))
324 file_content = 'Content!'
325
326 with field_handler.sync_dirs(self.message, self.destination, self.chroot):
327 self.assertExists(self.sf_dest_file)
328 osutils.WriteFile(self.sf_dest_file, file_content)
329
330 self.assertExists(self.sf_src_file)
331 self.assertEqual(file_content, osutils.ReadFile(self.sf_src_file))
332
333
Alex Kleinaae49772019-07-26 10:20:50 -0600334class ExtractResultsTest(cros_test_lib.TempDirTestCase):
335 """Tests for extract_results."""
Alex Kleinbd6edf82019-07-18 10:30:49 -0600336
337 def setUp(self):
338 # Setup the directories.
339 self.chroot_dir = os.path.join(self.tempdir, 'chroot')
340 self.source_dir = '/source'
341 self.chroot_source = os.path.join(self.chroot_dir,
342 self.source_dir.lstrip(os.sep))
343 self.source_dir2 = '/source2'
344 self.chroot_source2 = os.path.join(self.chroot_dir,
345 self.source_dir2.lstrip(os.sep))
346 self.dest_dir = os.path.join(self.tempdir, 'destination')
347 osutils.SafeMakedirs(self.chroot_source)
348 osutils.SafeMakedirs(self.chroot_source2)
349 osutils.SafeMakedirs(self.dest_dir)
350
351 # Two files in the same directory inside the chroot.
352 self.source_file1 = os.path.join(self.chroot_source, 'file1')
353 self.source_file1_inside = os.path.join(self.source_dir, 'file1')
354 self.file1_contents = 'file 1'
355 osutils.WriteFile(self.source_file1, self.file1_contents)
356
357 self.file2_contents = 'some data'
358 self.source_file2 = os.path.join(self.chroot_source, 'file2')
359 self.source_file2_inside = os.path.join(self.source_dir, 'file2')
360 osutils.WriteFile(self.source_file2, self.file2_contents)
361
362 # Third file in a different location.
363 self.file3_contents = 'another file'
364 self.source_file3 = os.path.join(self.chroot_source2, 'file3')
365 self.source_file3_inside = os.path.join(self.source_dir2, 'file3')
366 osutils.WriteFile(self.source_file3, self.file3_contents)
367
368 self.request = build_api_test_pb2.TestRequestMessage()
369 self.request.result_path.path.path = self.dest_dir
370 self.request.result_path.path.location = common_pb2.Path.OUTSIDE
371 self.response = build_api_test_pb2.TestResultMessage()
372 self.chroot = chroot_lib.Chroot(path=self.chroot_dir)
373
374 def _path_checks(self, path, destination, contents=None):
375 self.assertTrue(path)
376 self.assertStartsWith(path, destination)
377 self.assertExists(path)
378 if contents:
379 self.assertFileContents(path, contents)
380
381 def test_single_file(self):
382 """Test a single file."""
383 self.response.artifact.path = self.source_file1_inside
384 self.response.artifact.location = common_pb2.Path.INSIDE
385
Alex Kleinaae49772019-07-26 10:20:50 -0600386 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600387
388 self._path_checks(self.response.artifact.path, self.dest_dir,
389 contents=self.file1_contents)
390
391 def test_single_directory(self):
392 """Test a single directory."""
393 self.response.artifact.path = self.source_dir
394 self.response.artifact.location = common_pb2.Path.INSIDE
395
Alex Kleinaae49772019-07-26 10:20:50 -0600396 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600397
398 self._path_checks(self.response.artifact.path, self.dest_dir)
Mike Frysinger678735c2019-09-28 18:23:28 -0400399 self.assertCountEqual(os.listdir(self.chroot_source),
Alex Kleinbd6edf82019-07-18 10:30:49 -0600400 os.listdir(self.response.artifact.path))
401
402 def test_multiple_files(self):
403 """Test multiple files."""
404 self.response.artifact.path = self.source_file1_inside
405 self.response.artifact.location = common_pb2.Path.INSIDE
406 self.response.nested_artifact.path.path = self.source_file2_inside
407 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
408
409 artifact3 = self.response.artifacts.add()
410 artifact3.path = self.source_file3_inside
411 artifact3.location = common_pb2.Path.INSIDE
412
Alex Kleinaae49772019-07-26 10:20:50 -0600413 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600414
415 self._path_checks(self.response.artifact.path, self.dest_dir,
416 contents=self.file1_contents)
417 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir,
418 contents=self.file2_contents)
419
420 self.assertEqual(1, len(self.response.artifacts))
421 for artifact in self.response.artifacts:
422 self._path_checks(artifact.path, self.dest_dir,
423 contents=self.file3_contents)
424
425 def test_multiple_directories(self):
426 """Test multiple directories."""
427 self.response.artifact.path = self.source_dir
428 self.response.artifact.location = common_pb2.Path.INSIDE
429 self.response.nested_artifact.path.path = self.source_dir2
430 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
431
Alex Kleinaae49772019-07-26 10:20:50 -0600432 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600433
434 self._path_checks(self.response.artifact.path, self.dest_dir)
435 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir)
436
437 expected = os.listdir(self.chroot_source)
438 expected.extend(os.listdir(self.chroot_source2))
Mike Frysinger678735c2019-09-28 18:23:28 -0400439 self.assertCountEqual(expected, os.listdir(self.response.artifact.path))