blob: a2bc63095a2a0e502d5444423e4518331a3a740c [file] [log] [blame]
LaMont Jones8a1644f2019-04-16 14:30:17 -06001# -*- 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
8from __future__ import print_function
9
Mike Frysingeref94e4c2020-02-10 23:59:54 -050010import sys
11
Alex Klein4de25e82019-08-05 15:58:39 -060012import mock
13
Alex Klein231d2da2019-07-22 16:44:45 -060014from chromite.api import api_config
LaMont Jones8a1644f2019-04-16 14:30:17 -060015from chromite.api.controller import android
16from chromite.api.gen.chromite.api import android_pb2
17from chromite.api.gen.chromiumos import common_pb2
LaMont Jones8a1644f2019-04-16 14:30:17 -060018from chromite.lib import constants
19from chromite.lib import cros_build_lib
20from chromite.lib import cros_test_lib
21from chromite.lib import osutils
Alex Klein26e472b2020-03-10 14:35:01 -060022from chromite.lib import build_target_lib
Alex Klein4de25e82019-08-05 15:58:39 -060023from chromite.service import packages
24
LaMont Jones8a1644f2019-04-16 14:30:17 -060025
Mike Frysingeref94e4c2020-02-10 23:59:54 -050026assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
27
28
Alex Klein231d2da2019-07-22 16:44:45 -060029class MarkStableTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -060030 """Unittests for MarkStable."""
31
32 def setUp(self):
Alex Klein4de25e82019-08-05 15:58:39 -060033 self.uprev = self.PatchObject(packages, 'uprev_android')
LaMont Jones8a1644f2019-04-16 14:30:17 -060034
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 Klein4de25e82019-08-05 15:58:39 -060039 self.input_proto.build_targets.add().name = 'foo'
40 self.input_proto.build_targets.add().name = 'bar'
41
Alex Klein26e472b2020-03-10 14:35:01 -060042 self.build_targets = [build_target_lib.BuildTarget('foo'),
43 build_target_lib.BuildTarget('bar')]
LaMont Jones8a1644f2019-04-16 14:30:17 -060044
Alex Klein231d2da2019-07-22 16:44:45 -060045 self.response = android_pb2.MarkStableResponse()
46
47 def testValidateOnly(self):
48 """Sanity check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -060049 android.MarkStable(self.input_proto, self.response,
50 self.validate_only_config)
Alex Klein4de25e82019-08-05 15:58:39 -060051 self.uprev.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -060052
Michael Mortensen25626442019-11-22 10:06:59 -070053 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 Jones8a1644f2019-04-16 14:30:17 -060065 def testFailsIfTrackingBranchMissing(self):
66 """Fails if tracking_branch is missing."""
67 self.input_proto.tracking_branch = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -060068 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060069 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -060070 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -060071
72 def testFailsIfPackageNameMissing(self):
73 """Fails if package_name is missing."""
74 self.input_proto.package_name = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -060075 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060076 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -060077 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -060078
79 def testFailsIfAndroidBuildBranchMissing(self):
80 """Fails if android_build_branch is missing."""
81 self.input_proto.android_build_branch = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -060082 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060083 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -060084 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -060085
86 def testCallsCommandCorrectly(self):
87 """Test that commands.MarkAndroidAsStable is called correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -060088 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -060089 self.uprev.return_value = 'cat/android-1.2.3'
LaMont Jones8a1644f2019-04-16 14:30:17 -060090 atom = common_pb2.PackageInfo()
91 atom.category = 'cat'
92 atom.package_name = 'android'
93 atom.version = '1.2.3'
Alex Klein231d2da2019-07-22 16:44:45 -060094 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -060095 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -060096 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 Klein4de25e82019-08-05 15:58:39 -060099 chroot=mock.ANY,
100 build_targets=self.build_targets,
Shao-Chuan Lee9c39e0c2020-04-24 11:40:34 +0900101 android_version=self.input_proto.android_version)
Alex Klein231d2da2019-07-22 16:44:45 -0600102 self.assertEqual(self.response.android_atom, atom)
103 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600104 android_pb2.MARK_STABLE_STATUS_SUCCESS)
105
106 def testHandlesEarlyExit(self):
107 """Test that early exit is handled correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -0600108 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600109 self.uprev.return_value = ''
Alex Klein231d2da2019-07-22 16:44:45 -0600110 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600111 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600112 tracking_branch=self.input_proto.tracking_branch,
113 android_package=self.input_proto.package_name,
114 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600115 chroot=mock.ANY,
116 build_targets=self.build_targets,
Shao-Chuan Lee9c39e0c2020-04-24 11:40:34 +0900117 android_version=self.input_proto.android_version)
Alex Klein231d2da2019-07-22 16:44:45 -0600118 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600119 android_pb2.MARK_STABLE_STATUS_EARLY_EXIT)
120
121 def testHandlesPinnedUprevError(self):
122 """Test that pinned error is handled correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -0600123 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600124 self.uprev.side_effect = packages.AndroidIsPinnedUprevError('pin/xx-1.1')
LaMont Jones8a1644f2019-04-16 14:30:17 -0600125 atom = common_pb2.PackageInfo()
126 atom.category = 'pin'
127 atom.package_name = 'xx'
128 atom.version = '1.1'
Alex Klein231d2da2019-07-22 16:44:45 -0600129 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600130 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600131 tracking_branch=self.input_proto.tracking_branch,
132 android_package=self.input_proto.package_name,
133 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600134 chroot=mock.ANY,
135 build_targets=self.build_targets,
Shao-Chuan Lee9c39e0c2020-04-24 11:40:34 +0900136 android_version=self.input_proto.android_version)
Alex Klein231d2da2019-07-22 16:44:45 -0600137 self.assertEqual(self.response.android_atom, atom)
138 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600139 android_pb2.MARK_STABLE_STATUS_PINNED)
140
141
Alex Klein231d2da2019-07-22 16:44:45 -0600142class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -0600143 """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 Klein231d2da2019-07-22 16:44:45 -0600152 android.UnpinVersion(None, None, self.api_config)
LaMont Jones8a1644f2019-04-16 14:30:17 -0600153 safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH)
Alex Klein231d2da2019-07-22 16:44:45 -0600154
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 Mortensen25626442019-11-22 10:06:59 -0700161
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.