blob: dcd06a6132cbf5ce348558a8d030e97db2becf69 [file] [log] [blame]
Alex Kleina2e42c42019-04-17 16:13:19 -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"""The test controller tests."""
6
Alex Klein9f915782020-02-14 23:15:09 +00007import contextlib
Mike Frysingeref94e4c2020-02-10 23:59:54 -05008import os
Mike Frysinger166fea02021-02-12 05:30:33 -05009from unittest import mock
Mike Frysingeref94e4c2020-02-10 23:59:54 -050010
Alex Klein231d2da2019-07-22 16:44:45 -060011from chromite.api import api_config
Alex Klein8cb365a2019-05-15 16:24:53 -060012from chromite.api import controller
Alex Kleina2e42c42019-04-17 16:13:19 -060013from chromite.api.controller import test as test_controller
Evan Hernandez4e388a52019-05-01 12:16:33 -060014from chromite.api.gen.chromiumos import common_pb2
Alex Kleina2e42c42019-04-17 16:13:19 -060015from chromite.api.gen.chromite.api import test_pb2
Andrew Lamb763e3be2021-07-27 17:22:02 -060016from chromite.api.gen.chromiumos.build.api import system_image_pb2
17from chromite.api.gen.chromiumos.build.api import portage_pb2
Andrew Lambd814afa2021-08-11 11:04:20 -060018from chromite.api.gen.chromiumos.config.payload import flat_config_pb2
19from chromite.api.gen.chromiumos.config.api import design_pb2
20from chromite.api.gen.chromiumos.config.api import design_id_pb2
Andrew Lamb763e3be2021-07-27 17:22:02 -060021from chromite.api.gen.chromiumos.test.api import coverage_rule_pb2
22from chromite.api.gen.chromiumos.test.api import dut_attribute_pb2
23from chromite.api.gen.chromiumos.test.api import test_suite_pb2
24from chromite.api.gen.chromiumos.test.plan import source_test_plan_pb2
Jack Neusc9707c32021-07-23 21:48:54 +000025from chromite.lib import build_target_lib
Evan Hernandeze1e05d32019-07-19 12:32:18 -060026from chromite.lib import chroot_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060027from chromite.lib import cros_build_lib
28from chromite.lib import cros_test_lib
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060029from chromite.lib import image_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060030from chromite.lib import osutils
David Wellingc1433c22021-06-25 16:29:48 +000031from chromite.lib import sysroot_lib
Alex Klein18a60af2020-06-11 12:08:47 -060032from chromite.lib.parser import package_info
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060033from chromite.scripts import cros_set_lsb_release
34from chromite.service import test as test_service
Andrew Lamb763e3be2021-07-27 17:22:02 -060035from chromite.third_party.google.protobuf import json_format
Mike Frysingere652ba12019-09-08 00:57:43 -040036from chromite.utils import key_value_store
Alex Kleina2e42c42019-04-17 16:13:19 -060037
38
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -070039class DebugInfoTestTest(cros_test_lib.MockTempDirTestCase,
40 api_config.ApiConfigMixin):
41 """Tests for the DebugInfoTest function."""
42
43 def setUp(self):
44 self.board = 'board'
45 self.chroot_path = os.path.join(self.tempdir, 'chroot')
46 self.sysroot_path = '/build/board'
47 self.full_sysroot_path = os.path.join(self.chroot_path,
48 self.sysroot_path.lstrip(os.sep))
49 osutils.SafeMakedirs(self.full_sysroot_path)
50
51 def _GetInput(self, sysroot_path=None, build_target=None):
52 """Helper to build an input message instance."""
53 proto = test_pb2.DebugInfoTestRequest()
54 if sysroot_path:
55 proto.sysroot.path = sysroot_path
56 if build_target:
57 proto.sysroot.build_target.name = build_target
58 return proto
59
60 def _GetOutput(self):
61 """Helper to get an empty output message instance."""
62 return test_pb2.DebugInfoTestResponse()
63
64 def testValidateOnly(self):
65 """Sanity check that a validate only call does not execute any logic."""
66 patch = self.PatchObject(test_service, 'DebugInfoTest')
67 input_msg = self._GetInput(sysroot_path=self.full_sysroot_path)
68 test_controller.DebugInfoTest(input_msg, self._GetOutput(),
69 self.validate_only_config)
70 patch.assert_not_called()
71
Michael Mortensen85d38402019-12-12 09:50:29 -070072 def testMockError(self):
73 """Test mock error call does not execute any logic, returns error."""
74 patch = self.PatchObject(test_service, 'DebugInfoTest')
75
76 input_msg = self._GetInput(sysroot_path=self.full_sysroot_path)
77 rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(),
78 self.mock_error_config)
79 patch.assert_not_called()
80 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
81
82 def testMockCall(self):
83 """Test mock call does not execute any logic, returns success."""
84 patch = self.PatchObject(test_service, 'DebugInfoTest')
85
86 input_msg = self._GetInput(sysroot_path=self.full_sysroot_path)
87 rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(),
88 self.mock_call_config)
89 patch.assert_not_called()
90 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
91
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -070092 def testNoBuildTargetNoSysrootFails(self):
93 """Test missing build target name and sysroot path fails."""
94 input_msg = self._GetInput()
95 output_msg = self._GetOutput()
96 with self.assertRaises(cros_build_lib.DieSystemExit):
97 test_controller.DebugInfoTest(input_msg, output_msg, self.api_config)
98
99 def testDebugInfoTest(self):
100 """Call DebugInfoTest with valid sysroot_path."""
101 request = self._GetInput(sysroot_path=self.full_sysroot_path)
102
103 test_controller.DebugInfoTest(request, self._GetOutput(), self.api_config)
104
105
Alex Klein231d2da2019-07-22 16:44:45 -0600106class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase,
107 api_config.ApiConfigMixin):
Alex Kleina2e42c42019-04-17 16:13:19 -0600108 """Tests for the UnitTest function."""
109
Navil Perezc0b29a82020-07-07 14:17:48 +0000110 def _GetInput(self,
111 board=None,
112 result_path=None,
113 chroot_path=None,
114 cache_dir=None,
115 empty_sysroot=None,
116 packages=None,
Alex Kleinb64e5f82020-09-23 10:55:31 -0600117 blocklist=None):
Alex Kleina2e42c42019-04-17 16:13:19 -0600118 """Helper to build an input message instance."""
Navil Perezc0b29a82020-07-07 14:17:48 +0000119 formatted_packages = []
120 for pkg in packages or []:
121 formatted_packages.append({
122 'category': pkg.category,
123 'package_name': pkg.package
124 })
Alex Kleinb64e5f82020-09-23 10:55:31 -0600125 formatted_blocklist = []
126 for pkg in blocklist or []:
127 formatted_blocklist.append({'category': pkg.category,
Alex Kleinf2674462019-05-16 16:47:24 -0600128 'package_name': pkg.package})
129
Alex Kleina2e42c42019-04-17 16:13:19 -0600130 return test_pb2.BuildTargetUnitTestRequest(
131 build_target={'name': board}, result_path=result_path,
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600132 chroot={'path': chroot_path, 'cache_dir': cache_dir},
Alex Kleinf2674462019-05-16 16:47:24 -0600133 flags={'empty_sysroot': empty_sysroot},
Alex Klein64ac34c2020-09-23 10:21:33 -0600134 packages=formatted_packages,
Alex Klein157caf42021-07-01 14:36:43 -0600135 package_blocklist=formatted_blocklist,
Alex Kleina2e42c42019-04-17 16:13:19 -0600136 )
137
138 def _GetOutput(self):
139 """Helper to get an empty output message instance."""
140 return test_pb2.BuildTargetUnitTestResponse()
141
Alex Klein231d2da2019-07-22 16:44:45 -0600142 def testValidateOnly(self):
143 """Sanity check that a validate only call does not execute any logic."""
144 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
145
146 input_msg = self._GetInput(board='board', result_path=self.tempdir)
147 test_controller.BuildTargetUnitTest(input_msg, self._GetOutput(),
148 self.validate_only_config)
149 patch.assert_not_called()
150
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700151 def testMockCall(self):
152 """Test that a mock call does not execute logic, returns mocked value."""
153 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
154
155 input_msg = self._GetInput(board='board', result_path=self.tempdir)
156 response = self._GetOutput()
157 test_controller.BuildTargetUnitTest(input_msg, response,
158 self.mock_call_config)
159 patch.assert_not_called()
160 self.assertEqual(response.tarball_path,
161 os.path.join(input_msg.result_path, 'unit_tests.tar'))
162
163 def testMockError(self):
Michael Mortensen85d38402019-12-12 09:50:29 -0700164 """Test that a mock error does not execute logic, returns error."""
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700165 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
166
167 input_msg = self._GetInput(board='board', result_path=self.tempdir)
168 response = self._GetOutput()
169 rc = test_controller.BuildTargetUnitTest(input_msg, response,
170 self.mock_error_config)
171 patch.assert_not_called()
172 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
173 self.assertTrue(response.failed_packages)
174 self.assertEqual(response.failed_packages[0].category, 'foo')
175 self.assertEqual(response.failed_packages[0].package_name, 'bar')
176 self.assertEqual(response.failed_packages[1].category, 'cat')
177 self.assertEqual(response.failed_packages[1].package_name, 'pkg')
178
Alex Kleina2e42c42019-04-17 16:13:19 -0600179 def testNoArgumentFails(self):
180 """Test no arguments fails."""
181 input_msg = self._GetInput()
182 output_msg = self._GetOutput()
183 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600184 test_controller.BuildTargetUnitTest(input_msg, output_msg,
185 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600186
187 def testNoBuildTargetFails(self):
188 """Test missing build target name fails."""
189 input_msg = self._GetInput(result_path=self.tempdir)
190 output_msg = self._GetOutput()
191 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600192 test_controller.BuildTargetUnitTest(input_msg, output_msg,
193 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600194
195 def testNoResultPathFails(self):
196 """Test missing result path fails."""
197 # Missing result_path.
198 input_msg = self._GetInput(board='board')
199 output_msg = self._GetOutput()
200 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600201 test_controller.BuildTargetUnitTest(input_msg, output_msg,
202 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600203
Alex Klein64ac34c2020-09-23 10:21:33 -0600204 def testInvalidPackageFails(self):
205 """Test missing result path fails."""
206 # Missing result_path.
207 pkg = package_info.PackageInfo(package='bar')
208 input_msg = self._GetInput(board='board', result_path=self.tempdir,
209 packages=[pkg])
210 output_msg = self._GetOutput()
211 with self.assertRaises(cros_build_lib.DieSystemExit):
212 test_controller.BuildTargetUnitTest(input_msg, output_msg,
213 self.api_config)
214
Alex Kleina2e42c42019-04-17 16:13:19 -0600215 def testPackageBuildFailure(self):
216 """Test handling of raised BuildPackageFailure."""
217 tempdir = osutils.TempDir(base_dir=self.tempdir)
218 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
219
220 pkgs = ['cat/pkg', 'foo/bar']
221 expected = [('cat', 'pkg'), ('foo', 'bar')]
Alex Kleina2e42c42019-04-17 16:13:19 -0600222
Alex Klein38c7d9e2019-05-08 09:31:19 -0600223 result = test_service.BuildTargetUnitTestResult(1, None)
Alex Kleinea0c89e2021-09-09 15:17:35 -0600224 result.failed_pkgs = [package_info.parse(p) for p in pkgs]
Alex Klein38c7d9e2019-05-08 09:31:19 -0600225 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600226
227 input_msg = self._GetInput(board='board', result_path=self.tempdir)
228 output_msg = self._GetOutput()
229
Alex Klein231d2da2019-07-22 16:44:45 -0600230 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
231 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600232
Alex Klein8cb365a2019-05-15 16:24:53 -0600233 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600234 self.assertTrue(output_msg.failed_packages)
235 failed = []
236 for pi in output_msg.failed_packages:
237 failed.append((pi.category, pi.package_name))
Mike Frysinger678735c2019-09-28 18:23:28 -0400238 self.assertCountEqual(expected, failed)
Alex Kleina2e42c42019-04-17 16:13:19 -0600239
240 def testOtherBuildScriptFailure(self):
241 """Test build script failure due to non-package emerge error."""
242 tempdir = osutils.TempDir(base_dir=self.tempdir)
243 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
244
Alex Klein38c7d9e2019-05-08 09:31:19 -0600245 result = test_service.BuildTargetUnitTestResult(1, None)
246 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600247
Alex Kleinf2674462019-05-16 16:47:24 -0600248 pkgs = ['foo/bar', 'cat/pkg']
Alex Kleinb64e5f82020-09-23 10:55:31 -0600249 blocklist = [package_info.SplitCPV(p, strict=False) for p in pkgs]
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600250 input_msg = self._GetInput(board='board', result_path=self.tempdir,
Alex Kleinb64e5f82020-09-23 10:55:31 -0600251 empty_sysroot=True, blocklist=blocklist)
Alex Kleina2e42c42019-04-17 16:13:19 -0600252 output_msg = self._GetOutput()
253
Alex Klein231d2da2019-07-22 16:44:45 -0600254 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
255 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600256
Alex Klein8cb365a2019-05-15 16:24:53 -0600257 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600258 self.assertFalse(output_msg.failed_packages)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600259
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700260 def testBuildTargetUnitTest(self):
261 """Test BuildTargetUnitTest successful call."""
Navil Perezc0b29a82020-07-07 14:17:48 +0000262 pkgs = ['foo/bar', 'cat/pkg']
Alex Klein18a60af2020-06-11 12:08:47 -0600263 packages = [package_info.SplitCPV(p, strict=False) for p in pkgs]
Navil Perezc0b29a82020-07-07 14:17:48 +0000264 input_msg = self._GetInput(
265 board='board', result_path=self.tempdir, packages=packages)
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700266
267 result = test_service.BuildTargetUnitTestResult(0, None)
268 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
269
270 tarball_result = os.path.join(input_msg.result_path, 'unit_tests.tar')
271 self.PatchObject(test_service, 'BuildTargetUnitTestTarball',
272 return_value=tarball_result)
273
274 response = self._GetOutput()
275 test_controller.BuildTargetUnitTest(input_msg, response,
276 self.api_config)
277 self.assertEqual(response.tarball_path,
278 os.path.join(input_msg.result_path, 'unit_tests.tar'))
279
Evan Hernandez4e388a52019-05-01 12:16:33 -0600280
C Shapiro91af1ce2021-06-17 12:42:09 -0500281class BuildTestServiceContainers(cros_test_lib.MockTestCase,
David Wellingc1433c22021-06-25 16:29:48 +0000282 api_config.ApiConfigMixin):
C Shapiro91af1ce2021-06-17 12:42:09 -0500283 """Tests for the BuildTestServiceContainers function."""
284
285 def setUp(self):
286 self.request = test_pb2.BuildTestServiceContainersRequest(
287 chroot={'path': '/path/to/chroot'},
288 build_target={'name': 'build_target'},
David Wellingc1433c22021-06-25 16:29:48 +0000289 version='R93-14033.0.0',
C Shapiro91af1ce2021-06-17 12:42:09 -0500290 )
291
C Shapiro91af1ce2021-06-17 12:42:09 -0500292 def testSuccess(self):
293 """Check passing case with mocked cros_build_lib.run."""
294 patch = self.PatchObject(
295 cros_build_lib, 'run',
296 return_value=cros_build_lib.CommandResult(returncode=0))
297
298 response = test_pb2.BuildTestServiceContainersResponse()
299 test_controller.BuildTestServiceContainers(
300 self.request,
301 response,
302 self.api_config)
303 patch.assert_called()
304 for result in response.results:
305 self.assertEqual(result.WhichOneof('result'), 'success')
306
C Shapiro91af1ce2021-06-17 12:42:09 -0500307 def testFailure(self):
308 """Check failure case with mocked cros_build_lib.run."""
309 patch = self.PatchObject(
310 cros_build_lib, 'run',
311 return_value=cros_build_lib.CommandResult(returncode=1))
312
313 response = test_pb2.BuildTestServiceContainersResponse()
314 test_controller.BuildTestServiceContainers(
315 self.request,
316 response,
317 self.api_config)
318 patch.assert_called()
319 for result in response.results:
320 self.assertEqual(result.WhichOneof('result'), 'failure')
321
322
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -0700323class ChromiteUnitTestTest(cros_test_lib.MockTestCase,
324 api_config.ApiConfigMixin):
325 """Tests for the ChromiteInfoTest function."""
326
327 def setUp(self):
328 self.board = 'board'
329 self.chroot_path = '/path/to/chroot'
330
331 def _GetInput(self, chroot_path=None):
332 """Helper to build an input message instance."""
333 proto = test_pb2.ChromiteUnitTestRequest(
334 chroot={'path': chroot_path},
335 )
336 return proto
337
338 def _GetOutput(self):
339 """Helper to get an empty output message instance."""
340 return test_pb2.ChromiteUnitTestResponse()
341
342 def testValidateOnly(self):
343 """Sanity check that a validate only call does not execute any logic."""
344 patch = self.PatchObject(cros_build_lib, 'run')
345
346 input_msg = self._GetInput(chroot_path=self.chroot_path)
347 test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
348 self.validate_only_config)
349 patch.assert_not_called()
350
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700351 def testMockError(self):
352 """Test mock error call does not execute any logic, returns error."""
353 patch = self.PatchObject(cros_build_lib, 'run')
354
355 input_msg = self._GetInput(chroot_path=self.chroot_path)
356 rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
357 self.mock_error_config)
358 patch.assert_not_called()
359 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
360
361 def testMockCall(self):
362 """Test mock call does not execute any logic, returns success."""
363 patch = self.PatchObject(cros_build_lib, 'run')
364
365 input_msg = self._GetInput(chroot_path=self.chroot_path)
366 rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
367 self.mock_call_config)
368 patch.assert_not_called()
369 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
370
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -0700371 def testChromiteUnitTest(self):
372 """Call ChromiteUnitTest with mocked cros_build_lib.run."""
373 request = self._GetInput(chroot_path=self.chroot_path)
374 patch = self.PatchObject(
375 cros_build_lib, 'run',
376 return_value=cros_build_lib.CommandResult(returncode=0))
377
378 test_controller.ChromiteUnitTest(request, self._GetOutput(),
379 self.api_config)
380 patch.assert_called_once()
381
382
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600383class CrosSigningTestTest(cros_test_lib.RunCommandTestCase,
384 api_config.ApiConfigMixin):
385 """CrosSigningTest tests."""
386
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700387 def setUp(self):
388 self.chroot_path = '/path/to/chroot'
389
390 def _GetInput(self, chroot_path=None):
391 """Helper to build an input message instance."""
392 proto = test_pb2.CrosSigningTestRequest(
393 chroot={'path': chroot_path},
394 )
395 return proto
396
397 def _GetOutput(self):
398 """Helper to get an empty output message instance."""
399 return test_pb2.CrosSigningTestResponse()
400
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600401 def testValidateOnly(self):
402 """Sanity check that a validate only call does not execute any logic."""
403 test_controller.CrosSigningTest(None, None, self.validate_only_config)
404 self.assertFalse(self.rc.call_count)
405
Michael Mortensen7a7646d2019-12-12 15:36:14 -0700406 def testMockCall(self):
407 """Test mock call does not execute any logic, returns success."""
408 rc = test_controller.CrosSigningTest(None, None, self.mock_call_config)
409 self.assertFalse(self.rc.call_count)
410 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
411
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700412 def testCrosSigningTest(self):
413 """Call CrosSigningTest with mocked cros_build_lib.run."""
414 request = self._GetInput(chroot_path=self.chroot_path)
415 patch = self.PatchObject(
416 cros_build_lib, 'run',
417 return_value=cros_build_lib.CommandResult(returncode=0))
418
419 test_controller.CrosSigningTest(request, self._GetOutput(),
420 self.api_config)
421 patch.assert_called_once()
422
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600423
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600424class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase,
425 api_config.ApiConfigMixin):
426 """Test the SimpleChromeWorkflowTest endpoint."""
427
428 @staticmethod
429 def _Output():
430 return test_pb2.SimpleChromeWorkflowTestResponse()
431
David Wellingc1433c22021-06-25 16:29:48 +0000432 def _Input(self,
433 sysroot_path=None,
434 build_target=None,
435 chrome_root=None,
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600436 goma_config=None):
437 proto = test_pb2.SimpleChromeWorkflowTestRequest()
438 if sysroot_path:
439 proto.sysroot.path = sysroot_path
440 if build_target:
441 proto.sysroot.build_target.name = build_target
442 if chrome_root:
443 proto.chrome_root = chrome_root
444 if goma_config:
445 proto.goma_config = goma_config
446 return proto
447
448 def setUp(self):
449 self.chrome_path = 'path/to/chrome'
450 self.sysroot_dir = 'build/board'
451 self.build_target = 'amd64'
452 self.mock_simple_chrome_workflow_test = self.PatchObject(
453 test_service, 'SimpleChromeWorkflowTest')
454
455 def testMissingBuildTarget(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700456 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600457 input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir',
458 chrome_root='/chrome/path')
459 with self.assertRaises(cros_build_lib.DieSystemExit):
460 test_controller.SimpleChromeWorkflowTest(input_proto, None,
461 self.api_config)
462
463 def testMissingSysrootPath(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700464 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600465 input_proto = self._Input(build_target='board', sysroot_path=None,
466 chrome_root='/chrome/path')
467 with self.assertRaises(cros_build_lib.DieSystemExit):
468 test_controller.SimpleChromeWorkflowTest(input_proto, None,
469 self.api_config)
470
471 def testMissingChromeRoot(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700472 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600473 input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir',
474 chrome_root=None)
475 with self.assertRaises(cros_build_lib.DieSystemExit):
476 test_controller.SimpleChromeWorkflowTest(input_proto, None,
477 self.api_config)
478
479 def testSimpleChromeWorkflowTest(self):
480 """Call SimpleChromeWorkflowTest with valid args and temp dir."""
481 request = self._Input(sysroot_path='sysroot_path', build_target='board',
482 chrome_root='/path/to/chrome')
483 response = self._Output()
484
485 test_controller.SimpleChromeWorkflowTest(request, response, self.api_config)
486 self.mock_simple_chrome_workflow_test.assert_called()
487
488 def testValidateOnly(self):
489 request = self._Input(sysroot_path='sysroot_path', build_target='board',
490 chrome_root='/path/to/chrome')
491 test_controller.SimpleChromeWorkflowTest(request, self._Output(),
492 self.validate_only_config)
493 self.mock_simple_chrome_workflow_test.assert_not_called()
494
Michael Mortensen7a7646d2019-12-12 15:36:14 -0700495 def testMockCall(self):
496 """Test mock call does not execute any logic, returns success."""
497 patch = self.mock_simple_chrome_workflow_test = self.PatchObject(
498 test_service, 'SimpleChromeWorkflowTest')
499
500 request = self._Input(sysroot_path='sysroot_path', build_target='board',
501 chrome_root='/path/to/chrome')
502 rc = test_controller.SimpleChromeWorkflowTest(request, self._Output(),
503 self.mock_call_config)
504 patch.assert_not_called()
505 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
506
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600507
Alex Klein231d2da2019-07-22 16:44:45 -0600508class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin):
Evan Hernandez4e388a52019-05-01 12:16:33 -0600509 """Test the VmTest endpoint."""
510
511 def _GetInput(self, **kwargs):
512 values = dict(
513 build_target=common_pb2.BuildTarget(name='target'),
Alex Klein311b8022019-06-05 16:00:07 -0600514 vm_path=common_pb2.Path(path='/path/to/image.bin',
515 location=common_pb2.Path.INSIDE),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600516 test_harness=test_pb2.VmTestRequest.TAST,
517 vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')],
518 ssh_options=test_pb2.VmTestRequest.SshOptions(
Alex Klein231d2da2019-07-22 16:44:45 -0600519 port=1234, private_key_path={'path': '/path/to/id_rsa',
Alex Kleinaa705412019-06-04 15:00:30 -0600520 'location': common_pb2.Path.INSIDE}),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600521 )
522 values.update(kwargs)
523 return test_pb2.VmTestRequest(**values)
524
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700525 def _Output(self):
526 return test_pb2.VmTestResponse()
527
Alex Klein231d2da2019-07-22 16:44:45 -0600528 def testValidateOnly(self):
529 """Sanity check that a validate only call does not execute any logic."""
530 test_controller.VmTest(self._GetInput(), None, self.validate_only_config)
531 self.assertEqual(0, self.rc.call_count)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600532
Michael Mortensen7a7646d2019-12-12 15:36:14 -0700533 def testMockCall(self):
534 """Test mock call does not execute any logic."""
535 patch = self.PatchObject(cros_build_lib, 'run')
536
537 request = self._GetInput()
538 response = self._Output()
539 # VmTest does not return a value, checking mocked value is flagged by lint.
540 test_controller.VmTest(request, response, self.mock_call_config)
541 patch.assert_not_called()
542
Evan Hernandez4e388a52019-05-01 12:16:33 -0600543 def testTastAllOptions(self):
544 """Test VmTest for Tast with all options set."""
Alex Klein231d2da2019-07-22 16:44:45 -0600545 test_controller.VmTest(self._GetInput(), None, self.api_config)
546 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700547 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600548 '--board', 'target',
549 '--image-path', '/path/to/image.bin',
550 '--tast', 'suite',
551 '--ssh-port', '1234',
552 '--private-key', '/path/to/id_rsa',
553 ])
554
555 def testAutotestAllOptions(self):
556 """Test VmTest for Autotest with all options set."""
557 input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST)
Alex Klein231d2da2019-07-22 16:44:45 -0600558 test_controller.VmTest(input_proto, None, self.api_config)
559 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700560 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600561 '--board', 'target',
562 '--image-path', '/path/to/image.bin',
563 '--autotest', 'suite',
564 '--ssh-port', '1234',
565 '--private-key', '/path/to/id_rsa',
Greg Edelstondcb0e912020-08-31 11:09:40 -0600566 '--test_that-args=--allow-chrome-crashes',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600567 ])
568
569 def testMissingBuildTarget(self):
570 """Test VmTest dies when build_target not set."""
571 input_proto = self._GetInput(build_target=None)
572 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600573 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600574
575 def testMissingVmImage(self):
576 """Test VmTest dies when vm_image not set."""
Alex Klein311b8022019-06-05 16:00:07 -0600577 input_proto = self._GetInput(vm_path=None)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600578 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600579 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600580
581 def testMissingTestHarness(self):
582 """Test VmTest dies when test_harness not specified."""
583 input_proto = self._GetInput(
584 test_harness=test_pb2.VmTestRequest.UNSPECIFIED)
585 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600586 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600587
588 def testMissingVmTests(self):
589 """Test VmTest dies when vm_tests not set."""
590 input_proto = self._GetInput(vm_tests=[])
591 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600592 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600593
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700594 def testVmTest(self):
595 """Call VmTest with valid args and temp dir."""
596 request = self._GetInput()
597 response = self._Output()
598 patch = self.PatchObject(
599 cros_build_lib, 'run',
600 return_value=cros_build_lib.CommandResult(returncode=0))
601
602 test_controller.VmTest(request, response, self.api_config)
603 patch.assert_called()
604
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600605
Alex Klein231d2da2019-07-22 16:44:45 -0600606class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600607 """Test the MoblabVmTest endpoint."""
608
609 @staticmethod
610 def _Payload(path):
611 return test_pb2.MoblabVmTestRequest.Payload(
612 path=common_pb2.Path(path=path))
613
614 @staticmethod
615 def _Output():
616 return test_pb2.MoblabVmTestResponse()
617
618 def _Input(self):
619 return test_pb2.MoblabVmTestRequest(
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600620 chroot=common_pb2.Chroot(path=self.chroot_dir),
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600621 image_payload=self._Payload(self.image_payload_dir),
622 cache_payloads=[self._Payload(self.autotest_payload_dir)])
623
624 def setUp(self):
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600625 self.chroot_dir = '/chroot'
626 self.chroot_tmp_dir = '/chroot/tmp'
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600627 self.image_payload_dir = '/payloads/image'
628 self.autotest_payload_dir = '/payloads/autotest'
629 self.builder = 'moblab-generic-vm/R12-3.4.5-67.890'
630 self.image_cache_dir = '/mnt/moblab/cache'
631 self.image_mount_dir = '/mnt/image'
632
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600633 self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir)
Evan Hernandez655e8042019-06-13 12:50:44 -0600634
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600635 self.mock_create_moblab_vms = self.PatchObject(
636 test_service, 'CreateMoblabVm')
637 self.mock_prepare_moblab_vm_image_cache = self.PatchObject(
638 test_service, 'PrepareMoblabVmImageCache',
639 return_value=self.image_cache_dir)
640 self.mock_run_moblab_vm_tests = self.PatchObject(
641 test_service, 'RunMoblabVmTest')
642 self.mock_validate_moblab_vm_tests = self.PatchObject(
643 test_service, 'ValidateMoblabVmTest')
644
645 @contextlib.contextmanager
Alex Klein38c7d9e2019-05-08 09:31:19 -0600646 def MockLoopbackPartitions(*_args, **_kwargs):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600647 mount = mock.MagicMock()
Evan Hernandez40ee7452019-06-13 12:51:43 -0600648 mount.Mount.return_value = [self.image_mount_dir]
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600649 yield mount
Alex Klein231d2da2019-07-22 16:44:45 -0600650
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600651 self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions)
652
Alex Klein231d2da2019-07-22 16:44:45 -0600653 def testValidateOnly(self):
654 """Sanity check that a validate only call does not execute any logic."""
655 test_controller.MoblabVmTest(self._Input(), self._Output(),
656 self.validate_only_config)
657 self.mock_create_moblab_vms.assert_not_called()
658
Michael Mortensen7a7646d2019-12-12 15:36:14 -0700659 def testMockCall(self):
660 """Test mock call does not execute any logic."""
661 patch = self.PatchObject(key_value_store, 'LoadFile')
662
663 # MoblabVmTest does not return a value, checking mocked value is flagged by
664 # lint.
665 test_controller.MoblabVmTest(self._Input(), self._Output(),
666 self.mock_call_config)
667 patch.assert_not_called()
668
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600669 def testImageContainsBuilder(self):
670 """MoblabVmTest calls service with correct args."""
671 request = self._Input()
672 response = self._Output()
673
674 self.PatchObject(
Mike Frysingere652ba12019-09-08 00:57:43 -0400675 key_value_store, 'LoadFile',
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600676 return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder})
677
Alex Klein231d2da2019-07-22 16:44:45 -0600678 test_controller.MoblabVmTest(request, response, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600679
680 self.assertEqual(
681 self.mock_create_moblab_vms.call_args_list,
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600682 [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600683 self.assertEqual(
684 self.mock_prepare_moblab_vm_image_cache.call_args_list,
685 [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])])
686 self.assertEqual(
687 self.mock_run_moblab_vm_tests.call_args_list,
Evan Hernandez655e8042019-06-13 12:50:44 -0600688 [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir,
689 mock.ANY)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600690 self.assertEqual(
691 self.mock_validate_moblab_vm_tests.call_args_list,
692 [mock.call(mock.ANY)])
693
694 def testImageMissingBuilder(self):
695 """MoblabVmTest dies when builder path not found in lsb-release."""
696 request = self._Input()
697 response = self._Output()
698
Mike Frysingere652ba12019-09-08 00:57:43 -0400699 self.PatchObject(key_value_store, 'LoadFile', return_value={})
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600700
701 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600702 test_controller.MoblabVmTest(request, response, self.api_config)
David Wellingc1433c22021-06-25 16:29:48 +0000703
704
705class GetArtifactsTest(cros_test_lib.MockTempDirTestCase):
706 """Test GetArtifacts."""
707
708 CODE_COVERAGE_LLVM_ARTIFACT_TYPE = (
709 common_pb2.ArtifactsByService.Test.ArtifactType.CODE_COVERAGE_LLVM_JSON
710 )
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600711 UNIT_TEST_ARTIFACT_TYPE = (
712 common_pb2.ArtifactsByService.Test.ArtifactType.UNIT_TESTS
713 )
David Wellingc1433c22021-06-25 16:29:48 +0000714
715 def setUp(self):
716 """Set up the class for tests."""
717 chroot_dir = os.path.join(self.tempdir, 'chroot')
718 osutils.SafeMakedirs(chroot_dir)
719 osutils.SafeMakedirs(os.path.join(chroot_dir, 'tmp'))
720 self.chroot = chroot_lib.Chroot(chroot_dir)
721
722 sysroot_path = os.path.join(chroot_dir, 'build', 'board')
723 osutils.SafeMakedirs(sysroot_path)
724 self.sysroot = sysroot_lib.Sysroot(sysroot_path)
725
Jack Neusc9707c32021-07-23 21:48:54 +0000726 self.build_target = build_target_lib.BuildTarget('board')
727
David Wellingc1433c22021-06-25 16:29:48 +0000728 def testReturnsEmptyListWhenNoOutputArtifactsProvided(self):
729 """Test empty list is returned when there are no output_artifacts."""
730 result = test_controller.GetArtifacts(
731 common_pb2.ArtifactsByService.Test(output_artifacts=[]),
Jack Neusc9707c32021-07-23 21:48:54 +0000732 self.chroot, self.sysroot, self.build_target, self.tempdir)
David Wellingc1433c22021-06-25 16:29:48 +0000733
734 self.assertEqual(len(result), 0)
735
736 def testShouldCallBundleCodeCoverageLlvmJsonForEachValidArtifact(self):
737 """Test BundleCodeCoverageLlvmJson is called on each valid artifact."""
738 BundleCodeCoverageLlvmJson_mock = self.PatchObject(
739 test_service, 'BundleCodeCoverageLlvmJson', return_value='test')
740
741 test_controller.GetArtifacts(
742 common_pb2.ArtifactsByService.Test(output_artifacts=[
743 # Valid
744 common_pb2.ArtifactsByService.Test.ArtifactInfo(
745 artifact_types=[
746 self.CODE_COVERAGE_LLVM_ARTIFACT_TYPE
747 ]
748 ),
749
750 # Invalid
751 common_pb2.ArtifactsByService.Test.ArtifactInfo(
752 artifact_types=[
753 common_pb2.ArtifactsByService.Test.ArtifactType.UNIT_TESTS
754 ]
755 ),
756 ]),
Jack Neusc9707c32021-07-23 21:48:54 +0000757 self.chroot, self.sysroot, self.build_target, self.tempdir)
David Wellingc1433c22021-06-25 16:29:48 +0000758
759 BundleCodeCoverageLlvmJson_mock.assert_called_once()
760
761 def testShouldReturnValidResult(self):
762 """Test result contains paths and code_coverage_llvm_json type."""
763 self.PatchObject(test_service, 'BundleCodeCoverageLlvmJson',
764 return_value='test')
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600765 self.PatchObject(test_service, 'BuildTargetUnitTestTarball',
766 return_value='unit_tests.tar')
David Wellingc1433c22021-06-25 16:29:48 +0000767
768 result = test_controller.GetArtifacts(
769 common_pb2.ArtifactsByService.Test(output_artifacts=[
770 # Valid
771 common_pb2.ArtifactsByService.Test.ArtifactInfo(
772 artifact_types=[
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600773 self.UNIT_TEST_ARTIFACT_TYPE
774 ]
775 ),
776 common_pb2.ArtifactsByService.Test.ArtifactInfo(
777 artifact_types=[
David Wellingc1433c22021-06-25 16:29:48 +0000778 self.CODE_COVERAGE_LLVM_ARTIFACT_TYPE
779 ]
780 ),
781 ]),
Jack Neusc9707c32021-07-23 21:48:54 +0000782 self.chroot, self.sysroot, self.build_target, self.tempdir)
David Wellingc1433c22021-06-25 16:29:48 +0000783
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600784 self.assertEqual(result[0]['paths'], ['unit_tests.tar'])
785 self.assertEqual(result[0]['type'], self.UNIT_TEST_ARTIFACT_TYPE)
786 self.assertEqual(result[1]['paths'], ['test'])
787 self.assertEqual(result[1]['type'], self.CODE_COVERAGE_LLVM_ARTIFACT_TYPE)
Andrew Lamb763e3be2021-07-27 17:22:02 -0600788
789
790class GetCoverageRulesTest(cros_test_lib.RunCommandTempDirTestCase,
791 api_config.ApiConfigMixin):
792 """Tests for GetCoverageRules."""
793
Andrew Lambd814afa2021-08-11 11:04:20 -0600794 def _Input(self):
Andrew Lamb763e3be2021-07-27 17:22:02 -0600795 """Returns a sample GetCoverageRulesRequest for testing."""
Andrew Lambd814afa2021-08-11 11:04:20 -0600796 build_metadata_list_path = os.path.join(self.tempdir,
797 'build_metadata_list.jsonproto')
798 build_metadata_list = system_image_pb2.SystemImage.BuildMetadataList(
799 values=[
Andrew Lamb763e3be2021-07-27 17:22:02 -0600800 system_image_pb2.SystemImage.BuildMetadata(
801 build_target=system_image_pb2.SystemImage.BuildTarget(
802 portage_build_target=portage_pb2.Portage.BuildTarget(
803 overlay_name='overlayA')),
804 package_summary=system_image_pb2.SystemImage.BuildMetadata
805 .PackageSummary(
806 kernel=system_image_pb2.SystemImage.BuildMetadata.Kernel(
807 version='4.4')))
Andrew Lambd814afa2021-08-11 11:04:20 -0600808 ])
809 osutils.WriteFile(build_metadata_list_path,
810 json_format.MessageToJson(build_metadata_list))
811
812 dut_attribute_list_path = os.path.join(self.tempdir,
813 'dut_attribute_list.jsonproto')
814 dut_attribute_list = dut_attribute_pb2.DutAttributeList(dut_attributes=[
815 dut_attribute_pb2.DutAttribute(
816 id=dut_attribute_pb2.DutAttribute.Id(value='system_build_target'))
817 ])
818 osutils.WriteFile(dut_attribute_list_path,
819 json_format.MessageToJson(dut_attribute_list))
820
821 flat_config_list_path = os.path.join(self.tempdir,
822 'flat_config_list.jsonproto')
823 flat_config_list = flat_config_pb2.FlatConfigList(values=[
824 flat_config_pb2.FlatConfig(
825 hw_design=design_pb2.Design(
826 id=design_id_pb2.DesignId(value='design1')
827 )
828 )
829 ])
830 osutils.WriteFile(flat_config_list_path,
831 json_format.MessageToJson(flat_config_list))
832
833 return test_pb2.GetCoverageRulesRequest(
834 source_test_plans=[
835 source_test_plan_pb2.SourceTestPlan(
836 requirements=source_test_plan_pb2.SourceTestPlan.Requirements(
837 kernel_versions=source_test_plan_pb2.SourceTestPlan
838 .Requirements.KernelVersions()),
839 test_tags=['kernel']),
840 ],
841 build_metadata_list=common_pb2.Path(
842 path=build_metadata_list_path, location=common_pb2.Path.OUTSIDE),
843 dut_attribute_list=common_pb2.Path(
844 path=dut_attribute_list_path, location=common_pb2.Path.OUTSIDE),
845 flat_config_list=common_pb2.Path(
846 path=flat_config_list_path, location=common_pb2.Path.OUTSIDE),
847 )
Andrew Lamb763e3be2021-07-27 17:22:02 -0600848
849 @staticmethod
850 def _Output():
851 """Returns a sample GetCoverageRulesResponse for testing."""
852 return test_pb2.GetCoverageRulesResponse(coverage_rules=[
853 coverage_rule_pb2.CoverageRule(
854 name='kernel:4.4',
855 test_suites=[
856 test_suite_pb2.TestSuite(
857 test_case_tag_criteria=test_suite_pb2.TestSuite
858 .TestCaseTagCriteria(tags=['kernel']))
859 ],
860 dut_criteria=[
861 dut_attribute_pb2.DutCriterion(
862 attribute_id=dut_attribute_pb2.DutAttribute.Id(
863 value='system_build_target'),
864 values=['overlayA'],
865 )
866 ])
867 ])
868
869 @staticmethod
870 def _write_coverage_rules(path, coverage_rules):
871 """Write a list of CoverageRules in the same format as testplan."""
872 osutils.WriteFile(
873 path, '\n'.join(
874 json_format.MessageToJson(rule).replace('\n', '')
875 for rule in coverage_rules))
876
877 def testWritesInputsAndReturnsCoverageRules(self):
878 """Test inputs are written, and output of testplan is parsed."""
879 output_proto = test_pb2.GetCoverageRulesResponse()
880
881 self.rc.SetDefaultCmdResult(
882 side_effect=lambda _: self._write_coverage_rules(
883 os.path.join(self.tempdir, 'out.jsonpb'),
884 self._Output().coverage_rules))
885 self.PatchObject(osutils.TempDir, '__enter__', return_value=self.tempdir)
886
887 test_controller.GetCoverageRules(self._Input(), output_proto,
888 self.api_config)
889
Andrew Lamb763e3be2021-07-27 17:22:02 -0600890 self.assertEqual(output_proto, self._Output())