blob: bbe69765cc9b159c0ece735c08142b2721612035 [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
31 def __init__(self, success=True):
32 self.success = success
33
34 def get_latest_passed_builds(self, build_config):
35 if not self.success:
Xixuan Wu6ec23e32019-05-23 11:56:02 -070036 raise ValueError('No valid builds')
Xixuan Wuc6819012019-05-23 11:34:59 -070037
38 return build_lib.BuildInfo(board=build_config.split('-')[0],
39 model=None,
40 milestone=TaskTestCase.MILESTONE,
41 platform=TaskTestCase.PLATFORM,
42 build_config=build_config)
43
Xinan Lin318cf752019-07-19 14:50:23 -070044 def get_latest_passed_builds_firmware(self, board):
45 if not self.success:
46 raise ValueError('No valid builds')
47
48 return build_lib.BuildInfo(
49 board=board,
50 model=None,
51 milestone=TaskTestCase.MILESTONE,
52 platform=TaskTestCase.PLATFORM,
53 build_config='%s-firmware' % board)
54
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
189 def testGetFirmwareFromDB(self):
190 """Test get firmware from DB successfully."""
191 firmware = self.task._get_firmware_build(
Xixuan Wuc6819012019-05-23 11:34:59 -0700192 'firmware', self.BOARD, None, FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700193 self.assertEqual(
194 firmware,
195 '%s-firmware/R%s-%s' % (self.BOARD, self.MILESTONE, self.PLATFORM))
196
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700197 def testScheduleCrosSuccessfully(self):
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700198 """test schedule cros builds successfully."""
199 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700200 self.task._schedule_cros_builds(branch_builds,
Xixuan Wu1e42c752019-03-21 13:41:49 -0700201 self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700202 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700203 self.assertEqual(self._mock_push.call_count, 1)
204
205 def testScheduleCrosNonvalidBoard(self):
206 """Test schedule no cros builds due to non-allowed board."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700207 branch_builds = {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700208 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700209 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700210 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700211 self.assertEqual(self._mock_push.call_count, 0)
212
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700213 def testScheduleCrosExcludeBoard(self):
214 """Test schedule no cros builds due to board is excluded."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700215 branch_builds = {(self.EXCLUDE_BOARD, None, 'release', '56'): '0000.00.00'}
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700216 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700217 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700218 FakeBuildClient())
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700219 self.assertEqual(self._mock_push.call_count, 0)
220
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700221 def testScheduleCrosSuccessfullyWithSpecifiedModel(self):
222 """Test schedule unibuild with specific model."""
223 self.task.board = self.UNIBUILD_BOARD
224 branch_builds = {(self.UNIBUILD_BOARD, 'robo', 'release', '56'):
225 '0000.00.00',
226 (self.UNIBUILD_BOARD, 'lava', 'release', '56'):
227 '0000.00.00'}
228 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700229 self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700230 FakeBuildClient())
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700231 self.assertEqual(self._mock_push.call_count, 2)
232
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700233 def testScheduleCrosNonValidSpec(self):
234 """Test schedule no cros builds due to non-allowed branch milestone."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700235 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700236 # Only run tasks whose milestone = tot (R40)
237 self.task.branch_specs = re.split(r'\s*,\s*', '==tot')
238 self.task._set_spec_compare_info()
239 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700240 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700241 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700242 self.assertEqual(self._mock_push.call_count, 0)
243
244 def testScheduleCrosSuccessfullyWithValidFirmware(self):
245 """Test schedule cros builds successfully with valid firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700246 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700247 self.task.firmware_ro_build_spec = 'firmware'
248 self.task._schedule_cros_builds(branch_builds,
Xixuan Wu1e42c752019-03-21 13:41:49 -0700249 self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700250 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700251 self.assertEqual(self._mock_push.call_count, 1)
252
253 def testScheduleCrosWithNonValidFirmware(self):
254 """Test schedule no cros builds due to non-existent firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700255 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700256 self.task.firmware_ro_build_spec = 'firmware'
257 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700258 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700259 FakeBuildClient(success=False))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700260 self.assertEqual(self._mock_push.call_count, 0)
261
262 def testScheduleLaunchControlWithFullBranches(self):
263 """Test schedule all launch control builds successfully."""
264 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
265 'git_nyc-mr1-release/shamu-userdebug/3783920']}
266 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
267 self.task.launch_control_branches = [
268 t.lstrip() for t in lc_branches.split(',')]
269 self.task._schedule_launch_control_builds(lc_builds)
270 self.assertEqual(self._mock_push.call_count, 2)
271
272 def testScheduleLaunchControlWithPartlyBranches(self):
273 """Test schedule part of launch control builds due to branch check."""
274 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
275 'git_nyc-mr1-release/shamu-userdebug/3783920']}
276 lc_branches = 'git_nyc-mr1-release'
277 self.task.launch_control_branches = [
278 t.lstrip() for t in lc_branches.split(',')]
279 self.task._schedule_launch_control_builds(lc_builds)
280 self.assertEqual(self._mock_push.call_count, 1)
281
282 def testScheduleLaunchControlWithNoBranches(self):
283 """Test schedule none of launch control builds due to branch check."""
284 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
285 'git_nyc-mr1-release/shamu-userdebug/3783920']}
286 self.task.launch_control_branches = []
287 self.task._schedule_launch_control_builds(lc_builds)
288 self.assertEqual(self._mock_push.call_count, 0)
289
290 def testScheduleLaunchControlNonvalidBoard(self):
291 """Test schedule none of launch control builds due to board check."""
292 lc_builds = {'%s_2' % self.BOARD:
293 ['git_nyc-mr2-release/shamu-userdebug/3844975',
294 'git_nyc-mr1-release/shamu-userdebug/3783920']}
295 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
296 self.task.launch_control_branches = [
297 t.lstrip() for t in lc_branches.split(',')]
298 self.task._schedule_launch_control_builds(lc_builds)
299 self.assertEqual(self._mock_push.call_count, 0)
Xixuan Wu5451a662017-10-17 10:57:40 -0700300
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700301 def testScheduleLaunchControlExcludeBoard(self):
302 """Test schedule none of launch control builds due to board check."""
303 lc_builds = {self.EXCLUDE_BOARD:
304 ['git_nyc-mr2-release/shamu-userdebug/3844975',
305 'git_nyc-mr1-release/shamu-userdebug/3783920']}
306 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
307 self.task.launch_control_branches = [
308 t.lstrip() for t in lc_branches.split(',')]
309 self.task._schedule_launch_control_builds(lc_builds)
310 self.assertEqual(self._mock_push.call_count, 0)
311
Xixuan Wu5451a662017-10-17 10:57:40 -0700312
313class TaskPushTestCase(TaskBaseTestCase):
314
315 def setUp(self):
316 super(TaskPushTestCase, self).setUp()
317 mock_push = mock.patch('task_executor.push')
318 self._mock_push = mock_push.start()
319 self.addCleanup(mock_push.stop)
320
321 def testScheduleCrOSIsPushSuccessfully(self):
322 """Test IS_PUSHED is changed if some CrOS suites are scheduled."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700323 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu5451a662017-10-17 10:57:40 -0700324 self.task.os_type = build_lib.OS_TYPE_CROS
325 self.assertTrue(self.task.schedule(
Xixuan Wu1e42c752019-03-21 13:41:49 -0700326 [], (branch_builds, []), self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700327 FakeBuildClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700328
329 def testScheduleLaunchControlIsPushSuccessfully(self):
330 """Test IS_PUSHED is changed if some launch control suites are scheduled."""
331 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
332 'git_nyc-mr1-release/shamu-userdebug/3783920']}
333 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
334 self.task.launch_control_branches = [
335 t.lstrip() for t in lc_branches.split(',')]
336 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
337 self.assertTrue(self.task.schedule(
Xixuan Wuc6819012019-05-23 11:34:59 -0700338 lc_builds, ([], []), self._empty_configs, FakeBuildClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700339
340 def testScheduleCrosIsPushInvalidBoard(self):
341 """Test schedule no cros builds due to non-allowed board."""
Craig Bergstrom58263d32018-04-26 14:11:35 -0600342 branch_builds = (
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700343 {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}, {},
Craig Bergstrom58263d32018-04-26 14:11:35 -0600344 )
Xixuan Wu5451a662017-10-17 10:57:40 -0700345 self.task.os_type = build_lib.OS_TYPE_CROS
346 self.assertFalse(self.task.schedule(
Xixuan Wuc6819012019-05-23 11:34:59 -0700347 [], branch_builds, self._empty_configs, FakeBuildClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700348
349 def testScheduleLaunchControlIsPushInvalidBoard(self):
350 """Test schedule none of launch control builds due to board check."""
351 lc_builds = {'%s_2' % self.BOARD:
352 ['git_nyc-mr2-release/shamu-userdebug/3844975',
353 'git_nyc-mr1-release/shamu-userdebug/3783920']}
354 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
355 self.task.launch_control_branches = [
356 t.lstrip() for t in lc_branches.split(',')]
357 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
358 self.task._schedule_launch_control_builds(lc_builds)
359 self.assertFalse(self.task.schedule(
Xixuan Wuc6819012019-05-23 11:34:59 -0700360 lc_builds, ([], []), self._empty_configs, FakeBuildClient()))