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 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 10 | import sys |
| 11 | |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 12 | import mock |
| 13 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 14 | from chromite.api import api_config |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 15 | from chromite.api.controller import android |
| 16 | from chromite.api.gen.chromite.api import android_pb2 |
| 17 | from chromite.api.gen.chromiumos import common_pb2 |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 18 | from chromite.lib import constants |
| 19 | from chromite.lib import cros_build_lib |
| 20 | from chromite.lib import cros_test_lib |
| 21 | from chromite.lib import osutils |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 22 | from chromite.lib.build_target_util import BuildTarget |
| 23 | from chromite.service import packages |
| 24 | |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 25 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 26 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 27 | |
| 28 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 29 | class MarkStableTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 30 | """Unittests for MarkStable.""" |
| 31 | |
| 32 | def setUp(self): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 33 | self.uprev = self.PatchObject(packages, 'uprev_android') |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 34 | |
| 35 | self.input_proto = android_pb2.MarkStableRequest() |
| 36 | self.input_proto.tracking_branch = 'tracking-branch' |
| 37 | self.input_proto.package_name = 'android-package-name' |
| 38 | self.input_proto.android_build_branch = 'android_build_branch' |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 39 | self.input_proto.build_targets.add().name = 'foo' |
| 40 | self.input_proto.build_targets.add().name = 'bar' |
| 41 | |
| 42 | self.build_targets = [BuildTarget('foo'), BuildTarget('bar')] |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 43 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 44 | self.response = android_pb2.MarkStableResponse() |
| 45 | |
| 46 | def testValidateOnly(self): |
| 47 | """Sanity check that a validate only call does not execute any logic.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 48 | android.MarkStable(self.input_proto, self.response, |
| 49 | self.validate_only_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 50 | self.uprev.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 51 | |
Michael Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 52 | def testMockCall(self): |
| 53 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 54 | android.MarkStable(self.input_proto, self.response, |
| 55 | self.mock_call_config) |
| 56 | self.uprev.assert_not_called() |
| 57 | self.assertEqual(self.response.status, |
| 58 | android_pb2.MARK_STABLE_STATUS_SUCCESS) |
| 59 | self.assertEqual(self.response.android_atom.category, 'category') |
| 60 | self.assertEqual(self.response.android_atom.package_name, |
| 61 | 'android-package-name') |
| 62 | self.assertEqual(self.response.android_atom.version, '1.2') |
| 63 | |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 64 | def testFailsIfTrackingBranchMissing(self): |
| 65 | """Fails if tracking_branch is missing.""" |
| 66 | self.input_proto.tracking_branch = '' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 67 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 68 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 69 | self.uprev.assert_not_called() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 70 | |
| 71 | def testFailsIfPackageNameMissing(self): |
| 72 | """Fails if package_name is missing.""" |
| 73 | self.input_proto.package_name = '' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 74 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 75 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 76 | self.uprev.assert_not_called() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 77 | |
| 78 | def testFailsIfAndroidBuildBranchMissing(self): |
| 79 | """Fails if android_build_branch is missing.""" |
| 80 | self.input_proto.android_build_branch = '' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 81 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 82 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 83 | self.uprev.assert_not_called() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 84 | |
| 85 | def testCallsCommandCorrectly(self): |
| 86 | """Test that commands.MarkAndroidAsStable is called correctly.""" |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 87 | self.input_proto.android_version = 'android-version' |
| 88 | self.input_proto.android_gts_build_branch = 'gts-branch' |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 89 | self.uprev.return_value = 'cat/android-1.2.3' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 90 | atom = common_pb2.PackageInfo() |
| 91 | atom.category = 'cat' |
| 92 | atom.package_name = 'android' |
| 93 | atom.version = '1.2.3' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 94 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 95 | self.uprev.assert_called_once_with( |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 96 | tracking_branch=self.input_proto.tracking_branch, |
| 97 | android_package=self.input_proto.package_name, |
| 98 | android_build_branch=self.input_proto.android_build_branch, |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 99 | chroot=mock.ANY, |
| 100 | build_targets=self.build_targets, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 101 | android_version=self.input_proto.android_version, |
| 102 | android_gts_build_branch=self.input_proto.android_gts_build_branch) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 103 | self.assertEqual(self.response.android_atom, atom) |
| 104 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 105 | android_pb2.MARK_STABLE_STATUS_SUCCESS) |
| 106 | |
| 107 | def testHandlesEarlyExit(self): |
| 108 | """Test that early exit is handled correctly.""" |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 109 | self.input_proto.android_version = 'android-version' |
| 110 | self.input_proto.android_gts_build_branch = 'gts-branch' |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 111 | self.uprev.return_value = '' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 112 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 113 | self.uprev.assert_called_once_with( |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 114 | tracking_branch=self.input_proto.tracking_branch, |
| 115 | android_package=self.input_proto.package_name, |
| 116 | android_build_branch=self.input_proto.android_build_branch, |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 117 | chroot=mock.ANY, |
| 118 | build_targets=self.build_targets, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 119 | android_version=self.input_proto.android_version, |
| 120 | android_gts_build_branch=self.input_proto.android_gts_build_branch) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 121 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 122 | android_pb2.MARK_STABLE_STATUS_EARLY_EXIT) |
| 123 | |
| 124 | def testHandlesPinnedUprevError(self): |
| 125 | """Test that pinned error is handled correctly.""" |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 126 | self.input_proto.android_version = 'android-version' |
| 127 | self.input_proto.android_gts_build_branch = 'gts-branch' |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 128 | self.uprev.side_effect = packages.AndroidIsPinnedUprevError('pin/xx-1.1') |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 129 | atom = common_pb2.PackageInfo() |
| 130 | atom.category = 'pin' |
| 131 | atom.package_name = 'xx' |
| 132 | atom.version = '1.1' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 133 | android.MarkStable(self.input_proto, self.response, self.api_config) |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 134 | self.uprev.assert_called_once_with( |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 135 | tracking_branch=self.input_proto.tracking_branch, |
| 136 | android_package=self.input_proto.package_name, |
| 137 | android_build_branch=self.input_proto.android_build_branch, |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 138 | chroot=mock.ANY, |
| 139 | build_targets=self.build_targets, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 140 | android_version=self.input_proto.android_version, |
| 141 | android_gts_build_branch=self.input_proto.android_gts_build_branch) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 142 | self.assertEqual(self.response.android_atom, atom) |
| 143 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 144 | android_pb2.MARK_STABLE_STATUS_PINNED) |
| 145 | |
| 146 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 147 | class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 148 | """Unittests for UnpinVersion.""" |
| 149 | |
| 150 | def testCallsUnlink(self): |
| 151 | """SetAndroid calls service with correct args.""" |
| 152 | safeunlink = self.PatchObject(osutils, 'SafeUnlink') |
| 153 | self.PatchObject(constants, '_FindSourceRoot', return_value='SRCROOT') |
| 154 | |
| 155 | # This has the side effect of making sure that input and output proto are |
| 156 | # not actually used. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 157 | android.UnpinVersion(None, None, self.api_config) |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 158 | safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 159 | |
| 160 | def testValidateOnly(self): |
| 161 | """Sanity check that a validate only call does not execute any logic.""" |
| 162 | safeunlink = self.PatchObject(osutils, 'SafeUnlink') |
| 163 | |
| 164 | android.UnpinVersion(None, None, self.validate_only_config) |
| 165 | safeunlink.assert_not_called() |
Michael Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 166 | |
| 167 | def testMockCall(self): |
| 168 | """Test that a mock call does not execute logic.""" |
| 169 | safeunlink = self.PatchObject(osutils, 'SafeUnlink') |
| 170 | |
| 171 | android.UnpinVersion(None, None, self.mock_call_config) |
| 172 | safeunlink.assert_not_called() |
| 173 | # android.UnpinVersion does not modify the response. |