blob: 790fb4e02bf277143887fdb24df7e52011dab0cb [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'
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090072 self.input_proto.android_version = 'android-version'
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
LaMont Jones8a1644f2019-04-16 14:30:17 -0600107 def testCallsCommandCorrectly(self):
108 """Test that commands.MarkAndroidAsStable is called correctly."""
Alex Klein4de25e82019-08-05 15:58:39 -0600109 self.uprev.return_value = 'cat/android-1.2.3'
LaMont Jones8a1644f2019-04-16 14:30:17 -0600110 atom = common_pb2.PackageInfo()
111 atom.category = 'cat'
112 atom.package_name = 'android'
113 atom.version = '1.2.3'
Alex Klein231d2da2019-07-22 16:44:45 -0600114 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600115 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600116 android_package=self.input_proto.package_name,
Alex Klein4de25e82019-08-05 15:58:39 -0600117 chroot=mock.ANY,
118 build_targets=self.build_targets,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900119 android_build_branch=self.input_proto.android_build_branch,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900120 android_version=self.input_proto.android_version,
121 skip_commit=self.input_proto.skip_commit,
122 )
Alex Klein231d2da2019-07-22 16:44:45 -0600123 self.assertEqual(self.response.android_atom, atom)
124 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600125 android_pb2.MARK_STABLE_STATUS_SUCCESS)
126
127 def testHandlesEarlyExit(self):
128 """Test that early exit is handled correctly."""
Alex Klein4de25e82019-08-05 15:58:39 -0600129 self.uprev.return_value = ''
Alex Klein231d2da2019-07-22 16:44:45 -0600130 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600131 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600132 android_package=self.input_proto.package_name,
Alex Klein4de25e82019-08-05 15:58:39 -0600133 chroot=mock.ANY,
134 build_targets=self.build_targets,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900135 android_build_branch=self.input_proto.android_build_branch,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900136 android_version=self.input_proto.android_version,
137 skip_commit=self.input_proto.skip_commit,
138 )
Alex Klein231d2da2019-07-22 16:44:45 -0600139 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600140 android_pb2.MARK_STABLE_STATUS_EARLY_EXIT)
141
142 def testHandlesPinnedUprevError(self):
143 """Test that pinned error is handled correctly."""
Alex Klein4de25e82019-08-05 15:58:39 -0600144 self.uprev.side_effect = packages.AndroidIsPinnedUprevError('pin/xx-1.1')
LaMont Jones8a1644f2019-04-16 14:30:17 -0600145 atom = common_pb2.PackageInfo()
146 atom.category = 'pin'
147 atom.package_name = 'xx'
148 atom.version = '1.1'
Alex Klein231d2da2019-07-22 16:44:45 -0600149 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600150 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600151 android_package=self.input_proto.package_name,
Alex Klein4de25e82019-08-05 15:58:39 -0600152 chroot=mock.ANY,
153 build_targets=self.build_targets,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900154 android_build_branch=self.input_proto.android_build_branch,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900155 android_version=self.input_proto.android_version,
156 skip_commit=self.input_proto.skip_commit,
157 )
Alex Klein231d2da2019-07-22 16:44:45 -0600158 self.assertEqual(self.response.android_atom, atom)
159 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600160 android_pb2.MARK_STABLE_STATUS_PINNED)
161
162
Alex Klein231d2da2019-07-22 16:44:45 -0600163class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -0600164 """Unittests for UnpinVersion."""
165
166 def testCallsUnlink(self):
167 """SetAndroid calls service with correct args."""
168 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
169 self.PatchObject(constants, '_FindSourceRoot', return_value='SRCROOT')
170
171 # This has the side effect of making sure that input and output proto are
172 # not actually used.
Alex Klein231d2da2019-07-22 16:44:45 -0600173 android.UnpinVersion(None, None, self.api_config)
LaMont Jones8a1644f2019-04-16 14:30:17 -0600174 safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH)
Alex Klein231d2da2019-07-22 16:44:45 -0600175
176 def testValidateOnly(self):
177 """Sanity check that a validate only call does not execute any logic."""
178 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
179
180 android.UnpinVersion(None, None, self.validate_only_config)
181 safeunlink.assert_not_called()
Michael Mortensen25626442019-11-22 10:06:59 -0700182
183 def testMockCall(self):
184 """Test that a mock call does not execute logic."""
185 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
186
187 android.UnpinVersion(None, None, self.mock_call_config)
188 safeunlink.assert_not_called()
189 # android.UnpinVersion does not modify the response.