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 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 110 | 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 Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 138 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 143 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 144 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 145 | |
| 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 151 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 152 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 153 | |
| 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 160 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 161 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 162 | |
| 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 Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 170 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 171 | 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 Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 174 | |
| 175 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 176 | output_msg = self._GetOutput() |
| 177 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 178 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 179 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 180 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 181 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 182 | 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 Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 186 | self.assertCountEqual(expected, failed) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 187 | |
| 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 Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 193 | result = test_service.BuildTargetUnitTestResult(1, None) |
| 194 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 195 | |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 196 | pkgs = ['foo/bar', 'cat/pkg'] |
| 197 | blacklist = [portage_util.SplitCPV(p, strict=False) for p in pkgs] |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 198 | input_msg = self._GetInput(board='board', result_path=self.tempdir, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 199 | empty_sysroot=True, blacklist=blacklist) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 200 | output_msg = self._GetOutput() |
| 201 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 202 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 203 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 204 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 205 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 206 | self.assertFalse(output_msg.failed_packages) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 207 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 208 | 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 Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 225 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 226 | class 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 | |
Michael Mortensen | 7a860eb | 2019-12-03 20:25:15 -0700 | [diff] [blame] | 254 | def testMockError(self): |
| 255 | """Test mock error call does not execute any logic, returns error.""" |
| 256 | patch = self.PatchObject(cros_build_lib, 'run') |
| 257 | |
| 258 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 259 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 260 | self.mock_error_config) |
| 261 | patch.assert_not_called() |
| 262 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 263 | |
| 264 | def testMockCall(self): |
| 265 | """Test mock call does not execute any logic, returns success.""" |
| 266 | patch = self.PatchObject(cros_build_lib, 'run') |
| 267 | |
| 268 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 269 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 270 | self.mock_call_config) |
| 271 | patch.assert_not_called() |
| 272 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 273 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 274 | def testChromiteUnitTest(self): |
| 275 | """Call ChromiteUnitTest with mocked cros_build_lib.run.""" |
| 276 | request = self._GetInput(chroot_path=self.chroot_path) |
| 277 | patch = self.PatchObject( |
| 278 | cros_build_lib, 'run', |
| 279 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 280 | |
| 281 | test_controller.ChromiteUnitTest(request, self._GetOutput(), |
| 282 | self.api_config) |
| 283 | patch.assert_called_once() |
| 284 | |
| 285 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 286 | class CrosSigningTestTest(cros_test_lib.RunCommandTestCase, |
| 287 | api_config.ApiConfigMixin): |
| 288 | """CrosSigningTest tests.""" |
| 289 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 290 | def setUp(self): |
| 291 | self.chroot_path = '/path/to/chroot' |
| 292 | |
| 293 | def _GetInput(self, chroot_path=None): |
| 294 | """Helper to build an input message instance.""" |
| 295 | proto = test_pb2.CrosSigningTestRequest( |
| 296 | chroot={'path': chroot_path}, |
| 297 | ) |
| 298 | return proto |
| 299 | |
| 300 | def _GetOutput(self): |
| 301 | """Helper to get an empty output message instance.""" |
| 302 | return test_pb2.CrosSigningTestResponse() |
| 303 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 304 | def testValidateOnly(self): |
| 305 | """Sanity check that a validate only call does not execute any logic.""" |
| 306 | test_controller.CrosSigningTest(None, None, self.validate_only_config) |
| 307 | self.assertFalse(self.rc.call_count) |
| 308 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 309 | def testCrosSigningTest(self): |
| 310 | """Call CrosSigningTest with mocked cros_build_lib.run.""" |
| 311 | request = self._GetInput(chroot_path=self.chroot_path) |
| 312 | patch = self.PatchObject( |
| 313 | cros_build_lib, 'run', |
| 314 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 315 | |
| 316 | test_controller.CrosSigningTest(request, self._GetOutput(), |
| 317 | self.api_config) |
| 318 | patch.assert_called_once() |
| 319 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 320 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 321 | class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase, |
| 322 | api_config.ApiConfigMixin): |
| 323 | """Test the SimpleChromeWorkflowTest endpoint.""" |
| 324 | |
| 325 | @staticmethod |
| 326 | def _Output(): |
| 327 | return test_pb2.SimpleChromeWorkflowTestResponse() |
| 328 | |
| 329 | def _Input(self, sysroot_path=None, build_target=None, chrome_root=None, |
| 330 | goma_config=None): |
| 331 | proto = test_pb2.SimpleChromeWorkflowTestRequest() |
| 332 | if sysroot_path: |
| 333 | proto.sysroot.path = sysroot_path |
| 334 | if build_target: |
| 335 | proto.sysroot.build_target.name = build_target |
| 336 | if chrome_root: |
| 337 | proto.chrome_root = chrome_root |
| 338 | if goma_config: |
| 339 | proto.goma_config = goma_config |
| 340 | return proto |
| 341 | |
| 342 | def setUp(self): |
| 343 | self.chrome_path = 'path/to/chrome' |
| 344 | self.sysroot_dir = 'build/board' |
| 345 | self.build_target = 'amd64' |
| 346 | self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 347 | test_service, 'SimpleChromeWorkflowTest') |
| 348 | |
| 349 | def testMissingBuildTarget(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 350 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 351 | input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir', |
| 352 | chrome_root='/chrome/path') |
| 353 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 354 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 355 | self.api_config) |
| 356 | |
| 357 | def testMissingSysrootPath(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 358 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 359 | input_proto = self._Input(build_target='board', sysroot_path=None, |
| 360 | chrome_root='/chrome/path') |
| 361 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 362 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 363 | self.api_config) |
| 364 | |
| 365 | def testMissingChromeRoot(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 366 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 367 | input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir', |
| 368 | chrome_root=None) |
| 369 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 370 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 371 | self.api_config) |
| 372 | |
| 373 | def testSimpleChromeWorkflowTest(self): |
| 374 | """Call SimpleChromeWorkflowTest with valid args and temp dir.""" |
| 375 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 376 | chrome_root='/path/to/chrome') |
| 377 | response = self._Output() |
| 378 | |
| 379 | test_controller.SimpleChromeWorkflowTest(request, response, self.api_config) |
| 380 | self.mock_simple_chrome_workflow_test.assert_called() |
| 381 | |
| 382 | def testValidateOnly(self): |
| 383 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 384 | chrome_root='/path/to/chrome') |
| 385 | test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 386 | self.validate_only_config) |
| 387 | self.mock_simple_chrome_workflow_test.assert_not_called() |
| 388 | |
| 389 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 390 | class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 391 | """Test the VmTest endpoint.""" |
| 392 | |
| 393 | def _GetInput(self, **kwargs): |
| 394 | values = dict( |
| 395 | build_target=common_pb2.BuildTarget(name='target'), |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 396 | vm_path=common_pb2.Path(path='/path/to/image.bin', |
| 397 | location=common_pb2.Path.INSIDE), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 398 | test_harness=test_pb2.VmTestRequest.TAST, |
| 399 | vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')], |
| 400 | ssh_options=test_pb2.VmTestRequest.SshOptions( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 401 | port=1234, private_key_path={'path': '/path/to/id_rsa', |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 402 | 'location': common_pb2.Path.INSIDE}), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 403 | ) |
| 404 | values.update(kwargs) |
| 405 | return test_pb2.VmTestRequest(**values) |
| 406 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 407 | def _Output(self): |
| 408 | return test_pb2.VmTestResponse() |
| 409 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 410 | def testValidateOnly(self): |
| 411 | """Sanity check that a validate only call does not execute any logic.""" |
| 412 | test_controller.VmTest(self._GetInput(), None, self.validate_only_config) |
| 413 | self.assertEqual(0, self.rc.call_count) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 414 | |
| 415 | def testTastAllOptions(self): |
| 416 | """Test VmTest for Tast with all options set.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 417 | test_controller.VmTest(self._GetInput(), None, self.api_config) |
| 418 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 419 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 420 | '--board', 'target', |
| 421 | '--image-path', '/path/to/image.bin', |
| 422 | '--tast', 'suite', |
| 423 | '--ssh-port', '1234', |
| 424 | '--private-key', '/path/to/id_rsa', |
| 425 | ]) |
| 426 | |
| 427 | def testAutotestAllOptions(self): |
| 428 | """Test VmTest for Autotest with all options set.""" |
| 429 | input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 430 | test_controller.VmTest(input_proto, None, self.api_config) |
| 431 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 432 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 433 | '--board', 'target', |
| 434 | '--image-path', '/path/to/image.bin', |
| 435 | '--autotest', 'suite', |
| 436 | '--ssh-port', '1234', |
| 437 | '--private-key', '/path/to/id_rsa', |
| 438 | '--test_that-args=--whitelist-chrome-crashes', |
| 439 | ]) |
| 440 | |
| 441 | def testMissingBuildTarget(self): |
| 442 | """Test VmTest dies when build_target not set.""" |
| 443 | input_proto = self._GetInput(build_target=None) |
| 444 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 445 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 446 | |
| 447 | def testMissingVmImage(self): |
| 448 | """Test VmTest dies when vm_image not set.""" |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 449 | input_proto = self._GetInput(vm_path=None) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 450 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 451 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 452 | |
| 453 | def testMissingTestHarness(self): |
| 454 | """Test VmTest dies when test_harness not specified.""" |
| 455 | input_proto = self._GetInput( |
| 456 | test_harness=test_pb2.VmTestRequest.UNSPECIFIED) |
| 457 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 458 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 459 | |
| 460 | def testMissingVmTests(self): |
| 461 | """Test VmTest dies when vm_tests not set.""" |
| 462 | input_proto = self._GetInput(vm_tests=[]) |
| 463 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 464 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 465 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 466 | def testVmTest(self): |
| 467 | """Call VmTest with valid args and temp dir.""" |
| 468 | request = self._GetInput() |
| 469 | response = self._Output() |
| 470 | patch = self.PatchObject( |
| 471 | cros_build_lib, 'run', |
| 472 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 473 | |
| 474 | test_controller.VmTest(request, response, self.api_config) |
| 475 | patch.assert_called() |
| 476 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 477 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 478 | class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 479 | """Test the MoblabVmTest endpoint.""" |
| 480 | |
| 481 | @staticmethod |
| 482 | def _Payload(path): |
| 483 | return test_pb2.MoblabVmTestRequest.Payload( |
| 484 | path=common_pb2.Path(path=path)) |
| 485 | |
| 486 | @staticmethod |
| 487 | def _Output(): |
| 488 | return test_pb2.MoblabVmTestResponse() |
| 489 | |
| 490 | def _Input(self): |
| 491 | return test_pb2.MoblabVmTestRequest( |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 492 | chroot=common_pb2.Chroot(path=self.chroot_dir), |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 493 | image_payload=self._Payload(self.image_payload_dir), |
| 494 | cache_payloads=[self._Payload(self.autotest_payload_dir)]) |
| 495 | |
| 496 | def setUp(self): |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 497 | self.chroot_dir = '/chroot' |
| 498 | self.chroot_tmp_dir = '/chroot/tmp' |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 499 | self.image_payload_dir = '/payloads/image' |
| 500 | self.autotest_payload_dir = '/payloads/autotest' |
| 501 | self.builder = 'moblab-generic-vm/R12-3.4.5-67.890' |
| 502 | self.image_cache_dir = '/mnt/moblab/cache' |
| 503 | self.image_mount_dir = '/mnt/image' |
| 504 | |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 505 | self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir) |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 506 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 507 | self.mock_create_moblab_vms = self.PatchObject( |
| 508 | test_service, 'CreateMoblabVm') |
| 509 | self.mock_prepare_moblab_vm_image_cache = self.PatchObject( |
| 510 | test_service, 'PrepareMoblabVmImageCache', |
| 511 | return_value=self.image_cache_dir) |
| 512 | self.mock_run_moblab_vm_tests = self.PatchObject( |
| 513 | test_service, 'RunMoblabVmTest') |
| 514 | self.mock_validate_moblab_vm_tests = self.PatchObject( |
| 515 | test_service, 'ValidateMoblabVmTest') |
| 516 | |
| 517 | @contextlib.contextmanager |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 518 | def MockLoopbackPartitions(*_args, **_kwargs): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 519 | mount = mock.MagicMock() |
Evan Hernandez | 40ee745 | 2019-06-13 12:51:43 -0600 | [diff] [blame] | 520 | mount.Mount.return_value = [self.image_mount_dir] |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 521 | yield mount |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 522 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 523 | self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions) |
| 524 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 525 | def testValidateOnly(self): |
| 526 | """Sanity check that a validate only call does not execute any logic.""" |
| 527 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 528 | self.validate_only_config) |
| 529 | self.mock_create_moblab_vms.assert_not_called() |
| 530 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 531 | def testImageContainsBuilder(self): |
| 532 | """MoblabVmTest calls service with correct args.""" |
| 533 | request = self._Input() |
| 534 | response = self._Output() |
| 535 | |
| 536 | self.PatchObject( |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 537 | key_value_store, 'LoadFile', |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 538 | return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder}) |
| 539 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 540 | test_controller.MoblabVmTest(request, response, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 541 | |
| 542 | self.assertEqual( |
| 543 | self.mock_create_moblab_vms.call_args_list, |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 544 | [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 545 | self.assertEqual( |
| 546 | self.mock_prepare_moblab_vm_image_cache.call_args_list, |
| 547 | [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])]) |
| 548 | self.assertEqual( |
| 549 | self.mock_run_moblab_vm_tests.call_args_list, |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 550 | [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir, |
| 551 | mock.ANY)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 552 | self.assertEqual( |
| 553 | self.mock_validate_moblab_vm_tests.call_args_list, |
| 554 | [mock.call(mock.ANY)]) |
| 555 | |
| 556 | def testImageMissingBuilder(self): |
| 557 | """MoblabVmTest dies when builder path not found in lsb-release.""" |
| 558 | request = self._Input() |
| 559 | response = self._Output() |
| 560 | |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 561 | self.PatchObject(key_value_store, 'LoadFile', return_value={}) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 562 | |
| 563 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 564 | test_controller.MoblabVmTest(request, response, self.api_config) |