blob: 9687d0b051d6625b7212c2b64588f9f70a2edb8a [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
30class FakeCIDBClient(object):
31 """Mock class for cloud_sql_client.CIDBClient."""
32
33 def __init__(self, success=True):
34 self.success = success
35
36 def get_latest_passed_builds(self, build_config):
37 if not self.success:
38 raise MySQLdb.OperationalError('Failed to connect to db')
39
40 return cloud_sql_client.BuildInfo(board=build_config.split('-')[0],
Xixuan Wu8d2f2862018-08-28 16:48:04 -070041 model=None,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070042 milestone=TaskTestCase.MILESTONE,
43 platform=TaskTestCase.PLATFORM,
44 build_config=build_config)
45
46
Xixuan Wu8d2f2862018-08-28 16:48:04 -070047class FakeLabConfig(object):
Xixuan Wuf4a4c882019-03-15 14:48:26 -070048 """Mock class for config_reader.LabConfig."""
49
50 def get_cros_model_map(self):
51 return {'coral': ['robo', 'nasher', 'lava']}
52
53
54class FakeMigrationConfig(object):
55 """Mock class for config_reader.MigrationConfig."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -070056
Xixuan Wu1e42c752019-03-21 13:41:49 -070057 def get_skylab_suites_dump(self):
58 return {}
Xixuan Wu8d2f2862018-08-28 16:48:04 -070059
60
Xixuan Wu5451a662017-10-17 10:57:40 -070061class TaskBaseTestCase(unittest.TestCase):
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070062
63 BOARD = 'fake_board'
Xixuan Wu8d2f2862018-08-28 16:48:04 -070064 UNIBUILD_BOARD = 'coral'
Po-Hsien Wang6d589732018-05-15 17:19:34 -070065 EXCLUDE_BOARD = 'exclude_fake_board'
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070066 NAME = 'fake_name'
67 SUITE = 'fake_suite'
68 POOL = 'fake_pool'
69 PLATFORM = '6182.0.0-rc2'
70 MILESTONE = '30'
71 NUM = 1
72 PRIORITY = 50
73 TIMEOUT = 600
74
75 def setUp(self):
76 tot_patcher = mock.patch('tot_manager.TotMilestoneManager')
77 self._mock_tot = tot_patcher.start()
78 self.addCleanup(tot_patcher.stop)
79 # Can't pass in False since storage_client is not mocked.
80 self._mock_tot.return_value = FakeTotMilestoneManager(True)
Xixuan Wu51bb7102019-03-18 14:51:44 -070081 self.task = task.Task(task_config_reader.TaskInfo(
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070082 name=self.NAME,
83 suite=self.SUITE,
84 branch_specs=[],
85 pool=self.POOL,
86 num=self.NUM,
Xixuan Wu8d2f2862018-08-28 16:48:04 -070087 boards=','.join([self.BOARD, self.UNIBUILD_BOARD]),
Po-Hsien Wang6d589732018-05-15 17:19:34 -070088 exclude_boards=self.EXCLUDE_BOARD,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070089 priority=self.PRIORITY,
90 timeout=self.TIMEOUT))
91
Xixuan Wuf4a4c882019-03-15 14:48:26 -070092 self._empty_configs = config_reader.Configs(
93 lab_config=config_reader.LabConfig(None),
94 migration_config=config_reader.MigrationConfig(None))
95 self._fake_configs = config_reader.Configs(
96 lab_config=FakeLabConfig(),
97 migration_config=FakeMigrationConfig())
98
Xixuan Wu5451a662017-10-17 10:57:40 -070099
100class TaskTestCase(TaskBaseTestCase):
101
102 def setUp(self):
103 super(TaskTestCase, self).setUp()
104
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700105 mock_push = mock.patch('task.Task._push_suite')
106 self._mock_push = mock_push.start()
107 self.addCleanup(mock_push.stop)
108
109 def testSetSpecCompareInfoEqual(self):
110 """Test compare info setting for specs that equals to a milestone."""
111 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-2')
112 self.task._set_spec_compare_info()
113 self.assertTrue(self.task._version_equal_constraint)
114 self.assertFalse(self.task._version_gte_constraint)
115 self.assertFalse(self.task._version_lte_constraint)
116 self.assertEqual(self.task._bare_branches, [])
117
118 def testSetSpecCompareInfoLess(self):
119 """Test compare info setting for specs that is less than a milestone."""
120 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot')
121 self.task._set_spec_compare_info()
122 self.assertFalse(self.task._version_equal_constraint)
123 self.assertFalse(self.task._version_gte_constraint)
124 self.assertTrue(self.task._version_lte_constraint)
125 self.assertEqual(self.task._bare_branches, [])
126
127 def testSetSpecCompareInfoGreater(self):
128 """Test compare info setting for specs that is greater than a milestone."""
129 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
130 self.task._set_spec_compare_info()
131 self.assertFalse(self.task._version_equal_constraint)
132 self.assertTrue(self.task._version_gte_constraint)
133 self.assertFalse(self.task._version_lte_constraint)
134 self.assertEqual(self.task._bare_branches, [])
135
136 def testFitsSpecEqual(self):
137 """Test milestone check for specs that equals to a milestone."""
138 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-1')
139 self.task._set_spec_compare_info()
140 self.assertFalse(self.task._fits_spec('40'))
141 self.assertTrue(self.task._fits_spec('39'))
142 self.assertFalse(self.task._fits_spec('38'))
143
144 def testFitsSpecLess(self):
145 """Test milestone check for specs that is less than 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.assertTrue(self.task._fits_spec('38'))
151
152 def testFitsSpecGreater(self):
153 """Test milestone check for specs that is greater than a milestone."""
154 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
155 self.task._set_spec_compare_info()
156 self.assertTrue(self.task._fits_spec('39'))
157 self.assertTrue(self.task._fits_spec('38'))
158 self.assertFalse(self.task._fits_spec('37'))
159
160 def testGetFirmwareFromLabConfig(self):
161 """Test get firmware from lab config successfully."""
162 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
163 return_value='build1,build2'):
164 firmware = self.task._get_firmware_build(
165 'released_ro_2', self.BOARD, config_reader.LabConfig(None), None)
166 self.assertEqual(firmware, 'build2')
167
168 def testGetFirmwareFromLabConfigOutofIndex(self):
169 """Test get firmware from lab config when firmware index is invalid."""
170 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
171 return_value='build1,build2'):
172 self.assertRaises(ValueError,
173 self.task._get_latest_firmware_build_from_lab_config,
174 'released_ro_3',
175 self.BOARD,
176 config_reader.LabConfig(None))
177 self.assertIsNone(self.task._get_firmware_build(
178 'released_ro_3', self.BOARD, config_reader.LabConfig(None), None))
179
180 def testGetFirmwareFromDB(self):
181 """Test get firmware from DB successfully."""
182 firmware = self.task._get_firmware_build(
183 'firmware', self.BOARD, None, FakeCIDBClient())
184 self.assertEqual(
185 firmware,
186 '%s-firmware/R%s-%s' % (self.BOARD, self.MILESTONE, self.PLATFORM))
187
188 def testGetFirmwareFromDBConnectionError(self):
189 """Test get firmware from DB when DB connection is failed."""
190 self.assertIsNone(self.task._get_firmware_build(
191 'firmware', self.BOARD, None, FakeCIDBClient(success=False)))
192
193 def testScheduleCrosSuccessfully(self):
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700194 """test schedule cros builds successfully."""
195 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700196 self.task._schedule_cros_builds(branch_builds,
Xixuan Wu1e42c752019-03-21 13:41:49 -0700197 self._fake_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700198 FakeCIDBClient())
199 self.assertEqual(self._mock_push.call_count, 1)
200
201 def testScheduleCrosNonvalidBoard(self):
202 """Test schedule no cros builds due to non-allowed board."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700203 branch_builds = {('%s_2' % 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, 0)
208
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700209 def testScheduleCrosExcludeBoard(self):
210 """Test schedule no cros builds due to board is excluded."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700211 branch_builds = {(self.EXCLUDE_BOARD, None, 'release', '56'): '0000.00.00'}
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700212 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700213 self._empty_configs,
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700214 FakeCIDBClient())
215 self.assertEqual(self._mock_push.call_count, 0)
216
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700217 def testScheduleCrosSuccessfullyWithSpecifiedModel(self):
218 """Test schedule unibuild with specific model."""
219 self.task.board = self.UNIBUILD_BOARD
220 branch_builds = {(self.UNIBUILD_BOARD, 'robo', 'release', '56'):
221 '0000.00.00',
222 (self.UNIBUILD_BOARD, 'lava', 'release', '56'):
223 '0000.00.00'}
224 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700225 self._fake_configs,
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700226 FakeCIDBClient())
227 self.assertEqual(self._mock_push.call_count, 2)
228
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700229 def testScheduleCrosNonValidSpec(self):
230 """Test schedule no cros builds due to non-allowed branch milestone."""
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 # Only run tasks whose milestone = tot (R40)
233 self.task.branch_specs = re.split(r'\s*,\s*', '==tot')
234 self.task._set_spec_compare_info()
235 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700236 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700237 FakeCIDBClient())
238 self.assertEqual(self._mock_push.call_count, 0)
239
240 def testScheduleCrosSuccessfullyWithValidFirmware(self):
241 """Test schedule cros builds successfully with valid firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700242 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700243 self.task.firmware_ro_build_spec = 'firmware'
244 self.task._schedule_cros_builds(branch_builds,
Xixuan Wu1e42c752019-03-21 13:41:49 -0700245 self._fake_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700246 FakeCIDBClient())
247 self.assertEqual(self._mock_push.call_count, 1)
248
249 def testScheduleCrosWithNonValidFirmware(self):
250 """Test schedule no cros builds due to non-existent firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700251 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700252 self.task.firmware_ro_build_spec = 'firmware'
253 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700254 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700255 FakeCIDBClient(success=False))
256 self.assertEqual(self._mock_push.call_count, 0)
257
258 def testScheduleLaunchControlWithFullBranches(self):
259 """Test schedule all launch control builds successfully."""
260 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
261 'git_nyc-mr1-release/shamu-userdebug/3783920']}
262 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
263 self.task.launch_control_branches = [
264 t.lstrip() for t in lc_branches.split(',')]
265 self.task._schedule_launch_control_builds(lc_builds)
266 self.assertEqual(self._mock_push.call_count, 2)
267
268 def testScheduleLaunchControlWithPartlyBranches(self):
269 """Test schedule part of launch control builds due to branch check."""
270 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
271 'git_nyc-mr1-release/shamu-userdebug/3783920']}
272 lc_branches = 'git_nyc-mr1-release'
273 self.task.launch_control_branches = [
274 t.lstrip() for t in lc_branches.split(',')]
275 self.task._schedule_launch_control_builds(lc_builds)
276 self.assertEqual(self._mock_push.call_count, 1)
277
278 def testScheduleLaunchControlWithNoBranches(self):
279 """Test schedule none of launch control builds due to branch check."""
280 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
281 'git_nyc-mr1-release/shamu-userdebug/3783920']}
282 self.task.launch_control_branches = []
283 self.task._schedule_launch_control_builds(lc_builds)
284 self.assertEqual(self._mock_push.call_count, 0)
285
286 def testScheduleLaunchControlNonvalidBoard(self):
287 """Test schedule none of launch control builds due to board check."""
288 lc_builds = {'%s_2' % self.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)
Xixuan Wu5451a662017-10-17 10:57:40 -0700296
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700297 def testScheduleLaunchControlExcludeBoard(self):
298 """Test schedule none of launch control builds due to board check."""
299 lc_builds = {self.EXCLUDE_BOARD:
300 ['git_nyc-mr2-release/shamu-userdebug/3844975',
301 'git_nyc-mr1-release/shamu-userdebug/3783920']}
302 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
303 self.task.launch_control_branches = [
304 t.lstrip() for t in lc_branches.split(',')]
305 self.task._schedule_launch_control_builds(lc_builds)
306 self.assertEqual(self._mock_push.call_count, 0)
307
Xixuan Wu5451a662017-10-17 10:57:40 -0700308
309class TaskPushTestCase(TaskBaseTestCase):
310
311 def setUp(self):
312 super(TaskPushTestCase, self).setUp()
313 mock_push = mock.patch('task_executor.push')
314 self._mock_push = mock_push.start()
315 self.addCleanup(mock_push.stop)
316
317 def testScheduleCrOSIsPushSuccessfully(self):
318 """Test IS_PUSHED is changed if some CrOS suites are scheduled."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700319 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu5451a662017-10-17 10:57:40 -0700320 self.task.os_type = build_lib.OS_TYPE_CROS
321 self.assertTrue(self.task.schedule(
Xixuan Wu1e42c752019-03-21 13:41:49 -0700322 [], (branch_builds, []), self._fake_configs,
Craig Bergstrom58263d32018-04-26 14:11:35 -0600323 FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700324
325 def testScheduleLaunchControlIsPushSuccessfully(self):
326 """Test IS_PUSHED is changed if some launch control suites are scheduled."""
327 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
328 'git_nyc-mr1-release/shamu-userdebug/3783920']}
329 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
330 self.task.launch_control_branches = [
331 t.lstrip() for t in lc_branches.split(',')]
332 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
333 self.assertTrue(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700334 lc_builds, ([], []), self._empty_configs, FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700335
336 def testScheduleCrosIsPushInvalidBoard(self):
337 """Test schedule no cros builds due to non-allowed board."""
Craig Bergstrom58263d32018-04-26 14:11:35 -0600338 branch_builds = (
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700339 {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}, {},
Craig Bergstrom58263d32018-04-26 14:11:35 -0600340 )
Xixuan Wu5451a662017-10-17 10:57:40 -0700341 self.task.os_type = build_lib.OS_TYPE_CROS
342 self.assertFalse(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700343 [], branch_builds, self._empty_configs, FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700344
345 def testScheduleLaunchControlIsPushInvalidBoard(self):
346 """Test schedule none of launch control builds due to board check."""
347 lc_builds = {'%s_2' % self.BOARD:
348 ['git_nyc-mr2-release/shamu-userdebug/3844975',
349 'git_nyc-mr1-release/shamu-userdebug/3783920']}
350 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
351 self.task.launch_control_branches = [
352 t.lstrip() for t in lc_branches.split(',')]
353 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
354 self.task._schedule_launch_control_builds(lc_builds)
355 self.assertFalse(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700356 lc_builds, ([], []), self._empty_configs, FakeCIDBClient()))