Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 10 | import os |
| 11 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 12 | import contextlib |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 13 | import mock |
| 14 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 15 | from chromite.api import api_config |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 16 | from chromite.api import controller |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 17 | from chromite.api.controller import test as test_controller |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 18 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 19 | from chromite.api.gen.chromite.api import test_pb2 |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 20 | from chromite.lib import chroot_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 21 | from chromite.lib import cros_build_lib |
| 22 | from chromite.lib import cros_test_lib |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 23 | from chromite.lib import image_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 24 | from chromite.lib import osutils |
| 25 | from chromite.lib import portage_util |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 26 | from chromite.scripts import cros_set_lsb_release |
| 27 | from chromite.service import test as test_service |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 28 | from chromite.utils import key_value_store |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 29 | |
| 30 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 31 | class 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 78 | class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase, |
| 79 | api_config.ApiConfigMixin): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 80 | """Tests for the UnitTest function.""" |
| 81 | |
| 82 | def _GetInput(self, board=None, result_path=None, chroot_path=None, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 83 | cache_dir=None, empty_sysroot=None, blacklist=None): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 84 | """Helper to build an input message instance.""" |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 85 | formatted_blacklist = [] |
| 86 | for pkg in blacklist or []: |
| 87 | formatted_blacklist.append({'category': pkg.category, |
| 88 | 'package_name': pkg.package}) |
| 89 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 90 | return test_pb2.BuildTargetUnitTestRequest( |
| 91 | build_target={'name': board}, result_path=result_path, |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 92 | chroot={'path': chroot_path, 'cache_dir': cache_dir}, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 93 | flags={'empty_sysroot': empty_sysroot}, |
| 94 | package_blacklist=formatted_blacklist, |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 95 | ) |
| 96 | |
| 97 | def _GetOutput(self): |
| 98 | """Helper to get an empty output message instance.""" |
| 99 | return test_pb2.BuildTargetUnitTestResponse() |
| 100 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 101 | 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 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 110 | def testNoArgumentFails(self): |
| 111 | """Test no arguments fails.""" |
| 112 | input_msg = self._GetInput() |
| 113 | output_msg = self._GetOutput() |
| 114 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 115 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 116 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 117 | |
| 118 | def testNoBuildTargetFails(self): |
| 119 | """Test missing build target name fails.""" |
| 120 | input_msg = self._GetInput(result_path=self.tempdir) |
| 121 | output_msg = self._GetOutput() |
| 122 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 123 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 124 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 125 | |
| 126 | def testNoResultPathFails(self): |
| 127 | """Test missing result path fails.""" |
| 128 | # Missing result_path. |
| 129 | input_msg = self._GetInput(board='board') |
| 130 | output_msg = self._GetOutput() |
| 131 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 132 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 133 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 134 | |
| 135 | def testPackageBuildFailure(self): |
| 136 | """Test handling of raised BuildPackageFailure.""" |
| 137 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 138 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 139 | |
| 140 | pkgs = ['cat/pkg', 'foo/bar'] |
| 141 | expected = [('cat', 'pkg'), ('foo', 'bar')] |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 142 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 143 | result = test_service.BuildTargetUnitTestResult(1, None) |
| 144 | result.failed_cpvs = [portage_util.SplitCPV(p, strict=False) for p in pkgs] |
| 145 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 146 | |
| 147 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 148 | output_msg = self._GetOutput() |
| 149 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 150 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 151 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 152 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 153 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 154 | self.assertTrue(output_msg.failed_packages) |
| 155 | failed = [] |
| 156 | for pi in output_msg.failed_packages: |
| 157 | failed.append((pi.category, pi.package_name)) |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 158 | self.assertCountEqual(expected, failed) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 159 | |
| 160 | def testOtherBuildScriptFailure(self): |
| 161 | """Test build script failure due to non-package emerge error.""" |
| 162 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 163 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 164 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 165 | result = test_service.BuildTargetUnitTestResult(1, None) |
| 166 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 167 | |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 168 | pkgs = ['foo/bar', 'cat/pkg'] |
| 169 | blacklist = [portage_util.SplitCPV(p, strict=False) for p in pkgs] |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 170 | input_msg = self._GetInput(board='board', result_path=self.tempdir, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 171 | empty_sysroot=True, blacklist=blacklist) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 172 | output_msg = self._GetOutput() |
| 173 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 174 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 175 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 176 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 177 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 178 | self.assertFalse(output_msg.failed_packages) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 179 | |
| 180 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 181 | class ChromiteUnitTestTest(cros_test_lib.MockTestCase, |
| 182 | api_config.ApiConfigMixin): |
| 183 | """Tests for the ChromiteInfoTest function.""" |
| 184 | |
| 185 | def setUp(self): |
| 186 | self.board = 'board' |
| 187 | self.chroot_path = '/path/to/chroot' |
| 188 | |
| 189 | def _GetInput(self, chroot_path=None): |
| 190 | """Helper to build an input message instance.""" |
| 191 | proto = test_pb2.ChromiteUnitTestRequest( |
| 192 | chroot={'path': chroot_path}, |
| 193 | ) |
| 194 | return proto |
| 195 | |
| 196 | def _GetOutput(self): |
| 197 | """Helper to get an empty output message instance.""" |
| 198 | return test_pb2.ChromiteUnitTestResponse() |
| 199 | |
| 200 | def testValidateOnly(self): |
| 201 | """Sanity check that a validate only call does not execute any logic.""" |
| 202 | patch = self.PatchObject(cros_build_lib, 'run') |
| 203 | |
| 204 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 205 | test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 206 | self.validate_only_config) |
| 207 | patch.assert_not_called() |
| 208 | |
| 209 | def testChromiteUnitTest(self): |
| 210 | """Call ChromiteUnitTest with mocked cros_build_lib.run.""" |
| 211 | request = self._GetInput(chroot_path=self.chroot_path) |
| 212 | patch = self.PatchObject( |
| 213 | cros_build_lib, 'run', |
| 214 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 215 | |
| 216 | test_controller.ChromiteUnitTest(request, self._GetOutput(), |
| 217 | self.api_config) |
| 218 | patch.assert_called_once() |
| 219 | |
| 220 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 221 | class CrosSigningTestTest(cros_test_lib.RunCommandTestCase, |
| 222 | api_config.ApiConfigMixin): |
| 223 | """CrosSigningTest tests.""" |
| 224 | |
| 225 | def testValidateOnly(self): |
| 226 | """Sanity check that a validate only call does not execute any logic.""" |
| 227 | test_controller.CrosSigningTest(None, None, self.validate_only_config) |
| 228 | self.assertFalse(self.rc.call_count) |
| 229 | |
| 230 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 231 | class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase, |
| 232 | api_config.ApiConfigMixin): |
| 233 | """Test the SimpleChromeWorkflowTest endpoint.""" |
| 234 | |
| 235 | @staticmethod |
| 236 | def _Output(): |
| 237 | return test_pb2.SimpleChromeWorkflowTestResponse() |
| 238 | |
| 239 | def _Input(self, sysroot_path=None, build_target=None, chrome_root=None, |
| 240 | goma_config=None): |
| 241 | proto = test_pb2.SimpleChromeWorkflowTestRequest() |
| 242 | if sysroot_path: |
| 243 | proto.sysroot.path = sysroot_path |
| 244 | if build_target: |
| 245 | proto.sysroot.build_target.name = build_target |
| 246 | if chrome_root: |
| 247 | proto.chrome_root = chrome_root |
| 248 | if goma_config: |
| 249 | proto.goma_config = goma_config |
| 250 | return proto |
| 251 | |
| 252 | def setUp(self): |
| 253 | self.chrome_path = 'path/to/chrome' |
| 254 | self.sysroot_dir = 'build/board' |
| 255 | self.build_target = 'amd64' |
| 256 | self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 257 | test_service, 'SimpleChromeWorkflowTest') |
| 258 | |
| 259 | def testMissingBuildTarget(self): |
| 260 | """Test VmTest dies when build_target not set.""" |
| 261 | input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir', |
| 262 | chrome_root='/chrome/path') |
| 263 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 264 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 265 | self.api_config) |
| 266 | |
| 267 | def testMissingSysrootPath(self): |
| 268 | """Test VmTest dies when build_target not set.""" |
| 269 | input_proto = self._Input(build_target='board', sysroot_path=None, |
| 270 | chrome_root='/chrome/path') |
| 271 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 272 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 273 | self.api_config) |
| 274 | |
| 275 | def testMissingChromeRoot(self): |
| 276 | """Test VmTest dies when build_target not set.""" |
| 277 | input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir', |
| 278 | chrome_root=None) |
| 279 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 280 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 281 | self.api_config) |
| 282 | |
| 283 | def testSimpleChromeWorkflowTest(self): |
| 284 | """Call SimpleChromeWorkflowTest with valid args and temp dir.""" |
| 285 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 286 | chrome_root='/path/to/chrome') |
| 287 | response = self._Output() |
| 288 | |
| 289 | test_controller.SimpleChromeWorkflowTest(request, response, self.api_config) |
| 290 | self.mock_simple_chrome_workflow_test.assert_called() |
| 291 | |
| 292 | def testValidateOnly(self): |
| 293 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 294 | chrome_root='/path/to/chrome') |
| 295 | test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 296 | self.validate_only_config) |
| 297 | self.mock_simple_chrome_workflow_test.assert_not_called() |
| 298 | |
| 299 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 300 | class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 301 | """Test the VmTest endpoint.""" |
| 302 | |
| 303 | def _GetInput(self, **kwargs): |
| 304 | values = dict( |
| 305 | build_target=common_pb2.BuildTarget(name='target'), |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 306 | vm_path=common_pb2.Path(path='/path/to/image.bin', |
| 307 | location=common_pb2.Path.INSIDE), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 308 | test_harness=test_pb2.VmTestRequest.TAST, |
| 309 | vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')], |
| 310 | ssh_options=test_pb2.VmTestRequest.SshOptions( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 311 | port=1234, private_key_path={'path': '/path/to/id_rsa', |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 312 | 'location': common_pb2.Path.INSIDE}), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 313 | ) |
| 314 | values.update(kwargs) |
| 315 | return test_pb2.VmTestRequest(**values) |
| 316 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 317 | def testValidateOnly(self): |
| 318 | """Sanity check that a validate only call does not execute any logic.""" |
| 319 | test_controller.VmTest(self._GetInput(), None, self.validate_only_config) |
| 320 | self.assertEqual(0, self.rc.call_count) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 321 | |
| 322 | def testTastAllOptions(self): |
| 323 | """Test VmTest for Tast with all options set.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 324 | test_controller.VmTest(self._GetInput(), None, self.api_config) |
| 325 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 326 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 327 | '--board', 'target', |
| 328 | '--image-path', '/path/to/image.bin', |
| 329 | '--tast', 'suite', |
| 330 | '--ssh-port', '1234', |
| 331 | '--private-key', '/path/to/id_rsa', |
| 332 | ]) |
| 333 | |
| 334 | def testAutotestAllOptions(self): |
| 335 | """Test VmTest for Autotest with all options set.""" |
| 336 | input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 337 | test_controller.VmTest(input_proto, None, self.api_config) |
| 338 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 339 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 340 | '--board', 'target', |
| 341 | '--image-path', '/path/to/image.bin', |
| 342 | '--autotest', 'suite', |
| 343 | '--ssh-port', '1234', |
| 344 | '--private-key', '/path/to/id_rsa', |
| 345 | '--test_that-args=--whitelist-chrome-crashes', |
| 346 | ]) |
| 347 | |
| 348 | def testMissingBuildTarget(self): |
| 349 | """Test VmTest dies when build_target not set.""" |
| 350 | input_proto = self._GetInput(build_target=None) |
| 351 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 352 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 353 | |
| 354 | def testMissingVmImage(self): |
| 355 | """Test VmTest dies when vm_image not set.""" |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 356 | input_proto = self._GetInput(vm_path=None) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 357 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 358 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 359 | |
| 360 | def testMissingTestHarness(self): |
| 361 | """Test VmTest dies when test_harness not specified.""" |
| 362 | input_proto = self._GetInput( |
| 363 | test_harness=test_pb2.VmTestRequest.UNSPECIFIED) |
| 364 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 365 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 366 | |
| 367 | def testMissingVmTests(self): |
| 368 | """Test VmTest dies when vm_tests not set.""" |
| 369 | input_proto = self._GetInput(vm_tests=[]) |
| 370 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 371 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 372 | |
| 373 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 374 | class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 375 | """Test the MoblabVmTest endpoint.""" |
| 376 | |
| 377 | @staticmethod |
| 378 | def _Payload(path): |
| 379 | return test_pb2.MoblabVmTestRequest.Payload( |
| 380 | path=common_pb2.Path(path=path)) |
| 381 | |
| 382 | @staticmethod |
| 383 | def _Output(): |
| 384 | return test_pb2.MoblabVmTestResponse() |
| 385 | |
| 386 | def _Input(self): |
| 387 | return test_pb2.MoblabVmTestRequest( |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 388 | chroot=common_pb2.Chroot(path=self.chroot_dir), |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 389 | image_payload=self._Payload(self.image_payload_dir), |
| 390 | cache_payloads=[self._Payload(self.autotest_payload_dir)]) |
| 391 | |
| 392 | def setUp(self): |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 393 | self.chroot_dir = '/chroot' |
| 394 | self.chroot_tmp_dir = '/chroot/tmp' |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 395 | self.image_payload_dir = '/payloads/image' |
| 396 | self.autotest_payload_dir = '/payloads/autotest' |
| 397 | self.builder = 'moblab-generic-vm/R12-3.4.5-67.890' |
| 398 | self.image_cache_dir = '/mnt/moblab/cache' |
| 399 | self.image_mount_dir = '/mnt/image' |
| 400 | |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 401 | self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir) |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 402 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 403 | self.mock_create_moblab_vms = self.PatchObject( |
| 404 | test_service, 'CreateMoblabVm') |
| 405 | self.mock_prepare_moblab_vm_image_cache = self.PatchObject( |
| 406 | test_service, 'PrepareMoblabVmImageCache', |
| 407 | return_value=self.image_cache_dir) |
| 408 | self.mock_run_moblab_vm_tests = self.PatchObject( |
| 409 | test_service, 'RunMoblabVmTest') |
| 410 | self.mock_validate_moblab_vm_tests = self.PatchObject( |
| 411 | test_service, 'ValidateMoblabVmTest') |
| 412 | |
| 413 | @contextlib.contextmanager |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 414 | def MockLoopbackPartitions(*_args, **_kwargs): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 415 | mount = mock.MagicMock() |
Evan Hernandez | 40ee745 | 2019-06-13 12:51:43 -0600 | [diff] [blame] | 416 | mount.Mount.return_value = [self.image_mount_dir] |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 417 | yield mount |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 418 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 419 | self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions) |
| 420 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 421 | def testValidateOnly(self): |
| 422 | """Sanity check that a validate only call does not execute any logic.""" |
| 423 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 424 | self.validate_only_config) |
| 425 | self.mock_create_moblab_vms.assert_not_called() |
| 426 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 427 | def testImageContainsBuilder(self): |
| 428 | """MoblabVmTest calls service with correct args.""" |
| 429 | request = self._Input() |
| 430 | response = self._Output() |
| 431 | |
| 432 | self.PatchObject( |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 433 | key_value_store, 'LoadFile', |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 434 | return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder}) |
| 435 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 436 | test_controller.MoblabVmTest(request, response, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 437 | |
| 438 | self.assertEqual( |
| 439 | self.mock_create_moblab_vms.call_args_list, |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 440 | [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 441 | self.assertEqual( |
| 442 | self.mock_prepare_moblab_vm_image_cache.call_args_list, |
| 443 | [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])]) |
| 444 | self.assertEqual( |
| 445 | self.mock_run_moblab_vm_tests.call_args_list, |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 446 | [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir, |
| 447 | mock.ANY)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 448 | self.assertEqual( |
| 449 | self.mock_validate_moblab_vm_tests.call_args_list, |
| 450 | [mock.call(mock.ANY)]) |
| 451 | |
| 452 | def testImageMissingBuilder(self): |
| 453 | """MoblabVmTest dies when builder path not found in lsb-release.""" |
| 454 | request = self._Input() |
| 455 | response = self._Output() |
| 456 | |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 457 | self.PatchObject(key_value_store, 'LoadFile', return_value={}) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 458 | |
| 459 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 460 | test_controller.MoblabVmTest(request, response, self.api_config) |