blob: 01b62034d7f434344df028a9ab51f7a4010df6fa [file] [log] [blame]
Alex Klein19c4cc42019-02-27 14:47:57 -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"""SDK tests."""
7
8from __future__ import print_function
9
Alex Klein231d2da2019-07-22 16:44:45 -060010import mock
Alex Klein19c4cc42019-02-27 14:47:57 -070011
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
Alex Klein19c4cc42019-02-27 14:47:57 -070013from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060014from chromite.api.gen.chromite.api import sdk_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070015from chromite.lib import cros_build_lib
Alex Klein231d2da2019-07-22 16:44:45 -060016from chromite.lib import cros_test_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070017from chromite.service import sdk as sdk_service
18
19
Alex Klein231d2da2019-07-22 16:44:45 -060020class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein19c4cc42019-02-27 14:47:57 -070021 """Create tests."""
22
23 def setUp(self):
24 """Setup method."""
25 # We need to run the command outside the chroot.
26 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -060027 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070028
29 def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False,
Alex Klein00aa8072019-04-15 16:36:00 -060030 cache_path=None, chroot_path=None):
Alex Klein19c4cc42019-02-27 14:47:57 -070031 """Helper to build a create request message."""
32 request = sdk_pb2.CreateRequest()
33 request.flags.no_replace = no_replace
34 request.flags.bootstrap = bootstrap
35 request.flags.no_use_image = no_use_image
36
37 if cache_path:
Alex Klein00aa8072019-04-15 16:36:00 -060038 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070039 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060040 request.chroot.path = chroot_path
Alex Klein19c4cc42019-02-27 14:47:57 -070041
42 return request
43
Alex Klein231d2da2019-07-22 16:44:45 -060044 def testValidateOnly(self):
45 """Sanity check that a validate only call does not execute any logic."""
46 patch = self.PatchObject(sdk_service, 'Create')
47
48 sdk_controller.Create(self._GetRequest(), self.response,
49 self.validate_only_config)
50 patch.assert_not_called()
51
Alex Klein076841b2019-08-29 15:19:39 -060052 def testMockCall(self):
53 """Sanity check that a validate only call does not execute any logic."""
54 patch = self.PatchObject(sdk_service, 'Create')
55
56 rc = sdk_controller.Create(self._GetRequest(), self.response,
57 self.mock_call_config)
58 patch.assert_not_called()
59 self.assertFalse(rc)
60 self.assertTrue(self.response.version.version)
61
Alex Klein19c4cc42019-02-27 14:47:57 -070062 def testSuccess(self):
63 """Test the successful call output handling."""
64 self.PatchObject(sdk_service, 'Create', return_value=1)
65
66 request = self._GetRequest()
Alex Klein19c4cc42019-02-27 14:47:57 -070067
Alex Klein231d2da2019-07-22 16:44:45 -060068 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070069
Alex Klein231d2da2019-07-22 16:44:45 -060070 self.assertEqual(1, self.response.version.version)
Alex Klein19c4cc42019-02-27 14:47:57 -070071
Alex Klein231d2da2019-07-22 16:44:45 -060072 def testFalseArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060073 """Test False argument handling."""
Alex Klein19c4cc42019-02-27 14:47:57 -070074 # Create the patches.
75 self.PatchObject(sdk_service, 'Create', return_value=1)
Alex Klein231d2da2019-07-22 16:44:45 -060076 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070077
78 # Flag translation tests.
79 # Test all false values in the message.
80 request = self._GetRequest(no_replace=False, bootstrap=False,
81 no_use_image=False)
Alex Klein231d2da2019-07-22 16:44:45 -060082 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070083 args_patch.assert_called_with(replace=True, bootstrap=False,
Alex Klein231d2da2019-07-22 16:44:45 -060084 use_image=True, paths=mock.ANY)
85
86 def testTrueArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060087 """Test True arguments handling."""
Alex Klein231d2da2019-07-22 16:44:45 -060088 # Create the patches.
89 self.PatchObject(sdk_service, 'Create', return_value=1)
90 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070091
92 # Test all True values in the message.
93 request = self._GetRequest(no_replace=True, bootstrap=True,
94 no_use_image=True)
Alex Klein231d2da2019-07-22 16:44:45 -060095 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070096 args_patch.assert_called_with(replace=False, bootstrap=True,
Alex Klein231d2da2019-07-22 16:44:45 -060097 use_image=False, paths=mock.ANY)
98
99 def testPathArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -0600100 """Test the path arguments handling."""
Alex Klein231d2da2019-07-22 16:44:45 -0600101 # Create the patches.
102 self.PatchObject(sdk_service, 'Create', return_value=1)
103 paths_patch = self.PatchObject(sdk_service, 'ChrootPaths')
Alex Klein19c4cc42019-02-27 14:47:57 -0700104
105 # Test the path arguments get passed through.
106 cache_dir = '/cache/dir'
Alex Klein19c4cc42019-02-27 14:47:57 -0700107 chroot_path = '/chroot/path'
Alex Klein00aa8072019-04-15 16:36:00 -0600108 request = self._GetRequest(cache_path=cache_dir, chroot_path=chroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -0600109 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein00aa8072019-04-15 16:36:00 -0600110 paths_patch.assert_called_with(cache_dir=cache_dir, chroot_path=chroot_path)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700111
112
Alex Klein231d2da2019-07-22 16:44:45 -0600113class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700114 """Update tests."""
115
116 def setUp(self):
117 """Setup method."""
118 # We need to run the command inside the chroot.
119 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
120
Alex Klein231d2da2019-07-22 16:44:45 -0600121 self.response = sdk_pb2.UpdateResponse()
122
Alex Kleinaa5c4172019-02-27 17:12:20 -0700123 def _GetRequest(self, build_source=False, targets=None):
124 """Helper to simplify building a request instance."""
125 request = sdk_pb2.UpdateRequest()
126 request.flags.build_source = build_source
127
128 for target in targets or []:
129 added = request.toolchain_targets.add()
130 added.name = target
131
132 return request
133
Alex Klein231d2da2019-07-22 16:44:45 -0600134 def testValidateOnly(self):
135 """Sanity check that a validate only call does not execute any logic."""
136 patch = self.PatchObject(sdk_service, 'Update')
137
138 sdk_controller.Update(self._GetRequest(), self.response,
139 self.validate_only_config)
140 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700141
Alex Klein076841b2019-08-29 15:19:39 -0600142 def testMockCall(self):
143 """Sanity check that a validate only call does not execute any logic."""
144 patch = self.PatchObject(sdk_service, 'Update')
145
146 rc = sdk_controller.Create(self._GetRequest(), self.response,
147 self.mock_call_config)
148 patch.assert_not_called()
149 self.assertFalse(rc)
150 self.assertTrue(self.response.version.version)
151
Alex Kleinaa5c4172019-02-27 17:12:20 -0700152 def testSuccess(self):
153 """Successful call output handling test."""
154 expected_version = 1
155 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
156 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700157
Alex Klein231d2da2019-07-22 16:44:45 -0600158 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700159
Alex Klein231d2da2019-07-22 16:44:45 -0600160 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700161
162 def testArgumentHandling(self):
163 """Test the proto argument handling."""
164 args = sdk_service.UpdateArguments()
165 self.PatchObject(sdk_service, 'Update', return_value=1)
166 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
167 return_value=args)
168
Alex Kleinaa5c4172019-02-27 17:12:20 -0700169 # No boards and flags False.
170 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600171 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700172 args_patch.assert_called_with(
173 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700174
175 # Multiple boards and flags True.
176 targets = ['board1', 'board2']
177 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600178 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700179 args_patch.assert_called_with(
180 build_source=True, toolchain_targets=targets, toolchain_changed=False)