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