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