blob: 4029570b62ff628800a6a5bfe7dcbfa5bbbefc8d [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 cloud_sql_client
13import config_reader
14import mock
15import MySQLdb
16import task
Xixuan Wu51bb7102019-03-18 14:51:44 -070017import task_config_reader
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070018import tot_manager
19
20
21class FakeTotMilestoneManager(tot_manager.TotMilestoneManager):
22 """Mock class for tot_manager.TotMilestoneManager."""
23
24 def __init__(self, is_sanity):
25 self.is_sanity = is_sanity
26 self.storage_client = None
27 self.tot = self._tot_milestone()
28
29
Xixuan Wuc6819012019-05-23 11:34:59 -070030# TODO(xixuan): to be deprecated and deleted.
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070031class FakeCIDBClient(object):
32 """Mock class for cloud_sql_client.CIDBClient."""
33
34 def __init__(self, success=True):
35 self.success = success
36
37 def get_latest_passed_builds(self, build_config):
38 if not self.success:
39 raise MySQLdb.OperationalError('Failed to connect to db')
40
41 return cloud_sql_client.BuildInfo(board=build_config.split('-')[0],
Xixuan Wu8d2f2862018-08-28 16:48:04 -070042 model=None,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070043 milestone=TaskTestCase.MILESTONE,
44 platform=TaskTestCase.PLATFORM,
45 build_config=build_config)
46
47
Xixuan Wuc6819012019-05-23 11:34:59 -070048class FakeBuildClient(object):
49 """Mock rest_client.BuildBucketBigqueryClient."""
50
51 def __init__(self, success=True):
52 self.success = success
53
54 def get_latest_passed_builds(self, build_config):
55 if not self.success:
56 raise MySQLdb.OperationalError('Failed to connect to db')
57
58 return build_lib.BuildInfo(board=build_config.split('-')[0],
59 model=None,
60 milestone=TaskTestCase.MILESTONE,
61 platform=TaskTestCase.PLATFORM,
62 build_config=build_config)
63
64
Xixuan Wu8d2f2862018-08-28 16:48:04 -070065class FakeLabConfig(object):
Xixuan Wuf4a4c882019-03-15 14:48:26 -070066 """Mock class for config_reader.LabConfig."""
67
68 def get_cros_model_map(self):
69 return {'coral': ['robo', 'nasher', 'lava']}
70
71
72class FakeMigrationConfig(object):
73 """Mock class for config_reader.MigrationConfig."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -070074
Xixuan Wu1e42c752019-03-21 13:41:49 -070075 def get_skylab_suites_dump(self):
76 return {}
Xixuan Wu8d2f2862018-08-28 16:48:04 -070077
78
Xixuan Wu5451a662017-10-17 10:57:40 -070079class TaskBaseTestCase(unittest.TestCase):
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070080
81 BOARD = 'fake_board'
Xixuan Wu8d2f2862018-08-28 16:48:04 -070082 UNIBUILD_BOARD = 'coral'
Po-Hsien Wang6d589732018-05-15 17:19:34 -070083 EXCLUDE_BOARD = 'exclude_fake_board'
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070084 NAME = 'fake_name'
85 SUITE = 'fake_suite'
86 POOL = 'fake_pool'
87 PLATFORM = '6182.0.0-rc2'
88 MILESTONE = '30'
89 NUM = 1
90 PRIORITY = 50
91 TIMEOUT = 600
92
93 def setUp(self):
94 tot_patcher = mock.patch('tot_manager.TotMilestoneManager')
95 self._mock_tot = tot_patcher.start()
96 self.addCleanup(tot_patcher.stop)
97 # Can't pass in False since storage_client is not mocked.
98 self._mock_tot.return_value = FakeTotMilestoneManager(True)
Xixuan Wu51bb7102019-03-18 14:51:44 -070099 self.task = task.Task(task_config_reader.TaskInfo(
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700100 name=self.NAME,
101 suite=self.SUITE,
102 branch_specs=[],
103 pool=self.POOL,
104 num=self.NUM,
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700105 boards=','.join([self.BOARD, self.UNIBUILD_BOARD]),
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700106 exclude_boards=self.EXCLUDE_BOARD,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700107 priority=self.PRIORITY,
108 timeout=self.TIMEOUT))
109
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700110 self._empty_configs = config_reader.Configs(
111 lab_config=config_reader.LabConfig(None),
112 migration_config=config_reader.MigrationConfig(None))
113 self._fake_configs = config_reader.Configs(
114 lab_config=FakeLabConfig(),
115 migration_config=FakeMigrationConfig())
116
Xixuan Wu5451a662017-10-17 10:57:40 -0700117
118class TaskTestCase(TaskBaseTestCase):
119
120 def setUp(self):
121 super(TaskTestCase, self).setUp()
122
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700123 mock_push = mock.patch('task.Task._push_suite')
124 self._mock_push = mock_push.start()
125 self.addCleanup(mock_push.stop)
126
127 def testSetSpecCompareInfoEqual(self):
128 """Test compare info setting for specs that equals to a milestone."""
129 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-2')
130 self.task._set_spec_compare_info()
131 self.assertTrue(self.task._version_equal_constraint)
132 self.assertFalse(self.task._version_gte_constraint)
133 self.assertFalse(self.task._version_lte_constraint)
134 self.assertEqual(self.task._bare_branches, [])
135
136 def testSetSpecCompareInfoLess(self):
137 """Test compare info setting for specs that is less than a milestone."""
138 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot')
139 self.task._set_spec_compare_info()
140 self.assertFalse(self.task._version_equal_constraint)
141 self.assertFalse(self.task._version_gte_constraint)
142 self.assertTrue(self.task._version_lte_constraint)
143 self.assertEqual(self.task._bare_branches, [])
144
145 def testSetSpecCompareInfoGreater(self):
146 """Test compare info setting for specs that is greater than a milestone."""
147 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
148 self.task._set_spec_compare_info()
149 self.assertFalse(self.task._version_equal_constraint)
150 self.assertTrue(self.task._version_gte_constraint)
151 self.assertFalse(self.task._version_lte_constraint)
152 self.assertEqual(self.task._bare_branches, [])
153
154 def testFitsSpecEqual(self):
155 """Test milestone check for specs that equals to a milestone."""
156 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-1')
157 self.task._set_spec_compare_info()
158 self.assertFalse(self.task._fits_spec('40'))
159 self.assertTrue(self.task._fits_spec('39'))
160 self.assertFalse(self.task._fits_spec('38'))
161
162 def testFitsSpecLess(self):
163 """Test milestone check for specs that is less than a milestone."""
164 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot-1')
165 self.task._set_spec_compare_info()
166 self.assertFalse(self.task._fits_spec('40'))
167 self.assertTrue(self.task._fits_spec('39'))
168 self.assertTrue(self.task._fits_spec('38'))
169
170 def testFitsSpecGreater(self):
171 """Test milestone check for specs that is greater than a milestone."""
172 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
173 self.task._set_spec_compare_info()
174 self.assertTrue(self.task._fits_spec('39'))
175 self.assertTrue(self.task._fits_spec('38'))
176 self.assertFalse(self.task._fits_spec('37'))
177
178 def testGetFirmwareFromLabConfig(self):
179 """Test get firmware from lab config successfully."""
180 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
181 return_value='build1,build2'):
182 firmware = self.task._get_firmware_build(
183 'released_ro_2', self.BOARD, config_reader.LabConfig(None), None)
184 self.assertEqual(firmware, 'build2')
185
186 def testGetFirmwareFromLabConfigOutofIndex(self):
187 """Test get firmware from lab config when firmware index is invalid."""
188 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
189 return_value='build1,build2'):
190 self.assertRaises(ValueError,
191 self.task._get_latest_firmware_build_from_lab_config,
192 'released_ro_3',
193 self.BOARD,
194 config_reader.LabConfig(None))
195 self.assertIsNone(self.task._get_firmware_build(
196 'released_ro_3', self.BOARD, config_reader.LabConfig(None), None))
197
198 def testGetFirmwareFromDB(self):
199 """Test get firmware from DB successfully."""
200 firmware = self.task._get_firmware_build(
Xixuan Wuc6819012019-05-23 11:34:59 -0700201 'firmware', self.BOARD, None, FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700202 self.assertEqual(
203 firmware,
204 '%s-firmware/R%s-%s' % (self.BOARD, self.MILESTONE, self.PLATFORM))
205
206 def testGetFirmwareFromDBConnectionError(self):
207 """Test get firmware from DB when DB connection is failed."""
208 self.assertIsNone(self.task._get_firmware_build(
Xixuan Wuc6819012019-05-23 11:34:59 -0700209 'firmware', self.BOARD, None, FakeBuildClient(success=False)))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700210
211 def testScheduleCrosSuccessfully(self):
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700212 """test schedule cros builds successfully."""
213 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700214 self.task._schedule_cros_builds(branch_builds,
Xixuan Wu1e42c752019-03-21 13:41:49 -0700215 self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700216 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700217 self.assertEqual(self._mock_push.call_count, 1)
218
219 def testScheduleCrosNonvalidBoard(self):
220 """Test schedule no cros builds due to non-allowed board."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700221 branch_builds = {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700222 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700223 self._empty_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, 0)
226
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700227 def testScheduleCrosExcludeBoard(self):
228 """Test schedule no cros builds due to board is excluded."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700229 branch_builds = {(self.EXCLUDE_BOARD, None, 'release', '56'): '0000.00.00'}
Po-Hsien Wang6d589732018-05-15 17:19:34 -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())
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700233 self.assertEqual(self._mock_push.call_count, 0)
234
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700235 def testScheduleCrosSuccessfullyWithSpecifiedModel(self):
236 """Test schedule unibuild with specific model."""
237 self.task.board = self.UNIBUILD_BOARD
238 branch_builds = {(self.UNIBUILD_BOARD, 'robo', 'release', '56'):
239 '0000.00.00',
240 (self.UNIBUILD_BOARD, 'lava', 'release', '56'):
241 '0000.00.00'}
242 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700243 self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700244 FakeBuildClient())
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700245 self.assertEqual(self._mock_push.call_count, 2)
246
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700247 def testScheduleCrosNonValidSpec(self):
248 """Test schedule no cros builds due to non-allowed branch milestone."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700249 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700250 # Only run tasks whose milestone = tot (R40)
251 self.task.branch_specs = re.split(r'\s*,\s*', '==tot')
252 self.task._set_spec_compare_info()
253 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700254 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700255 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700256 self.assertEqual(self._mock_push.call_count, 0)
257
258 def testScheduleCrosSuccessfullyWithValidFirmware(self):
259 """Test schedule cros builds successfully with valid firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700260 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700261 self.task.firmware_ro_build_spec = 'firmware'
262 self.task._schedule_cros_builds(branch_builds,
Xixuan Wu1e42c752019-03-21 13:41:49 -0700263 self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700264 FakeBuildClient())
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700265 self.assertEqual(self._mock_push.call_count, 1)
266
267 def testScheduleCrosWithNonValidFirmware(self):
268 """Test schedule no cros builds due to non-existent firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700269 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700270 self.task.firmware_ro_build_spec = 'firmware'
271 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700272 self._empty_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700273 FakeBuildClient(success=False))
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700274 self.assertEqual(self._mock_push.call_count, 0)
275
276 def testScheduleLaunchControlWithFullBranches(self):
277 """Test schedule all launch control builds successfully."""
278 lc_builds = {self.BOARD: ['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, 2)
285
286 def testScheduleLaunchControlWithPartlyBranches(self):
287 """Test schedule part of launch control builds due to branch check."""
288 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
289 'git_nyc-mr1-release/shamu-userdebug/3783920']}
290 lc_branches = 'git_nyc-mr1-release'
291 self.task.launch_control_branches = [
292 t.lstrip() for t in lc_branches.split(',')]
293 self.task._schedule_launch_control_builds(lc_builds)
294 self.assertEqual(self._mock_push.call_count, 1)
295
296 def testScheduleLaunchControlWithNoBranches(self):
297 """Test schedule none of launch control builds due to branch check."""
298 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
299 'git_nyc-mr1-release/shamu-userdebug/3783920']}
300 self.task.launch_control_branches = []
301 self.task._schedule_launch_control_builds(lc_builds)
302 self.assertEqual(self._mock_push.call_count, 0)
303
304 def testScheduleLaunchControlNonvalidBoard(self):
305 """Test schedule none of launch control builds due to board check."""
306 lc_builds = {'%s_2' % self.BOARD:
307 ['git_nyc-mr2-release/shamu-userdebug/3844975',
308 'git_nyc-mr1-release/shamu-userdebug/3783920']}
309 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
310 self.task.launch_control_branches = [
311 t.lstrip() for t in lc_branches.split(',')]
312 self.task._schedule_launch_control_builds(lc_builds)
313 self.assertEqual(self._mock_push.call_count, 0)
Xixuan Wu5451a662017-10-17 10:57:40 -0700314
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700315 def testScheduleLaunchControlExcludeBoard(self):
316 """Test schedule none of launch control builds due to board check."""
317 lc_builds = {self.EXCLUDE_BOARD:
318 ['git_nyc-mr2-release/shamu-userdebug/3844975',
319 'git_nyc-mr1-release/shamu-userdebug/3783920']}
320 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
321 self.task.launch_control_branches = [
322 t.lstrip() for t in lc_branches.split(',')]
323 self.task._schedule_launch_control_builds(lc_builds)
324 self.assertEqual(self._mock_push.call_count, 0)
325
Xixuan Wu5451a662017-10-17 10:57:40 -0700326
327class TaskPushTestCase(TaskBaseTestCase):
328
329 def setUp(self):
330 super(TaskPushTestCase, self).setUp()
331 mock_push = mock.patch('task_executor.push')
332 self._mock_push = mock_push.start()
333 self.addCleanup(mock_push.stop)
334
335 def testScheduleCrOSIsPushSuccessfully(self):
336 """Test IS_PUSHED is changed if some CrOS suites are scheduled."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700337 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu5451a662017-10-17 10:57:40 -0700338 self.task.os_type = build_lib.OS_TYPE_CROS
339 self.assertTrue(self.task.schedule(
Xixuan Wu1e42c752019-03-21 13:41:49 -0700340 [], (branch_builds, []), self._fake_configs,
Xixuan Wuc6819012019-05-23 11:34:59 -0700341 FakeBuildClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700342
343 def testScheduleLaunchControlIsPushSuccessfully(self):
344 """Test IS_PUSHED is changed if some launch control suites are scheduled."""
345 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
346 'git_nyc-mr1-release/shamu-userdebug/3783920']}
347 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
348 self.task.launch_control_branches = [
349 t.lstrip() for t in lc_branches.split(',')]
350 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
351 self.assertTrue(self.task.schedule(
Xixuan Wuc6819012019-05-23 11:34:59 -0700352 lc_builds, ([], []), self._empty_configs, FakeBuildClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700353
354 def testScheduleCrosIsPushInvalidBoard(self):
355 """Test schedule no cros builds due to non-allowed board."""
Craig Bergstrom58263d32018-04-26 14:11:35 -0600356 branch_builds = (
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700357 {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}, {},
Craig Bergstrom58263d32018-04-26 14:11:35 -0600358 )
Xixuan Wu5451a662017-10-17 10:57:40 -0700359 self.task.os_type = build_lib.OS_TYPE_CROS
360 self.assertFalse(self.task.schedule(
Xixuan Wuc6819012019-05-23 11:34:59 -0700361 [], branch_builds, self._empty_configs, FakeBuildClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700362
363 def testScheduleLaunchControlIsPushInvalidBoard(self):
364 """Test schedule none of launch control builds due to board check."""
365 lc_builds = {'%s_2' % self.BOARD:
366 ['git_nyc-mr2-release/shamu-userdebug/3844975',
367 'git_nyc-mr1-release/shamu-userdebug/3783920']}
368 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
369 self.task.launch_control_branches = [
370 t.lstrip() for t in lc_branches.split(',')]
371 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
372 self.task._schedule_launch_control_builds(lc_builds)
373 self.assertFalse(self.task.schedule(
Xixuan Wuc6819012019-05-23 11:34:59 -0700374 lc_builds, ([], []), self._empty_configs, FakeBuildClient()))