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 |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 14 | # TODO(crbug/904939): implement service/android.py |
| 15 | from chromite.cbuildbot import commands |
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 |
| 20 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 21 | class MarkStableTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 22 | """Unittests for MarkStable.""" |
| 23 | |
| 24 | def setUp(self): |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 25 | self.command = self.PatchObject(commands, 'MarkAndroidAsStable') |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 26 | |
| 27 | self.input_proto = android_pb2.MarkStableRequest() |
| 28 | self.input_proto.tracking_branch = 'tracking-branch' |
| 29 | self.input_proto.package_name = 'android-package-name' |
| 30 | self.input_proto.android_build_branch = 'android_build_branch' |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 31 | self.board1 = self.input_proto.boards.add() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 32 | self.board1.name = 'board1' |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 33 | self.board2 = self.input_proto.boards.add() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 34 | self.board2.name = 'board2' |
| 35 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 36 | self.response = android_pb2.MarkStableResponse() |
| 37 | |
| 38 | def testValidateOnly(self): |
| 39 | """Sanity check that a validate only call does not execute any logic.""" |
| 40 | patch = self.PatchObject(commands, 'MarkAndroidAsStable') |
| 41 | |
| 42 | android.MarkStable(self.input_proto, self.response, |
| 43 | self.validate_only_config) |
| 44 | patch.assert_not_called() |
| 45 | |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 46 | def testFailsIfTrackingBranchMissing(self): |
| 47 | """Fails if tracking_branch is missing.""" |
| 48 | self.input_proto.tracking_branch = '' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 49 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 50 | android.MarkStable(self.input_proto, self.response, self.api_config) |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 51 | self.command.assert_not_called() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 52 | |
| 53 | def testFailsIfPackageNameMissing(self): |
| 54 | """Fails if package_name is missing.""" |
| 55 | self.input_proto.package_name = '' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 56 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 57 | android.MarkStable(self.input_proto, self.response, self.api_config) |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 58 | self.command.assert_not_called() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 59 | |
| 60 | def testFailsIfAndroidBuildBranchMissing(self): |
| 61 | """Fails if android_build_branch is missing.""" |
| 62 | self.input_proto.android_build_branch = '' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 63 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 64 | android.MarkStable(self.input_proto, self.response, self.api_config) |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 65 | self.command.assert_not_called() |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 66 | |
| 67 | def testCallsCommandCorrectly(self): |
| 68 | """Test that commands.MarkAndroidAsStable is called correctly.""" |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 69 | self.input_proto.android_version = 'android-version' |
| 70 | self.input_proto.android_gts_build_branch = 'gts-branch' |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 71 | self.command.return_value = 'cat/android-1.2.3' |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 72 | atom = common_pb2.PackageInfo() |
| 73 | atom.category = 'cat' |
| 74 | atom.package_name = 'android' |
| 75 | atom.version = '1.2.3' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 76 | android.MarkStable(self.input_proto, self.response, self.api_config) |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 77 | self.command.assert_called_once_with( |
| 78 | buildroot=self.input_proto.buildroot, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 79 | tracking_branch=self.input_proto.tracking_branch, |
| 80 | android_package=self.input_proto.package_name, |
| 81 | android_build_branch=self.input_proto.android_build_branch, |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 82 | boards=self.input_proto.boards, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 83 | android_version=self.input_proto.android_version, |
| 84 | android_gts_build_branch=self.input_proto.android_gts_build_branch) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 85 | self.assertEqual(self.response.android_atom, atom) |
| 86 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 87 | android_pb2.MARK_STABLE_STATUS_SUCCESS) |
| 88 | |
| 89 | def testHandlesEarlyExit(self): |
| 90 | """Test that early exit is handled correctly.""" |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 91 | self.input_proto.android_version = 'android-version' |
| 92 | self.input_proto.android_gts_build_branch = 'gts-branch' |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 93 | self.command.return_value = '' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 94 | android.MarkStable(self.input_proto, self.response, self.api_config) |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 95 | self.command.assert_called_once_with( |
| 96 | buildroot=self.input_proto.buildroot, |
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, |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 100 | boards=self.input_proto.boards, |
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.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 104 | android_pb2.MARK_STABLE_STATUS_EARLY_EXIT) |
| 105 | |
| 106 | def testHandlesPinnedUprevError(self): |
| 107 | """Test that pinned error is handled correctly.""" |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 108 | self.input_proto.android_version = 'android-version' |
| 109 | self.input_proto.android_gts_build_branch = 'gts-branch' |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 110 | self.command.side_effect = commands.AndroidIsPinnedUprevError('pin/xx-1.1') |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 111 | atom = common_pb2.PackageInfo() |
| 112 | atom.category = 'pin' |
| 113 | atom.package_name = 'xx' |
| 114 | atom.version = '1.1' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 115 | android.MarkStable(self.input_proto, self.response, self.api_config) |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 116 | self.command.assert_called_once_with( |
| 117 | buildroot=self.input_proto.buildroot, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 118 | tracking_branch=self.input_proto.tracking_branch, |
| 119 | android_package=self.input_proto.package_name, |
| 120 | android_build_branch=self.input_proto.android_build_branch, |
David Burger | ff1a92c | 2019-07-09 00:27:54 +0000 | [diff] [blame] | 121 | boards=self.input_proto.boards, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 122 | android_version=self.input_proto.android_version, |
| 123 | android_gts_build_branch=self.input_proto.android_gts_build_branch) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 124 | self.assertEqual(self.response.android_atom, atom) |
| 125 | self.assertEqual(self.response.status, |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 126 | android_pb2.MARK_STABLE_STATUS_PINNED) |
| 127 | |
| 128 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 129 | class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 130 | """Unittests for UnpinVersion.""" |
| 131 | |
| 132 | def testCallsUnlink(self): |
| 133 | """SetAndroid calls service with correct args.""" |
| 134 | safeunlink = self.PatchObject(osutils, 'SafeUnlink') |
| 135 | self.PatchObject(constants, '_FindSourceRoot', return_value='SRCROOT') |
| 136 | |
| 137 | # This has the side effect of making sure that input and output proto are |
| 138 | # not actually used. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 139 | android.UnpinVersion(None, None, self.api_config) |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 140 | safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 141 | |
| 142 | def testValidateOnly(self): |
| 143 | """Sanity check that a validate only call does not execute any logic.""" |
| 144 | safeunlink = self.PatchObject(osutils, 'SafeUnlink') |
| 145 | |
| 146 | android.UnpinVersion(None, None, self.validate_only_config) |
| 147 | safeunlink.assert_not_called() |