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 | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 21 | class CreateTest(cros_test_lib.MockTestCase): |
| 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 | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 35 | build_patch = self.PatchObject(image_service, 'Build', return_value=False) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 36 | input_proto = image_pb2.CreateImageRequest() |
| 37 | input_proto.build_target.name = 'board' |
| 38 | output_proto = image_pb2.CreateImageResult() |
| 39 | |
| 40 | # Should default to the base image. |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 41 | image_controller.Create(input_proto, output_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 42 | build_patch.assert_called_with(images=[constants.IMAGE_TYPE_BASE], |
| 43 | board=u'board', config=mock.ANY) |
| 44 | |
| 45 | # Should be using a value that's provided. |
| 46 | input_proto.image_types.append(image_pb2.Image.DEV) |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 47 | image_controller.Create(input_proto, output_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 48 | build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV], |
| 49 | board=u'board', config=mock.ANY) |
| 50 | |
| 51 | input_proto.image_types.append(image_pb2.Image.BASE) |
| 52 | input_proto.image_types.append(image_pb2.Image.TEST) |
| 53 | expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_DEV, |
| 54 | constants.IMAGE_TYPE_TEST] |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 55 | image_controller.Create(input_proto, output_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 56 | build_patch.assert_called_with(images=expected_images, board=u'board', |
| 57 | config=mock.ANY) |
| 58 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 59 | |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 60 | class ImageTest(cros_test_lib.MockTempDirTestCase): |
| 61 | """Image service tests.""" |
| 62 | |
| 63 | def setUp(self): |
| 64 | self.image_path = os.path.join(self.tempdir, 'image.bin') |
| 65 | self.board = 'board' |
| 66 | self.result_directory = os.path.join(self.tempdir, 'results') |
| 67 | |
| 68 | osutils.SafeMakedirs(self.result_directory) |
| 69 | osutils.Touch(self.image_path) |
| 70 | |
| 71 | def testTestArgumentValidation(self): |
| 72 | """Test function argument validation tests.""" |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 73 | self.PatchObject(image_service, 'Test', return_value=True) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 74 | input_proto = image_pb2.TestImageRequest() |
| 75 | output_proto = image_pb2.TestImageResult() |
| 76 | |
| 77 | # Nothing provided. |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 78 | with self.assertRaises(image_controller.InvalidArgumentError): |
| 79 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 80 | |
| 81 | # Just one argument. |
| 82 | input_proto.build_target.name = self.board |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 83 | with self.assertRaises(image_controller.InvalidArgumentError): |
| 84 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 85 | |
| 86 | # Two arguments provided. |
| 87 | input_proto.result.directory = self.result_directory |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 88 | with self.assertRaises(image_controller.InvalidArgumentError): |
| 89 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 90 | |
| 91 | # Invalid image path. |
| 92 | input_proto.image.path = '/invalid/image/path' |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 93 | with self.assertRaises(image_controller.InvalidArgumentError): |
| 94 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 95 | |
| 96 | # All valid arguments. |
| 97 | input_proto.image.path = self.image_path |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 98 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 99 | |
| 100 | def testTestOutputHandling(self): |
| 101 | """Test function output tests.""" |
| 102 | input_proto = image_pb2.TestImageRequest() |
| 103 | input_proto.image.path = self.image_path |
| 104 | input_proto.build_target.name = self.board |
| 105 | input_proto.result.directory = self.result_directory |
| 106 | output_proto = image_pb2.TestImageResult() |
| 107 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 108 | self.PatchObject(image_service, 'Test', return_value=True) |
| 109 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 110 | self.assertTrue(output_proto.success) |
| 111 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 112 | self.PatchObject(image_service, 'Test', return_value=False) |
| 113 | image_controller.Test(input_proto, output_proto) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 114 | self.assertFalse(output_proto.success) |