blob: 4e08b17585489de49032db713b0941390f8cc724 [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
64 def testNoBuildTargetNoSysrootFails(self):
65 """Test missing build target name and sysroot path fails."""
66 input_msg = self._GetInput()
67 output_msg = self._GetOutput()
68 with self.assertRaises(cros_build_lib.DieSystemExit):
69 test_controller.DebugInfoTest(input_msg, output_msg, self.api_config)
70
71 def testDebugInfoTest(self):
72 """Call DebugInfoTest with valid sysroot_path."""
73 request = self._GetInput(sysroot_path=self.full_sysroot_path)
74
75 test_controller.DebugInfoTest(request, self._GetOutput(), self.api_config)
76
77
Alex Klein231d2da2019-07-22 16:44:45 -060078class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase,
79 api_config.ApiConfigMixin):
Alex Kleina2e42c42019-04-17 16:13:19 -060080 """Tests for the UnitTest function."""
81
82 def _GetInput(self, board=None, result_path=None, chroot_path=None,
Alex Kleinf2674462019-05-16 16:47:24 -060083 cache_dir=None, empty_sysroot=None, blacklist=None):
Alex Kleina2e42c42019-04-17 16:13:19 -060084 """Helper to build an input message instance."""
Alex Kleinf2674462019-05-16 16:47:24 -060085 formatted_blacklist = []
86 for pkg in blacklist or []:
87 formatted_blacklist.append({'category': pkg.category,
88 'package_name': pkg.package})
89
Alex Kleina2e42c42019-04-17 16:13:19 -060090 return test_pb2.BuildTargetUnitTestRequest(
91 build_target={'name': board}, result_path=result_path,
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060092 chroot={'path': chroot_path, 'cache_dir': cache_dir},
Alex Kleinf2674462019-05-16 16:47:24 -060093 flags={'empty_sysroot': empty_sysroot},
94 package_blacklist=formatted_blacklist,
Alex Kleina2e42c42019-04-17 16:13:19 -060095 )
96
97 def _GetOutput(self):
98 """Helper to get an empty output message instance."""
99 return test_pb2.BuildTargetUnitTestResponse()
100
Alex Klein231d2da2019-07-22 16:44:45 -0600101 def testValidateOnly(self):
102 """Sanity check that a validate only call does not execute any logic."""
103 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
104
105 input_msg = self._GetInput(board='board', result_path=self.tempdir)
106 test_controller.BuildTargetUnitTest(input_msg, self._GetOutput(),
107 self.validate_only_config)
108 patch.assert_not_called()
109
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700110 def testMockCall(self):
111 """Test that a mock call does not execute logic, returns mocked value."""
112 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
113
114 input_msg = self._GetInput(board='board', result_path=self.tempdir)
115 response = self._GetOutput()
116 test_controller.BuildTargetUnitTest(input_msg, response,
117 self.mock_call_config)
118 patch.assert_not_called()
119 self.assertEqual(response.tarball_path,
120 os.path.join(input_msg.result_path, 'unit_tests.tar'))
121
122 def testMockError(self):
123 """Test that a mock error does not execute logic, returns mocked value."""
124 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
125
126 input_msg = self._GetInput(board='board', result_path=self.tempdir)
127 response = self._GetOutput()
128 rc = test_controller.BuildTargetUnitTest(input_msg, response,
129 self.mock_error_config)
130 patch.assert_not_called()
131 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
132 self.assertTrue(response.failed_packages)
133 self.assertEqual(response.failed_packages[0].category, 'foo')
134 self.assertEqual(response.failed_packages[0].package_name, 'bar')
135 self.assertEqual(response.failed_packages[1].category, 'cat')
136 self.assertEqual(response.failed_packages[1].package_name, 'pkg')
137
Alex Kleina2e42c42019-04-17 16:13:19 -0600138 def testNoArgumentFails(self):
139 """Test no arguments fails."""
140 input_msg = self._GetInput()
141 output_msg = self._GetOutput()
142 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600143 test_controller.BuildTargetUnitTest(input_msg, output_msg,
144 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600145
146 def testNoBuildTargetFails(self):
147 """Test missing build target name fails."""
148 input_msg = self._GetInput(result_path=self.tempdir)
149 output_msg = self._GetOutput()
150 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600151 test_controller.BuildTargetUnitTest(input_msg, output_msg,
152 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600153
154 def testNoResultPathFails(self):
155 """Test missing result path fails."""
156 # Missing result_path.
157 input_msg = self._GetInput(board='board')
158 output_msg = self._GetOutput()
159 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600160 test_controller.BuildTargetUnitTest(input_msg, output_msg,
161 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600162
163 def testPackageBuildFailure(self):
164 """Test handling of raised BuildPackageFailure."""
165 tempdir = osutils.TempDir(base_dir=self.tempdir)
166 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
167
168 pkgs = ['cat/pkg', 'foo/bar']
169 expected = [('cat', 'pkg'), ('foo', 'bar')]
Alex Kleina2e42c42019-04-17 16:13:19 -0600170
Alex Klein38c7d9e2019-05-08 09:31:19 -0600171 result = test_service.BuildTargetUnitTestResult(1, None)
172 result.failed_cpvs = [portage_util.SplitCPV(p, strict=False) for p in pkgs]
173 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600174
175 input_msg = self._GetInput(board='board', result_path=self.tempdir)
176 output_msg = self._GetOutput()
177
Alex Klein231d2da2019-07-22 16:44:45 -0600178 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
179 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600180
Alex Klein8cb365a2019-05-15 16:24:53 -0600181 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600182 self.assertTrue(output_msg.failed_packages)
183 failed = []
184 for pi in output_msg.failed_packages:
185 failed.append((pi.category, pi.package_name))
Mike Frysinger678735c2019-09-28 18:23:28 -0400186 self.assertCountEqual(expected, failed)
Alex Kleina2e42c42019-04-17 16:13:19 -0600187
188 def testOtherBuildScriptFailure(self):
189 """Test build script failure due to non-package emerge error."""
190 tempdir = osutils.TempDir(base_dir=self.tempdir)
191 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
192
Alex Klein38c7d9e2019-05-08 09:31:19 -0600193 result = test_service.BuildTargetUnitTestResult(1, None)
194 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600195
Alex Kleinf2674462019-05-16 16:47:24 -0600196 pkgs = ['foo/bar', 'cat/pkg']
197 blacklist = [portage_util.SplitCPV(p, strict=False) for p in pkgs]
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600198 input_msg = self._GetInput(board='board', result_path=self.tempdir,
Alex Kleinf2674462019-05-16 16:47:24 -0600199 empty_sysroot=True, blacklist=blacklist)
Alex Kleina2e42c42019-04-17 16:13:19 -0600200 output_msg = self._GetOutput()
201
Alex Klein231d2da2019-07-22 16:44:45 -0600202 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
203 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600204
Alex Klein8cb365a2019-05-15 16:24:53 -0600205 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600206 self.assertFalse(output_msg.failed_packages)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600207
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700208 def testBuildTargetUnitTest(self):
209 """Test BuildTargetUnitTest successful call."""
210 input_msg = self._GetInput(board='board', result_path=self.tempdir)
211
212 result = test_service.BuildTargetUnitTestResult(0, None)
213 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
214
215 tarball_result = os.path.join(input_msg.result_path, 'unit_tests.tar')
216 self.PatchObject(test_service, 'BuildTargetUnitTestTarball',
217 return_value=tarball_result)
218
219 response = self._GetOutput()
220 test_controller.BuildTargetUnitTest(input_msg, response,
221 self.api_config)
222 self.assertEqual(response.tarball_path,
223 os.path.join(input_msg.result_path, 'unit_tests.tar'))
224
Evan Hernandez4e388a52019-05-01 12:16:33 -0600225
Michael Mortensen8ca4d3b2019-11-27 09:35:22 -0700226class ChromiteUnitTestTest(cros_test_lib.MockTestCase,
227 api_config.ApiConfigMixin):
228 """Tests for the ChromiteInfoTest function."""
229
230 def setUp(self):
231 self.board = 'board'
232 self.chroot_path = '/path/to/chroot'
233
234 def _GetInput(self, chroot_path=None):
235 """Helper to build an input message instance."""
236 proto = test_pb2.ChromiteUnitTestRequest(
237 chroot={'path': chroot_path},
238 )
239 return proto
240
241 def _GetOutput(self):
242 """Helper to get an empty output message instance."""
243 return test_pb2.ChromiteUnitTestResponse()
244
245 def testValidateOnly(self):
246 """Sanity check that a validate only call does not execute any logic."""
247 patch = self.PatchObject(cros_build_lib, 'run')
248
249 input_msg = self._GetInput(chroot_path=self.chroot_path)
250 test_controller.ChromiteUnitTest(input_msg, self._GetOutput(),
251 self.validate_only_config)
252 patch.assert_not_called()
253
254 def testChromiteUnitTest(self):
255 """Call ChromiteUnitTest with mocked cros_build_lib.run."""
256 request = self._GetInput(chroot_path=self.chroot_path)
257 patch = self.PatchObject(
258 cros_build_lib, 'run',
259 return_value=cros_build_lib.CommandResult(returncode=0))
260
261 test_controller.ChromiteUnitTest(request, self._GetOutput(),
262 self.api_config)
263 patch.assert_called_once()
264
265
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600266class CrosSigningTestTest(cros_test_lib.RunCommandTestCase,
267 api_config.ApiConfigMixin):
268 """CrosSigningTest tests."""
269
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700270 def setUp(self):
271 self.chroot_path = '/path/to/chroot'
272
273 def _GetInput(self, chroot_path=None):
274 """Helper to build an input message instance."""
275 proto = test_pb2.CrosSigningTestRequest(
276 chroot={'path': chroot_path},
277 )
278 return proto
279
280 def _GetOutput(self):
281 """Helper to get an empty output message instance."""
282 return test_pb2.CrosSigningTestResponse()
283
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600284 def testValidateOnly(self):
285 """Sanity check that a validate only call does not execute any logic."""
286 test_controller.CrosSigningTest(None, None, self.validate_only_config)
287 self.assertFalse(self.rc.call_count)
288
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700289 def testCrosSigningTest(self):
290 """Call CrosSigningTest with mocked cros_build_lib.run."""
291 request = self._GetInput(chroot_path=self.chroot_path)
292 patch = self.PatchObject(
293 cros_build_lib, 'run',
294 return_value=cros_build_lib.CommandResult(returncode=0))
295
296 test_controller.CrosSigningTest(request, self._GetOutput(),
297 self.api_config)
298 patch.assert_called_once()
299
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600300
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600301class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase,
302 api_config.ApiConfigMixin):
303 """Test the SimpleChromeWorkflowTest endpoint."""
304
305 @staticmethod
306 def _Output():
307 return test_pb2.SimpleChromeWorkflowTestResponse()
308
309 def _Input(self, sysroot_path=None, build_target=None, chrome_root=None,
310 goma_config=None):
311 proto = test_pb2.SimpleChromeWorkflowTestRequest()
312 if sysroot_path:
313 proto.sysroot.path = sysroot_path
314 if build_target:
315 proto.sysroot.build_target.name = build_target
316 if chrome_root:
317 proto.chrome_root = chrome_root
318 if goma_config:
319 proto.goma_config = goma_config
320 return proto
321
322 def setUp(self):
323 self.chrome_path = 'path/to/chrome'
324 self.sysroot_dir = 'build/board'
325 self.build_target = 'amd64'
326 self.mock_simple_chrome_workflow_test = self.PatchObject(
327 test_service, 'SimpleChromeWorkflowTest')
328
329 def testMissingBuildTarget(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700330 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600331 input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir',
332 chrome_root='/chrome/path')
333 with self.assertRaises(cros_build_lib.DieSystemExit):
334 test_controller.SimpleChromeWorkflowTest(input_proto, None,
335 self.api_config)
336
337 def testMissingSysrootPath(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700338 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600339 input_proto = self._Input(build_target='board', sysroot_path=None,
340 chrome_root='/chrome/path')
341 with self.assertRaises(cros_build_lib.DieSystemExit):
342 test_controller.SimpleChromeWorkflowTest(input_proto, None,
343 self.api_config)
344
345 def testMissingChromeRoot(self):
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700346 """Test SimpleChromeWorkflowTest dies when build_target not set."""
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600347 input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir',
348 chrome_root=None)
349 with self.assertRaises(cros_build_lib.DieSystemExit):
350 test_controller.SimpleChromeWorkflowTest(input_proto, None,
351 self.api_config)
352
353 def testSimpleChromeWorkflowTest(self):
354 """Call SimpleChromeWorkflowTest with valid args and temp dir."""
355 request = self._Input(sysroot_path='sysroot_path', build_target='board',
356 chrome_root='/path/to/chrome')
357 response = self._Output()
358
359 test_controller.SimpleChromeWorkflowTest(request, response, self.api_config)
360 self.mock_simple_chrome_workflow_test.assert_called()
361
362 def testValidateOnly(self):
363 request = self._Input(sysroot_path='sysroot_path', build_target='board',
364 chrome_root='/path/to/chrome')
365 test_controller.SimpleChromeWorkflowTest(request, self._Output(),
366 self.validate_only_config)
367 self.mock_simple_chrome_workflow_test.assert_not_called()
368
369
Alex Klein231d2da2019-07-22 16:44:45 -0600370class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin):
Evan Hernandez4e388a52019-05-01 12:16:33 -0600371 """Test the VmTest endpoint."""
372
373 def _GetInput(self, **kwargs):
374 values = dict(
375 build_target=common_pb2.BuildTarget(name='target'),
Alex Klein311b8022019-06-05 16:00:07 -0600376 vm_path=common_pb2.Path(path='/path/to/image.bin',
377 location=common_pb2.Path.INSIDE),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600378 test_harness=test_pb2.VmTestRequest.TAST,
379 vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')],
380 ssh_options=test_pb2.VmTestRequest.SshOptions(
Alex Klein231d2da2019-07-22 16:44:45 -0600381 port=1234, private_key_path={'path': '/path/to/id_rsa',
Alex Kleinaa705412019-06-04 15:00:30 -0600382 'location': common_pb2.Path.INSIDE}),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600383 )
384 values.update(kwargs)
385 return test_pb2.VmTestRequest(**values)
386
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700387 def _Output(self):
388 return test_pb2.VmTestResponse()
389
Alex Klein231d2da2019-07-22 16:44:45 -0600390 def testValidateOnly(self):
391 """Sanity check that a validate only call does not execute any logic."""
392 test_controller.VmTest(self._GetInput(), None, self.validate_only_config)
393 self.assertEqual(0, self.rc.call_count)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600394
395 def testTastAllOptions(self):
396 """Test VmTest for Tast with all options set."""
Alex Klein231d2da2019-07-22 16:44:45 -0600397 test_controller.VmTest(self._GetInput(), None, self.api_config)
398 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700399 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600400 '--board', 'target',
401 '--image-path', '/path/to/image.bin',
402 '--tast', 'suite',
403 '--ssh-port', '1234',
404 '--private-key', '/path/to/id_rsa',
405 ])
406
407 def testAutotestAllOptions(self):
408 """Test VmTest for Autotest with all options set."""
409 input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST)
Alex Klein231d2da2019-07-22 16:44:45 -0600410 test_controller.VmTest(input_proto, None, self.api_config)
411 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700412 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600413 '--board', 'target',
414 '--image-path', '/path/to/image.bin',
415 '--autotest', 'suite',
416 '--ssh-port', '1234',
417 '--private-key', '/path/to/id_rsa',
418 '--test_that-args=--whitelist-chrome-crashes',
419 ])
420
421 def testMissingBuildTarget(self):
422 """Test VmTest dies when build_target not set."""
423 input_proto = self._GetInput(build_target=None)
424 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600425 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600426
427 def testMissingVmImage(self):
428 """Test VmTest dies when vm_image not set."""
Alex Klein311b8022019-06-05 16:00:07 -0600429 input_proto = self._GetInput(vm_path=None)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600430 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600431 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600432
433 def testMissingTestHarness(self):
434 """Test VmTest dies when test_harness not specified."""
435 input_proto = self._GetInput(
436 test_harness=test_pb2.VmTestRequest.UNSPECIFIED)
437 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600438 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600439
440 def testMissingVmTests(self):
441 """Test VmTest dies when vm_tests not set."""
442 input_proto = self._GetInput(vm_tests=[])
443 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600444 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600445
Michael Mortensen82cd62d2019-12-01 14:58:54 -0700446 def testVmTest(self):
447 """Call VmTest with valid args and temp dir."""
448 request = self._GetInput()
449 response = self._Output()
450 patch = self.PatchObject(
451 cros_build_lib, 'run',
452 return_value=cros_build_lib.CommandResult(returncode=0))
453
454 test_controller.VmTest(request, response, self.api_config)
455 patch.assert_called()
456
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600457
Alex Klein231d2da2019-07-22 16:44:45 -0600458class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600459 """Test the MoblabVmTest endpoint."""
460
461 @staticmethod
462 def _Payload(path):
463 return test_pb2.MoblabVmTestRequest.Payload(
464 path=common_pb2.Path(path=path))
465
466 @staticmethod
467 def _Output():
468 return test_pb2.MoblabVmTestResponse()
469
470 def _Input(self):
471 return test_pb2.MoblabVmTestRequest(
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600472 chroot=common_pb2.Chroot(path=self.chroot_dir),
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600473 image_payload=self._Payload(self.image_payload_dir),
474 cache_payloads=[self._Payload(self.autotest_payload_dir)])
475
476 def setUp(self):
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600477 self.chroot_dir = '/chroot'
478 self.chroot_tmp_dir = '/chroot/tmp'
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600479 self.image_payload_dir = '/payloads/image'
480 self.autotest_payload_dir = '/payloads/autotest'
481 self.builder = 'moblab-generic-vm/R12-3.4.5-67.890'
482 self.image_cache_dir = '/mnt/moblab/cache'
483 self.image_mount_dir = '/mnt/image'
484
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600485 self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir)
Evan Hernandez655e8042019-06-13 12:50:44 -0600486
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600487 self.mock_create_moblab_vms = self.PatchObject(
488 test_service, 'CreateMoblabVm')
489 self.mock_prepare_moblab_vm_image_cache = self.PatchObject(
490 test_service, 'PrepareMoblabVmImageCache',
491 return_value=self.image_cache_dir)
492 self.mock_run_moblab_vm_tests = self.PatchObject(
493 test_service, 'RunMoblabVmTest')
494 self.mock_validate_moblab_vm_tests = self.PatchObject(
495 test_service, 'ValidateMoblabVmTest')
496
497 @contextlib.contextmanager
Alex Klein38c7d9e2019-05-08 09:31:19 -0600498 def MockLoopbackPartitions(*_args, **_kwargs):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600499 mount = mock.MagicMock()
Evan Hernandez40ee7452019-06-13 12:51:43 -0600500 mount.Mount.return_value = [self.image_mount_dir]
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600501 yield mount
Alex Klein231d2da2019-07-22 16:44:45 -0600502
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600503 self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions)
504
Alex Klein231d2da2019-07-22 16:44:45 -0600505 def testValidateOnly(self):
506 """Sanity check that a validate only call does not execute any logic."""
507 test_controller.MoblabVmTest(self._Input(), self._Output(),
508 self.validate_only_config)
509 self.mock_create_moblab_vms.assert_not_called()
510
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600511 def testImageContainsBuilder(self):
512 """MoblabVmTest calls service with correct args."""
513 request = self._Input()
514 response = self._Output()
515
516 self.PatchObject(
Mike Frysingere652ba12019-09-08 00:57:43 -0400517 key_value_store, 'LoadFile',
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600518 return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder})
519
Alex Klein231d2da2019-07-22 16:44:45 -0600520 test_controller.MoblabVmTest(request, response, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600521
522 self.assertEqual(
523 self.mock_create_moblab_vms.call_args_list,
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600524 [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600525 self.assertEqual(
526 self.mock_prepare_moblab_vm_image_cache.call_args_list,
527 [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])])
528 self.assertEqual(
529 self.mock_run_moblab_vm_tests.call_args_list,
Evan Hernandez655e8042019-06-13 12:50:44 -0600530 [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir,
531 mock.ANY)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600532 self.assertEqual(
533 self.mock_validate_moblab_vm_tests.call_args_list,
534 [mock.call(mock.ANY)])
535
536 def testImageMissingBuilder(self):
537 """MoblabVmTest dies when builder path not found in lsb-release."""
538 request = self._Input()
539 response = self._Output()
540
Mike Frysingere652ba12019-09-08 00:57:43 -0400541 self.PatchObject(key_value_store, 'LoadFile', return_value={})
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600542
543 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600544 test_controller.MoblabVmTest(request, response, self.api_config)