blob: 59072b424fe76c3c74ff6a6a8640cb7dd17c3be2 [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
57 def get_skylab_board_list(self):
58 return []
59
60 def get_skylab_suite_list(self):
61 return []
62
Xixuan Wu5ff0fac2019-01-07 10:06:35 -080063 def get_skylab_model_map(self):
64 return {}
65
Xixuan Wu8d2f2862018-08-28 16:48:04 -070066 def get_cros_model_map(self):
67 return {'coral': ['robo', 'nasher', 'lava']}
68
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(
192 'firmware', self.BOARD, None, FakeCIDBClient())
193 self.assertEqual(
194 firmware,
195 '%s-firmware/R%s-%s' % (self.BOARD, self.MILESTONE, self.PLATFORM))
196
197 def testGetFirmwareFromDBConnectionError(self):
198 """Test get firmware from DB when DB connection is failed."""
199 self.assertIsNone(self.task._get_firmware_build(
200 'firmware', self.BOARD, None, FakeCIDBClient(success=False)))
201
202 def testScheduleCrosSuccessfully(self):
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700203 """test schedule cros builds successfully."""
204 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700205 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700206 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700207 FakeCIDBClient())
208 self.assertEqual(self._mock_push.call_count, 1)
209
210 def testScheduleCrosNonvalidBoard(self):
211 """Test schedule no cros builds due to non-allowed board."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700212 branch_builds = {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700213 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700214 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700215 FakeCIDBClient())
216 self.assertEqual(self._mock_push.call_count, 0)
217
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700218 def testScheduleCrosExcludeBoard(self):
219 """Test schedule no cros builds due to board is excluded."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700220 branch_builds = {(self.EXCLUDE_BOARD, None, 'release', '56'): '0000.00.00'}
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700221 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700222 self._empty_configs,
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700223 FakeCIDBClient())
224 self.assertEqual(self._mock_push.call_count, 0)
225
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700226 def testScheduleCrosSuccessfullyWithSpecifiedModel(self):
227 """Test schedule unibuild with specific model."""
228 self.task.board = self.UNIBUILD_BOARD
229 branch_builds = {(self.UNIBUILD_BOARD, 'robo', 'release', '56'):
230 '0000.00.00',
231 (self.UNIBUILD_BOARD, 'lava', 'release', '56'):
232 '0000.00.00'}
233 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700234 self._fake_configs,
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700235 FakeCIDBClient())
236 self.assertEqual(self._mock_push.call_count, 2)
237
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700238 def testScheduleCrosNonValidSpec(self):
239 """Test schedule no cros builds due to non-allowed branch milestone."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700240 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700241 # Only run tasks whose milestone = tot (R40)
242 self.task.branch_specs = re.split(r'\s*,\s*', '==tot')
243 self.task._set_spec_compare_info()
244 self.task._schedule_cros_builds(branch_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700245 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700246 FakeCIDBClient())
247 self.assertEqual(self._mock_push.call_count, 0)
248
249 def testScheduleCrosSuccessfullyWithValidFirmware(self):
250 """Test schedule cros builds successfully with valid 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())
256 self.assertEqual(self._mock_push.call_count, 1)
257
258 def testScheduleCrosWithNonValidFirmware(self):
259 """Test schedule no cros builds due to non-existent 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 Wuf4a4c882019-03-15 14:48:26 -0700263 self._empty_configs,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700264 FakeCIDBClient(success=False))
265 self.assertEqual(self._mock_push.call_count, 0)
266
267 def testScheduleLaunchControlWithFullBranches(self):
268 """Test schedule all launch control builds successfully."""
269 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
270 'git_nyc-mr1-release/shamu-userdebug/3783920']}
271 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
272 self.task.launch_control_branches = [
273 t.lstrip() for t in lc_branches.split(',')]
274 self.task._schedule_launch_control_builds(lc_builds)
275 self.assertEqual(self._mock_push.call_count, 2)
276
277 def testScheduleLaunchControlWithPartlyBranches(self):
278 """Test schedule part of launch control builds due to branch check."""
279 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
280 'git_nyc-mr1-release/shamu-userdebug/3783920']}
281 lc_branches = 'git_nyc-mr1-release'
282 self.task.launch_control_branches = [
283 t.lstrip() for t in lc_branches.split(',')]
284 self.task._schedule_launch_control_builds(lc_builds)
285 self.assertEqual(self._mock_push.call_count, 1)
286
287 def testScheduleLaunchControlWithNoBranches(self):
288 """Test schedule none of launch control builds due to branch check."""
289 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
290 'git_nyc-mr1-release/shamu-userdebug/3783920']}
291 self.task.launch_control_branches = []
292 self.task._schedule_launch_control_builds(lc_builds)
293 self.assertEqual(self._mock_push.call_count, 0)
294
295 def testScheduleLaunchControlNonvalidBoard(self):
296 """Test schedule none of launch control builds due to board check."""
297 lc_builds = {'%s_2' % self.BOARD:
298 ['git_nyc-mr2-release/shamu-userdebug/3844975',
299 'git_nyc-mr1-release/shamu-userdebug/3783920']}
300 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
301 self.task.launch_control_branches = [
302 t.lstrip() for t in lc_branches.split(',')]
303 self.task._schedule_launch_control_builds(lc_builds)
304 self.assertEqual(self._mock_push.call_count, 0)
Xixuan Wu5451a662017-10-17 10:57:40 -0700305
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700306 def testScheduleLaunchControlExcludeBoard(self):
307 """Test schedule none of launch control builds due to board check."""
308 lc_builds = {self.EXCLUDE_BOARD:
309 ['git_nyc-mr2-release/shamu-userdebug/3844975',
310 'git_nyc-mr1-release/shamu-userdebug/3783920']}
311 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
312 self.task.launch_control_branches = [
313 t.lstrip() for t in lc_branches.split(',')]
314 self.task._schedule_launch_control_builds(lc_builds)
315 self.assertEqual(self._mock_push.call_count, 0)
316
Xixuan Wu5451a662017-10-17 10:57:40 -0700317
318class TaskPushTestCase(TaskBaseTestCase):
319
320 def setUp(self):
321 super(TaskPushTestCase, self).setUp()
322 mock_push = mock.patch('task_executor.push')
323 self._mock_push = mock_push.start()
324 self.addCleanup(mock_push.stop)
325
326 def testScheduleCrOSIsPushSuccessfully(self):
327 """Test IS_PUSHED is changed if some CrOS suites are scheduled."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700328 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu5451a662017-10-17 10:57:40 -0700329 self.task.os_type = build_lib.OS_TYPE_CROS
330 self.assertTrue(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700331 [], (branch_builds, []), self._empty_configs,
Craig Bergstrom58263d32018-04-26 14:11:35 -0600332 FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700333
334 def testScheduleLaunchControlIsPushSuccessfully(self):
335 """Test IS_PUSHED is changed if some launch control suites are scheduled."""
336 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
337 'git_nyc-mr1-release/shamu-userdebug/3783920']}
338 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
339 self.task.launch_control_branches = [
340 t.lstrip() for t in lc_branches.split(',')]
341 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
342 self.assertTrue(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700343 lc_builds, ([], []), self._empty_configs, FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700344
345 def testScheduleCrosIsPushInvalidBoard(self):
346 """Test schedule no cros builds due to non-allowed board."""
Craig Bergstrom58263d32018-04-26 14:11:35 -0600347 branch_builds = (
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700348 {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}, {},
Craig Bergstrom58263d32018-04-26 14:11:35 -0600349 )
Xixuan Wu5451a662017-10-17 10:57:40 -0700350 self.task.os_type = build_lib.OS_TYPE_CROS
351 self.assertFalse(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700352 [], branch_builds, self._empty_configs, FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700353
354 def testScheduleLaunchControlIsPushInvalidBoard(self):
355 """Test schedule none of launch control builds due to board check."""
356 lc_builds = {'%s_2' % self.BOARD:
357 ['git_nyc-mr2-release/shamu-userdebug/3844975',
358 'git_nyc-mr1-release/shamu-userdebug/3783920']}
359 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
360 self.task.launch_control_branches = [
361 t.lstrip() for t in lc_branches.split(',')]
362 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
363 self.task._schedule_launch_control_builds(lc_builds)
364 self.assertFalse(self.task.schedule(
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700365 lc_builds, ([], []), self._empty_configs, FakeCIDBClient()))