blob: 36d3766b7a731bff2adf866f23864292e41fc0c6 [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
Alex Kleinb7cdbe62019-02-22 11:41:32 -070014from chromite.api.controller import image as image_controller
Alex Klein56355682019-02-07 10:36:54 -070015from chromite.lib import constants
Alex Klein2966e302019-01-17 13:29:38 -070016from chromite.lib import cros_test_lib
17from chromite.lib import osutils
Alex Kleinb7cdbe62019-02-22 11:41:32 -070018from chromite.service import image as image_service
Alex Klein2966e302019-01-17 13:29:38 -070019
20
Alex Klein1bcd9882019-03-19 13:25:24 -060021class CreateTest(cros_test_lib.MockTempDirTestCase):
Alex Klein56355682019-02-07 10:36:54 -070022 """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 Kleinb7cdbe62019-02-22 11:41:32 -070030 with self.assertRaises(image_controller.InvalidArgumentError):
31 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070032
33 def testImageTypeHandling(self):
34 """Test the image type handling."""
Alex Klein1bcd9882019-03-19 13:25:24 -060035 # 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 Klein56355682019-02-07 10:36:54 -070038 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 Kleinb7cdbe62019-02-22 11:41:32 -070043 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070044 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 Kleinb7cdbe62019-02-22 11:41:32 -070049 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070050 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 Kleinb7cdbe62019-02-22 11:41:32 -070057 image_controller.Create(input_proto, output_proto)
Alex Klein56355682019-02-07 10:36:54 -070058 build_patch.assert_called_with(images=expected_images, board=u'board',
59 config=mock.ANY)
60
Alex Klein1bcd9882019-03-19 13:25:24 -060061 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 Kleinb7cdbe62019-02-22 11:41:32 -070075
Alex Klein2966e302019-01-17 13:29:38 -070076class 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 Kleinb7cdbe62019-02-22 11:41:32 -070089 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -070090 input_proto = image_pb2.TestImageRequest()
91 output_proto = image_pb2.TestImageResult()
92
93 # Nothing provided.
Alex Kleinb7cdbe62019-02-22 11:41:32 -070094 with self.assertRaises(image_controller.InvalidArgumentError):
95 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -070096
97 # Just one argument.
98 input_proto.build_target.name = self.board
Alex Kleinb7cdbe62019-02-22 11:41:32 -070099 with self.assertRaises(image_controller.InvalidArgumentError):
100 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700101
102 # Two arguments provided.
103 input_proto.result.directory = self.result_directory
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700104 with self.assertRaises(image_controller.InvalidArgumentError):
105 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700106
107 # Invalid image path.
108 input_proto.image.path = '/invalid/image/path'
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700109 with self.assertRaises(image_controller.InvalidArgumentError):
110 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700111
112 # All valid arguments.
113 input_proto.image.path = self.image_path
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700114 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700115
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 Kleinb7cdbe62019-02-22 11:41:32 -0700124 self.PatchObject(image_service, 'Test', return_value=True)
125 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700126 self.assertTrue(output_proto.success)
127
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700128 self.PatchObject(image_service, 'Test', return_value=False)
129 image_controller.Test(input_proto, output_proto)
Alex Klein2966e302019-01-17 13:29:38 -0700130 self.assertFalse(output_proto.success)