blob: 7deeeba9d5c195c23abe2636442737c5281fed8e [file] [log] [blame]
Alex Kleina9d500b2019-04-22 15:37:51 -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"""controller_util unittests."""
7
8from __future__ import print_function
9
10from chromite.api.controller import controller_util
Alex Klein171da612019-08-06 14:00:42 -060011from chromite.api.gen.chromite.api import build_api_test_pb2
Alex Klein26e472b2020-03-10 14:35:01 -060012from chromite.api.gen.chromite.api import sysroot_pb2
Alex Kleina9d500b2019-04-22 15:37:51 -060013from chromite.api.gen.chromiumos import common_pb2
Alex Klein26e472b2020-03-10 14:35:01 -060014from chromite.lib import build_target_lib
Alex Klein18a60af2020-06-11 12:08:47 -060015from chromite.lib import cros_test_lib
16from chromite.lib.parser import package_info
Alex Klein171da612019-08-06 14:00:42 -060017from chromite.lib.chroot_lib import Chroot
18
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
50
51class ParseBuildTargetTest(cros_test_lib.TestCase):
52 """ParseBuildTarget tests."""
53
54 def testSuccess(self):
55 """Test successful handling case."""
56 name = 'board'
57 build_target_message = common_pb2.BuildTarget(name=name)
Alex Klein26e472b2020-03-10 14:35:01 -060058 expected = build_target_lib.BuildTarget(name)
Alex Klein171da612019-08-06 14:00:42 -060059 result = controller_util.ParseBuildTarget(build_target_message)
60
61 self.assertEqual(expected, result)
62
Alex Klein26e472b2020-03-10 14:35:01 -060063 def testParseProfile(self):
64 """Test the parsing of a profile."""
65 name = 'build-target-name'
66 profile = 'profile'
67 build_target_msg = common_pb2.BuildTarget(name=name)
68 profile_msg = sysroot_pb2.Profile(name=profile)
69
70 expected = build_target_lib.BuildTarget(name, profile=profile)
71 result = controller_util.ParseBuildTarget(
72 build_target_msg, profile_message=profile_msg)
73
74 self.assertEqual(expected, result)
75
76
Alex Klein171da612019-08-06 14:00:42 -060077 def testWrongMessage(self):
78 """Test invalid message type given."""
79 with self.assertRaises(AssertionError):
Alex Klein26e472b2020-03-10 14:35:01 -060080 controller_util.ParseBuildTarget(build_api_test_pb2.TestRequestMessage())
Alex Klein171da612019-08-06 14:00:42 -060081
82
83class ParseBuildTargetsTest(cros_test_lib.TestCase):
84 """ParseBuildTargets tests."""
85
86 def testSuccess(self):
87 """Test successful handling case."""
88 names = ['foo', 'bar', 'baz']
89 message = build_api_test_pb2.TestRequestMessage()
90 for name in names:
91 message.build_targets.add().name = name
92
93 result = controller_util.ParseBuildTargets(message.build_targets)
94
Alex Klein26e472b2020-03-10 14:35:01 -060095 expected = [build_target_lib.BuildTarget(name) for name in names]
96 self.assertCountEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060097
98 def testWrongMessage(self):
99 """Wrong message type handling."""
100 message = common_pb2.Chroot()
101 message.env.use_flags.add().flag = 'foo'
102 message.env.use_flags.add().flag = 'bar'
103
104 with self.assertRaises(AssertionError):
105 controller_util.ParseBuildTargets(message.env.use_flags)
Alex Kleina9d500b2019-04-22 15:37:51 -0600106
107
108class CPVToPackageInfoTest(cros_test_lib.TestCase):
109 """CPVToPackageInfo tests."""
110
111 def testAllFields(self):
Alex Kleine1abe2c2019-08-14 10:29:46 -0600112 """Test handling when all fields present."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600113 pi = common_pb2.PackageInfo()
Alex Klein18a60af2020-06-11 12:08:47 -0600114 cpv = package_info.SplitCPV('cat/pkg-2.0.0', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600115
116 controller_util.CPVToPackageInfo(cpv, pi)
117 self.assertEqual('cat', pi.category)
118 self.assertEqual('pkg', pi.package_name)
119 self.assertEqual('2.0.0', pi.version)
120
121 def testNoVersion(self):
Alex Kleine1abe2c2019-08-14 10:29:46 -0600122 """Test handling when no version given."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600123 pi = common_pb2.PackageInfo()
Alex Klein18a60af2020-06-11 12:08:47 -0600124 cpv = package_info.SplitCPV('cat/pkg', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600125
126 controller_util.CPVToPackageInfo(cpv, pi)
127 self.assertEqual('cat', pi.category)
128 self.assertEqual('pkg', pi.package_name)
129 self.assertEqual('', pi.version)
130
131 def testPackageOnly(self):
Alex Kleine1abe2c2019-08-14 10:29:46 -0600132 """Test handling when only given the package name."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600133 pi = common_pb2.PackageInfo()
Alex Klein18a60af2020-06-11 12:08:47 -0600134 cpv = package_info.SplitCPV('pkg', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600135
136 controller_util.CPVToPackageInfo(cpv, pi)
137 self.assertEqual('', pi.category)
138 self.assertEqual('pkg', pi.package_name)
139 self.assertEqual('', pi.version)
140
141
142class PackageInfoToCPVTest(cros_test_lib.TestCase):
143 """PackageInfoToCPV tests."""
144
145 def testAllFields(self):
146 """Quick sanity check it's working properly."""
147 pi = common_pb2.PackageInfo()
148 pi.package_name = 'pkg'
149 pi.category = 'cat'
150 pi.version = '2.0.0'
151
152 cpv = controller_util.PackageInfoToCPV(pi)
153
154 self.assertEqual('pkg', cpv.package)
155 self.assertEqual('cat', cpv.category)
156 self.assertEqual('2.0.0', cpv.version)
157
158 def testNoPackageInfo(self):
159 """Test no package info given."""
160 self.assertIsNone(controller_util.PackageInfoToCPV(None))
161
162 def testNoPackageName(self):
163 """Test no package name given."""
164 pi = common_pb2.PackageInfo()
165 pi.category = 'cat'
166 pi.version = '2.0.0'
167
168 self.assertIsNone(controller_util.PackageInfoToCPV(pi))
169
170
171class PackageInfoToStringTest(cros_test_lib.TestCase):
172 """PackageInfoToString tests."""
173
174 def testAllFields(self):
175 """Test all fields present."""
176 pi = common_pb2.PackageInfo()
177 pi.package_name = 'pkg'
178 pi.category = 'cat'
179 pi.version = '2.0.0'
180
181 cpv_str = controller_util.PackageInfoToString(pi)
182
183 self.assertEqual('cat/pkg-2.0.0', cpv_str)
184
185 def testNoVersion(self):
186 """Test no version provided."""
187 pi = common_pb2.PackageInfo()
188 pi.package_name = 'pkg'
189 pi.category = 'cat'
190
191 cpv_str = controller_util.PackageInfoToString(pi)
192
193 self.assertEqual('cat/pkg', cpv_str)
194
195 def testPackageOnly(self):
196 """Test no version provided."""
197 pi = common_pb2.PackageInfo()
198 pi.package_name = 'pkg'
199
200 cpv_str = controller_util.PackageInfoToString(pi)
201
202 self.assertEqual('pkg', cpv_str)
203
204 def testNoPackageName(self):
205 """Test no package name given."""
206 pi = common_pb2.PackageInfo()
207
208 with self.assertRaises(ValueError):
209 controller_util.PackageInfoToString(pi)
210
211
212class CPVToStringTest(cros_test_lib.TestCase):
213 """CPVToString tests."""
214
215 def testTranslations(self):
216 """Test standard translations used."""
217 cases = [
218 'cat/pkg-2.0.0-r1',
219 'cat/pkg-2.0.0',
220 'cat/pkg',
221 'pkg',
222 ]
223
224 for case in cases:
Alex Klein18a60af2020-06-11 12:08:47 -0600225 cpv = package_info.SplitCPV(case, strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600226 # We should end up with as much info as is available, so we should see
227 # the original value in each case.
228 self.assertEqual(case, controller_util.CPVToString(cpv))
229
230 def testInvalidCPV(self):
231 """Test invalid CPV object."""
Alex Klein18a60af2020-06-11 12:08:47 -0600232 cpv = package_info.SplitCPV('', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600233 with self.assertRaises(ValueError):
234 controller_util.CPVToString(cpv)