Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [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 | """Image service tests.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 10 | import mock |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 11 | import os |
| 12 | |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 13 | from chromite.api.gen.chromite.api import image_pb2 |
David Burger | 13e06be | 2019-05-13 20:33:16 -0600 | [diff] [blame] | 14 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 15 | from chromite.api.controller import image as image_controller |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 16 | from chromite.lib import constants |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 17 | from chromite.lib import cros_build_lib |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_test_lib |
| 19 | from chromite.lib import osutils |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 20 | from chromite.service import image as image_service |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 21 | |
| 22 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 23 | class CreateTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 24 | """Create image tests.""" |
| 25 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 26 | def _GetRequest(self, board=None, types=None, version=None, builder_path=None, |
| 27 | disable_rootfs_verification=False): |
| 28 | """Helper to build a request instance.""" |
| 29 | return image_pb2.CreateImageRequest( |
| 30 | build_target={'name': board}, |
| 31 | image_types=types, |
| 32 | disable_rootfs_verification=disable_rootfs_verification, |
| 33 | version=version, |
| 34 | builder_path=builder_path, |
| 35 | ) |
| 36 | |
| 37 | def _GetResponse(self): |
| 38 | """Helper to build an empty response instance.""" |
| 39 | return image_pb2.CreateImageResult() |
| 40 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 41 | def testArgumentValidation(self): |
| 42 | """Test the argument validation.""" |
| 43 | input_proto = image_pb2.CreateImageRequest() |
| 44 | output_proto = image_pb2.CreateImageResult() |
| 45 | |
| 46 | # No board should cause it to fail. |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 47 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 48 | image_controller.Create(input_proto, output_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 49 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 50 | def testNoTypeSpecified(self): |
| 51 | """Test the image type default.""" |
| 52 | request = self._GetRequest(board='board') |
| 53 | response = self._GetResponse() |
| 54 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 55 | # Failed result to avoid the success handling logic. |
| 56 | result = image_service.BuildResult(1, []) |
| 57 | build_patch = self.PatchObject(image_service, 'Build', return_value=result) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 58 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 59 | image_controller.Create(request, response) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 60 | build_patch.assert_called_with(images=[constants.IMAGE_TYPE_BASE], |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 61 | board='board', config=mock.ANY) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 62 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 63 | def testSingleTypeSpecified(self): |
| 64 | """Test it's properly using a specified type.""" |
| 65 | request = self._GetRequest(board='board', types=[common_pb2.DEV]) |
| 66 | response = self._GetResponse() |
| 67 | |
| 68 | # Failed result to avoid the success handling logic. |
| 69 | result = image_service.BuildResult(1, []) |
| 70 | build_patch = self.PatchObject(image_service, 'Build', return_value=result) |
| 71 | |
| 72 | image_controller.Create(request, response) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 73 | build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV], |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 74 | board='board', config=mock.ANY) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 75 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 76 | def testMultipleAndImpliedTypes(self): |
| 77 | """Test multiple types and implied type handling.""" |
| 78 | # The TEST_VM type should force it to build the test image. |
| 79 | types = [common_pb2.BASE, common_pb2.TEST_VM] |
| 80 | expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_TEST] |
| 81 | |
| 82 | request = self._GetRequest(board='board', types=types) |
| 83 | response = self._GetResponse() |
| 84 | |
| 85 | # Failed result to avoid the success handling logic. |
| 86 | result = image_service.BuildResult(1, []) |
| 87 | build_patch = self.PatchObject(image_service, 'Build', return_value=result) |
| 88 | |
| 89 | image_controller.Create(request, response) |
| 90 | build_patch.assert_called_with(images=expected_images, board='board', |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 91 | config=mock.ANY) |
| 92 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 93 | def testFailedPackageHandling(self): |
| 94 | """Test failed packages are populated correctly.""" |
| 95 | result = image_service.BuildResult(1, ['foo/bar', 'cat/pkg']) |
| 96 | expected_packages = [('foo', 'bar'), ('cat', 'pkg')] |
| 97 | self.PatchObject(image_service, 'Build', return_value=result) |
| 98 | |
| 99 | input_proto = image_pb2.CreateImageRequest() |
| 100 | input_proto.build_target.name = 'board' |
| 101 | output_proto = image_pb2.CreateImageResult() |
| 102 | |
| 103 | image_controller.Create(input_proto, output_proto) |
| 104 | for package in output_proto.failed_packages: |
| 105 | self.assertIn((package.category, package.package_name), expected_packages) |
| 106 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 107 | |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 108 | class CreateVmTest(cros_test_lib.MockTestCase): |
| 109 | """CreateVm tests.""" |
| 110 | |
| 111 | def _GetInput(self, board=None, image_type=None): |
| 112 | """Helper to create an input proto instance.""" |
| 113 | # pylint: disable=protected-access |
| 114 | |
| 115 | return image_pb2.CreateVmRequest( |
| 116 | image={'build_target': {'name': board}, 'type': image_type}) |
| 117 | |
| 118 | def _GetOutput(self): |
| 119 | """Helper to create an empty output proto instance.""" |
| 120 | return image_pb2.CreateVmResponse() |
| 121 | |
| 122 | def testNoArgsFails(self): |
| 123 | """Make sure it fails with no arguments.""" |
| 124 | request = self._GetInput() |
| 125 | response = self._GetOutput() |
| 126 | |
| 127 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 128 | image_controller.CreateVm(request, response) |
| 129 | |
| 130 | def testNoBuildTargetFails(self): |
| 131 | """Make sure it fails with no build target.""" |
David Burger | 13e06be | 2019-05-13 20:33:16 -0600 | [diff] [blame] | 132 | request = self._GetInput(image_type=common_pb2.TEST) |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 133 | response = self._GetOutput() |
| 134 | |
| 135 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 136 | image_controller.CreateVm(request, response) |
| 137 | |
| 138 | def testNoTypeFails(self): |
| 139 | """Make sure it fails with no build target.""" |
| 140 | request = self._GetInput(board='board') |
| 141 | response = self._GetOutput() |
| 142 | |
| 143 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 144 | image_controller.CreateVm(request, response) |
| 145 | |
| 146 | def testTestImage(self): |
| 147 | """Make sure the test image identification works properly.""" |
David Burger | 13e06be | 2019-05-13 20:33:16 -0600 | [diff] [blame] | 148 | request = self._GetInput(board='board', image_type=common_pb2.TEST) |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 149 | response = self._GetOutput() |
| 150 | create_patch = self.PatchObject(image_service, 'CreateVm', |
| 151 | return_value='/vm/path') |
| 152 | |
| 153 | image_controller.CreateVm(request, response) |
| 154 | |
| 155 | create_patch.assert_called_once_with('board', chroot=mock.ANY, is_test=True) |
| 156 | |
| 157 | def testNonTestImage(self): |
| 158 | """Make sure the test image identification works properly.""" |
David Burger | 13e06be | 2019-05-13 20:33:16 -0600 | [diff] [blame] | 159 | request = self._GetInput(board='board', image_type=common_pb2.BASE) |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 160 | response = self._GetOutput() |
| 161 | create_patch = self.PatchObject(image_service, 'CreateVm', |
| 162 | return_value='/vm/path') |
| 163 | |
| 164 | image_controller.CreateVm(request, response) |
| 165 | |
| 166 | create_patch.assert_called_once_with('board', chroot=mock.ANY, |
| 167 | is_test=False) |
| 168 | |
| 169 | |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 170 | class ImageTest(cros_test_lib.MockTempDirTestCase): |
| 171 | """Image service tests.""" |
| 172 | |
| 173 | def setUp(self): |
| 174 | self.image_path = os.path.join(self.tempdir, 'image.bin') |
| 175 | self.board = 'board' |
| 176 | self.result_directory = os.path.join(self.tempdir, 'results') |
| 177 | |
| 178 | osutils.SafeMakedirs(self.result_directory) |
| 179 | osutils.Touch(self.image_path) |
| 180 | |
| 181 | def testTestArgumentValidation(self): |
| 182 | """Test function argument validation tests.""" |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 183 | self.PatchObject(image_service, 'Test', return_value=True) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 184 | input_proto = image_pb2.TestImageRequest() |
| 185 | output_proto = image_pb2.TestImageResult() |
| 186 | |
| 187 | # Nothing provided. |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 188 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 189 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 190 | |
| 191 | # Just one argument. |
| 192 | input_proto.build_target.name = self.board |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 193 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 194 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 195 | |
| 196 | # Two arguments provided. |
| 197 | input_proto.result.directory = self.result_directory |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 198 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 199 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 200 | |
| 201 | # Invalid image path. |
| 202 | input_proto.image.path = '/invalid/image/path' |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 203 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 204 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 205 | |
| 206 | # All valid arguments. |
| 207 | input_proto.image.path = self.image_path |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 208 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 209 | |
| 210 | def testTestOutputHandling(self): |
| 211 | """Test function output tests.""" |
| 212 | input_proto = image_pb2.TestImageRequest() |
| 213 | input_proto.image.path = self.image_path |
| 214 | input_proto.build_target.name = self.board |
| 215 | input_proto.result.directory = self.result_directory |
| 216 | output_proto = image_pb2.TestImageResult() |
| 217 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 218 | self.PatchObject(image_service, 'Test', return_value=True) |
| 219 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 220 | self.assertTrue(output_proto.success) |
| 221 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 222 | self.PatchObject(image_service, 'Test', return_value=False) |
| 223 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 224 | self.assertFalse(output_proto.success) |