blob: 9c248bbf3054ee2de6747497248a50bb3273fb97 [file] [log] [blame]
LaMont Jones8a1644f2019-04-16 14:30:17 -06001# -*- 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"""Unittests for Android operations."""
7
8from __future__ import print_function
9
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import api_config
LaMont Jones8a1644f2019-04-16 14:30:17 -060011from chromite.api.controller import android
12from chromite.api.gen.chromite.api import android_pb2
13from chromite.api.gen.chromiumos import common_pb2
Mike Frysinger40ffb532021-02-12 07:36:08 -050014from chromite.lib import build_target_lib
LaMont Jones8a1644f2019-04-16 14:30:17 -060015from chromite.lib import constants
16from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
18from chromite.lib import osutils
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090019from chromite.service import android as service_android
Alex Klein4de25e82019-08-05 15:58:39 -060020from chromite.service import packages
Mike Frysinger40ffb532021-02-12 07:36:08 -050021from chromite.third_party import mock
Alex Klein4de25e82019-08-05 15:58:39 -060022
LaMont Jones8a1644f2019-04-16 14:30:17 -060023
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090024class GetLatestBuildTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
25 """Unittests for GetLatestBuild."""
26
27 def setUp(self):
28 self._mock = self.PatchObject(service_android, 'GetLatestBuild')
29 self._mock.return_value = ('7123456', {})
30 self._input_proto = android_pb2.GetLatestBuildRequest()
31 self._input_proto.android_build_branch = 'git_rvc-arc'
32 self._output_proto = android_pb2.GetLatestBuildResponse()
33
34 def testValidateOnly(self):
35 """Test that a validate only call does not execute any logic."""
36 android.GetLatestBuild(self._input_proto, self._output_proto,
37 self.validate_only_config)
38 self._mock.assert_not_called()
39
40 def testMockCall(self):
41 """Test that a mock call does not execute logic, returns mocked value."""
42 android.GetLatestBuild(self._input_proto, self._output_proto,
43 self.mock_call_config)
44 self._mock.assert_not_called()
45 self.assertEqual(self._output_proto.android_version, '7123456')
46
47 def testFailsIfAndroidBuildBranchMissing(self):
48 """Fails if package_name is missing."""
49 self._input_proto.android_build_branch = ''
50 with self.assertRaises(cros_build_lib.DieSystemExit):
51 android.GetLatestBuild(self._input_proto, self._output_proto,
52 self.api_config)
53 self._mock.assert_not_called()
54
55 def testActualCall(self):
56 """Test that the underlying method is being called in the usual case."""
57 android.GetLatestBuild(self._input_proto, self._output_proto,
58 self.api_config)
59 self._mock.assert_called_once_with('git_rvc-arc')
60 self.assertEqual(self._output_proto.android_version, '7123456')
61
62
Alex Klein231d2da2019-07-22 16:44:45 -060063class MarkStableTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -060064 """Unittests for MarkStable."""
65
66 def setUp(self):
Alex Klein4de25e82019-08-05 15:58:39 -060067 self.uprev = self.PatchObject(packages, 'uprev_android')
LaMont Jones8a1644f2019-04-16 14:30:17 -060068
69 self.input_proto = android_pb2.MarkStableRequest()
LaMont Jones8a1644f2019-04-16 14:30:17 -060070 self.input_proto.package_name = 'android-package-name'
71 self.input_proto.android_build_branch = 'android_build_branch'
Alex Klein4de25e82019-08-05 15:58:39 -060072 self.input_proto.build_targets.add().name = 'foo'
73 self.input_proto.build_targets.add().name = 'bar'
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +090074 self.input_proto.skip_commit = True
Alex Klein4de25e82019-08-05 15:58:39 -060075
Alex Klein26e472b2020-03-10 14:35:01 -060076 self.build_targets = [build_target_lib.BuildTarget('foo'),
77 build_target_lib.BuildTarget('bar')]
LaMont Jones8a1644f2019-04-16 14:30:17 -060078
Alex Klein231d2da2019-07-22 16:44:45 -060079 self.response = android_pb2.MarkStableResponse()
80
81 def testValidateOnly(self):
82 """Sanity check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -060083 android.MarkStable(self.input_proto, self.response,
84 self.validate_only_config)
Alex Klein4de25e82019-08-05 15:58:39 -060085 self.uprev.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -060086
Michael Mortensen25626442019-11-22 10:06:59 -070087 def testMockCall(self):
88 """Test that a mock call does not execute logic, returns mocked value."""
89 android.MarkStable(self.input_proto, self.response,
90 self.mock_call_config)
91 self.uprev.assert_not_called()
92 self.assertEqual(self.response.status,
93 android_pb2.MARK_STABLE_STATUS_SUCCESS)
94 self.assertEqual(self.response.android_atom.category, 'category')
95 self.assertEqual(self.response.android_atom.package_name,
96 'android-package-name')
97 self.assertEqual(self.response.android_atom.version, '1.2')
98
LaMont Jones8a1644f2019-04-16 14:30:17 -060099 def testFailsIfPackageNameMissing(self):
100 """Fails if package_name is missing."""
101 self.input_proto.package_name = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -0600102 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600103 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600104 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -0600105
106 def testFailsIfAndroidBuildBranchMissing(self):
107 """Fails if android_build_branch is missing."""
108 self.input_proto.android_build_branch = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -0600109 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600110 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600111 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -0600112
113 def testCallsCommandCorrectly(self):
114 """Test that commands.MarkAndroidAsStable is called correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -0600115 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600116 self.uprev.return_value = 'cat/android-1.2.3'
LaMont Jones8a1644f2019-04-16 14:30:17 -0600117 atom = common_pb2.PackageInfo()
118 atom.category = 'cat'
119 atom.package_name = 'android'
120 atom.version = '1.2.3'
Alex Klein231d2da2019-07-22 16:44:45 -0600121 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600122 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600123 android_package=self.input_proto.package_name,
124 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600125 chroot=mock.ANY,
126 build_targets=self.build_targets,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900127 android_version=self.input_proto.android_version,
128 skip_commit=self.input_proto.skip_commit,
129 )
Alex Klein231d2da2019-07-22 16:44:45 -0600130 self.assertEqual(self.response.android_atom, atom)
131 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600132 android_pb2.MARK_STABLE_STATUS_SUCCESS)
133
134 def testHandlesEarlyExit(self):
135 """Test that early exit is handled correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -0600136 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600137 self.uprev.return_value = ''
Alex Klein231d2da2019-07-22 16:44:45 -0600138 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600139 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600140 android_package=self.input_proto.package_name,
141 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600142 chroot=mock.ANY,
143 build_targets=self.build_targets,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900144 android_version=self.input_proto.android_version,
145 skip_commit=self.input_proto.skip_commit,
146 )
Alex Klein231d2da2019-07-22 16:44:45 -0600147 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600148 android_pb2.MARK_STABLE_STATUS_EARLY_EXIT)
149
150 def testHandlesPinnedUprevError(self):
151 """Test that pinned error is handled correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -0600152 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600153 self.uprev.side_effect = packages.AndroidIsPinnedUprevError('pin/xx-1.1')
LaMont Jones8a1644f2019-04-16 14:30:17 -0600154 atom = common_pb2.PackageInfo()
155 atom.category = 'pin'
156 atom.package_name = 'xx'
157 atom.version = '1.1'
Alex Klein231d2da2019-07-22 16:44:45 -0600158 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600159 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600160 android_package=self.input_proto.package_name,
161 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600162 chroot=mock.ANY,
163 build_targets=self.build_targets,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900164 android_version=self.input_proto.android_version,
165 skip_commit=self.input_proto.skip_commit,
166 )
Alex Klein231d2da2019-07-22 16:44:45 -0600167 self.assertEqual(self.response.android_atom, atom)
168 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600169 android_pb2.MARK_STABLE_STATUS_PINNED)
170
171
Alex Klein231d2da2019-07-22 16:44:45 -0600172class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -0600173 """Unittests for UnpinVersion."""
174
175 def testCallsUnlink(self):
176 """SetAndroid calls service with correct args."""
177 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
178 self.PatchObject(constants, '_FindSourceRoot', return_value='SRCROOT')
179
180 # This has the side effect of making sure that input and output proto are
181 # not actually used.
Alex Klein231d2da2019-07-22 16:44:45 -0600182 android.UnpinVersion(None, None, self.api_config)
LaMont Jones8a1644f2019-04-16 14:30:17 -0600183 safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH)
Alex Klein231d2da2019-07-22 16:44:45 -0600184
185 def testValidateOnly(self):
186 """Sanity check that a validate only call does not execute any logic."""
187 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
188
189 android.UnpinVersion(None, None, self.validate_only_config)
190 safeunlink.assert_not_called()
Michael Mortensen25626442019-11-22 10:06:59 -0700191
192 def testMockCall(self):
193 """Test that a mock call does not execute logic."""
194 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
195
196 android.UnpinVersion(None, None, self.mock_call_config)
197 safeunlink.assert_not_called()
198 # android.UnpinVersion does not modify the response.