LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 10 | import mock |
| 11 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 12 | from chromite.api import api_config |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 13 | from chromite.api.controller import android |
| 14 | from chromite.api.gen.chromite.api import android_pb2 |
| 15 | from chromite.api.gen.chromiumos import common_pb2 |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 16 | from chromite.lib import constants |
| 17 | from chromite.lib import cros_build_lib |
| 18 | from chromite.lib import cros_test_lib |
| 19 | from chromite.lib import osutils |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 20 | from chromite.lib import build_target_lib |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame^] | 21 | from chromite.service import android as service_android |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 22 | from chromite.service import packages |
| 23 | |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 24 | |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame^] | 25 | class 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 64 | class MarkStableTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 65 | """Unittests for MarkStable.""" |
| 66 | |
| 67 | def setUp(self): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 68 | self.uprev = self.PatchObject(packages, 'uprev_android') |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 69 | |
| 70 | self.input_proto = android_pb2.MarkStableRequest() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 71 | self.input_proto.package_name = 'android-package-name' |
| 72 | self.input_proto.android_build_branch = 'android_build_branch' |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 73 | self.input_proto.build_targets.add().name = 'foo' |
| 74 | self.input_proto.build_targets.add().name = 'bar' |
Shao-Chuan Lee | 85ba7ce | 2021-02-09 13:50:11 +0900 | [diff] [blame] | 75 | self.input_proto.skip_commit = True |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 76 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 77 | self.build_targets = [build_target_lib.BuildTarget('foo'), |
| 78 | build_target_lib.BuildTarget('bar')] |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 79 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 80 | self.response = android_pb2.MarkStableResponse() |
| 81 | |
| 82 | def testValidateOnly(self): |
| 83 | """Sanity check that a validate only call does not execute any logic.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 84 | android.MarkStable(self.input_proto, self.response, |
| 85 | self.validate_only_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 86 | self.uprev.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 87 | |
Michael Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 88 | 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 Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 100 | def testFailsIfPackageNameMissing(self): |
| 101 | """Fails if package_name is missing.""" |
| 102 | self.input_proto.package_name = '' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 103 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 104 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 105 | self.uprev.assert_not_called() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 106 | |
| 107 | def testFailsIfAndroidBuildBranchMissing(self): |
| 108 | """Fails if android_build_branch is missing.""" |
| 109 | self.input_proto.android_build_branch = '' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 110 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 111 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 112 | self.uprev.assert_not_called() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 113 | |
| 114 | def testCallsCommandCorrectly(self): |
| 115 | """Test that commands.MarkAndroidAsStable is called correctly.""" |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 116 | self.input_proto.android_version = 'android-version' |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 117 | self.uprev.return_value = 'cat/android-1.2.3' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 118 | atom = common_pb2.PackageInfo() |
| 119 | atom.category = 'cat' |
| 120 | atom.package_name = 'android' |
| 121 | atom.version = '1.2.3' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 122 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 123 | self.uprev.assert_called_once_with( |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 124 | android_package=self.input_proto.package_name, |
| 125 | android_build_branch=self.input_proto.android_build_branch, |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 126 | chroot=mock.ANY, |
| 127 | build_targets=self.build_targets, |
Shao-Chuan Lee | 85ba7ce | 2021-02-09 13:50:11 +0900 | [diff] [blame] | 128 | android_version=self.input_proto.android_version, |
| 129 | skip_commit=self.input_proto.skip_commit, |
| 130 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 131 | self.assertEqual(self.response.android_atom, atom) |
| 132 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 133 | android_pb2.MARK_STABLE_STATUS_SUCCESS) |
| 134 | |
| 135 | def testHandlesEarlyExit(self): |
| 136 | """Test that early exit is handled correctly.""" |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 137 | self.input_proto.android_version = 'android-version' |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 138 | self.uprev.return_value = '' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 139 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 140 | self.uprev.assert_called_once_with( |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 141 | android_package=self.input_proto.package_name, |
| 142 | android_build_branch=self.input_proto.android_build_branch, |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 143 | chroot=mock.ANY, |
| 144 | build_targets=self.build_targets, |
Shao-Chuan Lee | 85ba7ce | 2021-02-09 13:50:11 +0900 | [diff] [blame] | 145 | android_version=self.input_proto.android_version, |
| 146 | skip_commit=self.input_proto.skip_commit, |
| 147 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 148 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 149 | android_pb2.MARK_STABLE_STATUS_EARLY_EXIT) |
| 150 | |
| 151 | def testHandlesPinnedUprevError(self): |
| 152 | """Test that pinned error is handled correctly.""" |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 153 | self.input_proto.android_version = 'android-version' |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 154 | self.uprev.side_effect = packages.AndroidIsPinnedUprevError('pin/xx-1.1') |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 155 | atom = common_pb2.PackageInfo() |
| 156 | atom.category = 'pin' |
| 157 | atom.package_name = 'xx' |
| 158 | atom.version = '1.1' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 159 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 160 | self.uprev.assert_called_once_with( |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 161 | android_package=self.input_proto.package_name, |
| 162 | android_build_branch=self.input_proto.android_build_branch, |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 163 | chroot=mock.ANY, |
| 164 | build_targets=self.build_targets, |
Shao-Chuan Lee | 85ba7ce | 2021-02-09 13:50:11 +0900 | [diff] [blame] | 165 | android_version=self.input_proto.android_version, |
| 166 | skip_commit=self.input_proto.skip_commit, |
| 167 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 168 | self.assertEqual(self.response.android_atom, atom) |
| 169 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 170 | android_pb2.MARK_STABLE_STATUS_PINNED) |
| 171 | |
| 172 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 173 | class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 174 | """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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 183 | android.UnpinVersion(None, None, self.api_config) |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 184 | safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 185 | |
| 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 Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 192 | |
| 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. |