blob: 8da0dccd267197e6c06927657d0f6ae927eb81c3 [file] [log] [blame]
LaMont Jones8a1644f2019-04-16 14:30:17 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unittests for Android operations."""
6
Mike Frysinger166fea02021-02-12 05:30:33 -05007from unittest import mock
8
Alex Klein231d2da2019-07-22 16:44:45 -06009from chromite.api import api_config
LaMont Jones8a1644f2019-04-16 14:30:17 -060010from chromite.api.controller import android
11from chromite.api.gen.chromite.api import android_pb2
12from chromite.api.gen.chromiumos import common_pb2
Mike Frysinger40ffb532021-02-12 07:36:08 -050013from chromite.lib import build_target_lib
LaMont Jones8a1644f2019-04-16 14:30:17 -060014from chromite.lib import constants
15from chromite.lib import cros_build_lib
16from chromite.lib import cros_test_lib
17from chromite.lib import osutils
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090018from chromite.service import android as service_android
Alex Klein4de25e82019-08-05 15:58:39 -060019from chromite.service import packages
20
LaMont Jones8a1644f2019-04-16 14:30:17 -060021
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090022class GetLatestBuildTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
23 """Unittests for GetLatestBuild."""
24
25 def setUp(self):
26 self._mock = self.PatchObject(service_android, 'GetLatestBuild')
27 self._mock.return_value = ('7123456', {})
Shao-Chuan Lee6e8784a2021-05-13 09:34:46 +090028 self._mock_branch_for_package = self.PatchObject(
29 service_android, 'GetAndroidBranchForPackage',
30 return_value='android-branch-for-package')
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090031 self._output_proto = android_pb2.GetLatestBuildResponse()
32
Shao-Chuan Lee6e8784a2021-05-13 09:34:46 +090033 def _GetRequest(self, android_build_branch=None, android_package=None):
34 req = android_pb2.GetLatestBuildRequest()
35 if android_build_branch is not None:
36 req.android_build_branch = android_build_branch
37 if android_package is not None:
38 req.android_package = android_package
39 return req
40
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090041 def testValidateOnly(self):
42 """Test that a validate only call does not execute any logic."""
Shao-Chuan Lee6e8784a2021-05-13 09:34:46 +090043 req = self._GetRequest(android_package='android-package')
44 android.GetLatestBuild(req, self._output_proto, self.validate_only_config)
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090045 self._mock.assert_not_called()
46
47 def testMockCall(self):
48 """Test that a mock call does not execute logic, returns mocked value."""
Shao-Chuan Lee6e8784a2021-05-13 09:34:46 +090049 req = self._GetRequest(android_package='android-package')
50 android.GetLatestBuild(req, self._output_proto, self.mock_call_config)
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090051 self._mock.assert_not_called()
52 self.assertEqual(self._output_proto.android_version, '7123456')
53
Shao-Chuan Lee6e8784a2021-05-13 09:34:46 +090054 def testFailsIfBranchAndPackageMissing(self):
55 """Fails if both android_build_branch and android_package are missing."""
56 req = self._GetRequest()
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090057 with self.assertRaises(cros_build_lib.DieSystemExit):
Shao-Chuan Lee6e8784a2021-05-13 09:34:46 +090058 android.GetLatestBuild(req, self._output_proto, self.api_config)
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090059 self._mock.assert_not_called()
60
Shao-Chuan Lee6e8784a2021-05-13 09:34:46 +090061 def testBranchSpecified(self):
62 """Test calling with Android branch specified."""
63 req = self._GetRequest(android_build_branch='android-branch')
64 android.GetLatestBuild(req, self._output_proto, self.api_config)
65 self._mock.assert_called_once_with('android-branch')
66 self._mock_branch_for_package.assert_not_called()
67 self.assertEqual(self._output_proto.android_version, '7123456')
68
69 def testPackageSpecified(self):
70 """Test calling with Android package specified."""
71 req = self._GetRequest(android_package='android-package')
72 android.GetLatestBuild(req, self._output_proto, self.api_config)
73 self._mock.assert_called_once_with('android-branch-for-package')
74 self._mock_branch_for_package.assert_called_once_with('android-package')
75 self.assertEqual(self._output_proto.android_version, '7123456')
76
77 def testBranchAndPackageSpecified(self):
78 """Test calling with both Android branch and package specified."""
79 req = self._GetRequest(android_build_branch='android-branch',
80 android_package='android-package')
81 android.GetLatestBuild(req, self._output_proto, self.api_config)
82 self._mock.assert_called_once_with('android-branch')
83 self._mock_branch_for_package.assert_not_called()
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090084 self.assertEqual(self._output_proto.android_version, '7123456')
85
86
Alex Klein231d2da2019-07-22 16:44:45 -060087class MarkStableTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -060088 """Unittests for MarkStable."""
89
90 def setUp(self):
Alex Klein4de25e82019-08-05 15:58:39 -060091 self.uprev = self.PatchObject(packages, 'uprev_android')
LaMont Jones8a1644f2019-04-16 14:30:17 -060092
93 self.input_proto = android_pb2.MarkStableRequest()
LaMont Jones8a1644f2019-04-16 14:30:17 -060094 self.input_proto.package_name = 'android-package-name'
95 self.input_proto.android_build_branch = 'android_build_branch'
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090096 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -060097 self.input_proto.build_targets.add().name = 'foo'
98 self.input_proto.build_targets.add().name = 'bar'
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +090099 self.input_proto.skip_commit = True
Alex Klein4de25e82019-08-05 15:58:39 -0600100
Alex Klein26e472b2020-03-10 14:35:01 -0600101 self.build_targets = [build_target_lib.BuildTarget('foo'),
102 build_target_lib.BuildTarget('bar')]
LaMont Jones8a1644f2019-04-16 14:30:17 -0600103
Alex Klein231d2da2019-07-22 16:44:45 -0600104 self.response = android_pb2.MarkStableResponse()
105
106 def testValidateOnly(self):
107 """Sanity check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600108 android.MarkStable(self.input_proto, self.response,
109 self.validate_only_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600110 self.uprev.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600111
Michael Mortensen25626442019-11-22 10:06:59 -0700112 def testMockCall(self):
113 """Test that a mock call does not execute logic, returns mocked value."""
114 android.MarkStable(self.input_proto, self.response,
115 self.mock_call_config)
116 self.uprev.assert_not_called()
117 self.assertEqual(self.response.status,
118 android_pb2.MARK_STABLE_STATUS_SUCCESS)
119 self.assertEqual(self.response.android_atom.category, 'category')
120 self.assertEqual(self.response.android_atom.package_name,
121 'android-package-name')
122 self.assertEqual(self.response.android_atom.version, '1.2')
123
LaMont Jones8a1644f2019-04-16 14:30:17 -0600124 def testFailsIfPackageNameMissing(self):
125 """Fails if package_name is missing."""
126 self.input_proto.package_name = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -0600127 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600128 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600129 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -0600130
LaMont Jones8a1644f2019-04-16 14:30:17 -0600131 def testCallsCommandCorrectly(self):
132 """Test that commands.MarkAndroidAsStable is called correctly."""
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900133 self.uprev.return_value = packages.UprevAndroidResult(
134 revved=True,
135 android_atom='cat/android-1.2.3')
LaMont Jones8a1644f2019-04-16 14:30:17 -0600136 atom = common_pb2.PackageInfo()
137 atom.category = 'cat'
138 atom.package_name = 'android'
139 atom.version = '1.2.3'
Alex Klein231d2da2019-07-22 16:44:45 -0600140 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600141 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600142 android_package=self.input_proto.package_name,
Alex Klein4de25e82019-08-05 15:58:39 -0600143 chroot=mock.ANY,
144 build_targets=self.build_targets,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900145 android_build_branch=self.input_proto.android_build_branch,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900146 android_version=self.input_proto.android_version,
147 skip_commit=self.input_proto.skip_commit,
148 )
Alex Klein231d2da2019-07-22 16:44:45 -0600149 self.assertEqual(self.response.android_atom, atom)
150 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600151 android_pb2.MARK_STABLE_STATUS_SUCCESS)
152
153 def testHandlesEarlyExit(self):
154 """Test that early exit is handled correctly."""
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900155 self.uprev.return_value = packages.UprevAndroidResult(revved=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600156 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600157 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600158 android_package=self.input_proto.package_name,
Alex Klein4de25e82019-08-05 15:58:39 -0600159 chroot=mock.ANY,
160 build_targets=self.build_targets,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900161 android_build_branch=self.input_proto.android_build_branch,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900162 android_version=self.input_proto.android_version,
163 skip_commit=self.input_proto.skip_commit,
164 )
Alex Klein231d2da2019-07-22 16:44:45 -0600165 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600166 android_pb2.MARK_STABLE_STATUS_EARLY_EXIT)
167
168 def testHandlesPinnedUprevError(self):
169 """Test that pinned error is handled correctly."""
Alex Klein4de25e82019-08-05 15:58:39 -0600170 self.uprev.side_effect = packages.AndroidIsPinnedUprevError('pin/xx-1.1')
LaMont Jones8a1644f2019-04-16 14:30:17 -0600171 atom = common_pb2.PackageInfo()
172 atom.category = 'pin'
173 atom.package_name = 'xx'
174 atom.version = '1.1'
Alex Klein231d2da2019-07-22 16:44:45 -0600175 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600176 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600177 android_package=self.input_proto.package_name,
Alex Klein4de25e82019-08-05 15:58:39 -0600178 chroot=mock.ANY,
179 build_targets=self.build_targets,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900180 android_build_branch=self.input_proto.android_build_branch,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900181 android_version=self.input_proto.android_version,
182 skip_commit=self.input_proto.skip_commit,
183 )
Alex Klein231d2da2019-07-22 16:44:45 -0600184 self.assertEqual(self.response.android_atom, atom)
185 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600186 android_pb2.MARK_STABLE_STATUS_PINNED)
187
188
Alex Klein231d2da2019-07-22 16:44:45 -0600189class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -0600190 """Unittests for UnpinVersion."""
191
192 def testCallsUnlink(self):
193 """SetAndroid calls service with correct args."""
194 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
195 self.PatchObject(constants, '_FindSourceRoot', return_value='SRCROOT')
196
197 # This has the side effect of making sure that input and output proto are
198 # not actually used.
Alex Klein231d2da2019-07-22 16:44:45 -0600199 android.UnpinVersion(None, None, self.api_config)
LaMont Jones8a1644f2019-04-16 14:30:17 -0600200 safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH)
Alex Klein231d2da2019-07-22 16:44:45 -0600201
202 def testValidateOnly(self):
203 """Sanity check that a validate only call does not execute any logic."""
204 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
205
206 android.UnpinVersion(None, None, self.validate_only_config)
207 safeunlink.assert_not_called()
Michael Mortensen25626442019-11-22 10:06:59 -0700208
209 def testMockCall(self):
210 """Test that a mock call does not execute logic."""
211 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
212
213 android.UnpinVersion(None, None, self.mock_call_config)
214 safeunlink.assert_not_called()
215 # android.UnpinVersion does not modify the response.