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