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