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