blob: 9b66c7441fc0d2cba43c13d7a8703ae14c800c4a [file] [log] [blame]
Xixuan Wu0c76d5b2017-08-30 16:40:17 -07001# Copyright 2017 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"""Module for task unittests."""
Xixuan Wu5ff0fac2019-01-07 10:06:35 -08006# pylint: disable=g-missing-super-call
Xixuan Wu0c76d5b2017-08-30 16:40:17 -07007
8import re
9import unittest
10
Xixuan Wu5451a662017-10-17 10:57:40 -070011import build_lib
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070012import config_reader
13import mock
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070014import task
Xixuan Wu51bb7102019-03-18 14:51:44 -070015import task_config_reader
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070016import tot_manager
17
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070018class FakeTotMilestoneManager(tot_manager.TotMilestoneManager):
19 """Mock class for tot_manager.TotMilestoneManager."""
20
21 def __init__(self, is_sanity):
22 self.is_sanity = is_sanity
23 self.storage_client = None
24 self.tot = self._tot_milestone()
25
26
Xixuan Wu8d2f2862018-08-28 16:48:04 -070027class FakeLabConfig(object):
Xixuan Wuf4a4c882019-03-15 14:48:26 -070028 """Mock class for config_reader.LabConfig."""
29
30 def get_cros_model_map(self):
31 return {'coral': ['robo', 'nasher', 'lava']}
32
33
Xixuan Wu5451a662017-10-17 10:57:40 -070034class TaskBaseTestCase(unittest.TestCase):
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070035
36 BOARD = 'fake_board'
Xixuan Wu8d2f2862018-08-28 16:48:04 -070037 UNIBUILD_BOARD = 'coral'
Po-Hsien Wang6d589732018-05-15 17:19:34 -070038 EXCLUDE_BOARD = 'exclude_fake_board'
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070039 NAME = 'fake_name'
40 SUITE = 'fake_suite'
41 POOL = 'fake_pool'
42 PLATFORM = '6182.0.0-rc2'
43 MILESTONE = '30'
44 NUM = 1
45 PRIORITY = 50
46 TIMEOUT = 600
47
48 def setUp(self):
49 tot_patcher = mock.patch('tot_manager.TotMilestoneManager')
50 self._mock_tot = tot_patcher.start()
51 self.addCleanup(tot_patcher.stop)
52 # Can't pass in False since storage_client is not mocked.
53 self._mock_tot.return_value = FakeTotMilestoneManager(True)
Xixuan Wu51bb7102019-03-18 14:51:44 -070054 self.task = task.Task(task_config_reader.TaskInfo(
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070055 name=self.NAME,
56 suite=self.SUITE,
57 branch_specs=[],
58 pool=self.POOL,
59 num=self.NUM,
Xixuan Wu8d2f2862018-08-28 16:48:04 -070060 boards=','.join([self.BOARD, self.UNIBUILD_BOARD]),
Po-Hsien Wang6d589732018-05-15 17:19:34 -070061 exclude_boards=self.EXCLUDE_BOARD,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070062 priority=self.PRIORITY,
63 timeout=self.TIMEOUT))
64
Xixuan Wuf4a4c882019-03-15 14:48:26 -070065 self._empty_configs = config_reader.Configs(
Xinan Lin576022d2020-01-21 18:12:47 -080066 lab_config=config_reader.LabConfig(None))
Xixuan Wuf4a4c882019-03-15 14:48:26 -070067 self._fake_configs = config_reader.Configs(
Xinan Lin576022d2020-01-21 18:12:47 -080068 lab_config=FakeLabConfig())
Xixuan Wuf4a4c882019-03-15 14:48:26 -070069
Xinan Lin028f9582019-12-11 10:55:33 -080070 self.firmware_dict = {
71 ('cros', 'fake_board'): ('fake_board-release/R30-6182.0.0-rc2'),
72 ('firmware', 'fake_board'): ('firmware-fake_board-12345.67.'
73 'A-firmwarebranch/RFoo-1.0.0-b1234567'
74 '/fake_board')
75 }
76
Xixuan Wu5451a662017-10-17 10:57:40 -070077
78class TaskTestCase(TaskBaseTestCase):
79
80 def setUp(self):
81 super(TaskTestCase, self).setUp()
82
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070083 mock_push = mock.patch('task.Task._push_suite')
84 self._mock_push = mock_push.start()
85 self.addCleanup(mock_push.stop)
86
87 def testSetSpecCompareInfoEqual(self):
88 """Test compare info setting for specs that equals to a milestone."""
89 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-2')
90 self.task._set_spec_compare_info()
91 self.assertTrue(self.task._version_equal_constraint)
92 self.assertFalse(self.task._version_gte_constraint)
93 self.assertFalse(self.task._version_lte_constraint)
94 self.assertEqual(self.task._bare_branches, [])
95
96 def testSetSpecCompareInfoLess(self):
97 """Test compare info setting for specs that is less than a milestone."""
98 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot')
99 self.task._set_spec_compare_info()
100 self.assertFalse(self.task._version_equal_constraint)
101 self.assertFalse(self.task._version_gte_constraint)
102 self.assertTrue(self.task._version_lte_constraint)
103 self.assertEqual(self.task._bare_branches, [])
104
105 def testSetSpecCompareInfoGreater(self):
106 """Test compare info setting for specs that is greater than a milestone."""
107 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
108 self.task._set_spec_compare_info()
109 self.assertFalse(self.task._version_equal_constraint)
110 self.assertTrue(self.task._version_gte_constraint)
111 self.assertFalse(self.task._version_lte_constraint)
112 self.assertEqual(self.task._bare_branches, [])
113
114 def testFitsSpecEqual(self):
115 """Test milestone check for specs that equals to a milestone."""
116 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-1')
117 self.task._set_spec_compare_info()
118 self.assertFalse(self.task._fits_spec('40'))
119 self.assertTrue(self.task._fits_spec('39'))
120 self.assertFalse(self.task._fits_spec('38'))
121
122 def testFitsSpecLess(self):
123 """Test milestone check for specs that is less than a milestone."""
124 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot-1')
125 self.task._set_spec_compare_info()
126 self.assertFalse(self.task._fits_spec('40'))
127 self.assertTrue(self.task._fits_spec('39'))
128 self.assertTrue(self.task._fits_spec('38'))
129
130 def testFitsSpecGreater(self):
131 """Test milestone check for specs that is greater than a milestone."""
132 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
133 self.task._set_spec_compare_info()
134 self.assertTrue(self.task._fits_spec('39'))
135 self.assertTrue(self.task._fits_spec('38'))
136 self.assertFalse(self.task._fits_spec('37'))
137
138 def testGetFirmwareFromLabConfig(self):
139 """Test get firmware from lab config successfully."""
140 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
141 return_value='build1,build2'):
142 firmware = self.task._get_firmware_build(
Xinan Lin028f9582019-12-11 10:55:33 -0800143 'released_ro_2', self.BOARD, self.firmware_dict,
144 config_reader.LabConfig(None))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700145 self.assertEqual(firmware, 'build2')
146
147 def testGetFirmwareFromLabConfigOutofIndex(self):
148 """Test get firmware from lab config when firmware index is invalid."""
149 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
150 return_value='build1,build2'):
151 self.assertRaises(ValueError,
152 self.task._get_latest_firmware_build_from_lab_config,
153 'released_ro_3',
154 self.BOARD,
155 config_reader.LabConfig(None))
156 self.assertIsNone(self.task._get_firmware_build(
Xinan Lin028f9582019-12-11 10:55:33 -0800157 'released_ro_3', self.BOARD, self.firmware_dict,
158 config_reader.LabConfig(None)))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700159
Xinan Lin028f9582019-12-11 10:55:33 -0800160 def testGetFirmwareSuccessfully(self):
161 """Test get firmware from firmware_dict successfully."""
Xinan Lin39dcca82019-07-26 18:55:51 -0700162
163 firmware = self.task._get_firmware_build(
Xinan Lin028f9582019-12-11 10:55:33 -0800164 'cros', self.BOARD, self.firmware_dict, None)
Xinan Lin39dcca82019-07-26 18:55:51 -0700165 self.assertEqual(
166 firmware,
167 '{0}-release/R{1}-{2}'.format(self.BOARD,
168 self.MILESTONE,
169 self.PLATFORM))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700170 firmware = self.task._get_firmware_build(
Xinan Lin028f9582019-12-11 10:55:33 -0800171 'firmware', self.BOARD, self.firmware_dict, None)
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700172 self.assertEqual(
173 firmware,
Xinan Lin39dcca82019-07-26 18:55:51 -0700174 'firmware-{0}-12345.67.A-firmwarebranch/'
175 'RFoo-1.0.0-b1234567/{0}'.format(self.BOARD))
176
Xinan Lin028f9582019-12-11 10:55:33 -0800177 def testGetNonExistentBoard(self):
178 """Test get firmware for non-existent board from firmware_dict."""
179 self.assertIsNone(self.task._get_firmware_build(
180 'firmware', 'non-existent', self.firmware_dict, None))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700181
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700182 def testScheduleCrosSuccessfully(self):
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700183 """test schedule cros builds successfully."""
184 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700185 self.task._schedule_cros_builds(branch_builds,
Xinan Lin028f9582019-12-11 10:55:33 -0800186 self.firmware_dict,
187 self._fake_configs)
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700188 self.assertEqual(self._mock_push.call_count, 1)
189
190 def testScheduleCrosNonvalidBoard(self):
191 """Test schedule no cros builds due to non-allowed board."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700192 branch_builds = {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700193 self.task._schedule_cros_builds(branch_builds,
Xinan Lin028f9582019-12-11 10:55:33 -0800194 self.firmware_dict,
195 self._empty_configs)
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700196 self.assertEqual(self._mock_push.call_count, 0)
197
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700198 def testScheduleCrosExcludeBoard(self):
199 """Test schedule no cros builds due to board is excluded."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700200 branch_builds = {(self.EXCLUDE_BOARD, None, 'release', '56'): '0000.00.00'}
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700201 self.task._schedule_cros_builds(branch_builds,
Xinan Lin028f9582019-12-11 10:55:33 -0800202 self.firmware_dict,
203 self._empty_configs)
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700204 self.assertEqual(self._mock_push.call_count, 0)
205
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700206 def testScheduleCrosSuccessfullyWithSpecifiedModel(self):
207 """Test schedule unibuild with specific model."""
208 self.task.board = self.UNIBUILD_BOARD
209 branch_builds = {(self.UNIBUILD_BOARD, 'robo', 'release', '56'):
210 '0000.00.00',
211 (self.UNIBUILD_BOARD, 'lava', 'release', '56'):
212 '0000.00.00'}
213 self.task._schedule_cros_builds(branch_builds,
Xinan Lin028f9582019-12-11 10:55:33 -0800214 self.firmware_dict,
215 self._fake_configs)
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700216 self.assertEqual(self._mock_push.call_count, 2)
217
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700218 def testScheduleCrosNonValidSpec(self):
219 """Test schedule no cros builds due to non-allowed branch milestone."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700220 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700221 # Only run tasks whose milestone = tot (R40)
222 self.task.branch_specs = re.split(r'\s*,\s*', '==tot')
223 self.task._set_spec_compare_info()
224 self.task._schedule_cros_builds(branch_builds,
Xinan Lin028f9582019-12-11 10:55:33 -0800225 self.firmware_dict,
226 self._empty_configs)
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700227 self.assertEqual(self._mock_push.call_count, 0)
228
229 def testScheduleCrosSuccessfullyWithValidFirmware(self):
230 """Test schedule cros builds successfully with valid firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700231 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700232 self.task.firmware_ro_build_spec = 'firmware'
233 self.task._schedule_cros_builds(branch_builds,
Xinan Lin028f9582019-12-11 10:55:33 -0800234 self.firmware_dict,
235 self._fake_configs)
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700236 self.assertEqual(self._mock_push.call_count, 1)
237
238 def testScheduleCrosWithNonValidFirmware(self):
239 """Test schedule no cros builds due to non-existent firmware."""
Xinan Lin028f9582019-12-11 10:55:33 -0800240 branch_builds = {('foo-board', None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700241 self.task.firmware_ro_build_spec = 'firmware'
242 self.task._schedule_cros_builds(branch_builds,
Xinan Lin028f9582019-12-11 10:55:33 -0800243 self.firmware_dict,
244 self._empty_configs)
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700245 self.assertEqual(self._mock_push.call_count, 0)
246
247 def testScheduleLaunchControlWithFullBranches(self):
248 """Test schedule all launch control builds successfully."""
249 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
250 'git_nyc-mr1-release/shamu-userdebug/3783920']}
251 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
252 self.task.launch_control_branches = [
253 t.lstrip() for t in lc_branches.split(',')]
254 self.task._schedule_launch_control_builds(lc_builds)
255 self.assertEqual(self._mock_push.call_count, 2)
256
257 def testScheduleLaunchControlWithPartlyBranches(self):
258 """Test schedule part of launch control builds due to branch check."""
259 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
260 'git_nyc-mr1-release/shamu-userdebug/3783920']}
261 lc_branches = 'git_nyc-mr1-release'
262 self.task.launch_control_branches = [
263 t.lstrip() for t in lc_branches.split(',')]
264 self.task._schedule_launch_control_builds(lc_builds)
265 self.assertEqual(self._mock_push.call_count, 1)
266
267 def testScheduleLaunchControlWithNoBranches(self):
268 """Test schedule none of launch control builds due to branch check."""
269 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
270 'git_nyc-mr1-release/shamu-userdebug/3783920']}
271 self.task.launch_control_branches = []
272 self.task._schedule_launch_control_builds(lc_builds)
273 self.assertEqual(self._mock_push.call_count, 0)
274
275 def testScheduleLaunchControlNonvalidBoard(self):
276 """Test schedule none of launch control builds due to board check."""
277 lc_builds = {'%s_2' % self.BOARD:
278 ['git_nyc-mr2-release/shamu-userdebug/3844975',
279 'git_nyc-mr1-release/shamu-userdebug/3783920']}
280 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
281 self.task.launch_control_branches = [
282 t.lstrip() for t in lc_branches.split(',')]
283 self.task._schedule_launch_control_builds(lc_builds)
284 self.assertEqual(self._mock_push.call_count, 0)
Xixuan Wu5451a662017-10-17 10:57:40 -0700285
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700286 def testScheduleLaunchControlExcludeBoard(self):
287 """Test schedule none of launch control builds due to board check."""
288 lc_builds = {self.EXCLUDE_BOARD:
289 ['git_nyc-mr2-release/shamu-userdebug/3844975',
290 'git_nyc-mr1-release/shamu-userdebug/3783920']}
291 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
292 self.task.launch_control_branches = [
293 t.lstrip() for t in lc_branches.split(',')]
294 self.task._schedule_launch_control_builds(lc_builds)
295 self.assertEqual(self._mock_push.call_count, 0)
296
Xixuan Wu5451a662017-10-17 10:57:40 -0700297
298class TaskPushTestCase(TaskBaseTestCase):
299
300 def setUp(self):
301 super(TaskPushTestCase, self).setUp()
302 mock_push = mock.patch('task_executor.push')
303 self._mock_push = mock_push.start()
304 self.addCleanup(mock_push.stop)
305
306 def testScheduleCrOSIsPushSuccessfully(self):
307 """Test IS_PUSHED is changed if some CrOS suites are scheduled."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700308 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu5451a662017-10-17 10:57:40 -0700309 self.task.os_type = build_lib.OS_TYPE_CROS
310 self.assertTrue(self.task.schedule(
Xinan Lin028f9582019-12-11 10:55:33 -0800311 [], (branch_builds, []), self.firmware_dict, self._fake_configs))
Xixuan Wu5451a662017-10-17 10:57:40 -0700312
313 def testScheduleLaunchControlIsPushSuccessfully(self):
314 """Test IS_PUSHED is changed if some launch control suites are scheduled."""
315 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
316 'git_nyc-mr1-release/shamu-userdebug/3783920']}
317 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
318 self.task.launch_control_branches = [
319 t.lstrip() for t in lc_branches.split(',')]
320 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
321 self.assertTrue(self.task.schedule(
Xinan Lin028f9582019-12-11 10:55:33 -0800322 lc_builds, ([], []), self.firmware_dict,
323 self._empty_configs))
Xixuan Wu5451a662017-10-17 10:57:40 -0700324
325 def testScheduleCrosIsPushInvalidBoard(self):
326 """Test schedule no cros builds due to non-allowed board."""
Craig Bergstrom58263d32018-04-26 14:11:35 -0600327 branch_builds = (
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700328 {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}, {},
Craig Bergstrom58263d32018-04-26 14:11:35 -0600329 )
Xixuan Wu5451a662017-10-17 10:57:40 -0700330 self.task.os_type = build_lib.OS_TYPE_CROS
331 self.assertFalse(self.task.schedule(
Xinan Lin028f9582019-12-11 10:55:33 -0800332 [], branch_builds, self.firmware_dict, self._empty_configs))
Xixuan Wu5451a662017-10-17 10:57:40 -0700333
334 def testScheduleLaunchControlIsPushInvalidBoard(self):
335 """Test schedule none of launch control builds due to board check."""
336 lc_builds = {'%s_2' % self.BOARD:
337 ['git_nyc-mr2-release/shamu-userdebug/3844975',
338 'git_nyc-mr1-release/shamu-userdebug/3783920']}
339 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
340 self.task.launch_control_branches = [
341 t.lstrip() for t in lc_branches.split(',')]
342 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
343 self.task._schedule_launch_control_builds(lc_builds)
344 self.assertFalse(self.task.schedule(
Xinan Lin028f9582019-12-11 10:55:33 -0800345 lc_builds, ([], []), self.firmware_dict,
346 self._empty_configs))