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