blob: 6d5eb04688d5551227d0a08f26aa64c79d303d4d [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
Alex Klein4de25e82019-08-05 15:58:39 -060010import mock
11
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
LaMont Jones8a1644f2019-04-16 14:30:17 -060013from chromite.api.controller import android
14from chromite.api.gen.chromite.api import android_pb2
15from chromite.api.gen.chromiumos import common_pb2
LaMont Jones8a1644f2019-04-16 14:30:17 -060016from chromite.lib import constants
17from chromite.lib import cros_build_lib
18from chromite.lib import cros_test_lib
19from chromite.lib import osutils
Alex Klein26e472b2020-03-10 14:35:01 -060020from chromite.lib import build_target_lib
Alex Klein4de25e82019-08-05 15:58:39 -060021from chromite.service import packages
22
LaMont Jones8a1644f2019-04-16 14:30:17 -060023
Alex Klein231d2da2019-07-22 16:44:45 -060024class MarkStableTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -060025 """Unittests for MarkStable."""
26
27 def setUp(self):
Alex Klein4de25e82019-08-05 15:58:39 -060028 self.uprev = self.PatchObject(packages, 'uprev_android')
LaMont Jones8a1644f2019-04-16 14:30:17 -060029
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 Klein4de25e82019-08-05 15:58:39 -060034 self.input_proto.build_targets.add().name = 'foo'
35 self.input_proto.build_targets.add().name = 'bar'
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +090036 self.input_proto.skip_commit = True
Alex Klein4de25e82019-08-05 15:58:39 -060037
Alex Klein26e472b2020-03-10 14:35:01 -060038 self.build_targets = [build_target_lib.BuildTarget('foo'),
39 build_target_lib.BuildTarget('bar')]
LaMont Jones8a1644f2019-04-16 14:30:17 -060040
Alex Klein231d2da2019-07-22 16:44:45 -060041 self.response = android_pb2.MarkStableResponse()
42
43 def testValidateOnly(self):
44 """Sanity check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -060045 android.MarkStable(self.input_proto, self.response,
46 self.validate_only_config)
Alex Klein4de25e82019-08-05 15:58:39 -060047 self.uprev.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -060048
Michael Mortensen25626442019-11-22 10:06:59 -070049 def testMockCall(self):
50 """Test that a mock call does not execute logic, returns mocked value."""
51 android.MarkStable(self.input_proto, self.response,
52 self.mock_call_config)
53 self.uprev.assert_not_called()
54 self.assertEqual(self.response.status,
55 android_pb2.MARK_STABLE_STATUS_SUCCESS)
56 self.assertEqual(self.response.android_atom.category, 'category')
57 self.assertEqual(self.response.android_atom.package_name,
58 'android-package-name')
59 self.assertEqual(self.response.android_atom.version, '1.2')
60
LaMont Jones8a1644f2019-04-16 14:30:17 -060061 def testFailsIfPackageNameMissing(self):
62 """Fails if package_name is missing."""
63 self.input_proto.package_name = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -060064 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060065 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -060066 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -060067
68 def testFailsIfAndroidBuildBranchMissing(self):
69 """Fails if android_build_branch is missing."""
70 self.input_proto.android_build_branch = ''
LaMont Jones8a1644f2019-04-16 14:30:17 -060071 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060072 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -060073 self.uprev.assert_not_called()
LaMont Jones8a1644f2019-04-16 14:30:17 -060074
75 def testCallsCommandCorrectly(self):
76 """Test that commands.MarkAndroidAsStable is called correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -060077 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -060078 self.uprev.return_value = 'cat/android-1.2.3'
LaMont Jones8a1644f2019-04-16 14:30:17 -060079 atom = common_pb2.PackageInfo()
80 atom.category = 'cat'
81 atom.package_name = 'android'
82 atom.version = '1.2.3'
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_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -060085 tracking_branch=self.input_proto.tracking_branch,
86 android_package=self.input_proto.package_name,
87 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -060088 chroot=mock.ANY,
89 build_targets=self.build_targets,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +090090 android_version=self.input_proto.android_version,
91 skip_commit=self.input_proto.skip_commit,
92 )
Alex Klein231d2da2019-07-22 16:44:45 -060093 self.assertEqual(self.response.android_atom, atom)
94 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -060095 android_pb2.MARK_STABLE_STATUS_SUCCESS)
96
97 def testHandlesEarlyExit(self):
98 """Test that early exit is handled correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -060099 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600100 self.uprev.return_value = ''
Alex Klein231d2da2019-07-22 16:44:45 -0600101 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600102 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600103 tracking_branch=self.input_proto.tracking_branch,
104 android_package=self.input_proto.package_name,
105 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600106 chroot=mock.ANY,
107 build_targets=self.build_targets,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900108 android_version=self.input_proto.android_version,
109 skip_commit=self.input_proto.skip_commit,
110 )
Alex Klein231d2da2019-07-22 16:44:45 -0600111 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600112 android_pb2.MARK_STABLE_STATUS_EARLY_EXIT)
113
114 def testHandlesPinnedUprevError(self):
115 """Test that pinned error is handled correctly."""
LaMont Jones8a1644f2019-04-16 14:30:17 -0600116 self.input_proto.android_version = 'android-version'
Alex Klein4de25e82019-08-05 15:58:39 -0600117 self.uprev.side_effect = packages.AndroidIsPinnedUprevError('pin/xx-1.1')
LaMont Jones8a1644f2019-04-16 14:30:17 -0600118 atom = common_pb2.PackageInfo()
119 atom.category = 'pin'
120 atom.package_name = 'xx'
121 atom.version = '1.1'
Alex Klein231d2da2019-07-22 16:44:45 -0600122 android.MarkStable(self.input_proto, self.response, self.api_config)
Alex Klein4de25e82019-08-05 15:58:39 -0600123 self.uprev.assert_called_once_with(
LaMont Jones8a1644f2019-04-16 14:30:17 -0600124 tracking_branch=self.input_proto.tracking_branch,
125 android_package=self.input_proto.package_name,
126 android_build_branch=self.input_proto.android_build_branch,
Alex Klein4de25e82019-08-05 15:58:39 -0600127 chroot=mock.ANY,
128 build_targets=self.build_targets,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900129 android_version=self.input_proto.android_version,
130 skip_commit=self.input_proto.skip_commit,
131 )
Alex Klein231d2da2019-07-22 16:44:45 -0600132 self.assertEqual(self.response.android_atom, atom)
133 self.assertEqual(self.response.status,
LaMont Jones8a1644f2019-04-16 14:30:17 -0600134 android_pb2.MARK_STABLE_STATUS_PINNED)
135
136
Alex Klein231d2da2019-07-22 16:44:45 -0600137class UnpinVersionTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
LaMont Jones8a1644f2019-04-16 14:30:17 -0600138 """Unittests for UnpinVersion."""
139
140 def testCallsUnlink(self):
141 """SetAndroid calls service with correct args."""
142 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
143 self.PatchObject(constants, '_FindSourceRoot', return_value='SRCROOT')
144
145 # This has the side effect of making sure that input and output proto are
146 # not actually used.
Alex Klein231d2da2019-07-22 16:44:45 -0600147 android.UnpinVersion(None, None, self.api_config)
LaMont Jones8a1644f2019-04-16 14:30:17 -0600148 safeunlink.assert_called_once_with(android.ANDROIDPIN_MASK_PATH)
Alex Klein231d2da2019-07-22 16:44:45 -0600149
150 def testValidateOnly(self):
151 """Sanity check that a validate only call does not execute any logic."""
152 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
153
154 android.UnpinVersion(None, None, self.validate_only_config)
155 safeunlink.assert_not_called()
Michael Mortensen25626442019-11-22 10:06:59 -0700156
157 def testMockCall(self):
158 """Test that a mock call does not execute logic."""
159 safeunlink = self.PatchObject(osutils, 'SafeUnlink')
160
161 android.UnpinVersion(None, None, self.mock_call_config)
162 safeunlink.assert_not_called()
163 # android.UnpinVersion does not modify the response.