blob: a52ebb7436dc8b4f9d682dec874738833a1f6ada [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 Klein2557b4f2019-07-11 14:34:00 -0600109 def testNoPackagesFailureHandling(self):
110 """Test failed packages are populated correctly."""
111 result = image_service.BuildResult(1, [])
112 self.PatchObject(image_service, 'Build', return_value=result)
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700113
Alex Klein2557b4f2019-07-11 14:34:00 -0600114 input_proto = image_pb2.CreateImageRequest()
115 input_proto.build_target.name = 'board'
116 output_proto = image_pb2.CreateImageResult()
117
118 rc = image_controller.Create(input_proto, output_proto)
119 self.assertTrue(rc)
120 self.assertNotEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
121 rc)
122 self.assertFalse(output_proto.failed_packages)
123
124
125class ImageTestTest(cros_test_lib.MockTempDirTestCase):
126 """Image test tests."""
Alex Klein2966e302019-01-17 13:29:38 -0700127
128 def setUp(self):
129 self.image_path = os.path.join(self.tempdir, 'image.bin')
130 self.board = 'board'
131 self.result_directory = os.path.join(self.tempdir, 'results')
132
133 osutils.SafeMakedirs(self.result_directory)
134 osutils.Touch(self.image_path)
135
136 def testTestArgumentValidation(self):
137 """Test function argument validation tests."""
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700138 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -0700139 input_proto = image_pb2.TestImageRequest()
140 output_proto = image_pb2.TestImageResult()
141
142 # Nothing provided.
Alex Klein4f0eb432019-05-02 13:56:04 -0600143 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700144 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700145
146 # Just one argument.
147 input_proto.build_target.name = self.board
Alex Klein4f0eb432019-05-02 13:56:04 -0600148 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700149 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700150
151 # Two arguments provided.
152 input_proto.result.directory = self.result_directory
Alex Klein4f0eb432019-05-02 13:56:04 -0600153 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700154 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700155
156 # Invalid image path.
157 input_proto.image.path = '/invalid/image/path'
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 # All valid arguments.
162 input_proto.image.path = self.image_path
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700163 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700164
165 def testTestOutputHandling(self):
166 """Test function output tests."""
167 input_proto = image_pb2.TestImageRequest()
168 input_proto.image.path = self.image_path
169 input_proto.build_target.name = self.board
170 input_proto.result.directory = self.result_directory
171 output_proto = image_pb2.TestImageResult()
172
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700173 self.PatchObject(image_service, 'Test', return_value=True)
174 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700175 self.assertTrue(output_proto.success)
176
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700177 self.PatchObject(image_service, 'Test', return_value=False)
178 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700179 self.assertFalse(output_proto.success)