blob: c0e89adfc3145a6aeb813a9a75cb2b69ff230fc0 [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
17import tot_manager
18
19
20class FakeTotMilestoneManager(tot_manager.TotMilestoneManager):
21 """Mock class for tot_manager.TotMilestoneManager."""
22
23 def __init__(self, is_sanity):
24 self.is_sanity = is_sanity
25 self.storage_client = None
26 self.tot = self._tot_milestone()
27
28
29class FakeCIDBClient(object):
30 """Mock class for cloud_sql_client.CIDBClient."""
31
32 def __init__(self, success=True):
33 self.success = success
34
35 def get_latest_passed_builds(self, build_config):
36 if not self.success:
37 raise MySQLdb.OperationalError('Failed to connect to db')
38
39 return cloud_sql_client.BuildInfo(board=build_config.split('-')[0],
Xixuan Wu8d2f2862018-08-28 16:48:04 -070040 model=None,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070041 milestone=TaskTestCase.MILESTONE,
42 platform=TaskTestCase.PLATFORM,
43 build_config=build_config)
44
45
Xixuan Wu8d2f2862018-08-28 16:48:04 -070046class FakeLabConfig(object):
Xixuan Wuf4a4c882019-03-15 14:48:26 -070047 """Mock class for config_reader.LabConfig."""
48
49 def get_cros_model_map(self):
50 return {'coral': ['robo', 'nasher', 'lava']}
51
52
53class FakeMigrationConfig(object):
54 """Mock class for config_reader.MigrationConfig."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -070055
56 def get_skylab_board_list(self):
57 return []
58
59 def get_skylab_suite_list(self):
60 return []
61
Xixuan Wu5ff0fac2019-01-07 10:06:35 -080062 def get_skylab_model_map(self):
63 return {}
64
Xixuan Wu8d2f2862018-08-28 16:48:04 -070065 def get_cros_model_map(self):
66 return {'coral': ['robo', 'nasher', 'lava']}
67
68
Xixuan Wu5451a662017-10-17 10:57:40 -070069class TaskBaseTestCase(unittest.TestCase):
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070070
71 BOARD = 'fake_board'
Xixuan Wu8d2f2862018-08-28 16:48:04 -070072 UNIBUILD_BOARD = 'coral'
Po-Hsien Wang6d589732018-05-15 17:19:34 -070073 EXCLUDE_BOARD = 'exclude_fake_board'
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070074 NAME = 'fake_name'
75 SUITE = 'fake_suite'
76 POOL = 'fake_pool'
77 PLATFORM = '6182.0.0-rc2'
78 MILESTONE = '30'
79 NUM = 1
80 PRIORITY = 50
81 TIMEOUT = 600
82
83 def setUp(self):
84 tot_patcher = mock.patch('tot_manager.TotMilestoneManager')
85 self._mock_tot = tot_patcher.start()
86 self.addCleanup(tot_patcher.stop)
87 # Can't pass in False since storage_client is not mocked.
88 self._mock_tot.return_value = FakeTotMilestoneManager(True)
89 self.task = task.Task(config_reader.TaskInfo(
90 name=self.NAME,
91 suite=self.SUITE,
92 branch_specs=[],
93 pool=self.POOL,
94 num=self.NUM,
Xixuan Wu8d2f2862018-08-28 16:48:04 -070095 boards=','.join([self.BOARD, self.UNIBUILD_BOARD]),
Po-Hsien Wang6d589732018-05-15 17:19:34 -070096 exclude_boards=self.EXCLUDE_BOARD,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070097 priority=self.PRIORITY,
98 timeout=self.TIMEOUT))
99
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700100 self._empty_configs = config_reader.Configs(
101 lab_config=config_reader.LabConfig(None),
102 migration_config=config_reader.MigrationConfig(None))
103 self._fake_configs = config_reader.Configs(
104 lab_config=FakeLabConfig(),
105 migration_config=FakeMigrationConfig())
106
Xixuan Wu5451a662017-10-17 10:57:40 -0700107
108class TaskTestCase(TaskBaseTestCase):
109
110 def setUp(self):
111 super(TaskTestCase, self).setUp()
112
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700113 mock_push = mock.patch('task.Task._push_suite')
114 self._mock_push = mock_push.start()
115 self.addCleanup(mock_push.stop)
116
117 def testSetSpecCompareInfoEqual(self):
118 """Test compare info setting for specs that equals to a milestone."""
119 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-2')
120 self.task._set_spec_compare_info()
121 self.assertTrue(self.task._version_equal_constraint)
122 self.assertFalse(self.task._version_gte_constraint)
123 self.assertFalse(self.task._version_lte_constraint)
124 self.assertEqual(self.task._bare_branches, [])
125
126 def testSetSpecCompareInfoLess(self):
127 """Test compare info setting for specs that is less than a milestone."""
128 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot')
129 self.task._set_spec_compare_info()
130 self.assertFalse(self.task._version_equal_constraint)
131 self.assertFalse(self.task._version_gte_constraint)
132 self.assertTrue(self.task._version_lte_constraint)
133 self.assertEqual(self.task._bare_branches, [])
134
135 def testSetSpecCompareInfoGreater(self):
136 """Test compare info setting for specs that is greater than a milestone."""
137 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
138 self.task._set_spec_compare_info()
139 self.assertFalse(self.task._version_equal_constraint)
140 self.assertTrue(self.task._version_gte_constraint)
141 self.assertFalse(self.task._version_lte_constraint)
142 self.assertEqual(self.task._bare_branches, [])
143
144 def testFitsSpecEqual(self):
145 """Test milestone check for specs that equals to a milestone."""
146 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-1')
147 self.task._set_spec_compare_info()
148 self.assertFalse(self.task._fits_spec('40'))
149 self.assertTrue(self.task._fits_spec('39'))
150 self.assertFalse(self.task._fits_spec('38'))
151
152 def testFitsSpecLess(self):
153 """Test milestone check for specs that is less than a milestone."""
154 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot-1')
155 self.task._set_spec_compare_info()
156 self.assertFalse(self.task._fits_spec('40'))
157 self.assertTrue(self.task._fits_spec('39'))
158 self.assertTrue(self.task._fits_spec('38'))
159
160 def testFitsSpecGreater(self):
161 """Test milestone check for specs that is greater than a milestone."""
162 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
163 self.task._set_spec_compare_info()
164 self.assertTrue(self.task._fits_spec('39'))
165 self.assertTrue(self.task._fits_spec('38'))
166 self.assertFalse(self.task._fits_spec('37'))
167
168 def testGetFirmwareFromLabConfig(self):
169 """Test get firmware from lab config successfully."""
170 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
171 return_value='build1,build2'):
172 firmware = self.task._get_firmware_build(
173 'released_ro_2', self.BOARD, config_reader.LabConfig(None), None)
174 self.assertEqual(firmware, 'build2')
175
176 def testGetFirmwareFromLabConfigOutofIndex(self):
177 """Test get firmware from lab config when firmware index is invalid."""
178 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
179 return_value='build1,build2'):
180 self.assertRaises(ValueError,
181 self.task._get_latest_firmware_build_from_lab_config,
182 'released_ro_3',
183 self.BOARD,
184 config_reader.LabConfig(None))
185 self.assertIsNone(self.task._get_firmware_build(
186 'released_ro_3', self.BOARD, config_reader.LabConfig(None), None))
187
188 def testGetFirmwareFromDB(self):
189 """Test get firmware from DB successfully."""
190 firmware = self.task._get_firmware_build(
191 'firmware', self.BOARD, None, FakeCIDBClient())
192 self.assertEqual(
193 firmware,
194 '%s-firmware/R%s-%s' % (self.BOARD, self.MILESTONE, self.PLATFORM))
195
196 def testGetFirmwareFromDBConnectionError(self):
197 """Test get firmware from DB when DB connection is failed."""
198 self.assertIsNone(self.task._get_firmware_build(
199 'firmware', self.BOARD, None, FakeCIDBClient(success=False)))
200
201 def testScheduleCrosSuccessfully(self):
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700202 """test schedule cros builds successfully."""
203 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700204 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700205 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700206 FakeCIDBClient())
207 self.assertEqual(self._mock_push.call_count, 1)
208
209 def testScheduleCrosNonvalidBoard(self):
210 """Test schedule no cros builds due to non-allowed board."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700211 branch_builds = {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700212 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700213 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700214 FakeCIDBClient())
215 self.assertEqual(self._mock_push.call_count, 0)
216
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700217 def testScheduleCrosExcludeBoard(self):
218 """Test schedule no cros builds due to board is excluded."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700219 branch_builds = {(self.EXCLUDE_BOARD, None, 'release', '56'): '0000.00.00'}
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700220 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700221 self._empty_configs,
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700222 FakeCIDBClient())
223 self.assertEqual(self._mock_push.call_count, 0)
224
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700225 def testScheduleCrosSuccessfullyWithSpecifiedModel(self):
226 """Test schedule unibuild with specific model."""
227 self.task.board = self.UNIBUILD_BOARD
228 branch_builds = {(self.UNIBUILD_BOARD, 'robo', 'release', '56'):
229 '0000.00.00',
230 (self.UNIBUILD_BOARD, 'lava', 'release', '56'):
231 '0000.00.00'}
232 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700233 self._fake_configs,
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700234 FakeCIDBClient())
235 self.assertEqual(self._mock_push.call_count, 2)
236
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700237 def testScheduleCrosNonValidSpec(self):
238 """Test schedule no cros builds due to non-allowed branch milestone."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700239 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700240 # Only run tasks whose milestone = tot (R40)
241 self.task.branch_specs = re.split(r'\s*,\s*', '==tot')
242 self.task._set_spec_compare_info()
243 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700244 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700245 FakeCIDBClient())
246 self.assertEqual(self._mock_push.call_count, 0)
247
248 def testScheduleCrosSuccessfullyWithValidFirmware(self):
249 """Test schedule cros builds successfully with valid firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700250 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700251 self.task.firmware_ro_build_spec = 'firmware'
252 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700253 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700254 FakeCIDBClient())
255 self.assertEqual(self._mock_push.call_count, 1)
256
257 def testScheduleCrosWithNonValidFirmware(self):
258 """Test schedule no cros builds due to non-existent firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700259 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700260 self.task.firmware_ro_build_spec = 'firmware'
261 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700262 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700263 FakeCIDBClient(success=False))
264 self.assertEqual(self._mock_push.call_count, 0)
265
266 def testScheduleLaunchControlWithFullBranches(self):
267 """Test schedule all launch control builds successfully."""
268 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
269 'git_nyc-mr1-release/shamu-userdebug/3783920']}
270 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
271 self.task.launch_control_branches = [
272 t.lstrip() for t in lc_branches.split(',')]
273 self.task._schedule_launch_control_builds(lc_builds)
274 self.assertEqual(self._mock_push.call_count, 2)
275
276 def testScheduleLaunchControlWithPartlyBranches(self):
277 """Test schedule part of launch control builds due to branch check."""
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'
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, 1)
285
286 def testScheduleLaunchControlWithNoBranches(self):
287 """Test schedule none 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 self.task.launch_control_branches = []
291 self.task._schedule_launch_control_builds(lc_builds)
292 self.assertEqual(self._mock_push.call_count, 0)
293
294 def testScheduleLaunchControlNonvalidBoard(self):
295 """Test schedule none of launch control builds due to board check."""
296 lc_builds = {'%s_2' % self.BOARD:
297 ['git_nyc-mr2-release/shamu-userdebug/3844975',
298 'git_nyc-mr1-release/shamu-userdebug/3783920']}
299 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
300 self.task.launch_control_branches = [
301 t.lstrip() for t in lc_branches.split(',')]
302 self.task._schedule_launch_control_builds(lc_builds)
303 self.assertEqual(self._mock_push.call_count, 0)
Xixuan Wu5451a662017-10-17 10:57:40 -0700304
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700305 def testScheduleLaunchControlExcludeBoard(self):
306 """Test schedule none of launch control builds due to board check."""
307 lc_builds = {self.EXCLUDE_BOARD:
308 ['git_nyc-mr2-release/shamu-userdebug/3844975',
309 'git_nyc-mr1-release/shamu-userdebug/3783920']}
310 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
311 self.task.launch_control_branches = [
312 t.lstrip() for t in lc_branches.split(',')]
313 self.task._schedule_launch_control_builds(lc_builds)
314 self.assertEqual(self._mock_push.call_count, 0)
315
Xixuan Wu5451a662017-10-17 10:57:40 -0700316
317class TaskPushTestCase(TaskBaseTestCase):
318
319 def setUp(self):
320 super(TaskPushTestCase, self).setUp()
321 mock_push = mock.patch('task_executor.push')
322 self._mock_push = mock_push.start()
323 self.addCleanup(mock_push.stop)
324
325 def testScheduleCrOSIsPushSuccessfully(self):
326 """Test IS_PUSHED is changed if some CrOS suites are scheduled."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700327 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu5451a662017-10-17 10:57:40 -0700328 self.task.os_type = build_lib.OS_TYPE_CROS
329 self.assertTrue(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700330 [], (branch_builds, []), self._empty_configs,
Craig Bergstrom58263d32018-04-26 14:11:35 -0600331 FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700332
333 def testScheduleLaunchControlIsPushSuccessfully(self):
334 """Test IS_PUSHED is changed if some launch control suites are scheduled."""
335 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
336 'git_nyc-mr1-release/shamu-userdebug/3783920']}
337 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
338 self.task.launch_control_branches = [
339 t.lstrip() for t in lc_branches.split(',')]
340 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
341 self.assertTrue(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700342 lc_builds, ([], []), self._empty_configs, FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700343
344 def testScheduleCrosIsPushInvalidBoard(self):
345 """Test schedule no cros builds due to non-allowed board."""
Craig Bergstrom58263d32018-04-26 14:11:35 -0600346 branch_builds = (
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700347 {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}, {},
Craig Bergstrom58263d32018-04-26 14:11:35 -0600348 )
Xixuan Wu5451a662017-10-17 10:57:40 -0700349 self.task.os_type = build_lib.OS_TYPE_CROS
350 self.assertFalse(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700351 [], branch_builds, self._empty_configs, FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700352
353 def testScheduleLaunchControlIsPushInvalidBoard(self):
354 """Test schedule none of launch control builds due to board check."""
355 lc_builds = {'%s_2' % self.BOARD:
356 ['git_nyc-mr2-release/shamu-userdebug/3844975',
357 'git_nyc-mr1-release/shamu-userdebug/3783920']}
358 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
359 self.task.launch_control_branches = [
360 t.lstrip() for t in lc_branches.split(',')]
361 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
362 self.task._schedule_launch_control_builds(lc_builds)
363 self.assertFalse(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700364 lc_builds, ([], []), self._empty_configs, FakeCIDBClient()))