blob: dff358982936523f1f450659e390d0a1d7a904b6 [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
Alex Kleinaa5c4172019-02-27 17:12:20 -070099
Alex Klein231d2da2019-07-22 16:44:45 -0600100class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700101 """Update tests."""
102
103 def setUp(self):
104 """Setup method."""
105 # We need to run the command inside the chroot.
106 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
107
Alex Klein231d2da2019-07-22 16:44:45 -0600108 self.response = sdk_pb2.UpdateResponse()
109
Alex Kleinaa5c4172019-02-27 17:12:20 -0700110 def _GetRequest(self, build_source=False, targets=None):
111 """Helper to simplify building a request instance."""
112 request = sdk_pb2.UpdateRequest()
113 request.flags.build_source = build_source
114
115 for target in targets or []:
116 added = request.toolchain_targets.add()
117 added.name = target
118
119 return request
120
Alex Klein231d2da2019-07-22 16:44:45 -0600121 def testValidateOnly(self):
122 """Sanity check that a validate only call does not execute any logic."""
123 patch = self.PatchObject(sdk_service, 'Update')
124
125 sdk_controller.Update(self._GetRequest(), self.response,
126 self.validate_only_config)
127 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700128
Alex Klein076841b2019-08-29 15:19:39 -0600129 def testMockCall(self):
130 """Sanity check that a validate only call does not execute any logic."""
131 patch = self.PatchObject(sdk_service, 'Update')
132
133 rc = sdk_controller.Create(self._GetRequest(), self.response,
134 self.mock_call_config)
135 patch.assert_not_called()
136 self.assertFalse(rc)
137 self.assertTrue(self.response.version.version)
138
Alex Kleinaa5c4172019-02-27 17:12:20 -0700139 def testSuccess(self):
140 """Successful call output handling test."""
141 expected_version = 1
142 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
143 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700144
Alex Klein231d2da2019-07-22 16:44:45 -0600145 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700146
Alex Klein231d2da2019-07-22 16:44:45 -0600147 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700148
149 def testArgumentHandling(self):
150 """Test the proto argument handling."""
151 args = sdk_service.UpdateArguments()
152 self.PatchObject(sdk_service, 'Update', return_value=1)
153 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
154 return_value=args)
155
Alex Kleinaa5c4172019-02-27 17:12:20 -0700156 # No boards and flags False.
157 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600158 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700159 args_patch.assert_called_with(
160 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700161
162 # Multiple boards and flags True.
163 targets = ['board1', 'board2']
164 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600165 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700166 args_patch.assert_called_with(
167 build_source=True, toolchain_targets=targets, toolchain_changed=False)