Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 10 | import mock |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 11 | import os |
| 12 | |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 13 | from chromite.api.gen.chromite.api import image_pb2 |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 14 | from chromite.api.controller import image as image_controller |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 15 | from chromite.lib import constants |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_test_lib |
| 17 | from chromite.lib import osutils |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 18 | from chromite.service import image as image_service |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 19 | |
| 20 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame^] | 21 | class CreateTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 22 | """Create image tests.""" |
| 23 | |
| 24 | def testArgumentValidation(self): |
| 25 | """Test the argument validation.""" |
| 26 | input_proto = image_pb2.CreateImageRequest() |
| 27 | output_proto = image_pb2.CreateImageResult() |
| 28 | |
| 29 | # No board should cause it to fail. |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 30 | with self.assertRaises(image_controller.InvalidArgumentError): |
| 31 | image_controller.Create(input_proto, output_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 32 | |
| 33 | def testImageTypeHandling(self): |
| 34 | """Test the image type handling.""" |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame^] | 35 | # Failed result to avoid the success handling logic. |
| 36 | result = image_service.BuildResult(1, []) |
| 37 | build_patch = self.PatchObject(image_service, 'Build', return_value=result) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 38 | input_proto = image_pb2.CreateImageRequest() |
| 39 | input_proto.build_target.name = 'board' |
| 40 | output_proto = image_pb2.CreateImageResult() |
| 41 | |
| 42 | # Should default to the base image. |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 43 | image_controller.Create(input_proto, output_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 44 | build_patch.assert_called_with(images=[constants.IMAGE_TYPE_BASE], |
| 45 | board=u'board', config=mock.ANY) |
| 46 | |
| 47 | # Should be using a value that's provided. |
| 48 | input_proto.image_types.append(image_pb2.Image.DEV) |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 49 | image_controller.Create(input_proto, output_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 50 | build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV], |
| 51 | board=u'board', config=mock.ANY) |
| 52 | |
| 53 | input_proto.image_types.append(image_pb2.Image.BASE) |
| 54 | input_proto.image_types.append(image_pb2.Image.TEST) |
| 55 | expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_DEV, |
| 56 | constants.IMAGE_TYPE_TEST] |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 57 | image_controller.Create(input_proto, output_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 58 | build_patch.assert_called_with(images=expected_images, board=u'board', |
| 59 | config=mock.ANY) |
| 60 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame^] | 61 | def testFailedPackageHandling(self): |
| 62 | """Test failed packages are populated correctly.""" |
| 63 | result = image_service.BuildResult(1, ['foo/bar', 'cat/pkg']) |
| 64 | expected_packages = [('foo', 'bar'), ('cat', 'pkg')] |
| 65 | self.PatchObject(image_service, 'Build', return_value=result) |
| 66 | |
| 67 | input_proto = image_pb2.CreateImageRequest() |
| 68 | input_proto.build_target.name = 'board' |
| 69 | output_proto = image_pb2.CreateImageResult() |
| 70 | |
| 71 | image_controller.Create(input_proto, output_proto) |
| 72 | for package in output_proto.failed_packages: |
| 73 | self.assertIn((package.category, package.package_name), expected_packages) |
| 74 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 75 | |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 76 | class ImageTest(cros_test_lib.MockTempDirTestCase): |
| 77 | """Image service tests.""" |
| 78 | |
| 79 | def setUp(self): |
| 80 | self.image_path = os.path.join(self.tempdir, 'image.bin') |
| 81 | self.board = 'board' |
| 82 | self.result_directory = os.path.join(self.tempdir, 'results') |
| 83 | |
| 84 | osutils.SafeMakedirs(self.result_directory) |
| 85 | osutils.Touch(self.image_path) |
| 86 | |
| 87 | def testTestArgumentValidation(self): |
| 88 | """Test function argument validation tests.""" |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 89 | self.PatchObject(image_service, 'Test', return_value=True) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 90 | input_proto = image_pb2.TestImageRequest() |
| 91 | output_proto = image_pb2.TestImageResult() |
| 92 | |
| 93 | # Nothing provided. |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 94 | with self.assertRaises(image_controller.InvalidArgumentError): |
| 95 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 96 | |
| 97 | # Just one argument. |
| 98 | input_proto.build_target.name = self.board |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 99 | with self.assertRaises(image_controller.InvalidArgumentError): |
| 100 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 101 | |
| 102 | # Two arguments provided. |
| 103 | input_proto.result.directory = self.result_directory |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 104 | with self.assertRaises(image_controller.InvalidArgumentError): |
| 105 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 106 | |
| 107 | # Invalid image path. |
| 108 | input_proto.image.path = '/invalid/image/path' |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 109 | with self.assertRaises(image_controller.InvalidArgumentError): |
| 110 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 111 | |
| 112 | # All valid arguments. |
| 113 | input_proto.image.path = self.image_path |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 114 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 115 | |
| 116 | def testTestOutputHandling(self): |
| 117 | """Test function output tests.""" |
| 118 | input_proto = image_pb2.TestImageRequest() |
| 119 | input_proto.image.path = self.image_path |
| 120 | input_proto.build_target.name = self.board |
| 121 | input_proto.result.directory = self.result_directory |
| 122 | output_proto = image_pb2.TestImageResult() |
| 123 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 124 | self.PatchObject(image_service, 'Test', return_value=True) |
| 125 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 126 | self.assertTrue(output_proto.success) |
| 127 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 128 | self.PatchObject(image_service, 'Test', return_value=False) |
| 129 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 130 | self.assertFalse(output_proto.success) |