blob: f26abb1d9dc7561d4524aeb638f0b65d86507415 [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
David Burger13e06be2019-05-13 20:33:16 -060014from chromite.api.gen.chromiumos import common_pb2
Alex Kleinb7cdbe62019-02-22 11:41:32 -070015from chromite.api.controller import image as image_controller
Alex Klein56355682019-02-07 10:36:54 -070016from chromite.lib import constants
Alex Klein4f0eb432019-05-02 13:56:04 -060017from chromite.lib import cros_build_lib
Alex Klein2966e302019-01-17 13:29:38 -070018from chromite.lib import cros_test_lib
19from chromite.lib import osutils
Alex Kleinb7cdbe62019-02-22 11:41:32 -070020from chromite.service import image as image_service
Alex Klein2966e302019-01-17 13:29:38 -070021
22
Alex Klein1bcd9882019-03-19 13:25:24 -060023class CreateTest(cros_test_lib.MockTempDirTestCase):
Alex Klein56355682019-02-07 10:36:54 -070024 """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 Klein4f0eb432019-05-02 13:56:04 -060032 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -070033 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070034
35 def testImageTypeHandling(self):
36 """Test the image type handling."""
Alex Klein1bcd9882019-03-19 13:25:24 -060037 # 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 Klein56355682019-02-07 10:36:54 -070040 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 Kleinb7cdbe62019-02-22 11:41:32 -070045 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070046 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 Burger13e06be2019-05-13 20:33:16 -060050 input_proto.image_types.append(common_pb2.DEV)
Alex Kleinb7cdbe62019-02-22 11:41:32 -070051 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070052 build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV],
53 board=u'board', config=mock.ANY)
54
David Burger13e06be2019-05-13 20:33:16 -060055 input_proto.image_types.append(common_pb2.BASE)
56 input_proto.image_types.append(common_pb2.TEST)
Alex Klein56355682019-02-07 10:36:54 -070057 expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_DEV,
58 constants.IMAGE_TYPE_TEST]
Alex Kleinb7cdbe62019-02-22 11:41:32 -070059 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070060 build_patch.assert_called_with(images=expected_images, board=u'board',
61 config=mock.ANY)
62
Alex Klein1bcd9882019-03-19 13:25:24 -060063 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 Kleinb7cdbe62019-02-22 11:41:32 -070077
Alex Klein4f0eb432019-05-02 13:56:04 -060078class 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 Burger13e06be2019-05-13 20:33:16 -0600102 request = self._GetInput(image_type=common_pb2.TEST)
Alex Klein4f0eb432019-05-02 13:56:04 -0600103 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 Burger13e06be2019-05-13 20:33:16 -0600118 request = self._GetInput(board='board', image_type=common_pb2.TEST)
Alex Klein4f0eb432019-05-02 13:56:04 -0600119 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 Burger13e06be2019-05-13 20:33:16 -0600129 request = self._GetInput(board='board', image_type=common_pb2.BASE)
Alex Klein4f0eb432019-05-02 13:56:04 -0600130 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 Klein2966e302019-01-17 13:29:38 -0700140class 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 Kleinb7cdbe62019-02-22 11:41:32 -0700153 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -0700154 input_proto = image_pb2.TestImageRequest()
155 output_proto = image_pb2.TestImageResult()
156
157 # Nothing provided.
Alex Klein4f0eb432019-05-02 13:56:04 -0600158 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700159 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700160
161 # Just one argument.
162 input_proto.build_target.name = self.board
Alex Klein4f0eb432019-05-02 13:56:04 -0600163 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700164 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700165
166 # Two arguments provided.
167 input_proto.result.directory = self.result_directory
Alex Klein4f0eb432019-05-02 13:56:04 -0600168 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700169 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700170
171 # Invalid image path.
172 input_proto.image.path = '/invalid/image/path'
Alex Klein4f0eb432019-05-02 13:56:04 -0600173 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700174 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700175
176 # All valid arguments.
177 input_proto.image.path = self.image_path
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700178 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700179
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 Kleinb7cdbe62019-02-22 11:41:32 -0700188 self.PatchObject(image_service, 'Test', return_value=True)
189 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700190 self.assertTrue(output_proto.success)
191
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700192 self.PatchObject(image_service, 'Test', return_value=False)
193 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700194 self.assertFalse(output_proto.success)