blob: 516278078e4c99b37876984e9a03c082bceff8a4 [file] [log] [blame]
Alex Kleina2e42c42019-04-17 16:13:19 -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"""The test controller tests."""
7
8from __future__ import print_function
9
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -070010import os
11
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060012import contextlib
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060013import mock
14
Alex Klein231d2da2019-07-22 16:44:45 -060015from chromite.api import api_config
Alex Klein8cb365a2019-05-15 16:24:53 -060016from chromite.api import controller
Alex Kleina2e42c42019-04-17 16:13:19 -060017from chromite.api.controller import test as test_controller
Evan Hernandez4e388a52019-05-01 12:16:33 -060018from chromite.api.gen.chromiumos import common_pb2
Alex Kleina2e42c42019-04-17 16:13:19 -060019from chromite.api.gen.chromite.api import test_pb2
Evan Hernandeze1e05d32019-07-19 12:32:18 -060020from chromite.lib import chroot_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060021from chromite.lib import cros_build_lib
22from chromite.lib import cros_test_lib
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060023from chromite.lib import image_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060024from chromite.lib import osutils
25from chromite.lib import portage_util
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060026from chromite.scripts import cros_set_lsb_release
27from chromite.service import test as test_service
Mike Frysingere652ba12019-09-08 00:57:43 -040028from chromite.utils import key_value_store
Alex Kleina2e42c42019-04-17 16:13:19 -060029
30
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -070031class DebugInfoTestTest(cros_test_lib.MockTempDirTestCase,
32 api_config.ApiConfigMixin):
33 """Tests for the DebugInfoTest function."""
34
35 def setUp(self):
36 self.board = 'board'
37 self.chroot_path = os.path.join(self.tempdir, 'chroot')
38 self.sysroot_path = '/build/board'
39 self.full_sysroot_path = os.path.join(self.chroot_path,
40 self.sysroot_path.lstrip(os.sep))
41 osutils.SafeMakedirs(self.full_sysroot_path)
42
43 def _GetInput(self, sysroot_path=None, build_target=None):
44 """Helper to build an input message instance."""
45 proto = test_pb2.DebugInfoTestRequest()
46 if sysroot_path:
47 proto.sysroot.path = sysroot_path
48 if build_target:
49 proto.sysroot.build_target.name = build_target
50 return proto
51
52 def _GetOutput(self):
53 """Helper to get an empty output message instance."""
54 return test_pb2.DebugInfoTestResponse()
55
56 def testValidateOnly(self):
57 """Sanity check that a validate only call does not execute any logic."""
58 patch = self.PatchObject(test_service, 'DebugInfoTest')
59 input_msg = self._GetInput(sysroot_path=self.full_sysroot_path)
60 test_controller.DebugInfoTest(input_msg, self._GetOutput(),
61 self.validate_only_config)
62 patch.assert_not_called()
63
Michael Mortensen85d38402019-12-12 09:50:29 -070064 def testMockError(self):
65 """Test mock error call does not execute any logic, returns error."""
66 patch = self.PatchObject(test_service, 'DebugInfoTest')
67
68 input_msg = self._GetInput(sysroot_path=self.full_sysroot_path)
69 rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(),
70 self.mock_error_config)
71 patch.assert_not_called()
72 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
73
74 def testMockCall(self):
75 """Test mock call does not execute any logic, returns success."""
76 patch = self.PatchObject(test_service, 'DebugInfoTest')
77
78 input_msg = self._GetInput(sysroot_path=self.full_sysroot_path)
79 rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(),
80 self.mock_call_config)
81 patch.assert_not_called()
82 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
83
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -070084 def testNoBuildTargetNoSysrootFails(self):
85 """Test missing build target name and sysroot path fails."""
86 input_msg = self._GetInput()
87 output_msg = self._GetOutput()
88 with self.assertRaises(cros_build_lib.DieSystemExit):
89 test_controller.DebugInfoTest(input_msg, output_msg, self.api_config)
90
91 def testDebugInfoTest(self):
92 """Call DebugInfoTest with valid sysroot_path."""
93 request = self._GetInput(sysroot_path=self.full_sysroot_path)
94
95 test_controller.DebugInfoTest(request, self._GetOutput(), self.api_config)
96
97
Alex Klein231d2da2019-07-22 16:44:45 -060098class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase,
99 api_config.ApiConfigMixin):
Alex Kleina2e42c42019-04-17 16:13:19 -0600100 """Tests for the UnitTest function."""
101
102 def _GetInput(self, board=None, result_path=None, chroot_path=None,
Alex Kleinf2674462019-05-16 16:47:24 -0600103 cache_dir=None, empty_sysroot=None, blacklist=None):
Alex Kleina2e42c42019-04-17 16:13:19 -0600104 """Helper to build an input message instance."""
Alex Kleinf2674462019-05-16 16:47:24 -0600105 formatted_blacklist = []
106 for pkg in blacklist or []:
107 formatted_blacklist.append({'category': pkg.category,
108 'package_name': pkg.package})
109
Alex Kleina2e42c42019-04-17 16:13:19 -0600110 return test_pb2.BuildTargetUnitTestRequest(
111 build_target={'name': board}, result_path=result_path,
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600112 chroot={'path': chroot_path, 'cache_dir': cache_dir},
Alex Kleinf2674462019-05-16 16:47:24 -0600113 flags={'empty_sysroot': empty_sysroot},
114 package_blacklist=formatted_blacklist,
Alex Kleina2e42c42019-04-17 16:13:19 -0600115 )
116
117 def _GetOutput(self):
118 """Helper to get an empty output message instance."""
119 return test_pb2.BuildTargetUnitTestResponse()
120
Alex Klein231d2da2019-07-22 16:44:45 -0600121 def testValidateOnly(self):
122 """Sanity check that a validate only call does not execute any logic."""
123 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
124
125 input_msg = self._GetInput(board='board', result_path=self.tempdir)
126 test_controller.BuildTargetUnitTest(input_msg, self._GetOutput(),
127 self.validate_only_config)
128 patch.assert_not_called()
129
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700130 def testMockCall(self):
131 """Test that a mock call does not execute logic, returns mocked value."""
132 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
133
134 input_msg = self._GetInput(board='board', result_path=self.tempdir)
135 response = self._GetOutput()
136 test_controller.BuildTargetUnitTest(input_msg, response,
137 self.mock_call_config)
138 patch.assert_not_called()
139 self.assertEqual(response.tarball_path,
140 os.path.join(input_msg.result_path, 'unit_tests.tar'))
141
142 def testMockError(self):
Michael Mortensen85d38402019-12-12 09:50:29 -0700143 """Test that a mock error does not execute logic, returns error."""
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700144 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
145
146 input_msg = self._GetInput(board='board', result_path=self.tempdir)
147 response = self._GetOutput()
148 rc = test_controller.BuildTargetUnitTest(input_msg, response,
149 self.mock_error_config)
150 patch.assert_not_called()
151 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
152 self.assertTrue(response.failed_packages)
153 self.assertEqual(response.failed_packages[0].category, 'foo')
154 self.assertEqual(response.failed_packages[0].package_name, 'bar')
155 self.assertEqual(response.failed_packages[1].category, 'cat')
156 self.assertEqual(response.failed_packages[1].package_name, 'pkg')
157
Alex Kleina2e42c42019-04-17 16:13:19 -0600158 def testNoArgumentFails(self):
159 """Test no arguments fails."""
160 input_msg = self._GetInput()
161 output_msg = self._GetOutput()
162 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600163 test_controller.BuildTargetUnitTest(input_msg, output_msg,
164 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600165
166 def testNoBuildTargetFails(self):
167 """Test missing build target name fails."""
168 input_msg = self._GetInput(result_path=self.tempdir)
169 output_msg = self._GetOutput()
170 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600171 test_controller.BuildTargetUnitTest(input_msg, output_msg,
172 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600173
174 def testNoResultPathFails(self):
175 """Test missing result path fails."""
176 # Missing result_path.
177 input_msg = self._GetInput(board='board')
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 testPackageBuildFailure(self):
184 """Test handling of raised BuildPackageFailure."""
185 tempdir = osutils.TempDir(base_dir=self.tempdir)
186 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
187
188 pkgs = ['cat/pkg', 'foo/bar']
189 expected = [('cat', 'pkg'), ('foo', 'bar')]
Alex Kleina2e42c42019-04-17 16:13:19 -0600190
Alex Klein38c7d9e2019-05-08 09:31:19 -0600191 result = test_service.BuildTargetUnitTestResult(1, None)
192 result.failed_cpvs = [portage_util.SplitCPV(p, strict=False) for p in pkgs]
193 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600194
195 input_msg = self._GetInput(board='board', result_path=self.tempdir)
196 output_msg = self._GetOutput()
197
Alex Klein231d2da2019-07-22 16:44:45 -0600198 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
199 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600200
Alex Klein8cb365a2019-05-15 16:24:53 -0600201 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600202 self.assertTrue(output_msg.failed_packages)
203 failed = []
204 for pi in output_msg.failed_packages:
205 failed.append((pi.category, pi.package_name))
Mike Frysinger678735c2019-09-28 18:23:28 -0400206 self.assertCountEqual(expected, failed)
Alex Kleina2e42c42019-04-17 16:13:19 -0600207
208 def testOtherBuildScriptFailure(self):
209 """Test build script failure due to non-package emerge error."""
210 tempdir = osutils.TempDir(base_dir=self.tempdir)
211 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
212
Alex Klein38c7d9e2019-05-08 09:31:19 -0600213 result = test_service.BuildTargetUnitTestResult(1, None)
214 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600215
Alex Kleinf2674462019-05-16 16:47:24 -0600216 pkgs = ['foo/bar', 'cat/pkg']
217 blacklist = [portage_util.SplitCPV(p, strict=False) for p in pkgs]
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600218 input_msg = self._GetInput(board='board', result_path=self.tempdir,
Alex Kleinf2674462019-05-16 16:47:24 -0600219 empty_sysroot=True, blacklist=blacklist)
Alex Kleina2e42c42019-04-17 16:13:19 -0600220 output_msg = self._GetOutput()
221
Alex Klein231d2da2019-07-22 16:44:45 -0600222 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
223 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600224
Alex Klein8cb365a2019-05-15 16:24:53 -0600225 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600226 self.assertFalse(output_msg.failed_packages)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600227
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700228 def testBuildTargetUnitTest(self):
229 """Test BuildTargetUnitTest successful call."""
230 input_msg = self._GetInput(board='board', result_path=self.tempdir)
231
232 result = test_service.BuildTargetUnitTestResult(0, None)
233 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
234
235 tarball_result = os.path.join(input_msg.result_path, 'unit_tests.tar')
236 self.PatchObject(test_service, 'BuildTargetUnitTestTarball',
237 return_value=tarball_result)
238
239 response = self._GetOutput()
240 test_controller.BuildTargetUnitTest(input_msg, response,
241 self.api_config)
242 self.assertEqual(response.tarball_path,
243 os.path.join(input_msg.result_path, 'unit_tests.tar'))
244
Evan Hernandez4e388a52019-05-01 12:16:33 -0600245
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -0700246class ChromiteUnitTestTest(cros_test_lib.MockTestCase,
247 api_config.ApiConfigMixin):
248 """Tests for the ChromiteInfoTest function."""
249
250 def setUp(self):
251 self.board = 'board'
252 self.chroot_path = '/path/to/chroot'
253
254 def _GetInput(self, chroot_path=None):
255 """Helper to build an input message instance."""
256 proto = test_pb2.ChromiteUnitTestRequest(
257 chroot={'path': chroot_path},
258 )
259 return proto
260
261 def _GetOutput(self):
262 """Helper to get an empty output message instance."""
263 return test_pb2.ChromiteUnitTestResponse()
264
265 def testValidateOnly(self):
266 """Sanity check that a validate only call does not execute any logic."""
267 patch = self.PatchObject(cros_build_lib, 'run')
268
269 input_msg = self._GetInput(chroot_path=self.chroot_path)
270 test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
271 self.validate_only_config)
272 patch.assert_not_called()
273
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700274 def testMockError(self):
275 """Test mock error call does not execute any logic, returns error."""
276 patch = self.PatchObject(cros_build_lib, 'run')
277
278 input_msg = self._GetInput(chroot_path=self.chroot_path)
279 rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
280 self.mock_error_config)
281 patch.assert_not_called()
282 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
283
284 def testMockCall(self):
285 """Test mock call does not execute any logic, returns success."""
286 patch = self.PatchObject(cros_build_lib, 'run')
287
288 input_msg = self._GetInput(chroot_path=self.chroot_path)
289 rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
290 self.mock_call_config)
291 patch.assert_not_called()
292 self.assertEqual(controller.RETURN_CODE_SUCCESS, rc)
293
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -0700294 def testChromiteUnitTest(self):
295 """Call ChromiteUnitTest with mocked cros_build_lib.run."""
296 request = self._GetInput(chroot_path=self.chroot_path)
297 patch = self.PatchObject(
298 cros_build_lib, 'run',
299 return_value=cros_build_lib.CommandResult(returncode=0))
300
301 test_controller.ChromiteUnitTest(request, self._GetOutput(),
302 self.api_config)
303 patch.assert_called_once()
304
305
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600306class CrosSigningTestTest(cros_test_lib.RunCommandTestCase,
307 api_config.ApiConfigMixin):
308 """CrosSigningTest tests."""
309
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700310 def setUp(self):
311 self.chroot_path = '/path/to/chroot'
312
313 def _GetInput(self, chroot_path=None):
314 """Helper to build an input message instance."""
315 proto = test_pb2.CrosSigningTestRequest(
316 chroot={'path': chroot_path},
317 )
318 return proto
319
320 def _GetOutput(self):
321 """Helper to get an empty output message instance."""
322 return test_pb2.CrosSigningTestResponse()
323
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600324 def testValidateOnly(self):
325 """Sanity check that a validate only call does not execute any logic."""
326 test_controller.CrosSigningTest(None, None, self.validate_only_config)
327 self.assertFalse(self.rc.call_count)
328
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700329 def testCrosSigningTest(self):
330 """Call CrosSigningTest with mocked cros_build_lib.run."""
331 request = self._GetInput(chroot_path=self.chroot_path)
332 patch = self.PatchObject(
333 cros_build_lib, 'run',
334 return_value=cros_build_lib.CommandResult(returncode=0))
335
336 test_controller.CrosSigningTest(request, self._GetOutput(),
337 self.api_config)
338 patch.assert_called_once()
339
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600340
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600341class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase,
342 api_config.ApiConfigMixin):
343 """Test the SimpleChromeWorkflowTest endpoint."""
344
345 @staticmethod
346 def _Output():
347 return test_pb2.SimpleChromeWorkflowTestResponse()
348
349 def _Input(self, sysroot_path=None, build_target=None, chrome_root=None,
350 goma_config=None):
351 proto = test_pb2.SimpleChromeWorkflowTestRequest()
352 if sysroot_path:
353 proto.sysroot.path = sysroot_path
354 if build_target:
355 proto.sysroot.build_target.name = build_target
356 if chrome_root:
357 proto.chrome_root = chrome_root
358 if goma_config:
359 proto.goma_config = goma_config
360 return proto
361
362 def setUp(self):
363 self.chrome_path = 'path/to/chrome'
364 self.sysroot_dir = 'build/board'
365 self.build_target = 'amd64'
366 self.mock_simple_chrome_workflow_test = self.PatchObject(
367 test_service, 'SimpleChromeWorkflowTest')
368
369 def testMissingBuildTarget(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700370 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600371 input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir',
372 chrome_root='/chrome/path')
373 with self.assertRaises(cros_build_lib.DieSystemExit):
374 test_controller.SimpleChromeWorkflowTest(input_proto, None,
375 self.api_config)
376
377 def testMissingSysrootPath(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700378 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600379 input_proto = self._Input(build_target='board', sysroot_path=None,
380 chrome_root='/chrome/path')
381 with self.assertRaises(cros_build_lib.DieSystemExit):
382 test_controller.SimpleChromeWorkflowTest(input_proto, None,
383 self.api_config)
384
385 def testMissingChromeRoot(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700386 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600387 input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir',
388 chrome_root=None)
389 with self.assertRaises(cros_build_lib.DieSystemExit):
390 test_controller.SimpleChromeWorkflowTest(input_proto, None,
391 self.api_config)
392
393 def testSimpleChromeWorkflowTest(self):
394 """Call SimpleChromeWorkflowTest with valid args and temp dir."""
395 request = self._Input(sysroot_path='sysroot_path', build_target='board',
396 chrome_root='/path/to/chrome')
397 response = self._Output()
398
399 test_controller.SimpleChromeWorkflowTest(request, response, self.api_config)
400 self.mock_simple_chrome_workflow_test.assert_called()
401
402 def testValidateOnly(self):
403 request = self._Input(sysroot_path='sysroot_path', build_target='board',
404 chrome_root='/path/to/chrome')
405 test_controller.SimpleChromeWorkflowTest(request, self._Output(),
406 self.validate_only_config)
407 self.mock_simple_chrome_workflow_test.assert_not_called()
408
409
Alex Klein231d2da2019-07-22 16:44:45 -0600410class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin):
Evan Hernandez4e388a52019-05-01 12:16:33 -0600411 """Test the VmTest endpoint."""
412
413 def _GetInput(self, **kwargs):
414 values = dict(
415 build_target=common_pb2.BuildTarget(name='target'),
Alex Klein311b8022019-06-05 16:00:07 -0600416 vm_path=common_pb2.Path(path='/path/to/image.bin',
417 location=common_pb2.Path.INSIDE),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600418 test_harness=test_pb2.VmTestRequest.TAST,
419 vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')],
420 ssh_options=test_pb2.VmTestRequest.SshOptions(
Alex Klein231d2da2019-07-22 16:44:45 -0600421 port=1234, private_key_path={'path': '/path/to/id_rsa',
Alex Kleinaa705412019-06-04 15:00:30 -0600422 'location': common_pb2.Path.INSIDE}),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600423 )
424 values.update(kwargs)
425 return test_pb2.VmTestRequest(**values)
426
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700427 def _Output(self):
428 return test_pb2.VmTestResponse()
429
Alex Klein231d2da2019-07-22 16:44:45 -0600430 def testValidateOnly(self):
431 """Sanity check that a validate only call does not execute any logic."""
432 test_controller.VmTest(self._GetInput(), None, self.validate_only_config)
433 self.assertEqual(0, self.rc.call_count)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600434
435 def testTastAllOptions(self):
436 """Test VmTest for Tast with all options set."""
Alex Klein231d2da2019-07-22 16:44:45 -0600437 test_controller.VmTest(self._GetInput(), None, self.api_config)
438 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700439 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600440 '--board', 'target',
441 '--image-path', '/path/to/image.bin',
442 '--tast', 'suite',
443 '--ssh-port', '1234',
444 '--private-key', '/path/to/id_rsa',
445 ])
446
447 def testAutotestAllOptions(self):
448 """Test VmTest for Autotest with all options set."""
449 input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST)
Alex Klein231d2da2019-07-22 16:44:45 -0600450 test_controller.VmTest(input_proto, None, self.api_config)
451 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700452 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600453 '--board', 'target',
454 '--image-path', '/path/to/image.bin',
455 '--autotest', 'suite',
456 '--ssh-port', '1234',
457 '--private-key', '/path/to/id_rsa',
458 '--test_that-args=--whitelist-chrome-crashes',
459 ])
460
461 def testMissingBuildTarget(self):
462 """Test VmTest dies when build_target not set."""
463 input_proto = self._GetInput(build_target=None)
464 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600465 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600466
467 def testMissingVmImage(self):
468 """Test VmTest dies when vm_image not set."""
Alex Klein311b8022019-06-05 16:00:07 -0600469 input_proto = self._GetInput(vm_path=None)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600470 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600471 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600472
473 def testMissingTestHarness(self):
474 """Test VmTest dies when test_harness not specified."""
475 input_proto = self._GetInput(
476 test_harness=test_pb2.VmTestRequest.UNSPECIFIED)
477 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600478 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600479
480 def testMissingVmTests(self):
481 """Test VmTest dies when vm_tests not set."""
482 input_proto = self._GetInput(vm_tests=[])
483 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600484 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600485
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700486 def testVmTest(self):
487 """Call VmTest with valid args and temp dir."""
488 request = self._GetInput()
489 response = self._Output()
490 patch = self.PatchObject(
491 cros_build_lib, 'run',
492 return_value=cros_build_lib.CommandResult(returncode=0))
493
494 test_controller.VmTest(request, response, self.api_config)
495 patch.assert_called()
496
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600497
Alex Klein231d2da2019-07-22 16:44:45 -0600498class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600499 """Test the MoblabVmTest endpoint."""
500
501 @staticmethod
502 def _Payload(path):
503 return test_pb2.MoblabVmTestRequest.Payload(
504 path=common_pb2.Path(path=path))
505
506 @staticmethod
507 def _Output():
508 return test_pb2.MoblabVmTestResponse()
509
510 def _Input(self):
511 return test_pb2.MoblabVmTestRequest(
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600512 chroot=common_pb2.Chroot(path=self.chroot_dir),
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600513 image_payload=self._Payload(self.image_payload_dir),
514 cache_payloads=[self._Payload(self.autotest_payload_dir)])
515
516 def setUp(self):
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600517 self.chroot_dir = '/chroot'
518 self.chroot_tmp_dir = '/chroot/tmp'
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600519 self.image_payload_dir = '/payloads/image'
520 self.autotest_payload_dir = '/payloads/autotest'
521 self.builder = 'moblab-generic-vm/R12-3.4.5-67.890'
522 self.image_cache_dir = '/mnt/moblab/cache'
523 self.image_mount_dir = '/mnt/image'
524
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600525 self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir)
Evan Hernandez655e8042019-06-13 12:50:44 -0600526
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600527 self.mock_create_moblab_vms = self.PatchObject(
528 test_service, 'CreateMoblabVm')
529 self.mock_prepare_moblab_vm_image_cache = self.PatchObject(
530 test_service, 'PrepareMoblabVmImageCache',
531 return_value=self.image_cache_dir)
532 self.mock_run_moblab_vm_tests = self.PatchObject(
533 test_service, 'RunMoblabVmTest')
534 self.mock_validate_moblab_vm_tests = self.PatchObject(
535 test_service, 'ValidateMoblabVmTest')
536
537 @contextlib.contextmanager
Alex Klein38c7d9e2019-05-08 09:31:19 -0600538 def MockLoopbackPartitions(*_args, **_kwargs):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600539 mount = mock.MagicMock()
Evan Hernandez40ee7452019-06-13 12:51:43 -0600540 mount.Mount.return_value = [self.image_mount_dir]
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600541 yield mount
Alex Klein231d2da2019-07-22 16:44:45 -0600542
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600543 self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions)
544
Alex Klein231d2da2019-07-22 16:44:45 -0600545 def testValidateOnly(self):
546 """Sanity check that a validate only call does not execute any logic."""
547 test_controller.MoblabVmTest(self._Input(), self._Output(),
548 self.validate_only_config)
549 self.mock_create_moblab_vms.assert_not_called()
550
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600551 def testImageContainsBuilder(self):
552 """MoblabVmTest calls service with correct args."""
553 request = self._Input()
554 response = self._Output()
555
556 self.PatchObject(
Mike Frysingere652ba12019-09-08 00:57:43 -0400557 key_value_store, 'LoadFile',
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600558 return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder})
559
Alex Klein231d2da2019-07-22 16:44:45 -0600560 test_controller.MoblabVmTest(request, response, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600561
562 self.assertEqual(
563 self.mock_create_moblab_vms.call_args_list,
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600564 [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600565 self.assertEqual(
566 self.mock_prepare_moblab_vm_image_cache.call_args_list,
567 [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])])
568 self.assertEqual(
569 self.mock_run_moblab_vm_tests.call_args_list,
Evan Hernandez655e8042019-06-13 12:50:44 -0600570 [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir,
571 mock.ANY)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600572 self.assertEqual(
573 self.mock_validate_moblab_vm_tests.call_args_list,
574 [mock.call(mock.ANY)])
575
576 def testImageMissingBuilder(self):
577 """MoblabVmTest dies when builder path not found in lsb-release."""
578 request = self._Input()
579 response = self._Output()
580
Mike Frysingere652ba12019-09-08 00:57:43 -0400581 self.PatchObject(key_value_store, 'LoadFile', return_value={})
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600582
583 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600584 test_controller.MoblabVmTest(request, response, self.api_config)