blob: f33b1020e46e28ebe35cc25bb868cfd65cbb0c83 [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
41 chroot_handler = field_handler.ChrootHandler(clear_field=False)
42 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.
55 chroot_handler = field_handler.ChrootHandler(clear_field=False)
56 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.
62 clear_chroot_handler = field_handler.ChrootHandler(clear_field=True)
63 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()
71 empty_chroot = chroot_lib.Chroot(env={'FEATURES': 'separatedebug'})
72
73 chroot_handler = field_handler.ChrootHandler(clear_field=False)
74 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.
166 self.assertItemsEqual(os.listdir(self.source_dir), os.listdir(new_path))
167
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 Kleinaae49772019-07-26 10:20:50 -0600194class ExtractResultsTest(cros_test_lib.TempDirTestCase):
195 """Tests for extract_results."""
Alex Kleinbd6edf82019-07-18 10:30:49 -0600196
197 def setUp(self):
198 # Setup the directories.
199 self.chroot_dir = os.path.join(self.tempdir, 'chroot')
200 self.source_dir = '/source'
201 self.chroot_source = os.path.join(self.chroot_dir,
202 self.source_dir.lstrip(os.sep))
203 self.source_dir2 = '/source2'
204 self.chroot_source2 = os.path.join(self.chroot_dir,
205 self.source_dir2.lstrip(os.sep))
206 self.dest_dir = os.path.join(self.tempdir, 'destination')
207 osutils.SafeMakedirs(self.chroot_source)
208 osutils.SafeMakedirs(self.chroot_source2)
209 osutils.SafeMakedirs(self.dest_dir)
210
211 # Two files in the same directory inside the chroot.
212 self.source_file1 = os.path.join(self.chroot_source, 'file1')
213 self.source_file1_inside = os.path.join(self.source_dir, 'file1')
214 self.file1_contents = 'file 1'
215 osutils.WriteFile(self.source_file1, self.file1_contents)
216
217 self.file2_contents = 'some data'
218 self.source_file2 = os.path.join(self.chroot_source, 'file2')
219 self.source_file2_inside = os.path.join(self.source_dir, 'file2')
220 osutils.WriteFile(self.source_file2, self.file2_contents)
221
222 # Third file in a different location.
223 self.file3_contents = 'another file'
224 self.source_file3 = os.path.join(self.chroot_source2, 'file3')
225 self.source_file3_inside = os.path.join(self.source_dir2, 'file3')
226 osutils.WriteFile(self.source_file3, self.file3_contents)
227
228 self.request = build_api_test_pb2.TestRequestMessage()
229 self.request.result_path.path.path = self.dest_dir
230 self.request.result_path.path.location = common_pb2.Path.OUTSIDE
231 self.response = build_api_test_pb2.TestResultMessage()
232 self.chroot = chroot_lib.Chroot(path=self.chroot_dir)
233
234 def _path_checks(self, path, destination, contents=None):
235 self.assertTrue(path)
236 self.assertStartsWith(path, destination)
237 self.assertExists(path)
238 if contents:
239 self.assertFileContents(path, contents)
240
241 def test_single_file(self):
242 """Test a single file."""
243 self.response.artifact.path = self.source_file1_inside
244 self.response.artifact.location = common_pb2.Path.INSIDE
245
Alex Kleinaae49772019-07-26 10:20:50 -0600246 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600247
248 self._path_checks(self.response.artifact.path, self.dest_dir,
249 contents=self.file1_contents)
250
251 def test_single_directory(self):
252 """Test a single directory."""
253 self.response.artifact.path = self.source_dir
254 self.response.artifact.location = common_pb2.Path.INSIDE
255
Alex Kleinaae49772019-07-26 10:20:50 -0600256 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600257
258 self._path_checks(self.response.artifact.path, self.dest_dir)
259 self.assertItemsEqual(os.listdir(self.chroot_source),
260 os.listdir(self.response.artifact.path))
261
262 def test_multiple_files(self):
263 """Test multiple files."""
264 self.response.artifact.path = self.source_file1_inside
265 self.response.artifact.location = common_pb2.Path.INSIDE
266 self.response.nested_artifact.path.path = self.source_file2_inside
267 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
268
269 artifact3 = self.response.artifacts.add()
270 artifact3.path = self.source_file3_inside
271 artifact3.location = common_pb2.Path.INSIDE
272
Alex Kleinaae49772019-07-26 10:20:50 -0600273 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600274
275 self._path_checks(self.response.artifact.path, self.dest_dir,
276 contents=self.file1_contents)
277 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir,
278 contents=self.file2_contents)
279
280 self.assertEqual(1, len(self.response.artifacts))
281 for artifact in self.response.artifacts:
282 self._path_checks(artifact.path, self.dest_dir,
283 contents=self.file3_contents)
284
285 def test_multiple_directories(self):
286 """Test multiple directories."""
287 self.response.artifact.path = self.source_dir
288 self.response.artifact.location = common_pb2.Path.INSIDE
289 self.response.nested_artifact.path.path = self.source_dir2
290 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
291
Alex Kleinaae49772019-07-26 10:20:50 -0600292 field_handler.extract_results(self.request, self.response, self.chroot)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600293
294 self._path_checks(self.response.artifact.path, self.dest_dir)
295 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir)
296
297 expected = os.listdir(self.chroot_source)
298 expected.extend(os.listdir(self.chroot_source2))
299 self.assertItemsEqual(expected, os.listdir(self.response.artifact.path))