blob: def308066c6ebeb1c67e86cd2198e35036ba2688 [file] [log] [blame]
Alex Kleina9d500b2019-04-22 15:37:51 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""controller_util unittests."""
6
Alex Kleina9d500b2019-04-22 15:37:51 -06007from chromite.api.controller import controller_util
Alex Klein171da612019-08-06 14:00:42 -06008from chromite.api.gen.chromite.api import build_api_test_pb2
Alex Klein26e472b2020-03-10 14:35:01 -06009from chromite.api.gen.chromite.api import sysroot_pb2
Alex Kleina9d500b2019-04-22 15:37:51 -060010from chromite.api.gen.chromiumos import common_pb2
Alex Klein26e472b2020-03-10 14:35:01 -060011from chromite.lib import build_target_lib
Lizzy Presland29e62452022-01-05 21:58:21 +000012from chromite.lib import cros_build_lib
Alex Klein18a60af2020-06-11 12:08:47 -060013from chromite.lib import cros_test_lib
14from chromite.lib.parser import package_info
Alex Klein171da612019-08-06 14:00:42 -060015from chromite.lib.chroot_lib import Chroot
Lizzy Preslandfebffa72022-02-24 23:38:58 +000016from chromite.lib.sysroot_lib import Sysroot
17from chromite.lib.sysroot_lib import PackageInstallError
Alex Klein171da612019-08-06 14:00:42 -060018
19
Michael Mortensen9a73c322019-10-03 17:14:37 -060020class ParseChrootTest(cros_test_lib.MockTestCase):
Alex Klein171da612019-08-06 14:00:42 -060021 """ParseChroot tests."""
22
23 def testSuccess(self):
24 """Test successful handling case."""
25 path = '/chroot/path'
26 cache_dir = '/cache/dir'
27 chrome_root = '/chrome/root'
28 use_flags = [{'flag': 'useflag1'}, {'flag': 'useflag2'}]
29 features = [{'feature': 'feature1'}, {'feature': 'feature2'}]
30 expected_env = {'USE': 'useflag1 useflag2',
Alex Kleinb7485bb2019-09-19 13:23:37 -060031 'FEATURES': 'feature1 feature2',
32 'CHROME_ORIGIN': 'LOCAL_SOURCE'}
Alex Klein171da612019-08-06 14:00:42 -060033
34 chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir,
35 chrome_dir=chrome_root,
36 env={'use_flags': use_flags,
37 'features': features})
38
39 expected = Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root,
40 env=expected_env)
41 result = controller_util.ParseChroot(chroot_message)
42
43 self.assertEqual(expected, result)
44
45 def testWrongMessage(self):
46 """Test invalid message type given."""
47 with self.assertRaises(AssertionError):
48 controller_util.ParseChroot(common_pb2.BuildTarget())
49
George Engelbrechtc9a8e812021-06-16 18:14:17 -060050class ParseSysrootTest(cros_test_lib.MockTestCase):
51 """ParseSysroot tests."""
52
53 def testSuccess(self):
54 """test successful handling case."""
55 path = '/build/rare_pokemon'
56 sysroot_message = sysroot_pb2.Sysroot(path=path)
Lizzy Preslandfebffa72022-02-24 23:38:58 +000057 expected = Sysroot(path=path)
George Engelbrechtc9a8e812021-06-16 18:14:17 -060058 result = controller_util.ParseSysroot(sysroot_message)
59 self.assertEqual(expected, result)
60
61 def testWrongMessage(self):
62 with self.assertRaises(AssertionError):
63 controller_util.ParseSysroot(common_pb2.BuildTarget())
Alex Klein171da612019-08-06 14:00:42 -060064
65class ParseBuildTargetTest(cros_test_lib.TestCase):
66 """ParseBuildTarget tests."""
67
68 def testSuccess(self):
69 """Test successful handling case."""
70 name = 'board'
71 build_target_message = common_pb2.BuildTarget(name=name)
Alex Klein26e472b2020-03-10 14:35:01 -060072 expected = build_target_lib.BuildTarget(name)
Alex Klein171da612019-08-06 14:00:42 -060073 result = controller_util.ParseBuildTarget(build_target_message)
74
75 self.assertEqual(expected, result)
76
Alex Klein26e472b2020-03-10 14:35:01 -060077 def testParseProfile(self):
78 """Test the parsing of a profile."""
79 name = 'build-target-name'
80 profile = 'profile'
81 build_target_msg = common_pb2.BuildTarget(name=name)
82 profile_msg = sysroot_pb2.Profile(name=profile)
83
84 expected = build_target_lib.BuildTarget(name, profile=profile)
85 result = controller_util.ParseBuildTarget(
86 build_target_msg, profile_message=profile_msg)
87
88 self.assertEqual(expected, result)
89
90
Alex Klein171da612019-08-06 14:00:42 -060091 def testWrongMessage(self):
92 """Test invalid message type given."""
93 with self.assertRaises(AssertionError):
Alex Klein26e472b2020-03-10 14:35:01 -060094 controller_util.ParseBuildTarget(build_api_test_pb2.TestRequestMessage())
Alex Klein171da612019-08-06 14:00:42 -060095
96
97class ParseBuildTargetsTest(cros_test_lib.TestCase):
98 """ParseBuildTargets tests."""
99
100 def testSuccess(self):
101 """Test successful handling case."""
102 names = ['foo', 'bar', 'baz']
103 message = build_api_test_pb2.TestRequestMessage()
104 for name in names:
105 message.build_targets.add().name = name
106
107 result = controller_util.ParseBuildTargets(message.build_targets)
108
Alex Klein26e472b2020-03-10 14:35:01 -0600109 expected = [build_target_lib.BuildTarget(name) for name in names]
110 self.assertCountEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -0600111
112 def testWrongMessage(self):
113 """Wrong message type handling."""
114 message = common_pb2.Chroot()
115 message.env.use_flags.add().flag = 'foo'
116 message.env.use_flags.add().flag = 'bar'
117
118 with self.assertRaises(AssertionError):
119 controller_util.ParseBuildTargets(message.env.use_flags)
Alex Kleina9d500b2019-04-22 15:37:51 -0600120
121
Alex Kleina9d500b2019-04-22 15:37:51 -0600122class PackageInfoToCPVTest(cros_test_lib.TestCase):
123 """PackageInfoToCPV tests."""
124
125 def testAllFields(self):
126 """Quick sanity check it's working properly."""
127 pi = common_pb2.PackageInfo()
128 pi.package_name = 'pkg'
129 pi.category = 'cat'
130 pi.version = '2.0.0'
131
132 cpv = controller_util.PackageInfoToCPV(pi)
133
134 self.assertEqual('pkg', cpv.package)
135 self.assertEqual('cat', cpv.category)
136 self.assertEqual('2.0.0', cpv.version)
137
138 def testNoPackageInfo(self):
139 """Test no package info given."""
140 self.assertIsNone(controller_util.PackageInfoToCPV(None))
141
142 def testNoPackageName(self):
143 """Test no package name given."""
144 pi = common_pb2.PackageInfo()
145 pi.category = 'cat'
146 pi.version = '2.0.0'
147
148 self.assertIsNone(controller_util.PackageInfoToCPV(pi))
149
150
151class PackageInfoToStringTest(cros_test_lib.TestCase):
152 """PackageInfoToString tests."""
153
154 def testAllFields(self):
155 """Test all fields present."""
156 pi = common_pb2.PackageInfo()
157 pi.package_name = 'pkg'
158 pi.category = 'cat'
159 pi.version = '2.0.0'
160
161 cpv_str = controller_util.PackageInfoToString(pi)
162
163 self.assertEqual('cat/pkg-2.0.0', cpv_str)
164
165 def testNoVersion(self):
166 """Test no version provided."""
167 pi = common_pb2.PackageInfo()
168 pi.package_name = 'pkg'
169 pi.category = 'cat'
170
171 cpv_str = controller_util.PackageInfoToString(pi)
172
173 self.assertEqual('cat/pkg', cpv_str)
174
175 def testPackageOnly(self):
176 """Test no version provided."""
177 pi = common_pb2.PackageInfo()
178 pi.package_name = 'pkg'
179
180 cpv_str = controller_util.PackageInfoToString(pi)
181
182 self.assertEqual('pkg', cpv_str)
183
184 def testNoPackageName(self):
185 """Test no package name given."""
186 pi = common_pb2.PackageInfo()
187
188 with self.assertRaises(ValueError):
189 controller_util.PackageInfoToString(pi)
190
191
192class CPVToStringTest(cros_test_lib.TestCase):
193 """CPVToString tests."""
194
195 def testTranslations(self):
196 """Test standard translations used."""
197 cases = [
198 'cat/pkg-2.0.0-r1',
199 'cat/pkg-2.0.0',
200 'cat/pkg',
201 'pkg',
202 ]
203
204 for case in cases:
Alex Klein18a60af2020-06-11 12:08:47 -0600205 cpv = package_info.SplitCPV(case, strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600206 # We should end up with as much info as is available, so we should see
207 # the original value in each case.
208 self.assertEqual(case, controller_util.CPVToString(cpv))
209
210 def testInvalidCPV(self):
211 """Test invalid CPV object."""
Alex Klein18a60af2020-06-11 12:08:47 -0600212 cpv = package_info.SplitCPV('', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600213 with self.assertRaises(ValueError):
214 controller_util.CPVToString(cpv)
Alex Klein1e68a8e2020-10-06 17:25:11 -0600215
216
217def test_serialize_package_info():
218 pkg_info = package_info.parse('foo/bar-1.2.3-r4')
219 pkg_info_msg = common_pb2.PackageInfo()
220 controller_util.serialize_package_info(pkg_info, pkg_info_msg)
221 assert pkg_info_msg.category == 'foo'
222 assert pkg_info_msg.package_name == 'bar'
223 assert pkg_info_msg.version == '1.2.3-r4'
224
225
226def test_deserialize_package_info():
227 pkg_info_msg = common_pb2.PackageInfo()
228 pkg_info_msg.category = 'foo'
229 pkg_info_msg.package_name = 'bar'
230 pkg_info_msg.version = '1.2.3-r4'
231 pkg_info = controller_util.deserialize_package_info(pkg_info_msg)
232 assert pkg_info.cpvr == 'foo/bar-1.2.3-r4'
Lizzy Presland29e62452022-01-05 21:58:21 +0000233
234
Lizzy Preslandfebffa72022-02-24 23:38:58 +0000235def test_retrieve_package_log_paths():
236 error = PackageInstallError(
Lizzy Presland29e62452022-01-05 21:58:21 +0000237 msg='Failed to install 3 packages',
238 result=cros_build_lib.CommandResult(),
239 packages=[package_info.parse('foo/bar%d-1.0-r1' % num)
240 for num in range(1, 4)])
241 output_proto = sysroot_pb2.InstallPackagesResponse()
Lizzy Preslandfebffa72022-02-24 23:38:58 +0000242 target_sysroot = Sysroot(path='/path/to/sysroot')
Lizzy Presland29e62452022-01-05 21:58:21 +0000243 controller_util.retrieve_package_log_paths(error,
244 output_proto,
Lizzy Preslandfebffa72022-02-24 23:38:58 +0000245 target_sysroot)
Lizzy Presland29e62452022-01-05 21:58:21 +0000246 assert len(output_proto.failed_package_data) == 3