blob: 0786958d6262e0532fd740f7d2eab481e92feef2 [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
Alex Klein18a60af2020-06-11 12:08:47 -060012from chromite.lib import cros_test_lib
13from chromite.lib.parser import package_info
Alex Klein171da612019-08-06 14:00:42 -060014from chromite.lib.chroot_lib import Chroot
15
16
Michael Mortensen9a73c322019-10-03 17:14:37 -060017class ParseChrootTest(cros_test_lib.MockTestCase):
Alex Klein171da612019-08-06 14:00:42 -060018 """ParseChroot tests."""
19
20 def testSuccess(self):
21 """Test successful handling case."""
22 path = '/chroot/path'
23 cache_dir = '/cache/dir'
24 chrome_root = '/chrome/root'
25 use_flags = [{'flag': 'useflag1'}, {'flag': 'useflag2'}]
26 features = [{'feature': 'feature1'}, {'feature': 'feature2'}]
27 expected_env = {'USE': 'useflag1 useflag2',
Alex Kleinb7485bb2019-09-19 13:23:37 -060028 'FEATURES': 'feature1 feature2',
29 'CHROME_ORIGIN': 'LOCAL_SOURCE'}
Alex Klein171da612019-08-06 14:00:42 -060030
31 chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir,
32 chrome_dir=chrome_root,
33 env={'use_flags': use_flags,
34 'features': features})
35
36 expected = Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root,
37 env=expected_env)
38 result = controller_util.ParseChroot(chroot_message)
39
40 self.assertEqual(expected, result)
41
42 def testWrongMessage(self):
43 """Test invalid message type given."""
44 with self.assertRaises(AssertionError):
45 controller_util.ParseChroot(common_pb2.BuildTarget())
46
47
48class ParseBuildTargetTest(cros_test_lib.TestCase):
49 """ParseBuildTarget tests."""
50
51 def testSuccess(self):
52 """Test successful handling case."""
53 name = 'board'
54 build_target_message = common_pb2.BuildTarget(name=name)
Alex Klein26e472b2020-03-10 14:35:01 -060055 expected = build_target_lib.BuildTarget(name)
Alex Klein171da612019-08-06 14:00:42 -060056 result = controller_util.ParseBuildTarget(build_target_message)
57
58 self.assertEqual(expected, result)
59
Alex Klein26e472b2020-03-10 14:35:01 -060060 def testParseProfile(self):
61 """Test the parsing of a profile."""
62 name = 'build-target-name'
63 profile = 'profile'
64 build_target_msg = common_pb2.BuildTarget(name=name)
65 profile_msg = sysroot_pb2.Profile(name=profile)
66
67 expected = build_target_lib.BuildTarget(name, profile=profile)
68 result = controller_util.ParseBuildTarget(
69 build_target_msg, profile_message=profile_msg)
70
71 self.assertEqual(expected, result)
72
73
Alex Klein171da612019-08-06 14:00:42 -060074 def testWrongMessage(self):
75 """Test invalid message type given."""
76 with self.assertRaises(AssertionError):
Alex Klein26e472b2020-03-10 14:35:01 -060077 controller_util.ParseBuildTarget(build_api_test_pb2.TestRequestMessage())
Alex Klein171da612019-08-06 14:00:42 -060078
79
80class ParseBuildTargetsTest(cros_test_lib.TestCase):
81 """ParseBuildTargets tests."""
82
83 def testSuccess(self):
84 """Test successful handling case."""
85 names = ['foo', 'bar', 'baz']
86 message = build_api_test_pb2.TestRequestMessage()
87 for name in names:
88 message.build_targets.add().name = name
89
90 result = controller_util.ParseBuildTargets(message.build_targets)
91
Alex Klein26e472b2020-03-10 14:35:01 -060092 expected = [build_target_lib.BuildTarget(name) for name in names]
93 self.assertCountEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060094
95 def testWrongMessage(self):
96 """Wrong message type handling."""
97 message = common_pb2.Chroot()
98 message.env.use_flags.add().flag = 'foo'
99 message.env.use_flags.add().flag = 'bar'
100
101 with self.assertRaises(AssertionError):
102 controller_util.ParseBuildTargets(message.env.use_flags)
Alex Kleina9d500b2019-04-22 15:37:51 -0600103
104
105class CPVToPackageInfoTest(cros_test_lib.TestCase):
106 """CPVToPackageInfo tests."""
107
108 def testAllFields(self):
Alex Kleine1abe2c2019-08-14 10:29:46 -0600109 """Test handling when all fields present."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600110 pi = common_pb2.PackageInfo()
Alex Klein18a60af2020-06-11 12:08:47 -0600111 cpv = package_info.SplitCPV('cat/pkg-2.0.0', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600112
113 controller_util.CPVToPackageInfo(cpv, pi)
114 self.assertEqual('cat', pi.category)
115 self.assertEqual('pkg', pi.package_name)
116 self.assertEqual('2.0.0', pi.version)
117
118 def testNoVersion(self):
Alex Kleine1abe2c2019-08-14 10:29:46 -0600119 """Test handling when no version given."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600120 pi = common_pb2.PackageInfo()
Alex Klein18a60af2020-06-11 12:08:47 -0600121 cpv = package_info.SplitCPV('cat/pkg', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600122
123 controller_util.CPVToPackageInfo(cpv, pi)
124 self.assertEqual('cat', pi.category)
125 self.assertEqual('pkg', pi.package_name)
126 self.assertEqual('', pi.version)
127
128 def testPackageOnly(self):
Alex Kleine1abe2c2019-08-14 10:29:46 -0600129 """Test handling when only given the package name."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600130 pi = common_pb2.PackageInfo()
Alex Klein18a60af2020-06-11 12:08:47 -0600131 cpv = package_info.SplitCPV('pkg', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600132
133 controller_util.CPVToPackageInfo(cpv, pi)
134 self.assertEqual('', pi.category)
135 self.assertEqual('pkg', pi.package_name)
136 self.assertEqual('', pi.version)
137
138
139class PackageInfoToCPVTest(cros_test_lib.TestCase):
140 """PackageInfoToCPV tests."""
141
142 def testAllFields(self):
143 """Quick sanity check it's working properly."""
144 pi = common_pb2.PackageInfo()
145 pi.package_name = 'pkg'
146 pi.category = 'cat'
147 pi.version = '2.0.0'
148
149 cpv = controller_util.PackageInfoToCPV(pi)
150
151 self.assertEqual('pkg', cpv.package)
152 self.assertEqual('cat', cpv.category)
153 self.assertEqual('2.0.0', cpv.version)
154
155 def testNoPackageInfo(self):
156 """Test no package info given."""
157 self.assertIsNone(controller_util.PackageInfoToCPV(None))
158
159 def testNoPackageName(self):
160 """Test no package name given."""
161 pi = common_pb2.PackageInfo()
162 pi.category = 'cat'
163 pi.version = '2.0.0'
164
165 self.assertIsNone(controller_util.PackageInfoToCPV(pi))
166
167
168class PackageInfoToStringTest(cros_test_lib.TestCase):
169 """PackageInfoToString tests."""
170
171 def testAllFields(self):
172 """Test all fields present."""
173 pi = common_pb2.PackageInfo()
174 pi.package_name = 'pkg'
175 pi.category = 'cat'
176 pi.version = '2.0.0'
177
178 cpv_str = controller_util.PackageInfoToString(pi)
179
180 self.assertEqual('cat/pkg-2.0.0', cpv_str)
181
182 def testNoVersion(self):
183 """Test no version provided."""
184 pi = common_pb2.PackageInfo()
185 pi.package_name = 'pkg'
186 pi.category = 'cat'
187
188 cpv_str = controller_util.PackageInfoToString(pi)
189
190 self.assertEqual('cat/pkg', cpv_str)
191
192 def testPackageOnly(self):
193 """Test no version provided."""
194 pi = common_pb2.PackageInfo()
195 pi.package_name = 'pkg'
196
197 cpv_str = controller_util.PackageInfoToString(pi)
198
199 self.assertEqual('pkg', cpv_str)
200
201 def testNoPackageName(self):
202 """Test no package name given."""
203 pi = common_pb2.PackageInfo()
204
205 with self.assertRaises(ValueError):
206 controller_util.PackageInfoToString(pi)
207
208
209class CPVToStringTest(cros_test_lib.TestCase):
210 """CPVToString tests."""
211
212 def testTranslations(self):
213 """Test standard translations used."""
214 cases = [
215 'cat/pkg-2.0.0-r1',
216 'cat/pkg-2.0.0',
217 'cat/pkg',
218 'pkg',
219 ]
220
221 for case in cases:
Alex Klein18a60af2020-06-11 12:08:47 -0600222 cpv = package_info.SplitCPV(case, strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600223 # We should end up with as much info as is available, so we should see
224 # the original value in each case.
225 self.assertEqual(case, controller_util.CPVToString(cpv))
226
227 def testInvalidCPV(self):
228 """Test invalid CPV object."""
Alex Klein18a60af2020-06-11 12:08:47 -0600229 cpv = package_info.SplitCPV('', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600230 with self.assertRaises(ValueError):
231 controller_util.CPVToString(cpv)
Alex Klein1e68a8e2020-10-06 17:25:11 -0600232
233
234def test_serialize_package_info():
235 pkg_info = package_info.parse('foo/bar-1.2.3-r4')
236 pkg_info_msg = common_pb2.PackageInfo()
237 controller_util.serialize_package_info(pkg_info, pkg_info_msg)
238 assert pkg_info_msg.category == 'foo'
239 assert pkg_info_msg.package_name == 'bar'
240 assert pkg_info_msg.version == '1.2.3-r4'
241
242
243def test_deserialize_package_info():
244 pkg_info_msg = common_pb2.PackageInfo()
245 pkg_info_msg.category = 'foo'
246 pkg_info_msg.package_name = 'bar'
247 pkg_info_msg.version = '1.2.3-r4'
248 pkg_info = controller_util.deserialize_package_info(pkg_info_msg)
249 assert pkg_info.cpvr == 'foo/bar-1.2.3-r4'