blob: 4e68068de7673a14761868b077ebd0cd75e180cc [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 Kleinc05f3d12019-05-29 14:16:21 -060027 self.env = {'FEATURES': 'thing'}
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
79class PathHandlerTest(cros_test_lib.TempDirTestCase):
80 """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
115 with field_handler.handle_paths(message, self.dest_dir, delete=True):
116 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 Kleinaa705412019-06-04 15:00:30 -0600121 # Make sure it gets reset.
122 self.assertEqual(message.path.path, self.source_file1)
Alex Kleinc05f3d12019-05-29 14:16:21 -0600123
124 def test_handle_files(self):
125 """Test handling of multiple files."""
126 message = build_api_test_pb2.TestRequestMessage()
127 message.path.path = self.source_file1
128 message.path.location = common_pb2.Path.OUTSIDE
129 message.another_path.path = self.source_file2
130 message.another_path.location = common_pb2.Path.OUTSIDE
131
132 with field_handler.handle_paths(message, self.dest_dir, delete=False):
133 new_path1 = message.path.path
134 new_path2 = message.another_path.path
135
136 self._path_checks(self.source_file1, new_path1, self.file1_contents)
137 self._path_checks(self.source_file2, new_path2, self.file2_contents)
138
139 # The files should still exist with delete=False.
140 self.assertExists(new_path1)
141 self.assertExists(new_path2)
142
Alex Kleinaa705412019-06-04 15:00:30 -0600143 def test_handle_nested_file(self):
144 """Test the nested path handling."""
145 message = build_api_test_pb2.TestRequestMessage()
146 message.nested_path.path.path = self.source_file1
147 message.nested_path.path.location = common_pb2.Path.OUTSIDE
148
149 with field_handler.handle_paths(message, self.dest_dir):
150 new_path = message.nested_path.path.path
151 self._path_checks(self.source_file1, new_path, self.file1_contents)
152
Alex Kleinc05f3d12019-05-29 14:16:21 -0600153 def test_handle_directory(self):
154 """Test handling of a directory."""
155 message = build_api_test_pb2.TestRequestMessage()
156 message.path.path = self.source_dir
157 message.path.location = common_pb2.Path.OUTSIDE
158
159 with field_handler.handle_paths(message, self.dest_dir):
160 new_path = message.path.path
161
162 self._path_checks(self.source_dir, self.dest_dir)
163 # Make sure both directories have the same files.
164 self.assertItemsEqual(os.listdir(self.source_dir), os.listdir(new_path))
165
166 def test_direction(self):
167 """Test the direction argument preventing copies."""
168 message = build_api_test_pb2.TestRequestMessage()
169 message.path.path = self.source_file1
170 message.path.location = common_pb2.Path.INSIDE
171
172 direction = field_handler.PathHandler.INSIDE
173 with field_handler.handle_paths(message, self.dest_dir, delete=True,
174 direction=direction):
175 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
187 with field_handler.handle_paths(message, self.dest_dir,
188 direction=field_handler.PathHandler.INSIDE,
189 prefix=self.tempdir):
190 new_path = message.path.path
191 # The prefix should be removed.
192 self.assertFalse(new_path.startswith(self.tempdir))
193
194 def test_prefix_outside(self):
195 """Test the transfer outside prefix handling."""
196 message = build_api_test_pb2.TestRequestMessage()
197 message.path.path = self.source_dir.replace(self.tempdir, '')
198 message.path.location = common_pb2.Path.INSIDE
199
200 with field_handler.handle_paths(message, self.dest_dir,
201 direction=field_handler.PathHandler.OUTSIDE,
202 prefix=self.tempdir):
203 new_path = message.path.path
204 # The source directory should have been properly reconstructed and
205 # everything copied over to the destination.
206 self.assertItemsEqual(os.listdir(self.source_dir), os.listdir(new_path))
207
208
209class HandleResultPathsTest(cros_test_lib.TempDirTestCase):
210 """Tests for handle_result_paths."""
211
212 def setUp(self):
213 # Setup the directories.
214 self.chroot_dir = os.path.join(self.tempdir, 'chroot')
215 self.source_dir = '/source'
216 self.chroot_source = os.path.join(self.chroot_dir,
217 self.source_dir.lstrip(os.sep))
218 self.source_dir2 = '/source2'
219 self.chroot_source2 = os.path.join(self.chroot_dir,
220 self.source_dir2.lstrip(os.sep))
221 self.dest_dir = os.path.join(self.tempdir, 'destination')
222 osutils.SafeMakedirs(self.chroot_source)
223 osutils.SafeMakedirs(self.chroot_source2)
224 osutils.SafeMakedirs(self.dest_dir)
225
226 # Two files in the same directory inside the chroot.
227 self.source_file1 = os.path.join(self.chroot_source, 'file1')
228 self.source_file1_inside = os.path.join(self.source_dir, 'file1')
229 self.file1_contents = 'file 1'
230 osutils.WriteFile(self.source_file1, self.file1_contents)
231
232 self.file2_contents = 'some data'
233 self.source_file2 = os.path.join(self.chroot_source, 'file2')
234 self.source_file2_inside = os.path.join(self.source_dir, 'file2')
235 osutils.WriteFile(self.source_file2, self.file2_contents)
236
237 # Third file in a different location.
238 self.file3_contents = 'another file'
239 self.source_file3 = os.path.join(self.chroot_source2, 'file3')
240 self.source_file3_inside = os.path.join(self.source_dir2, 'file3')
241 osutils.WriteFile(self.source_file3, self.file3_contents)
242
243 self.request = build_api_test_pb2.TestRequestMessage()
244 self.request.result_path.path.path = self.dest_dir
245 self.request.result_path.path.location = common_pb2.Path.OUTSIDE
246 self.response = build_api_test_pb2.TestResultMessage()
247 self.chroot = chroot_lib.Chroot(path=self.chroot_dir)
248
249 def _path_checks(self, path, destination, contents=None):
250 self.assertTrue(path)
251 self.assertStartsWith(path, destination)
252 self.assertExists(path)
253 if contents:
254 self.assertFileContents(path, contents)
255
256 def test_single_file(self):
257 """Test a single file."""
258 self.response.artifact.path = self.source_file1_inside
259 self.response.artifact.location = common_pb2.Path.INSIDE
260
261 field_handler.handle_result_paths(self.request, self.response, self.chroot)
262
263 self._path_checks(self.response.artifact.path, self.dest_dir,
264 contents=self.file1_contents)
265
266 def test_single_directory(self):
267 """Test a single directory."""
268 self.response.artifact.path = self.source_dir
269 self.response.artifact.location = common_pb2.Path.INSIDE
270
271 field_handler.handle_result_paths(self.request, self.response, self.chroot)
272
273 self._path_checks(self.response.artifact.path, self.dest_dir)
274 self.assertItemsEqual(os.listdir(self.chroot_source),
275 os.listdir(self.response.artifact.path))
276
277 def test_multiple_files(self):
278 """Test multiple files."""
279 self.response.artifact.path = self.source_file1_inside
280 self.response.artifact.location = common_pb2.Path.INSIDE
281 self.response.nested_artifact.path.path = self.source_file2_inside
282 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
283
284 artifact3 = self.response.artifacts.add()
285 artifact3.path = self.source_file3_inside
286 artifact3.location = common_pb2.Path.INSIDE
287
288 field_handler.handle_result_paths(self.request, self.response, self.chroot)
289
290 self._path_checks(self.response.artifact.path, self.dest_dir,
291 contents=self.file1_contents)
292 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir,
293 contents=self.file2_contents)
294
295 self.assertEqual(1, len(self.response.artifacts))
296 for artifact in self.response.artifacts:
297 self._path_checks(artifact.path, self.dest_dir,
298 contents=self.file3_contents)
299
300 def test_multiple_directories(self):
301 """Test multiple directories."""
302 self.response.artifact.path = self.source_dir
303 self.response.artifact.location = common_pb2.Path.INSIDE
304 self.response.nested_artifact.path.path = self.source_dir2
305 self.response.nested_artifact.path.location = common_pb2.Path.INSIDE
306
307 field_handler.handle_result_paths(self.request, self.response, self.chroot)
308
309 self._path_checks(self.response.artifact.path, self.dest_dir)
310 self._path_checks(self.response.nested_artifact.path.path, self.dest_dir)
311
312 expected = os.listdir(self.chroot_source)
313 expected.extend(os.listdir(self.chroot_source2))
314 self.assertItemsEqual(expected, os.listdir(self.response.artifact.path))