blob: a0d60a4f4e53e32ac52ba1274ec23f54f4d0b545 [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
13from chromite.api.gen import image_pb2
14from chromite.api.service import image as image_service
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
18from chromite.lib.api import image as image_lib
19
20
Alex Klein56355682019-02-07 10:36:54 -070021class 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.
30 with self.assertRaises(image_service.InvalidArgumentError):
31 image_service.Create(input_proto, output_proto)
32
33 def testImageTypeHandling(self):
34 """Test the image type handling."""
35 build_patch = self.PatchObject(image_lib, 'Build', return_value=False)
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.
41 image_service.Create(input_proto, output_proto)
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)
47 image_service.Create(input_proto, output_proto)
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]
55 image_service.Create(input_proto, output_proto)
56 build_patch.assert_called_with(images=expected_images, board=u'board',
57 config=mock.ANY)
58
Alex Klein2966e302019-01-17 13:29:38 -070059class ImageTest(cros_test_lib.MockTempDirTestCase):
60 """Image service tests."""
61
62 def setUp(self):
63 self.image_path = os.path.join(self.tempdir, 'image.bin')
64 self.board = 'board'
65 self.result_directory = os.path.join(self.tempdir, 'results')
66
67 osutils.SafeMakedirs(self.result_directory)
68 osutils.Touch(self.image_path)
69
70 def testTestArgumentValidation(self):
71 """Test function argument validation tests."""
72 self.PatchObject(image_lib, 'Test', return_value=True)
73 input_proto = image_pb2.TestImageRequest()
74 output_proto = image_pb2.TestImageResult()
75
76 # Nothing provided.
Alex Klein56355682019-02-07 10:36:54 -070077 with self.assertRaises(image_service.InvalidArgumentError):
Alex Klein2966e302019-01-17 13:29:38 -070078 image_service.Test(input_proto, output_proto)
79
80 # Just one argument.
81 input_proto.build_target.name = self.board
Alex Klein56355682019-02-07 10:36:54 -070082 with self.assertRaises(image_service.InvalidArgumentError):
Alex Klein2966e302019-01-17 13:29:38 -070083 image_service.Test(input_proto, output_proto)
84
85 # Two arguments provided.
86 input_proto.result.directory = self.result_directory
Alex Klein56355682019-02-07 10:36:54 -070087 with self.assertRaises(image_service.InvalidArgumentError):
Alex Klein2966e302019-01-17 13:29:38 -070088 image_service.Test(input_proto, output_proto)
89
90 # Invalid image path.
91 input_proto.image.path = '/invalid/image/path'
Alex Klein56355682019-02-07 10:36:54 -070092 with self.assertRaises(image_service.InvalidArgumentError):
Alex Klein2966e302019-01-17 13:29:38 -070093 image_service.Test(input_proto, output_proto)
94
95 # All valid arguments.
96 input_proto.image.path = self.image_path
97 image_service.Test(input_proto, output_proto)
98
99 def testTestOutputHandling(self):
100 """Test function output tests."""
101 input_proto = image_pb2.TestImageRequest()
102 input_proto.image.path = self.image_path
103 input_proto.build_target.name = self.board
104 input_proto.result.directory = self.result_directory
105 output_proto = image_pb2.TestImageResult()
106
107 self.PatchObject(image_lib, 'Test', return_value=True)
108 image_service.Test(input_proto, output_proto)
109 self.assertTrue(output_proto.success)
110
111 self.PatchObject(image_lib, 'Test', return_value=False)
112 image_service.Test(input_proto, output_proto)
113 self.assertFalse(output_proto.success)