blob: f35e87a568ca6f038b50963b391e5fc93451645d [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 Klein7107bdd2019-03-14 17:14:31 -060013from chromite.api.gen.chromite.api import image_pb2
Alex Kleinb7cdbe62019-02-22 11:41:32 -070014from chromite.api.controller import image as image_controller
Alex Klein56355682019-02-07 10:36:54 -070015from chromite.lib import constants
Alex Klein4f0eb432019-05-02 13:56:04 -060016from chromite.lib import cros_build_lib
Alex Klein2966e302019-01-17 13:29:38 -070017from chromite.lib import cros_test_lib
18from chromite.lib import osutils
Alex Kleinb7cdbe62019-02-22 11:41:32 -070019from chromite.service import image as image_service
Alex Klein2966e302019-01-17 13:29:38 -070020
21
Alex Klein1bcd9882019-03-19 13:25:24 -060022class CreateTest(cros_test_lib.MockTempDirTestCase):
Alex Klein56355682019-02-07 10:36:54 -070023 """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 Klein4f0eb432019-05-02 13:56:04 -060031 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -070032 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070033
34 def testImageTypeHandling(self):
35 """Test the image type handling."""
Alex Klein1bcd9882019-03-19 13:25:24 -060036 # 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 Klein56355682019-02-07 10:36:54 -070039 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 Kleinb7cdbe62019-02-22 11:41:32 -070044 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070045 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 Kleinb7cdbe62019-02-22 11:41:32 -070050 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070051 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 Kleinb7cdbe62019-02-22 11:41:32 -070058 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070059 build_patch.assert_called_with(images=expected_images, board=u'board',
60 config=mock.ANY)
61
Alex Klein1bcd9882019-03-19 13:25:24 -060062 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 Kleinb7cdbe62019-02-22 11:41:32 -070076
Alex Klein4f0eb432019-05-02 13:56:04 -060077class 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 Klein2966e302019-01-17 13:29:38 -0700139class 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 Kleinb7cdbe62019-02-22 11:41:32 -0700152 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -0700153 input_proto = image_pb2.TestImageRequest()
154 output_proto = image_pb2.TestImageResult()
155
156 # Nothing provided.
Alex Klein4f0eb432019-05-02 13:56:04 -0600157 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700158 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700159
160 # Just one argument.
161 input_proto.build_target.name = self.board
Alex Klein4f0eb432019-05-02 13:56:04 -0600162 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700163 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700164
165 # Two arguments provided.
166 input_proto.result.directory = self.result_directory
Alex Klein4f0eb432019-05-02 13:56:04 -0600167 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700168 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700169
170 # Invalid image path.
171 input_proto.image.path = '/invalid/image/path'
Alex Klein4f0eb432019-05-02 13:56:04 -0600172 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700173 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700174
175 # All valid arguments.
176 input_proto.image.path = self.image_path
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700177 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700178
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 Kleinb7cdbe62019-02-22 11:41:32 -0700187 self.PatchObject(image_service, 'Test', return_value=True)
188 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700189 self.assertTrue(output_proto.success)
190
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700191 self.PatchObject(image_service, 'Test', return_value=False)
192 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700193 self.assertFalse(output_proto.success)