blob: a0f0df97a5c5a69d66acbec7d69b645e0683391a [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 Klein2966e302019-01-17 13:29:38 -0700110class ImageTest(cros_test_lib.MockTempDirTestCase):
111 """Image service tests."""
112
113 def setUp(self):
114 self.image_path = os.path.join(self.tempdir, 'image.bin')
115 self.board = 'board'
116 self.result_directory = os.path.join(self.tempdir, 'results')
117
118 osutils.SafeMakedirs(self.result_directory)
119 osutils.Touch(self.image_path)
120
121 def testTestArgumentValidation(self):
122 """Test function argument validation tests."""
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700123 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -0700124 input_proto = image_pb2.TestImageRequest()
125 output_proto = image_pb2.TestImageResult()
126
127 # Nothing provided.
Alex Klein4f0eb432019-05-02 13:56:04 -0600128 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700129 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700130
131 # Just one argument.
132 input_proto.build_target.name = self.board
Alex Klein4f0eb432019-05-02 13:56:04 -0600133 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700134 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700135
136 # Two arguments provided.
137 input_proto.result.directory = self.result_directory
Alex Klein4f0eb432019-05-02 13:56:04 -0600138 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700139 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700140
141 # Invalid image path.
142 input_proto.image.path = '/invalid/image/path'
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 # All valid arguments.
147 input_proto.image.path = self.image_path
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700148 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700149
150 def testTestOutputHandling(self):
151 """Test function output tests."""
152 input_proto = image_pb2.TestImageRequest()
153 input_proto.image.path = self.image_path
154 input_proto.build_target.name = self.board
155 input_proto.result.directory = self.result_directory
156 output_proto = image_pb2.TestImageResult()
157
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700158 self.PatchObject(image_service, 'Test', return_value=True)
159 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700160 self.assertTrue(output_proto.success)
161
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700162 self.PatchObject(image_service, 'Test', return_value=False)
163 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700164 self.assertFalse(output_proto.success)