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 | |
Alex Klein | 9f91578 | 2020-02-14 23:15:09 +0000 | [diff] [blame] | 10 | import contextlib |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 11 | import os |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 12 | |
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 |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 25 | from chromite.lib.parser import package_info |
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 | |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 64 | 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 Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 84 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 98 | class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase, |
| 99 | api_config.ApiConfigMixin): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 100 | """Tests for the UnitTest function.""" |
| 101 | |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 102 | def _GetInput(self, |
| 103 | board=None, |
| 104 | result_path=None, |
| 105 | chroot_path=None, |
| 106 | cache_dir=None, |
| 107 | empty_sysroot=None, |
| 108 | packages=None, |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 109 | blocklist=None): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 110 | """Helper to build an input message instance.""" |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 111 | formatted_packages = [] |
| 112 | for pkg in packages or []: |
| 113 | formatted_packages.append({ |
| 114 | 'category': pkg.category, |
| 115 | 'package_name': pkg.package |
| 116 | }) |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 117 | formatted_blocklist = [] |
| 118 | for pkg in blocklist or []: |
| 119 | formatted_blocklist.append({'category': pkg.category, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 120 | 'package_name': pkg.package}) |
| 121 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 122 | return test_pb2.BuildTargetUnitTestRequest( |
| 123 | build_target={'name': board}, result_path=result_path, |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 124 | chroot={'path': chroot_path, 'cache_dir': cache_dir}, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 125 | flags={'empty_sysroot': empty_sysroot}, |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 126 | package_blacklist=formatted_blocklist, |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 127 | ) |
| 128 | |
| 129 | def _GetOutput(self): |
| 130 | """Helper to get an empty output message instance.""" |
| 131 | return test_pb2.BuildTargetUnitTestResponse() |
| 132 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 133 | def testValidateOnly(self): |
| 134 | """Sanity check that a validate only call does not execute any logic.""" |
| 135 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 136 | |
| 137 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 138 | test_controller.BuildTargetUnitTest(input_msg, self._GetOutput(), |
| 139 | self.validate_only_config) |
| 140 | patch.assert_not_called() |
| 141 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 142 | def testMockCall(self): |
| 143 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 144 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 145 | |
| 146 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 147 | response = self._GetOutput() |
| 148 | test_controller.BuildTargetUnitTest(input_msg, response, |
| 149 | self.mock_call_config) |
| 150 | patch.assert_not_called() |
| 151 | self.assertEqual(response.tarball_path, |
| 152 | os.path.join(input_msg.result_path, 'unit_tests.tar')) |
| 153 | |
| 154 | def testMockError(self): |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 155 | """Test that a mock error does not execute logic, returns error.""" |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 156 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 157 | |
| 158 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 159 | response = self._GetOutput() |
| 160 | rc = test_controller.BuildTargetUnitTest(input_msg, response, |
| 161 | self.mock_error_config) |
| 162 | patch.assert_not_called() |
| 163 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
| 164 | self.assertTrue(response.failed_packages) |
| 165 | self.assertEqual(response.failed_packages[0].category, 'foo') |
| 166 | self.assertEqual(response.failed_packages[0].package_name, 'bar') |
| 167 | self.assertEqual(response.failed_packages[1].category, 'cat') |
| 168 | self.assertEqual(response.failed_packages[1].package_name, 'pkg') |
| 169 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 170 | def testNoArgumentFails(self): |
| 171 | """Test no arguments fails.""" |
| 172 | input_msg = self._GetInput() |
| 173 | output_msg = self._GetOutput() |
| 174 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 175 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 176 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 177 | |
| 178 | def testNoBuildTargetFails(self): |
| 179 | """Test missing build target name fails.""" |
| 180 | input_msg = self._GetInput(result_path=self.tempdir) |
| 181 | output_msg = self._GetOutput() |
| 182 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 183 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 184 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 185 | |
| 186 | def testNoResultPathFails(self): |
| 187 | """Test missing result path fails.""" |
| 188 | # Missing result_path. |
| 189 | input_msg = self._GetInput(board='board') |
| 190 | output_msg = self._GetOutput() |
| 191 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 192 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 193 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 194 | |
| 195 | def testPackageBuildFailure(self): |
| 196 | """Test handling of raised BuildPackageFailure.""" |
| 197 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 198 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 199 | |
| 200 | pkgs = ['cat/pkg', 'foo/bar'] |
| 201 | expected = [('cat', 'pkg'), ('foo', 'bar')] |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 202 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 203 | result = test_service.BuildTargetUnitTestResult(1, None) |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 204 | result.failed_cpvs = [package_info.SplitCPV(p, strict=False) for p in pkgs] |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 205 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 206 | |
| 207 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 208 | output_msg = self._GetOutput() |
| 209 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 210 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 211 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 212 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 213 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 214 | self.assertTrue(output_msg.failed_packages) |
| 215 | failed = [] |
| 216 | for pi in output_msg.failed_packages: |
| 217 | failed.append((pi.category, pi.package_name)) |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 218 | self.assertCountEqual(expected, failed) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 219 | |
| 220 | def testOtherBuildScriptFailure(self): |
| 221 | """Test build script failure due to non-package emerge error.""" |
| 222 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 223 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 224 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 225 | result = test_service.BuildTargetUnitTestResult(1, None) |
| 226 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 227 | |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 228 | pkgs = ['foo/bar', 'cat/pkg'] |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 229 | blocklist = [package_info.SplitCPV(p, strict=False) for p in pkgs] |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 230 | input_msg = self._GetInput(board='board', result_path=self.tempdir, |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 231 | empty_sysroot=True, blocklist=blocklist) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 232 | output_msg = self._GetOutput() |
| 233 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 234 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 235 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 236 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 237 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 238 | self.assertFalse(output_msg.failed_packages) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 239 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 240 | def testBuildTargetUnitTest(self): |
| 241 | """Test BuildTargetUnitTest successful call.""" |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 242 | pkgs = ['foo/bar', 'cat/pkg'] |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 243 | packages = [package_info.SplitCPV(p, strict=False) for p in pkgs] |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 244 | input_msg = self._GetInput( |
| 245 | board='board', result_path=self.tempdir, packages=packages) |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 246 | |
| 247 | result = test_service.BuildTargetUnitTestResult(0, None) |
| 248 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
| 249 | |
| 250 | tarball_result = os.path.join(input_msg.result_path, 'unit_tests.tar') |
| 251 | self.PatchObject(test_service, 'BuildTargetUnitTestTarball', |
| 252 | return_value=tarball_result) |
| 253 | |
| 254 | response = self._GetOutput() |
| 255 | test_controller.BuildTargetUnitTest(input_msg, response, |
| 256 | self.api_config) |
| 257 | self.assertEqual(response.tarball_path, |
| 258 | os.path.join(input_msg.result_path, 'unit_tests.tar')) |
| 259 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 260 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 261 | class ChromiteUnitTestTest(cros_test_lib.MockTestCase, |
| 262 | api_config.ApiConfigMixin): |
| 263 | """Tests for the ChromiteInfoTest function.""" |
| 264 | |
| 265 | def setUp(self): |
| 266 | self.board = 'board' |
| 267 | self.chroot_path = '/path/to/chroot' |
| 268 | |
| 269 | def _GetInput(self, chroot_path=None): |
| 270 | """Helper to build an input message instance.""" |
| 271 | proto = test_pb2.ChromiteUnitTestRequest( |
| 272 | chroot={'path': chroot_path}, |
| 273 | ) |
| 274 | return proto |
| 275 | |
| 276 | def _GetOutput(self): |
| 277 | """Helper to get an empty output message instance.""" |
| 278 | return test_pb2.ChromiteUnitTestResponse() |
| 279 | |
| 280 | def testValidateOnly(self): |
| 281 | """Sanity check that a validate only call does not execute any logic.""" |
| 282 | patch = self.PatchObject(cros_build_lib, 'run') |
| 283 | |
| 284 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 285 | test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 286 | self.validate_only_config) |
| 287 | patch.assert_not_called() |
| 288 | |
Michael Mortensen | 7a860eb | 2019-12-03 20:25:15 -0700 | [diff] [blame] | 289 | def testMockError(self): |
| 290 | """Test mock error call does not execute any logic, returns error.""" |
| 291 | patch = self.PatchObject(cros_build_lib, 'run') |
| 292 | |
| 293 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 294 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 295 | self.mock_error_config) |
| 296 | patch.assert_not_called() |
| 297 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 298 | |
| 299 | def testMockCall(self): |
| 300 | """Test mock call does not execute any logic, returns success.""" |
| 301 | patch = self.PatchObject(cros_build_lib, 'run') |
| 302 | |
| 303 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 304 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 305 | self.mock_call_config) |
| 306 | patch.assert_not_called() |
| 307 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 308 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 309 | def testChromiteUnitTest(self): |
| 310 | """Call ChromiteUnitTest 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.ChromiteUnitTest(request, self._GetOutput(), |
| 317 | self.api_config) |
| 318 | patch.assert_called_once() |
| 319 | |
| 320 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 321 | class CrosSigningTestTest(cros_test_lib.RunCommandTestCase, |
| 322 | api_config.ApiConfigMixin): |
| 323 | """CrosSigningTest tests.""" |
| 324 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 325 | def setUp(self): |
| 326 | self.chroot_path = '/path/to/chroot' |
| 327 | |
| 328 | def _GetInput(self, chroot_path=None): |
| 329 | """Helper to build an input message instance.""" |
| 330 | proto = test_pb2.CrosSigningTestRequest( |
| 331 | chroot={'path': chroot_path}, |
| 332 | ) |
| 333 | return proto |
| 334 | |
| 335 | def _GetOutput(self): |
| 336 | """Helper to get an empty output message instance.""" |
| 337 | return test_pb2.CrosSigningTestResponse() |
| 338 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 339 | def testValidateOnly(self): |
| 340 | """Sanity check that a validate only call does not execute any logic.""" |
| 341 | test_controller.CrosSigningTest(None, None, self.validate_only_config) |
| 342 | self.assertFalse(self.rc.call_count) |
| 343 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 344 | def testMockCall(self): |
| 345 | """Test mock call does not execute any logic, returns success.""" |
| 346 | rc = test_controller.CrosSigningTest(None, None, self.mock_call_config) |
| 347 | self.assertFalse(self.rc.call_count) |
| 348 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 349 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 350 | def testCrosSigningTest(self): |
| 351 | """Call CrosSigningTest with mocked cros_build_lib.run.""" |
| 352 | request = self._GetInput(chroot_path=self.chroot_path) |
| 353 | patch = self.PatchObject( |
| 354 | cros_build_lib, 'run', |
| 355 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 356 | |
| 357 | test_controller.CrosSigningTest(request, self._GetOutput(), |
| 358 | self.api_config) |
| 359 | patch.assert_called_once() |
| 360 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 361 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 362 | class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase, |
| 363 | api_config.ApiConfigMixin): |
| 364 | """Test the SimpleChromeWorkflowTest endpoint.""" |
| 365 | |
| 366 | @staticmethod |
| 367 | def _Output(): |
| 368 | return test_pb2.SimpleChromeWorkflowTestResponse() |
| 369 | |
| 370 | def _Input(self, sysroot_path=None, build_target=None, chrome_root=None, |
| 371 | goma_config=None): |
| 372 | proto = test_pb2.SimpleChromeWorkflowTestRequest() |
| 373 | if sysroot_path: |
| 374 | proto.sysroot.path = sysroot_path |
| 375 | if build_target: |
| 376 | proto.sysroot.build_target.name = build_target |
| 377 | if chrome_root: |
| 378 | proto.chrome_root = chrome_root |
| 379 | if goma_config: |
| 380 | proto.goma_config = goma_config |
| 381 | return proto |
| 382 | |
| 383 | def setUp(self): |
| 384 | self.chrome_path = 'path/to/chrome' |
| 385 | self.sysroot_dir = 'build/board' |
| 386 | self.build_target = 'amd64' |
| 387 | self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 388 | test_service, 'SimpleChromeWorkflowTest') |
| 389 | |
| 390 | def testMissingBuildTarget(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 391 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 392 | input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir', |
| 393 | chrome_root='/chrome/path') |
| 394 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 395 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 396 | self.api_config) |
| 397 | |
| 398 | def testMissingSysrootPath(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 399 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 400 | input_proto = self._Input(build_target='board', sysroot_path=None, |
| 401 | chrome_root='/chrome/path') |
| 402 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 403 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 404 | self.api_config) |
| 405 | |
| 406 | def testMissingChromeRoot(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 407 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 408 | input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir', |
| 409 | chrome_root=None) |
| 410 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 411 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 412 | self.api_config) |
| 413 | |
| 414 | def testSimpleChromeWorkflowTest(self): |
| 415 | """Call SimpleChromeWorkflowTest with valid args and temp dir.""" |
| 416 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 417 | chrome_root='/path/to/chrome') |
| 418 | response = self._Output() |
| 419 | |
| 420 | test_controller.SimpleChromeWorkflowTest(request, response, self.api_config) |
| 421 | self.mock_simple_chrome_workflow_test.assert_called() |
| 422 | |
| 423 | def testValidateOnly(self): |
| 424 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 425 | chrome_root='/path/to/chrome') |
| 426 | test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 427 | self.validate_only_config) |
| 428 | self.mock_simple_chrome_workflow_test.assert_not_called() |
| 429 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 430 | def testMockCall(self): |
| 431 | """Test mock call does not execute any logic, returns success.""" |
| 432 | patch = self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 433 | test_service, 'SimpleChromeWorkflowTest') |
| 434 | |
| 435 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 436 | chrome_root='/path/to/chrome') |
| 437 | rc = test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 438 | self.mock_call_config) |
| 439 | patch.assert_not_called() |
| 440 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 441 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 442 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 443 | class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 444 | """Test the VmTest endpoint.""" |
| 445 | |
| 446 | def _GetInput(self, **kwargs): |
| 447 | values = dict( |
| 448 | build_target=common_pb2.BuildTarget(name='target'), |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 449 | vm_path=common_pb2.Path(path='/path/to/image.bin', |
| 450 | location=common_pb2.Path.INSIDE), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 451 | test_harness=test_pb2.VmTestRequest.TAST, |
| 452 | vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')], |
| 453 | ssh_options=test_pb2.VmTestRequest.SshOptions( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 454 | port=1234, private_key_path={'path': '/path/to/id_rsa', |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 455 | 'location': common_pb2.Path.INSIDE}), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 456 | ) |
| 457 | values.update(kwargs) |
| 458 | return test_pb2.VmTestRequest(**values) |
| 459 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 460 | def _Output(self): |
| 461 | return test_pb2.VmTestResponse() |
| 462 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 463 | def testValidateOnly(self): |
| 464 | """Sanity check that a validate only call does not execute any logic.""" |
| 465 | test_controller.VmTest(self._GetInput(), None, self.validate_only_config) |
| 466 | self.assertEqual(0, self.rc.call_count) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 467 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 468 | def testMockCall(self): |
| 469 | """Test mock call does not execute any logic.""" |
| 470 | patch = self.PatchObject(cros_build_lib, 'run') |
| 471 | |
| 472 | request = self._GetInput() |
| 473 | response = self._Output() |
| 474 | # VmTest does not return a value, checking mocked value is flagged by lint. |
| 475 | test_controller.VmTest(request, response, self.mock_call_config) |
| 476 | patch.assert_not_called() |
| 477 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 478 | def testTastAllOptions(self): |
| 479 | """Test VmTest for Tast with all options set.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 480 | test_controller.VmTest(self._GetInput(), None, self.api_config) |
| 481 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 482 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 483 | '--board', 'target', |
| 484 | '--image-path', '/path/to/image.bin', |
| 485 | '--tast', 'suite', |
| 486 | '--ssh-port', '1234', |
| 487 | '--private-key', '/path/to/id_rsa', |
| 488 | ]) |
| 489 | |
| 490 | def testAutotestAllOptions(self): |
| 491 | """Test VmTest for Autotest with all options set.""" |
| 492 | input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 493 | test_controller.VmTest(input_proto, None, self.api_config) |
| 494 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 495 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 496 | '--board', 'target', |
| 497 | '--image-path', '/path/to/image.bin', |
| 498 | '--autotest', 'suite', |
| 499 | '--ssh-port', '1234', |
| 500 | '--private-key', '/path/to/id_rsa', |
Greg Edelston | dcb0e91 | 2020-08-31 11:09:40 -0600 | [diff] [blame] | 501 | '--test_that-args=--allow-chrome-crashes', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 502 | ]) |
| 503 | |
| 504 | def testMissingBuildTarget(self): |
| 505 | """Test VmTest dies when build_target not set.""" |
| 506 | input_proto = self._GetInput(build_target=None) |
| 507 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 508 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 509 | |
| 510 | def testMissingVmImage(self): |
| 511 | """Test VmTest dies when vm_image not set.""" |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 512 | input_proto = self._GetInput(vm_path=None) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 513 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 514 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 515 | |
| 516 | def testMissingTestHarness(self): |
| 517 | """Test VmTest dies when test_harness not specified.""" |
| 518 | input_proto = self._GetInput( |
| 519 | test_harness=test_pb2.VmTestRequest.UNSPECIFIED) |
| 520 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 521 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 522 | |
| 523 | def testMissingVmTests(self): |
| 524 | """Test VmTest dies when vm_tests not set.""" |
| 525 | input_proto = self._GetInput(vm_tests=[]) |
| 526 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 527 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 528 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 529 | def testVmTest(self): |
| 530 | """Call VmTest with valid args and temp dir.""" |
| 531 | request = self._GetInput() |
| 532 | response = self._Output() |
| 533 | patch = self.PatchObject( |
| 534 | cros_build_lib, 'run', |
| 535 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 536 | |
| 537 | test_controller.VmTest(request, response, self.api_config) |
| 538 | patch.assert_called() |
| 539 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 540 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 541 | class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 542 | """Test the MoblabVmTest endpoint.""" |
| 543 | |
| 544 | @staticmethod |
| 545 | def _Payload(path): |
| 546 | return test_pb2.MoblabVmTestRequest.Payload( |
| 547 | path=common_pb2.Path(path=path)) |
| 548 | |
| 549 | @staticmethod |
| 550 | def _Output(): |
| 551 | return test_pb2.MoblabVmTestResponse() |
| 552 | |
| 553 | def _Input(self): |
| 554 | return test_pb2.MoblabVmTestRequest( |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 555 | chroot=common_pb2.Chroot(path=self.chroot_dir), |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 556 | image_payload=self._Payload(self.image_payload_dir), |
| 557 | cache_payloads=[self._Payload(self.autotest_payload_dir)]) |
| 558 | |
| 559 | def setUp(self): |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 560 | self.chroot_dir = '/chroot' |
| 561 | self.chroot_tmp_dir = '/chroot/tmp' |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 562 | self.image_payload_dir = '/payloads/image' |
| 563 | self.autotest_payload_dir = '/payloads/autotest' |
| 564 | self.builder = 'moblab-generic-vm/R12-3.4.5-67.890' |
| 565 | self.image_cache_dir = '/mnt/moblab/cache' |
| 566 | self.image_mount_dir = '/mnt/image' |
| 567 | |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 568 | self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir) |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 569 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 570 | self.mock_create_moblab_vms = self.PatchObject( |
| 571 | test_service, 'CreateMoblabVm') |
| 572 | self.mock_prepare_moblab_vm_image_cache = self.PatchObject( |
| 573 | test_service, 'PrepareMoblabVmImageCache', |
| 574 | return_value=self.image_cache_dir) |
| 575 | self.mock_run_moblab_vm_tests = self.PatchObject( |
| 576 | test_service, 'RunMoblabVmTest') |
| 577 | self.mock_validate_moblab_vm_tests = self.PatchObject( |
| 578 | test_service, 'ValidateMoblabVmTest') |
| 579 | |
| 580 | @contextlib.contextmanager |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 581 | def MockLoopbackPartitions(*_args, **_kwargs): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 582 | mount = mock.MagicMock() |
Evan Hernandez | 40ee745 | 2019-06-13 12:51:43 -0600 | [diff] [blame] | 583 | mount.Mount.return_value = [self.image_mount_dir] |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 584 | yield mount |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 585 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 586 | self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions) |
| 587 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 588 | def testValidateOnly(self): |
| 589 | """Sanity check that a validate only call does not execute any logic.""" |
| 590 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 591 | self.validate_only_config) |
| 592 | self.mock_create_moblab_vms.assert_not_called() |
| 593 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 594 | def testMockCall(self): |
| 595 | """Test mock call does not execute any logic.""" |
| 596 | patch = self.PatchObject(key_value_store, 'LoadFile') |
| 597 | |
| 598 | # MoblabVmTest does not return a value, checking mocked value is flagged by |
| 599 | # lint. |
| 600 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 601 | self.mock_call_config) |
| 602 | patch.assert_not_called() |
| 603 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 604 | def testImageContainsBuilder(self): |
| 605 | """MoblabVmTest calls service with correct args.""" |
| 606 | request = self._Input() |
| 607 | response = self._Output() |
| 608 | |
| 609 | self.PatchObject( |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 610 | key_value_store, 'LoadFile', |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 611 | return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder}) |
| 612 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 613 | test_controller.MoblabVmTest(request, response, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 614 | |
| 615 | self.assertEqual( |
| 616 | self.mock_create_moblab_vms.call_args_list, |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 617 | [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 618 | self.assertEqual( |
| 619 | self.mock_prepare_moblab_vm_image_cache.call_args_list, |
| 620 | [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])]) |
| 621 | self.assertEqual( |
| 622 | self.mock_run_moblab_vm_tests.call_args_list, |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 623 | [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir, |
| 624 | mock.ANY)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 625 | self.assertEqual( |
| 626 | self.mock_validate_moblab_vm_tests.call_args_list, |
| 627 | [mock.call(mock.ANY)]) |
| 628 | |
| 629 | def testImageMissingBuilder(self): |
| 630 | """MoblabVmTest dies when builder path not found in lsb-release.""" |
| 631 | request = self._Input() |
| 632 | response = self._Output() |
| 633 | |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 634 | self.PatchObject(key_value_store, 'LoadFile', return_value={}) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 635 | |
| 636 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 637 | test_controller.MoblabVmTest(request, response, self.api_config) |