blob: 7d94d4b2f633de19a22dcb32369dd79999764172 [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 Klein4de25e82019-08-05 15:58:39 -060010import mock
11
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
LaMont Jones8a1644f2019-04-16 14:30:17 -060013from chromite.api.controller import android
14from chromite.api.gen.chromite.api import android_pb2
15from chromite.api.gen.chromiumos import common_pb2
LaMont Jones8a1644f2019-04-16 14:30:17 -060016from chromite.lib import constants
17from chromite.lib import cros_build_lib
18from chromite.lib import cros_test_lib
19from chromite.lib import osutils
Alex Klein26e472b2020-03-10 14:35:01 -060020from chromite.lib import build_target_lib
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090021from chromite.service import android as service_android
Alex Klein4de25e82019-08-05 15:58:39 -060022from chromite.service import packages
23
LaMont Jones8a1644f2019-04-16 14:30:17 -060024
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090025class GetLatestBuildTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
26 """Unittests for GetLatestBuild."""
27
28 def setUp(self):
29 self._mock = self.PatchObject(service_android, 'GetLatestBuild')
30 self._mock.return_value = ('7123456', {})
31 self._input_proto = android_pb2.GetLatestBuildRequest()
32 self._input_proto.android_build_branch = 'git_rvc-arc'
33 self._output_proto = android_pb2.GetLatestBuildResponse()
34
35 def testValidateOnly(self):
36 """Test that a validate only call does not execute any logic."""
37 android.GetLatestBuild(self._input_proto, self._output_proto,
38 self.validate_only_config)
39 self._mock.assert_not_called()
40
41 def testMockCall(self):
42 """Test that a mock call does not execute logic, returns mocked value."""
43 android.GetLatestBuild(self._input_proto, self._output_proto,
44 self.mock_call_config)
45 self._mock.assert_not_called()
46 self.assertEqual(self._output_proto.android_version, '7123456')
47
48 def testFailsIfAndroidBuildBranchMissing(self):
49 """Fails if package_name is missing."""
50 self._input_proto.android_build_branch = ''
51 with self.assertRaises(cros_build_lib.DieSystemExit):
52 android.GetLatestBuild(self._input_proto, self._output_proto,
53 self.api_config)
54 self._mock.assert_not_called()
55
56 def testActualCall(self):
57 """Test that the underlying method is being called in the usual case."""
58 android.GetLatestBuild(self._input_proto, self._output_proto,
59 self.api_config)
60 self._mock.assert_called_once_with('git_rvc-arc')
61 self.assertEqual(self._output_proto.android_version, '7123456')
62
63
Alex Klein231d2da2019-07-22 16:44:45 -060064class MarkStableTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -060065 """Unittests for MarkStable."""
66
67 def setUp(self):
Alex Klein4de25e82019-08-05 15:58:39 -060068 self.uprev = self.PatchObject(packages, 'uprev_android')
LaMont Jones8a1644f2019-04-16 14:30:17 -060069
70 self.input_proto = android_pb2.MarkStableRequest()
LaMont Jones8a1644f2019-04-16 14:30:17 -060071 self.input_proto.package_name = 'android-package-name'
72 self.input_proto.android_build_branch = 'android_build_branch'
Alex Klein4de25e82019-08-05 15:58:39 -060073 self.input_proto.build_targets.add().name = 'foo'
74 self.input_proto.build_targets.add().name = 'bar'
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +090075 self.input_proto.skip_commit = True
Alex Klein4de25e82019-08-05 15:58:39 -060076
Alex Klein26e472b2020-03-10 14:35:01 -060077 self.build_targets = [build_target_lib.BuildTarget('foo'),
78 build_target_lib.BuildTarget('bar')]
LaMont Jones8a1644f2019-04-16 14:30:17 -060079
Alex Klein231d2da2019-07-22 16:44:45 -060080 self.response = android_pb2.MarkStableResponse()
81
82 def testValidateOnly(self):
83 """Sanity check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -060084 android.MarkStable(self.input_proto, self.response,
85 self.validate_only_config)
Alex Klein4de25e82019-08-05 15:58:39 -060086 self.uprev.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -060087
Michael Mortensen25626442019-11-22 10:06:59 -070088 def testMockCall(self):
89 """Test that a mock call does not execute logic, returns mocked value."""
90 android.MarkStable(self.input_proto, self.response,
91 self.mock_call_config)
92 self.uprev.assert_not_called()
93 self.assertEqual(self.response.status,
94 android_pb2.MARK_STABLE_STATUS_SUCCESS)
95 self.assertEqual(self.response.android_atom.category, 'category')
96 self.assertEqual(self.response.android_atom.package_name,
97 'android-package-name')
98 self.assertEqual(self.response.android_atom.version, '1.2')
99
LaMont Jones8a1644f2019-04-16 14:30:17 -0600100 def testFailsIfPackageNameMissing(self):
101 """Fails if package_name is missing."""
102 self.input_proto.package_name = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -0600103 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600104 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600105 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -0600106
107 def testFailsIfAndroidBuildBranchMissing(self):
108 """Fails if android_build_branch is missing."""
109 self.input_proto.android_build_branch = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -0600110 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600111 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600112 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -0600113
114 def testCallsCommandCorrectly(self):
115 """Test that commands.MarkAndroidAsStable is called correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -0600116 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600117 self.uprev.return_value = 'cat/android-1.2.3'
LaMont Jones8a1644f2019-04-16 14:30:17 -0600118 atom = common_pb2.PackageInfo()
119 atom.category = 'cat'
120 atom.package_name = 'android'
121 atom.version = '1.2.3'
Alex Klein231d2da2019-07-22 16:44:45 -0600122 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600123 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600124 android_package=self.input_proto.package_name,
125 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600126 chroot=mock.ANY,
127 build_targets=self.build_targets,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900128 android_version=self.input_proto.android_version,
129 skip_commit=self.input_proto.skip_commit,
130 )
Alex Klein231d2da2019-07-22 16:44:45 -0600131 self.assertEqual(self.response.android_atom, atom)
132 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600133 android_pb2.MARK_STABLE_STATUS_SUCCESS)
134
135 def testHandlesEarlyExit(self):
136 """Test that early exit is handled correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -0600137 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600138 self.uprev.return_value = ''
Alex Klein231d2da2019-07-22 16:44:45 -0600139 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600140 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600141 android_package=self.input_proto.package_name,
142 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600143 chroot=mock.ANY,
144 build_targets=self.build_targets,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900145 android_version=self.input_proto.android_version,
146 skip_commit=self.input_proto.skip_commit,
147 )
Alex Klein231d2da2019-07-22 16:44:45 -0600148 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600149 android_pb2.MARK_STABLE_STATUS_EARLY_EXIT)
150
151 def testHandlesPinnedUprevError(self):
152 """Test that pinned error is handled correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -0600153 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600154 self.uprev.side_effect = packages.AndroidIsPinnedUprevError('pin/xx-1.1')
LaMont Jones8a1644f2019-04-16 14:30:17 -0600155 atom = common_pb2.PackageInfo()
156 atom.category = 'pin'
157 atom.package_name = 'xx'
158 atom.version = '1.1'
Alex Klein231d2da2019-07-22 16:44:45 -0600159 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600160 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600161 android_package=self.input_proto.package_name,
162 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600163 chroot=mock.ANY,
164 build_targets=self.build_targets,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900165 android_version=self.input_proto.android_version,
166 skip_commit=self.input_proto.skip_commit,
167 )
Alex Klein231d2da2019-07-22 16:44:45 -0600168 self.assertEqual(self.response.android_atom, atom)
169 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600170 android_pb2.MARK_STABLE_STATUS_PINNED)
171
172
Alex Klein231d2da2019-07-22 16:44:45 -0600173class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -0600174 """Unittests for UnpinVersion."""
175
176 def testCallsUnlink(self):
177 """SetAndroid calls service with correct args."""
178 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
179 self.PatchObject(constants, '_FindSourceRoot', return_value='SRCROOT')
180
181 # This has the side effect of making sure that input and output proto are
182 # not actually used.
Alex Klein231d2da2019-07-22 16:44:45 -0600183 android.UnpinVersion(None, None, self.api_config)
LaMont Jones8a1644f2019-04-16 14:30:17 -0600184 safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH)
Alex Klein231d2da2019-07-22 16:44:45 -0600185
186 def testValidateOnly(self):
187 """Sanity check that a validate only call does not execute any logic."""
188 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
189
190 android.UnpinVersion(None, None, self.validate_only_config)
191 safeunlink.assert_not_called()
Michael Mortensen25626442019-11-22 10:06:59 -0700192
193 def testMockCall(self):
194 """Test that a mock call does not execute logic."""
195 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
196
197 android.UnpinVersion(None, None, self.mock_call_config)
198 safeunlink.assert_not_called()
199 # android.UnpinVersion does not modify the response.