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 | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 10 | from chromite.api import api_config |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 11 | from chromite.api.controller import android |
| 12 | from chromite.api.gen.chromite.api import android_pb2 |
| 13 | from chromite.api.gen.chromiumos import common_pb2 |
Mike Frysinger | 40ffb53 | 2021-02-12 07:36:08 -0500 | [diff] [blame] | 14 | from chromite.lib import build_target_lib |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 15 | from chromite.lib import constants |
| 16 | from chromite.lib import cros_build_lib |
| 17 | from chromite.lib import cros_test_lib |
| 18 | from chromite.lib import osutils |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame] | 19 | from chromite.service import android as service_android |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 20 | from chromite.service import packages |
Mike Frysinger | 40ffb53 | 2021-02-12 07:36:08 -0500 | [diff] [blame] | 21 | from chromite.third_party import mock |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 22 | |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 23 | |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame] | 24 | class 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 63 | class MarkStableTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 64 | """Unittests for MarkStable.""" |
| 65 | |
| 66 | def setUp(self): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 67 | self.uprev = self.PatchObject(packages, 'uprev_android') |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 68 | |
| 69 | self.input_proto = android_pb2.MarkStableRequest() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 70 | self.input_proto.package_name = 'android-package-name' |
| 71 | self.input_proto.android_build_branch = 'android_build_branch' |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame^] | 72 | self.input_proto.android_version = 'android-version' |
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 | |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 107 | def testCallsCommandCorrectly(self): |
| 108 | """Test that commands.MarkAndroidAsStable is called correctly.""" |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 109 | self.uprev.return_value = 'cat/android-1.2.3' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 110 | atom = common_pb2.PackageInfo() |
| 111 | atom.category = 'cat' |
| 112 | atom.package_name = 'android' |
| 113 | atom.version = '1.2.3' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 114 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 115 | self.uprev.assert_called_once_with( |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 116 | android_package=self.input_proto.package_name, |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 117 | chroot=mock.ANY, |
| 118 | build_targets=self.build_targets, |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame^] | 119 | android_build_branch=self.input_proto.android_build_branch, |
Shao-Chuan Lee | 85ba7ce | 2021-02-09 13:50:11 +0900 | [diff] [blame] | 120 | android_version=self.input_proto.android_version, |
| 121 | skip_commit=self.input_proto.skip_commit, |
| 122 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 123 | self.assertEqual(self.response.android_atom, atom) |
| 124 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 125 | android_pb2.MARK_STABLE_STATUS_SUCCESS) |
| 126 | |
| 127 | def testHandlesEarlyExit(self): |
| 128 | """Test that early exit is handled correctly.""" |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 129 | self.uprev.return_value = '' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 130 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 131 | self.uprev.assert_called_once_with( |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 132 | android_package=self.input_proto.package_name, |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 133 | chroot=mock.ANY, |
| 134 | build_targets=self.build_targets, |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame^] | 135 | android_build_branch=self.input_proto.android_build_branch, |
Shao-Chuan Lee | 85ba7ce | 2021-02-09 13:50:11 +0900 | [diff] [blame] | 136 | android_version=self.input_proto.android_version, |
| 137 | skip_commit=self.input_proto.skip_commit, |
| 138 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 139 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 140 | android_pb2.MARK_STABLE_STATUS_EARLY_EXIT) |
| 141 | |
| 142 | def testHandlesPinnedUprevError(self): |
| 143 | """Test that pinned error is handled correctly.""" |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 144 | self.uprev.side_effect = packages.AndroidIsPinnedUprevError('pin/xx-1.1') |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 145 | atom = common_pb2.PackageInfo() |
| 146 | atom.category = 'pin' |
| 147 | atom.package_name = 'xx' |
| 148 | atom.version = '1.1' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 149 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 150 | self.uprev.assert_called_once_with( |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 151 | android_package=self.input_proto.package_name, |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 152 | chroot=mock.ANY, |
| 153 | build_targets=self.build_targets, |
Shao-Chuan Lee | a4b4f30 | 2021-05-12 14:40:20 +0900 | [diff] [blame^] | 154 | android_build_branch=self.input_proto.android_build_branch, |
Shao-Chuan Lee | 85ba7ce | 2021-02-09 13:50:11 +0900 | [diff] [blame] | 155 | android_version=self.input_proto.android_version, |
| 156 | skip_commit=self.input_proto.skip_commit, |
| 157 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 158 | self.assertEqual(self.response.android_atom, atom) |
| 159 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 160 | android_pb2.MARK_STABLE_STATUS_PINNED) |
| 161 | |
| 162 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 163 | class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 164 | """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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 173 | android.UnpinVersion(None, None, self.api_config) |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 174 | safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 175 | |
| 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 Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 182 | |
| 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. |