blob: 77f191838349e9e7b9334b844d09614af4500019 [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
18
19class FakeTotMilestoneManager(tot_manager.TotMilestoneManager):
20 """Mock class for tot_manager.TotMilestoneManager."""
21
22 def __init__(self, is_sanity):
23 self.is_sanity = is_sanity
24 self.storage_client = None
25 self.tot = self._tot_milestone()
26
27
Xixuan Wuc6819012019-05-23 11:34:59 -070028class FakeBuildClient(object):
29 """Mock rest_client.BuildBucketBigqueryClient."""
30
Xinan Lin39dcca82019-07-26 18:55:51 -070031 def __init__(self, success=True, failed_result=None):
Xixuan Wuc6819012019-05-23 11:34:59 -070032 self.success = success
Xinan Lin39dcca82019-07-26 18:55:51 -070033 self.failed_result = failed_result
34 self.firmware_artifact_cros = ('gs://chromeos-image-archive/'
35 'fake_board-release/'
36 'R30-6182.0.0-rc2')
37 self.firmware_artifact = ('gs://chromeos-image-archive/'
38 'firmware-fake_board-12345.67.A-firmwarebranch/'
39 'RFoo-1.0.0-b1234567')
Xixuan Wuc6819012019-05-23 11:34:59 -070040
Xinan Lin39dcca82019-07-26 18:55:51 -070041 def get_latest_passed_builds_artifact_link(self, build_config):
Xixuan Wuc6819012019-05-23 11:34:59 -070042 if not self.success:
Xixuan Wu6ec23e32019-05-23 11:56:02 -070043 raise ValueError('No valid builds')
Xinan Lin39dcca82019-07-26 18:55:51 -070044 if self.failed_result:
45 return self.failed_result
46 return self.firmware_artifact_cros
Xixuan Wuc6819012019-05-23 11:34:59 -070047
Xinan Lin39dcca82019-07-26 18:55:51 -070048 def get_latest_passed_builds_artifact_link_firmware(self, board):
Xinan Lin318cf752019-07-19 14:50:23 -070049 if not self.success:
50 raise ValueError('No valid builds')
Xinan Lin39dcca82019-07-26 18:55:51 -070051 if self.failed_result:
52 return self.failed_result
53 return self.firmware_artifact
Xinan Lin318cf752019-07-19 14:50:23 -070054
Xixuan Wuc6819012019-05-23 11:34:59 -070055
Xixuan Wu8d2f2862018-08-28 16:48:04 -070056class FakeLabConfig(object):
Xixuan Wuf4a4c882019-03-15 14:48:26 -070057 """Mock class for config_reader.LabConfig."""
58
59 def get_cros_model_map(self):
60 return {'coral': ['robo', 'nasher', 'lava']}
61
62
63class FakeMigrationConfig(object):
64 """Mock class for config_reader.MigrationConfig."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -070065
Xixuan Wu1e42c752019-03-21 13:41:49 -070066 def get_skylab_suites_dump(self):
67 return {}
Xixuan Wu8d2f2862018-08-28 16:48:04 -070068
69
Xixuan Wu5451a662017-10-17 10:57:40 -070070class TaskBaseTestCase(unittest.TestCase):
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070071
72 BOARD = 'fake_board'
Xixuan Wu8d2f2862018-08-28 16:48:04 -070073 UNIBUILD_BOARD = 'coral'
Po-Hsien Wang6d589732018-05-15 17:19:34 -070074 EXCLUDE_BOARD = 'exclude_fake_board'
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070075 NAME = 'fake_name'
76 SUITE = 'fake_suite'
77 POOL = 'fake_pool'
78 PLATFORM = '6182.0.0-rc2'
79 MILESTONE = '30'
80 NUM = 1
81 PRIORITY = 50
82 TIMEOUT = 600
83
84 def setUp(self):
85 tot_patcher = mock.patch('tot_manager.TotMilestoneManager')
86 self._mock_tot = tot_patcher.start()
87 self.addCleanup(tot_patcher.stop)
88 # Can't pass in False since storage_client is not mocked.
89 self._mock_tot.return_value = FakeTotMilestoneManager(True)
Xixuan Wu51bb7102019-03-18 14:51:44 -070090 self.task = task.Task(task_config_reader.TaskInfo(
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070091 name=self.NAME,
92 suite=self.SUITE,
93 branch_specs=[],
94 pool=self.POOL,
95 num=self.NUM,
Xixuan Wu8d2f2862018-08-28 16:48:04 -070096 boards=','.join([self.BOARD, self.UNIBUILD_BOARD]),
Po-Hsien Wang6d589732018-05-15 17:19:34 -070097 exclude_boards=self.EXCLUDE_BOARD,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070098 priority=self.PRIORITY,
99 timeout=self.TIMEOUT))
100
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700101 self._empty_configs = config_reader.Configs(
102 lab_config=config_reader.LabConfig(None),
103 migration_config=config_reader.MigrationConfig(None))
104 self._fake_configs = config_reader.Configs(
105 lab_config=FakeLabConfig(),
106 migration_config=FakeMigrationConfig())
107
Xixuan Wu5451a662017-10-17 10:57:40 -0700108
109class TaskTestCase(TaskBaseTestCase):
110
111 def setUp(self):
112 super(TaskTestCase, self).setUp()
113
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700114 mock_push = mock.patch('task.Task._push_suite')
115 self._mock_push = mock_push.start()
116 self.addCleanup(mock_push.stop)
117
118 def testSetSpecCompareInfoEqual(self):
119 """Test compare info setting for specs that equals to a milestone."""
120 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-2')
121 self.task._set_spec_compare_info()
122 self.assertTrue(self.task._version_equal_constraint)
123 self.assertFalse(self.task._version_gte_constraint)
124 self.assertFalse(self.task._version_lte_constraint)
125 self.assertEqual(self.task._bare_branches, [])
126
127 def testSetSpecCompareInfoLess(self):
128 """Test compare info setting for specs that is less than a milestone."""
129 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot')
130 self.task._set_spec_compare_info()
131 self.assertFalse(self.task._version_equal_constraint)
132 self.assertFalse(self.task._version_gte_constraint)
133 self.assertTrue(self.task._version_lte_constraint)
134 self.assertEqual(self.task._bare_branches, [])
135
136 def testSetSpecCompareInfoGreater(self):
137 """Test compare info setting for specs that is greater than a milestone."""
138 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
139 self.task._set_spec_compare_info()
140 self.assertFalse(self.task._version_equal_constraint)
141 self.assertTrue(self.task._version_gte_constraint)
142 self.assertFalse(self.task._version_lte_constraint)
143 self.assertEqual(self.task._bare_branches, [])
144
145 def testFitsSpecEqual(self):
146 """Test milestone check for specs that equals to a milestone."""
147 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-1')
148 self.task._set_spec_compare_info()
149 self.assertFalse(self.task._fits_spec('40'))
150 self.assertTrue(self.task._fits_spec('39'))
151 self.assertFalse(self.task._fits_spec('38'))
152
153 def testFitsSpecLess(self):
154 """Test milestone check for specs that is less than a milestone."""
155 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot-1')
156 self.task._set_spec_compare_info()
157 self.assertFalse(self.task._fits_spec('40'))
158 self.assertTrue(self.task._fits_spec('39'))
159 self.assertTrue(self.task._fits_spec('38'))
160
161 def testFitsSpecGreater(self):
162 """Test milestone check for specs that is greater than a milestone."""
163 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
164 self.task._set_spec_compare_info()
165 self.assertTrue(self.task._fits_spec('39'))
166 self.assertTrue(self.task._fits_spec('38'))
167 self.assertFalse(self.task._fits_spec('37'))
168
169 def testGetFirmwareFromLabConfig(self):
170 """Test get firmware from lab config successfully."""
171 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
172 return_value='build1,build2'):
173 firmware = self.task._get_firmware_build(
174 'released_ro_2', self.BOARD, config_reader.LabConfig(None), None)
175 self.assertEqual(firmware, 'build2')
176
177 def testGetFirmwareFromLabConfigOutofIndex(self):
178 """Test get firmware from lab config when firmware index is invalid."""
179 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
180 return_value='build1,build2'):
181 self.assertRaises(ValueError,
182 self.task._get_latest_firmware_build_from_lab_config,
183 'released_ro_3',
184 self.BOARD,
185 config_reader.LabConfig(None))
186 self.assertIsNone(self.task._get_firmware_build(
187 'released_ro_3', self.BOARD, config_reader.LabConfig(None), None))
188
Xinan Lin39dcca82019-07-26 18:55:51 -0700189 def testGetFirmwareFromDBSuccessfully(self):
190 """Test get firmware from BuildBucket successfully."""
191
192 firmware = self.task._get_firmware_build(
193 'cros', self.BOARD, None, FakeBuildClient())
194 self.assertEqual(
195 firmware,
196 '{0}-release/R{1}-{2}'.format(self.BOARD,
197 self.MILESTONE,
198 self.PLATFORM))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700199 firmware = self.task._get_firmware_build(
Xixuan Wuc6819012019-05-23 11:34:59 -0700200 'firmware', self.BOARD, None, FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700201 self.assertEqual(
202 firmware,
Xinan Lin39dcca82019-07-26 18:55:51 -0700203 'firmware-{0}-12345.67.A-firmwarebranch/'
204 'RFoo-1.0.0-b1234567/{0}'.format(self.BOARD))
205
206 def testGetFirmwareFromDBFailure(self):
207 """Test get invalid firmware build from BuildBucket."""
208
209 # This should raise a ValueError due to the unexpected prefix.
210 foo_artifact_link = ('gs://chromeos-image-foo/'
211 'firmware-fake_board-12345.67.A-firmwarebranch/'
212 'RFoo-1.0.0-b1234567')
213 self.assertRaises(ValueError, self.task._get_firmware_build(
214 'firmware',
215 self.BOARD,
216 None,
217 FakeBuildClient(failed_result=foo_artifact_link)))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700218
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700219 def testScheduleCrosSuccessfully(self):
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700220 """test schedule cros builds successfully."""
221 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700222 self.task._schedule_cros_builds(branch_builds,
Xixuan Wu1e42c752019-03-21 13:41:49 -0700223 self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700224 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700225 self.assertEqual(self._mock_push.call_count, 1)
226
227 def testScheduleCrosNonvalidBoard(self):
228 """Test schedule no cros builds due to non-allowed board."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700229 branch_builds = {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700230 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700231 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700232 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700233 self.assertEqual(self._mock_push.call_count, 0)
234
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700235 def testScheduleCrosExcludeBoard(self):
236 """Test schedule no cros builds due to board is excluded."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700237 branch_builds = {(self.EXCLUDE_BOARD, None, 'release', '56'): '0000.00.00'}
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700238 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700239 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700240 FakeBuildClient())
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700241 self.assertEqual(self._mock_push.call_count, 0)
242
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700243 def testScheduleCrosSuccessfullyWithSpecifiedModel(self):
244 """Test schedule unibuild with specific model."""
245 self.task.board = self.UNIBUILD_BOARD
246 branch_builds = {(self.UNIBUILD_BOARD, 'robo', 'release', '56'):
247 '0000.00.00',
248 (self.UNIBUILD_BOARD, 'lava', 'release', '56'):
249 '0000.00.00'}
250 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700251 self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700252 FakeBuildClient())
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700253 self.assertEqual(self._mock_push.call_count, 2)
254
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700255 def testScheduleCrosNonValidSpec(self):
256 """Test schedule no cros builds due to non-allowed branch milestone."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700257 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700258 # Only run tasks whose milestone = tot (R40)
259 self.task.branch_specs = re.split(r'\s*,\s*', '==tot')
260 self.task._set_spec_compare_info()
261 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700262 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700263 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700264 self.assertEqual(self._mock_push.call_count, 0)
265
266 def testScheduleCrosSuccessfullyWithValidFirmware(self):
267 """Test schedule cros builds successfully with valid firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700268 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700269 self.task.firmware_ro_build_spec = 'firmware'
270 self.task._schedule_cros_builds(branch_builds,
Xixuan Wu1e42c752019-03-21 13:41:49 -0700271 self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700272 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700273 self.assertEqual(self._mock_push.call_count, 1)
274
275 def testScheduleCrosWithNonValidFirmware(self):
276 """Test schedule no cros builds due to non-existent firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700277 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700278 self.task.firmware_ro_build_spec = 'firmware'
279 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700280 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700281 FakeBuildClient(success=False))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700282 self.assertEqual(self._mock_push.call_count, 0)
283
284 def testScheduleLaunchControlWithFullBranches(self):
285 """Test schedule all launch control builds successfully."""
286 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
287 'git_nyc-mr1-release/shamu-userdebug/3783920']}
288 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
289 self.task.launch_control_branches = [
290 t.lstrip() for t in lc_branches.split(',')]
291 self.task._schedule_launch_control_builds(lc_builds)
292 self.assertEqual(self._mock_push.call_count, 2)
293
294 def testScheduleLaunchControlWithPartlyBranches(self):
295 """Test schedule part of launch control builds due to branch check."""
296 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
297 'git_nyc-mr1-release/shamu-userdebug/3783920']}
298 lc_branches = 'git_nyc-mr1-release'
299 self.task.launch_control_branches = [
300 t.lstrip() for t in lc_branches.split(',')]
301 self.task._schedule_launch_control_builds(lc_builds)
302 self.assertEqual(self._mock_push.call_count, 1)
303
304 def testScheduleLaunchControlWithNoBranches(self):
305 """Test schedule none of launch control builds due to branch check."""
306 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
307 'git_nyc-mr1-release/shamu-userdebug/3783920']}
308 self.task.launch_control_branches = []
309 self.task._schedule_launch_control_builds(lc_builds)
310 self.assertEqual(self._mock_push.call_count, 0)
311
312 def testScheduleLaunchControlNonvalidBoard(self):
313 """Test schedule none of launch control builds due to board check."""
314 lc_builds = {'%s_2' % self.BOARD:
315 ['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._schedule_launch_control_builds(lc_builds)
321 self.assertEqual(self._mock_push.call_count, 0)
Xixuan Wu5451a662017-10-17 10:57:40 -0700322
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700323 def testScheduleLaunchControlExcludeBoard(self):
324 """Test schedule none of launch control builds due to board check."""
325 lc_builds = {self.EXCLUDE_BOARD:
326 ['git_nyc-mr2-release/shamu-userdebug/3844975',
327 'git_nyc-mr1-release/shamu-userdebug/3783920']}
328 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
329 self.task.launch_control_branches = [
330 t.lstrip() for t in lc_branches.split(',')]
331 self.task._schedule_launch_control_builds(lc_builds)
332 self.assertEqual(self._mock_push.call_count, 0)
333
Xixuan Wu5451a662017-10-17 10:57:40 -0700334
335class TaskPushTestCase(TaskBaseTestCase):
336
337 def setUp(self):
338 super(TaskPushTestCase, self).setUp()
339 mock_push = mock.patch('task_executor.push')
340 self._mock_push = mock_push.start()
341 self.addCleanup(mock_push.stop)
342
343 def testScheduleCrOSIsPushSuccessfully(self):
344 """Test IS_PUSHED is changed if some CrOS suites are scheduled."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700345 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu5451a662017-10-17 10:57:40 -0700346 self.task.os_type = build_lib.OS_TYPE_CROS
347 self.assertTrue(self.task.schedule(
Xixuan Wu1e42c752019-03-21 13:41:49 -0700348 [], (branch_builds, []), self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700349 FakeBuildClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700350
351 def testScheduleLaunchControlIsPushSuccessfully(self):
352 """Test IS_PUSHED is changed if some launch control suites are scheduled."""
353 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
354 'git_nyc-mr1-release/shamu-userdebug/3783920']}
355 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
356 self.task.launch_control_branches = [
357 t.lstrip() for t in lc_branches.split(',')]
358 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
359 self.assertTrue(self.task.schedule(
Xixuan Wuc6819012019-05-23 11:34:59 -0700360 lc_builds, ([], []), self._empty_configs, FakeBuildClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700361
362 def testScheduleCrosIsPushInvalidBoard(self):
363 """Test schedule no cros builds due to non-allowed board."""
Craig Bergstrom58263d32018-04-26 14:11:35 -0600364 branch_builds = (
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700365 {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}, {},
Craig Bergstrom58263d32018-04-26 14:11:35 -0600366 )
Xixuan Wu5451a662017-10-17 10:57:40 -0700367 self.task.os_type = build_lib.OS_TYPE_CROS
368 self.assertFalse(self.task.schedule(
Xixuan Wuc6819012019-05-23 11:34:59 -0700369 [], branch_builds, self._empty_configs, FakeBuildClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700370
371 def testScheduleLaunchControlIsPushInvalidBoard(self):
372 """Test schedule none of launch control builds due to board check."""
373 lc_builds = {'%s_2' % self.BOARD:
374 ['git_nyc-mr2-release/shamu-userdebug/3844975',
375 'git_nyc-mr1-release/shamu-userdebug/3783920']}
376 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
377 self.task.launch_control_branches = [
378 t.lstrip() for t in lc_branches.split(',')]
379 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
380 self.task._schedule_launch_control_builds(lc_builds)
381 self.assertFalse(self.task.schedule(
Xixuan Wuc6819012019-05-23 11:34:59 -0700382 lc_builds, ([], []), self._empty_configs, FakeBuildClient()))