blob: 46efa5e334f0608a896cc75809b0fd0193e152f0 [file] [log] [blame]
Alex Klein2966e302019-01-17 13:29:38 -07001# -*- 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
8from __future__ import print_function
9
Alex Klein56355682019-02-07 10:36:54 -070010import mock
Alex Klein2966e302019-01-17 13:29:38 -070011import os
12
Alex Klein8cb365a2019-05-15 16:24:53 -060013from chromite.api import controller
14from chromite.api.controller import image as image_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060015from chromite.api.gen.chromite.api import image_pb2
David Burger13e06be2019-05-13 20:33:16 -060016from chromite.api.gen.chromiumos import common_pb2
Alex Klein56355682019-02-07 10:36:54 -070017from chromite.lib import constants
Alex Klein4f0eb432019-05-02 13:56:04 -060018from chromite.lib import cros_build_lib
Alex Klein2966e302019-01-17 13:29:38 -070019from chromite.lib import cros_test_lib
20from chromite.lib import osutils
Alex Kleinb7cdbe62019-02-22 11:41:32 -070021from chromite.service import image as image_service
Alex Klein2966e302019-01-17 13:29:38 -070022
23
Alex Klein1bcd9882019-03-19 13:25:24 -060024class CreateTest(cros_test_lib.MockTempDirTestCase):
Alex Klein56355682019-02-07 10:36:54 -070025 """Create image tests."""
26
Alex Klein21b95022019-05-09 14:14:46 -060027 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 Klein56355682019-02-07 10:36:54 -070042 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 Klein4f0eb432019-05-02 13:56:04 -060048 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -070049 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070050
Alex Klein21b95022019-05-09 14:14:46 -060051 def testNoTypeSpecified(self):
52 """Test the image type default."""
53 request = self._GetRequest(board='board')
54 response = self._GetResponse()
55
Alex Klein1bcd9882019-03-19 13:25:24 -060056 # 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 Klein56355682019-02-07 10:36:54 -070059
Alex Klein21b95022019-05-09 14:14:46 -060060 image_controller.Create(request, response)
Alex Klein56355682019-02-07 10:36:54 -070061 build_patch.assert_called_with(images=[constants.IMAGE_TYPE_BASE],
Alex Klein21b95022019-05-09 14:14:46 -060062 board='board', config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -070063
Alex Klein21b95022019-05-09 14:14:46 -060064 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 Klein56355682019-02-07 10:36:54 -070074 build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV],
Alex Klein21b95022019-05-09 14:14:46 -060075 board='board', config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -070076
Alex Klein21b95022019-05-09 14:14:46 -060077 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 Klein56355682019-02-07 10:36:54 -070092 config=mock.ANY)
93
Alex Klein1bcd9882019-03-19 13:25:24 -060094 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 Klein8cb365a2019-05-15 16:24:53 -0600104 rc = image_controller.Create(input_proto, output_proto)
105 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Klein1bcd9882019-03-19 13:25:24 -0600106 for package in output_proto.failed_packages:
107 self.assertIn((package.category, package.package_name), expected_packages)
108
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700109
Alex Klein4f0eb432019-05-02 13:56:04 -0600110class CreateVmTest(cros_test_lib.MockTestCase):
111 """CreateVm tests."""
112
113 def _GetInput(self, board=None, image_type=None):
114 """Helper to create an input proto instance."""
115 # pylint: disable=protected-access
116
117 return image_pb2.CreateVmRequest(
118 image={'build_target': {'name': board}, 'type': image_type})
119
120 def _GetOutput(self):
121 """Helper to create an empty output proto instance."""
122 return image_pb2.CreateVmResponse()
123
124 def testNoArgsFails(self):
125 """Make sure it fails with no arguments."""
126 request = self._GetInput()
127 response = self._GetOutput()
128
129 with self.assertRaises(cros_build_lib.DieSystemExit):
130 image_controller.CreateVm(request, response)
131
132 def testNoBuildTargetFails(self):
133 """Make sure it fails with no build target."""
David Burger13e06be2019-05-13 20:33:16 -0600134 request = self._GetInput(image_type=common_pb2.TEST)
Alex Klein4f0eb432019-05-02 13:56:04 -0600135 response = self._GetOutput()
136
137 with self.assertRaises(cros_build_lib.DieSystemExit):
138 image_controller.CreateVm(request, response)
139
140 def testNoTypeFails(self):
141 """Make sure it fails with no build target."""
142 request = self._GetInput(board='board')
143 response = self._GetOutput()
144
145 with self.assertRaises(cros_build_lib.DieSystemExit):
146 image_controller.CreateVm(request, response)
147
148 def testTestImage(self):
149 """Make sure the test image identification works properly."""
David Burger13e06be2019-05-13 20:33:16 -0600150 request = self._GetInput(board='board', image_type=common_pb2.TEST)
Alex Klein4f0eb432019-05-02 13:56:04 -0600151 response = self._GetOutput()
152 create_patch = self.PatchObject(image_service, 'CreateVm',
153 return_value='/vm/path')
154
155 image_controller.CreateVm(request, response)
156
157 create_patch.assert_called_once_with('board', chroot=mock.ANY, is_test=True)
158
159 def testNonTestImage(self):
160 """Make sure the test image identification works properly."""
David Burger13e06be2019-05-13 20:33:16 -0600161 request = self._GetInput(board='board', image_type=common_pb2.BASE)
Alex Klein4f0eb432019-05-02 13:56:04 -0600162 response = self._GetOutput()
163 create_patch = self.PatchObject(image_service, 'CreateVm',
164 return_value='/vm/path')
165
166 image_controller.CreateVm(request, response)
167
168 create_patch.assert_called_once_with('board', chroot=mock.ANY,
169 is_test=False)
170
171
Alex Klein2966e302019-01-17 13:29:38 -0700172class ImageTest(cros_test_lib.MockTempDirTestCase):
173 """Image service tests."""
174
175 def setUp(self):
176 self.image_path = os.path.join(self.tempdir, 'image.bin')
177 self.board = 'board'
178 self.result_directory = os.path.join(self.tempdir, 'results')
179
180 osutils.SafeMakedirs(self.result_directory)
181 osutils.Touch(self.image_path)
182
183 def testTestArgumentValidation(self):
184 """Test function argument validation tests."""
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700185 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -0700186 input_proto = image_pb2.TestImageRequest()
187 output_proto = image_pb2.TestImageResult()
188
189 # Nothing provided.
Alex Klein4f0eb432019-05-02 13:56:04 -0600190 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700191 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700192
193 # Just one argument.
194 input_proto.build_target.name = self.board
Alex Klein4f0eb432019-05-02 13:56:04 -0600195 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700196 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700197
198 # Two arguments provided.
199 input_proto.result.directory = self.result_directory
Alex Klein4f0eb432019-05-02 13:56:04 -0600200 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700201 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700202
203 # Invalid image path.
204 input_proto.image.path = '/invalid/image/path'
Alex Klein4f0eb432019-05-02 13:56:04 -0600205 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700206 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700207
208 # All valid arguments.
209 input_proto.image.path = self.image_path
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700210 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700211
212 def testTestOutputHandling(self):
213 """Test function output tests."""
214 input_proto = image_pb2.TestImageRequest()
215 input_proto.image.path = self.image_path
216 input_proto.build_target.name = self.board
217 input_proto.result.directory = self.result_directory
218 output_proto = image_pb2.TestImageResult()
219
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700220 self.PatchObject(image_service, 'Test', return_value=True)
221 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700222 self.assertTrue(output_proto.success)
223
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700224 self.PatchObject(image_service, 'Test', return_value=False)
225 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700226 self.assertFalse(output_proto.success)