blob: 43c2d1b0aeddf1166d039a55ae77a7afde6e6394 [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 Frysingeref94e4c2020-02-10 23:59:54 -05009
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import api_config
Alex Klein8cb365a2019-05-15 16:24:53 -060011from chromite.api import controller
Alex Kleina2e42c42019-04-17 16:13:19 -060012from chromite.api.controller import test as test_controller
Evan Hernandez4e388a52019-05-01 12:16:33 -060013from chromite.api.gen.chromiumos import common_pb2
Alex Kleina2e42c42019-04-17 16:13:19 -060014from chromite.api.gen.chromite.api import test_pb2
Evan Hernandeze1e05d32019-07-19 12:32:18 -060015from chromite.lib import chroot_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060016from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060018from chromite.lib import image_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060019from chromite.lib import osutils
Alex Klein18a60af2020-06-11 12:08:47 -060020from chromite.lib.parser import package_info
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060021from chromite.scripts import cros_set_lsb_release
22from chromite.service import test as test_service
Mike Frysinger40ffb532021-02-12 07:36:08 -050023from chromite.third_party import mock
Mike Frysingere652ba12019-09-08 00:57:43 -040024from chromite.utils import key_value_store
Alex Kleina2e42c42019-04-17 16:13:19 -060025
26
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -070027class DebugInfoTestTest(cros_test_lib.MockTempDirTestCase,
28 api_config.ApiConfigMixin):
29 """Tests for the DebugInfoTest function."""
30
31 def setUp(self):
32 self.board = 'board'
33 self.chroot_path = os.path.join(self.tempdir, 'chroot')
34 self.sysroot_path = '/build/board'
35 self.full_sysroot_path = os.path.join(self.chroot_path,
36 self.sysroot_path.lstrip(os.sep))
37 osutils.SafeMakedirs(self.full_sysroot_path)
38
39 def _GetInput(self, sysroot_path=None, build_target=None):
40 """Helper to build an input message instance."""
41 proto = test_pb2.DebugInfoTestRequest()
42 if sysroot_path:
43 proto.sysroot.path = sysroot_path
44 if build_target:
45 proto.sysroot.build_target.name = build_target
46 return proto
47
48 def _GetOutput(self):
49 """Helper to get an empty output message instance."""
50 return test_pb2.DebugInfoTestResponse()
51
52 def testValidateOnly(self):
53 """Sanity check that a validate only call does not execute any logic."""
54 patch = self.PatchObject(test_service, 'DebugInfoTest')
55 input_msg = self._GetInput(sysroot_path=self.full_sysroot_path)
56 test_controller.DebugInfoTest(input_msg, self._GetOutput(),
57 self.validate_only_config)
58 patch.assert_not_called()
59
Michael Mortensen85d38402019-12-12 09:50:29 -070060 def testMockError(self):
61 """Test mock error call does not execute any logic, returns error."""
62 patch = self.PatchObject(test_service, 'DebugInfoTest')
63
64 input_msg = self._GetInput(sysroot_path=self.full_sysroot_path)
65 rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(),
66 self.mock_error_config)
67 patch.assert_not_called()
68 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
69
70 def testMockCall(self):
71 """Test mock call does not execute any logic, returns success."""
72 patch = self.PatchObject(test_service, 'DebugInfoTest')
73
74 input_msg = self._GetInput(sysroot_path=self.full_sysroot_path)
75 rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(),
76 self.mock_call_config)
77 patch.assert_not_called()
78 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
79
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -070080 def testNoBuildTargetNoSysrootFails(self):
81 """Test missing build target name and sysroot path fails."""
82 input_msg = self._GetInput()
83 output_msg = self._GetOutput()
84 with self.assertRaises(cros_build_lib.DieSystemExit):
85 test_controller.DebugInfoTest(input_msg, output_msg, self.api_config)
86
87 def testDebugInfoTest(self):
88 """Call DebugInfoTest with valid sysroot_path."""
89 request = self._GetInput(sysroot_path=self.full_sysroot_path)
90
91 test_controller.DebugInfoTest(request, self._GetOutput(), self.api_config)
92
93
Alex Klein231d2da2019-07-22 16:44:45 -060094class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase,
95 api_config.ApiConfigMixin):
Alex Kleina2e42c42019-04-17 16:13:19 -060096 """Tests for the UnitTest function."""
97
Navil Perezc0b29a82020-07-07 14:17:48 +000098 def _GetInput(self,
99 board=None,
100 result_path=None,
101 chroot_path=None,
102 cache_dir=None,
103 empty_sysroot=None,
104 packages=None,
Alex Kleinb64e5f82020-09-23 10:55:31 -0600105 blocklist=None):
Alex Kleina2e42c42019-04-17 16:13:19 -0600106 """Helper to build an input message instance."""
Navil Perezc0b29a82020-07-07 14:17:48 +0000107 formatted_packages = []
108 for pkg in packages or []:
109 formatted_packages.append({
110 'category': pkg.category,
111 'package_name': pkg.package
112 })
Alex Kleinb64e5f82020-09-23 10:55:31 -0600113 formatted_blocklist = []
114 for pkg in blocklist or []:
115 formatted_blocklist.append({'category': pkg.category,
Alex Kleinf2674462019-05-16 16:47:24 -0600116 'package_name': pkg.package})
117
Alex Kleina2e42c42019-04-17 16:13:19 -0600118 return test_pb2.BuildTargetUnitTestRequest(
119 build_target={'name': board}, result_path=result_path,
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600120 chroot={'path': chroot_path, 'cache_dir': cache_dir},
Alex Kleinf2674462019-05-16 16:47:24 -0600121 flags={'empty_sysroot': empty_sysroot},
Alex Klein64ac34c2020-09-23 10:21:33 -0600122 packages=formatted_packages,
Alex Kleinb64e5f82020-09-23 10:55:31 -0600123 package_blacklist=formatted_blocklist,
Alex Kleina2e42c42019-04-17 16:13:19 -0600124 )
125
126 def _GetOutput(self):
127 """Helper to get an empty output message instance."""
128 return test_pb2.BuildTargetUnitTestResponse()
129
Alex Klein231d2da2019-07-22 16:44:45 -0600130 def testValidateOnly(self):
131 """Sanity check that a validate only call does not execute any logic."""
132 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
133
134 input_msg = self._GetInput(board='board', result_path=self.tempdir)
135 test_controller.BuildTargetUnitTest(input_msg, self._GetOutput(),
136 self.validate_only_config)
137 patch.assert_not_called()
138
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700139 def testMockCall(self):
140 """Test that a mock call does not execute logic, returns mocked value."""
141 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
142
143 input_msg = self._GetInput(board='board', result_path=self.tempdir)
144 response = self._GetOutput()
145 test_controller.BuildTargetUnitTest(input_msg, response,
146 self.mock_call_config)
147 patch.assert_not_called()
148 self.assertEqual(response.tarball_path,
149 os.path.join(input_msg.result_path, 'unit_tests.tar'))
150
151 def testMockError(self):
Michael Mortensen85d38402019-12-12 09:50:29 -0700152 """Test that a mock error does not execute logic, returns error."""
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700153 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
154
155 input_msg = self._GetInput(board='board', result_path=self.tempdir)
156 response = self._GetOutput()
157 rc = test_controller.BuildTargetUnitTest(input_msg, response,
158 self.mock_error_config)
159 patch.assert_not_called()
160 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
161 self.assertTrue(response.failed_packages)
162 self.assertEqual(response.failed_packages[0].category, 'foo')
163 self.assertEqual(response.failed_packages[0].package_name, 'bar')
164 self.assertEqual(response.failed_packages[1].category, 'cat')
165 self.assertEqual(response.failed_packages[1].package_name, 'pkg')
166
Alex Kleina2e42c42019-04-17 16:13:19 -0600167 def testNoArgumentFails(self):
168 """Test no arguments fails."""
169 input_msg = self._GetInput()
170 output_msg = self._GetOutput()
171 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600172 test_controller.BuildTargetUnitTest(input_msg, output_msg,
173 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600174
175 def testNoBuildTargetFails(self):
176 """Test missing build target name fails."""
177 input_msg = self._GetInput(result_path=self.tempdir)
178 output_msg = self._GetOutput()
179 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600180 test_controller.BuildTargetUnitTest(input_msg, output_msg,
181 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600182
183 def testNoResultPathFails(self):
184 """Test missing result path fails."""
185 # Missing result_path.
186 input_msg = self._GetInput(board='board')
187 output_msg = self._GetOutput()
188 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600189 test_controller.BuildTargetUnitTest(input_msg, output_msg,
190 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600191
Alex Klein64ac34c2020-09-23 10:21:33 -0600192 def testInvalidPackageFails(self):
193 """Test missing result path fails."""
194 # Missing result_path.
195 pkg = package_info.PackageInfo(package='bar')
196 input_msg = self._GetInput(board='board', result_path=self.tempdir,
197 packages=[pkg])
198 output_msg = self._GetOutput()
199 with self.assertRaises(cros_build_lib.DieSystemExit):
200 test_controller.BuildTargetUnitTest(input_msg, output_msg,
201 self.api_config)
202
Alex Kleina2e42c42019-04-17 16:13:19 -0600203 def testPackageBuildFailure(self):
204 """Test handling of raised BuildPackageFailure."""
205 tempdir = osutils.TempDir(base_dir=self.tempdir)
206 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
207
208 pkgs = ['cat/pkg', 'foo/bar']
209 expected = [('cat', 'pkg'), ('foo', 'bar')]
Alex Kleina2e42c42019-04-17 16:13:19 -0600210
Alex Klein38c7d9e2019-05-08 09:31:19 -0600211 result = test_service.BuildTargetUnitTestResult(1, None)
Alex Klein18a60af2020-06-11 12:08:47 -0600212 result.failed_cpvs = [package_info.SplitCPV(p, strict=False) for p in pkgs]
Alex Klein38c7d9e2019-05-08 09:31:19 -0600213 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600214
215 input_msg = self._GetInput(board='board', result_path=self.tempdir)
216 output_msg = self._GetOutput()
217
Alex Klein231d2da2019-07-22 16:44:45 -0600218 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
219 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600220
Alex Klein8cb365a2019-05-15 16:24:53 -0600221 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600222 self.assertTrue(output_msg.failed_packages)
223 failed = []
224 for pi in output_msg.failed_packages:
225 failed.append((pi.category, pi.package_name))
Mike Frysinger678735c2019-09-28 18:23:28 -0400226 self.assertCountEqual(expected, failed)
Alex Kleina2e42c42019-04-17 16:13:19 -0600227
228 def testOtherBuildScriptFailure(self):
229 """Test build script failure due to non-package emerge error."""
230 tempdir = osutils.TempDir(base_dir=self.tempdir)
231 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
232
Alex Klein38c7d9e2019-05-08 09:31:19 -0600233 result = test_service.BuildTargetUnitTestResult(1, None)
234 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600235
Alex Kleinf2674462019-05-16 16:47:24 -0600236 pkgs = ['foo/bar', 'cat/pkg']
Alex Kleinb64e5f82020-09-23 10:55:31 -0600237 blocklist = [package_info.SplitCPV(p, strict=False) for p in pkgs]
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600238 input_msg = self._GetInput(board='board', result_path=self.tempdir,
Alex Kleinb64e5f82020-09-23 10:55:31 -0600239 empty_sysroot=True, blocklist=blocklist)
Alex Kleina2e42c42019-04-17 16:13:19 -0600240 output_msg = self._GetOutput()
241
Alex Klein231d2da2019-07-22 16:44:45 -0600242 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
243 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600244
Alex Klein8cb365a2019-05-15 16:24:53 -0600245 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600246 self.assertFalse(output_msg.failed_packages)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600247
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700248 def testBuildTargetUnitTest(self):
249 """Test BuildTargetUnitTest successful call."""
Navil Perezc0b29a82020-07-07 14:17:48 +0000250 pkgs = ['foo/bar', 'cat/pkg']
Alex Klein18a60af2020-06-11 12:08:47 -0600251 packages = [package_info.SplitCPV(p, strict=False) for p in pkgs]
Navil Perezc0b29a82020-07-07 14:17:48 +0000252 input_msg = self._GetInput(
253 board='board', result_path=self.tempdir, packages=packages)
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700254
255 result = test_service.BuildTargetUnitTestResult(0, None)
256 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
257
258 tarball_result = os.path.join(input_msg.result_path, 'unit_tests.tar')
259 self.PatchObject(test_service, 'BuildTargetUnitTestTarball',
260 return_value=tarball_result)
261
262 response = self._GetOutput()
263 test_controller.BuildTargetUnitTest(input_msg, response,
264 self.api_config)
265 self.assertEqual(response.tarball_path,
266 os.path.join(input_msg.result_path, 'unit_tests.tar'))
267
Evan Hernandez4e388a52019-05-01 12:16:33 -0600268
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -0700269class ChromiteUnitTestTest(cros_test_lib.MockTestCase,
270 api_config.ApiConfigMixin):
271 """Tests for the ChromiteInfoTest function."""
272
273 def setUp(self):
274 self.board = 'board'
275 self.chroot_path = '/path/to/chroot'
276
277 def _GetInput(self, chroot_path=None):
278 """Helper to build an input message instance."""
279 proto = test_pb2.ChromiteUnitTestRequest(
280 chroot={'path': chroot_path},
281 )
282 return proto
283
284 def _GetOutput(self):
285 """Helper to get an empty output message instance."""
286 return test_pb2.ChromiteUnitTestResponse()
287
288 def testValidateOnly(self):
289 """Sanity check that a validate only call does not execute any logic."""
290 patch = self.PatchObject(cros_build_lib, 'run')
291
292 input_msg = self._GetInput(chroot_path=self.chroot_path)
293 test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
294 self.validate_only_config)
295 patch.assert_not_called()
296
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700297 def testMockError(self):
298 """Test mock error call does not execute any logic, returns error."""
299 patch = self.PatchObject(cros_build_lib, 'run')
300
301 input_msg = self._GetInput(chroot_path=self.chroot_path)
302 rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
303 self.mock_error_config)
304 patch.assert_not_called()
305 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
306
307 def testMockCall(self):
308 """Test mock call does not execute any logic, returns success."""
309 patch = self.PatchObject(cros_build_lib, 'run')
310
311 input_msg = self._GetInput(chroot_path=self.chroot_path)
312 rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
313 self.mock_call_config)
314 patch.assert_not_called()
315 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
316
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -0700317 def testChromiteUnitTest(self):
318 """Call ChromiteUnitTest with mocked cros_build_lib.run."""
319 request = self._GetInput(chroot_path=self.chroot_path)
320 patch = self.PatchObject(
321 cros_build_lib, 'run',
322 return_value=cros_build_lib.CommandResult(returncode=0))
323
324 test_controller.ChromiteUnitTest(request, self._GetOutput(),
325 self.api_config)
326 patch.assert_called_once()
327
328
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600329class CrosSigningTestTest(cros_test_lib.RunCommandTestCase,
330 api_config.ApiConfigMixin):
331 """CrosSigningTest tests."""
332
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700333 def setUp(self):
334 self.chroot_path = '/path/to/chroot'
335
336 def _GetInput(self, chroot_path=None):
337 """Helper to build an input message instance."""
338 proto = test_pb2.CrosSigningTestRequest(
339 chroot={'path': chroot_path},
340 )
341 return proto
342
343 def _GetOutput(self):
344 """Helper to get an empty output message instance."""
345 return test_pb2.CrosSigningTestResponse()
346
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600347 def testValidateOnly(self):
348 """Sanity check that a validate only call does not execute any logic."""
349 test_controller.CrosSigningTest(None, None, self.validate_only_config)
350 self.assertFalse(self.rc.call_count)
351
Michael Mortensen7a7646d2019-12-12 15:36:14 -0700352 def testMockCall(self):
353 """Test mock call does not execute any logic, returns success."""
354 rc = test_controller.CrosSigningTest(None, None, self.mock_call_config)
355 self.assertFalse(self.rc.call_count)
356 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
357
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700358 def testCrosSigningTest(self):
359 """Call CrosSigningTest with mocked cros_build_lib.run."""
360 request = self._GetInput(chroot_path=self.chroot_path)
361 patch = self.PatchObject(
362 cros_build_lib, 'run',
363 return_value=cros_build_lib.CommandResult(returncode=0))
364
365 test_controller.CrosSigningTest(request, self._GetOutput(),
366 self.api_config)
367 patch.assert_called_once()
368
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600369
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600370class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase,
371 api_config.ApiConfigMixin):
372 """Test the SimpleChromeWorkflowTest endpoint."""
373
374 @staticmethod
375 def _Output():
376 return test_pb2.SimpleChromeWorkflowTestResponse()
377
378 def _Input(self, sysroot_path=None, build_target=None, chrome_root=None,
379 goma_config=None):
380 proto = test_pb2.SimpleChromeWorkflowTestRequest()
381 if sysroot_path:
382 proto.sysroot.path = sysroot_path
383 if build_target:
384 proto.sysroot.build_target.name = build_target
385 if chrome_root:
386 proto.chrome_root = chrome_root
387 if goma_config:
388 proto.goma_config = goma_config
389 return proto
390
391 def setUp(self):
392 self.chrome_path = 'path/to/chrome'
393 self.sysroot_dir = 'build/board'
394 self.build_target = 'amd64'
395 self.mock_simple_chrome_workflow_test = self.PatchObject(
396 test_service, 'SimpleChromeWorkflowTest')
397
398 def testMissingBuildTarget(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700399 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600400 input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir',
401 chrome_root='/chrome/path')
402 with self.assertRaises(cros_build_lib.DieSystemExit):
403 test_controller.SimpleChromeWorkflowTest(input_proto, None,
404 self.api_config)
405
406 def testMissingSysrootPath(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700407 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600408 input_proto = self._Input(build_target='board', sysroot_path=None,
409 chrome_root='/chrome/path')
410 with self.assertRaises(cros_build_lib.DieSystemExit):
411 test_controller.SimpleChromeWorkflowTest(input_proto, None,
412 self.api_config)
413
414 def testMissingChromeRoot(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700415 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600416 input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir',
417 chrome_root=None)
418 with self.assertRaises(cros_build_lib.DieSystemExit):
419 test_controller.SimpleChromeWorkflowTest(input_proto, None,
420 self.api_config)
421
422 def testSimpleChromeWorkflowTest(self):
423 """Call SimpleChromeWorkflowTest with valid args and temp dir."""
424 request = self._Input(sysroot_path='sysroot_path', build_target='board',
425 chrome_root='/path/to/chrome')
426 response = self._Output()
427
428 test_controller.SimpleChromeWorkflowTest(request, response, self.api_config)
429 self.mock_simple_chrome_workflow_test.assert_called()
430
431 def testValidateOnly(self):
432 request = self._Input(sysroot_path='sysroot_path', build_target='board',
433 chrome_root='/path/to/chrome')
434 test_controller.SimpleChromeWorkflowTest(request, self._Output(),
435 self.validate_only_config)
436 self.mock_simple_chrome_workflow_test.assert_not_called()
437
Michael Mortensen7a7646d2019-12-12 15:36:14 -0700438 def testMockCall(self):
439 """Test mock call does not execute any logic, returns success."""
440 patch = self.mock_simple_chrome_workflow_test = self.PatchObject(
441 test_service, 'SimpleChromeWorkflowTest')
442
443 request = self._Input(sysroot_path='sysroot_path', build_target='board',
444 chrome_root='/path/to/chrome')
445 rc = test_controller.SimpleChromeWorkflowTest(request, self._Output(),
446 self.mock_call_config)
447 patch.assert_not_called()
448 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
449
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600450
Alex Klein231d2da2019-07-22 16:44:45 -0600451class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin):
Evan Hernandez4e388a52019-05-01 12:16:33 -0600452 """Test the VmTest endpoint."""
453
454 def _GetInput(self, **kwargs):
455 values = dict(
456 build_target=common_pb2.BuildTarget(name='target'),
Alex Klein311b8022019-06-05 16:00:07 -0600457 vm_path=common_pb2.Path(path='/path/to/image.bin',
458 location=common_pb2.Path.INSIDE),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600459 test_harness=test_pb2.VmTestRequest.TAST,
460 vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')],
461 ssh_options=test_pb2.VmTestRequest.SshOptions(
Alex Klein231d2da2019-07-22 16:44:45 -0600462 port=1234, private_key_path={'path': '/path/to/id_rsa',
Alex Kleinaa705412019-06-04 15:00:30 -0600463 'location': common_pb2.Path.INSIDE}),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600464 )
465 values.update(kwargs)
466 return test_pb2.VmTestRequest(**values)
467
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700468 def _Output(self):
469 return test_pb2.VmTestResponse()
470
Alex Klein231d2da2019-07-22 16:44:45 -0600471 def testValidateOnly(self):
472 """Sanity check that a validate only call does not execute any logic."""
473 test_controller.VmTest(self._GetInput(), None, self.validate_only_config)
474 self.assertEqual(0, self.rc.call_count)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600475
Michael Mortensen7a7646d2019-12-12 15:36:14 -0700476 def testMockCall(self):
477 """Test mock call does not execute any logic."""
478 patch = self.PatchObject(cros_build_lib, 'run')
479
480 request = self._GetInput()
481 response = self._Output()
482 # VmTest does not return a value, checking mocked value is flagged by lint.
483 test_controller.VmTest(request, response, self.mock_call_config)
484 patch.assert_not_called()
485
Evan Hernandez4e388a52019-05-01 12:16:33 -0600486 def testTastAllOptions(self):
487 """Test VmTest for Tast with all options set."""
Alex Klein231d2da2019-07-22 16:44:45 -0600488 test_controller.VmTest(self._GetInput(), None, self.api_config)
489 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700490 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600491 '--board', 'target',
492 '--image-path', '/path/to/image.bin',
493 '--tast', 'suite',
494 '--ssh-port', '1234',
495 '--private-key', '/path/to/id_rsa',
496 ])
497
498 def testAutotestAllOptions(self):
499 """Test VmTest for Autotest with all options set."""
500 input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST)
Alex Klein231d2da2019-07-22 16:44:45 -0600501 test_controller.VmTest(input_proto, None, self.api_config)
502 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700503 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600504 '--board', 'target',
505 '--image-path', '/path/to/image.bin',
506 '--autotest', 'suite',
507 '--ssh-port', '1234',
508 '--private-key', '/path/to/id_rsa',
Greg Edelstondcb0e912020-08-31 11:09:40 -0600509 '--test_that-args=--allow-chrome-crashes',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600510 ])
511
512 def testMissingBuildTarget(self):
513 """Test VmTest dies when build_target not set."""
514 input_proto = self._GetInput(build_target=None)
515 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600516 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600517
518 def testMissingVmImage(self):
519 """Test VmTest dies when vm_image not set."""
Alex Klein311b8022019-06-05 16:00:07 -0600520 input_proto = self._GetInput(vm_path=None)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600521 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600522 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600523
524 def testMissingTestHarness(self):
525 """Test VmTest dies when test_harness not specified."""
526 input_proto = self._GetInput(
527 test_harness=test_pb2.VmTestRequest.UNSPECIFIED)
528 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600529 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600530
531 def testMissingVmTests(self):
532 """Test VmTest dies when vm_tests not set."""
533 input_proto = self._GetInput(vm_tests=[])
534 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600535 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600536
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700537 def testVmTest(self):
538 """Call VmTest with valid args and temp dir."""
539 request = self._GetInput()
540 response = self._Output()
541 patch = self.PatchObject(
542 cros_build_lib, 'run',
543 return_value=cros_build_lib.CommandResult(returncode=0))
544
545 test_controller.VmTest(request, response, self.api_config)
546 patch.assert_called()
547
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600548
Alex Klein231d2da2019-07-22 16:44:45 -0600549class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600550 """Test the MoblabVmTest endpoint."""
551
552 @staticmethod
553 def _Payload(path):
554 return test_pb2.MoblabVmTestRequest.Payload(
555 path=common_pb2.Path(path=path))
556
557 @staticmethod
558 def _Output():
559 return test_pb2.MoblabVmTestResponse()
560
561 def _Input(self):
562 return test_pb2.MoblabVmTestRequest(
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600563 chroot=common_pb2.Chroot(path=self.chroot_dir),
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600564 image_payload=self._Payload(self.image_payload_dir),
565 cache_payloads=[self._Payload(self.autotest_payload_dir)])
566
567 def setUp(self):
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600568 self.chroot_dir = '/chroot'
569 self.chroot_tmp_dir = '/chroot/tmp'
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600570 self.image_payload_dir = '/payloads/image'
571 self.autotest_payload_dir = '/payloads/autotest'
572 self.builder = 'moblab-generic-vm/R12-3.4.5-67.890'
573 self.image_cache_dir = '/mnt/moblab/cache'
574 self.image_mount_dir = '/mnt/image'
575
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600576 self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir)
Evan Hernandez655e8042019-06-13 12:50:44 -0600577
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600578 self.mock_create_moblab_vms = self.PatchObject(
579 test_service, 'CreateMoblabVm')
580 self.mock_prepare_moblab_vm_image_cache = self.PatchObject(
581 test_service, 'PrepareMoblabVmImageCache',
582 return_value=self.image_cache_dir)
583 self.mock_run_moblab_vm_tests = self.PatchObject(
584 test_service, 'RunMoblabVmTest')
585 self.mock_validate_moblab_vm_tests = self.PatchObject(
586 test_service, 'ValidateMoblabVmTest')
587
588 @contextlib.contextmanager
Alex Klein38c7d9e2019-05-08 09:31:19 -0600589 def MockLoopbackPartitions(*_args, **_kwargs):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600590 mount = mock.MagicMock()
Evan Hernandez40ee7452019-06-13 12:51:43 -0600591 mount.Mount.return_value = [self.image_mount_dir]
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600592 yield mount
Alex Klein231d2da2019-07-22 16:44:45 -0600593
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600594 self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions)
595
Alex Klein231d2da2019-07-22 16:44:45 -0600596 def testValidateOnly(self):
597 """Sanity check that a validate only call does not execute any logic."""
598 test_controller.MoblabVmTest(self._Input(), self._Output(),
599 self.validate_only_config)
600 self.mock_create_moblab_vms.assert_not_called()
601
Michael Mortensen7a7646d2019-12-12 15:36:14 -0700602 def testMockCall(self):
603 """Test mock call does not execute any logic."""
604 patch = self.PatchObject(key_value_store, 'LoadFile')
605
606 # MoblabVmTest does not return a value, checking mocked value is flagged by
607 # lint.
608 test_controller.MoblabVmTest(self._Input(), self._Output(),
609 self.mock_call_config)
610 patch.assert_not_called()
611
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600612 def testImageContainsBuilder(self):
613 """MoblabVmTest calls service with correct args."""
614 request = self._Input()
615 response = self._Output()
616
617 self.PatchObject(
Mike Frysingere652ba12019-09-08 00:57:43 -0400618 key_value_store, 'LoadFile',
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600619 return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder})
620
Alex Klein231d2da2019-07-22 16:44:45 -0600621 test_controller.MoblabVmTest(request, response, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600622
623 self.assertEqual(
624 self.mock_create_moblab_vms.call_args_list,
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600625 [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600626 self.assertEqual(
627 self.mock_prepare_moblab_vm_image_cache.call_args_list,
628 [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])])
629 self.assertEqual(
630 self.mock_run_moblab_vm_tests.call_args_list,
Evan Hernandez655e8042019-06-13 12:50:44 -0600631 [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir,
632 mock.ANY)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600633 self.assertEqual(
634 self.mock_validate_moblab_vm_tests.call_args_list,
635 [mock.call(mock.ANY)])
636
637 def testImageMissingBuilder(self):
638 """MoblabVmTest dies when builder path not found in lsb-release."""
639 request = self._Input()
640 response = self._Output()
641
Mike Frysingere652ba12019-09-08 00:57:43 -0400642 self.PatchObject(key_value_store, 'LoadFile', return_value={})
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600643
644 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600645 test_controller.MoblabVmTest(request, response, self.api_config)