blob: 4cbf40a70dcc8e0a8d7aac049a5f3f7c81590619 [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):
47 """Mock class for cloud_sql_client.CIDBClient."""
48
49 def get_skylab_board_list(self):
50 return []
51
52 def get_skylab_suite_list(self):
53 return []
54
Xixuan Wu5ff0fac2019-01-07 10:06:35 -080055 def get_skylab_model_map(self):
56 return {}
57
Xixuan Wu8d2f2862018-08-28 16:48:04 -070058 def get_cros_model_map(self):
59 return {'coral': ['robo', 'nasher', 'lava']}
60
61
Xixuan Wu5451a662017-10-17 10:57:40 -070062class TaskBaseTestCase(unittest.TestCase):
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070063
64 BOARD = 'fake_board'
Xixuan Wu8d2f2862018-08-28 16:48:04 -070065 UNIBUILD_BOARD = 'coral'
Po-Hsien Wang6d589732018-05-15 17:19:34 -070066 EXCLUDE_BOARD = 'exclude_fake_board'
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070067 NAME = 'fake_name'
68 SUITE = 'fake_suite'
69 POOL = 'fake_pool'
70 PLATFORM = '6182.0.0-rc2'
71 MILESTONE = '30'
72 NUM = 1
73 PRIORITY = 50
74 TIMEOUT = 600
75
76 def setUp(self):
77 tot_patcher = mock.patch('tot_manager.TotMilestoneManager')
78 self._mock_tot = tot_patcher.start()
79 self.addCleanup(tot_patcher.stop)
80 # Can't pass in False since storage_client is not mocked.
81 self._mock_tot.return_value = FakeTotMilestoneManager(True)
82 self.task = task.Task(config_reader.TaskInfo(
83 name=self.NAME,
84 suite=self.SUITE,
85 branch_specs=[],
86 pool=self.POOL,
87 num=self.NUM,
Xixuan Wu8d2f2862018-08-28 16:48:04 -070088 boards=','.join([self.BOARD, self.UNIBUILD_BOARD]),
Po-Hsien Wang6d589732018-05-15 17:19:34 -070089 exclude_boards=self.EXCLUDE_BOARD,
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070090 priority=self.PRIORITY,
91 timeout=self.TIMEOUT))
92
Xixuan Wu5451a662017-10-17 10:57:40 -070093
94class TaskTestCase(TaskBaseTestCase):
95
96 def setUp(self):
97 super(TaskTestCase, self).setUp()
98
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070099 mock_push = mock.patch('task.Task._push_suite')
100 self._mock_push = mock_push.start()
101 self.addCleanup(mock_push.stop)
102
103 def testSetSpecCompareInfoEqual(self):
104 """Test compare info setting for specs that equals to a milestone."""
105 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-2')
106 self.task._set_spec_compare_info()
107 self.assertTrue(self.task._version_equal_constraint)
108 self.assertFalse(self.task._version_gte_constraint)
109 self.assertFalse(self.task._version_lte_constraint)
110 self.assertEqual(self.task._bare_branches, [])
111
112 def testSetSpecCompareInfoLess(self):
113 """Test compare info setting for specs that is less than a milestone."""
114 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot')
115 self.task._set_spec_compare_info()
116 self.assertFalse(self.task._version_equal_constraint)
117 self.assertFalse(self.task._version_gte_constraint)
118 self.assertTrue(self.task._version_lte_constraint)
119 self.assertEqual(self.task._bare_branches, [])
120
121 def testSetSpecCompareInfoGreater(self):
122 """Test compare info setting for specs that is greater than a milestone."""
123 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
124 self.task._set_spec_compare_info()
125 self.assertFalse(self.task._version_equal_constraint)
126 self.assertTrue(self.task._version_gte_constraint)
127 self.assertFalse(self.task._version_lte_constraint)
128 self.assertEqual(self.task._bare_branches, [])
129
130 def testFitsSpecEqual(self):
131 """Test milestone check for specs that equals to a milestone."""
132 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-1')
133 self.task._set_spec_compare_info()
134 self.assertFalse(self.task._fits_spec('40'))
135 self.assertTrue(self.task._fits_spec('39'))
136 self.assertFalse(self.task._fits_spec('38'))
137
138 def testFitsSpecLess(self):
139 """Test milestone check for specs that is less than a milestone."""
140 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot-1')
141 self.task._set_spec_compare_info()
142 self.assertFalse(self.task._fits_spec('40'))
143 self.assertTrue(self.task._fits_spec('39'))
144 self.assertTrue(self.task._fits_spec('38'))
145
146 def testFitsSpecGreater(self):
147 """Test milestone check for specs that is greater than a milestone."""
148 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
149 self.task._set_spec_compare_info()
150 self.assertTrue(self.task._fits_spec('39'))
151 self.assertTrue(self.task._fits_spec('38'))
152 self.assertFalse(self.task._fits_spec('37'))
153
154 def testGetFirmwareFromLabConfig(self):
155 """Test get firmware from lab config successfully."""
156 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
157 return_value='build1,build2'):
158 firmware = self.task._get_firmware_build(
159 'released_ro_2', self.BOARD, config_reader.LabConfig(None), None)
160 self.assertEqual(firmware, 'build2')
161
162 def testGetFirmwareFromLabConfigOutofIndex(self):
163 """Test get firmware from lab config when firmware index is invalid."""
164 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
165 return_value='build1,build2'):
166 self.assertRaises(ValueError,
167 self.task._get_latest_firmware_build_from_lab_config,
168 'released_ro_3',
169 self.BOARD,
170 config_reader.LabConfig(None))
171 self.assertIsNone(self.task._get_firmware_build(
172 'released_ro_3', self.BOARD, config_reader.LabConfig(None), None))
173
174 def testGetFirmwareFromDB(self):
175 """Test get firmware from DB successfully."""
176 firmware = self.task._get_firmware_build(
177 'firmware', self.BOARD, None, FakeCIDBClient())
178 self.assertEqual(
179 firmware,
180 '%s-firmware/R%s-%s' % (self.BOARD, self.MILESTONE, self.PLATFORM))
181
182 def testGetFirmwareFromDBConnectionError(self):
183 """Test get firmware from DB when DB connection is failed."""
184 self.assertIsNone(self.task._get_firmware_build(
185 'firmware', self.BOARD, None, FakeCIDBClient(success=False)))
186
187 def testScheduleCrosSuccessfully(self):
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700188 """test schedule cros builds successfully."""
189 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700190 self.task._schedule_cros_builds(branch_builds,
191 config_reader.LabConfig(None),
192 FakeCIDBClient())
193 self.assertEqual(self._mock_push.call_count, 1)
194
195 def testScheduleCrosNonvalidBoard(self):
196 """Test schedule no cros builds due to non-allowed board."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700197 branch_builds = {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700198 self.task._schedule_cros_builds(branch_builds,
199 config_reader.LabConfig(None),
200 FakeCIDBClient())
201 self.assertEqual(self._mock_push.call_count, 0)
202
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700203 def testScheduleCrosExcludeBoard(self):
204 """Test schedule no cros builds due to board is excluded."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700205 branch_builds = {(self.EXCLUDE_BOARD, None, 'release', '56'): '0000.00.00'}
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700206 self.task._schedule_cros_builds(branch_builds,
207 config_reader.LabConfig(None),
208 FakeCIDBClient())
209 self.assertEqual(self._mock_push.call_count, 0)
210
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700211 def testScheduleCrosSuccessfullyWithSpecifiedModel(self):
212 """Test schedule unibuild with specific model."""
213 self.task.board = self.UNIBUILD_BOARD
214 branch_builds = {(self.UNIBUILD_BOARD, 'robo', 'release', '56'):
215 '0000.00.00',
216 (self.UNIBUILD_BOARD, 'lava', 'release', '56'):
217 '0000.00.00'}
218 self.task._schedule_cros_builds(branch_builds,
219 FakeLabConfig(),
220 FakeCIDBClient())
221 self.assertEqual(self._mock_push.call_count, 2)
222
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700223 def testScheduleCrosNonValidSpec(self):
224 """Test schedule no cros builds due to non-allowed branch milestone."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700225 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700226 # Only run tasks whose milestone = tot (R40)
227 self.task.branch_specs = re.split(r'\s*,\s*', '==tot')
228 self.task._set_spec_compare_info()
229 self.task._schedule_cros_builds(branch_builds,
230 config_reader.LabConfig(None),
231 FakeCIDBClient())
232 self.assertEqual(self._mock_push.call_count, 0)
233
234 def testScheduleCrosSuccessfullyWithValidFirmware(self):
235 """Test schedule cros builds successfully with valid firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700236 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700237 self.task.firmware_ro_build_spec = 'firmware'
238 self.task._schedule_cros_builds(branch_builds,
239 config_reader.LabConfig(None),
240 FakeCIDBClient())
241 self.assertEqual(self._mock_push.call_count, 1)
242
243 def testScheduleCrosWithNonValidFirmware(self):
244 """Test schedule no cros builds due to non-existent firmware."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700245 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu0c76d5b2017-08-30 16:40:17 -0700246 self.task.firmware_ro_build_spec = 'firmware'
247 self.task._schedule_cros_builds(branch_builds,
248 config_reader.LabConfig(None),
249 FakeCIDBClient(success=False))
250 self.assertEqual(self._mock_push.call_count, 0)
251
252 def testScheduleLaunchControlWithFullBranches(self):
253 """Test schedule all launch control builds successfully."""
254 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
255 'git_nyc-mr1-release/shamu-userdebug/3783920']}
256 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
257 self.task.launch_control_branches = [
258 t.lstrip() for t in lc_branches.split(',')]
259 self.task._schedule_launch_control_builds(lc_builds)
260 self.assertEqual(self._mock_push.call_count, 2)
261
262 def testScheduleLaunchControlWithPartlyBranches(self):
263 """Test schedule part of launch control builds due to branch check."""
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'
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, 1)
271
272 def testScheduleLaunchControlWithNoBranches(self):
273 """Test schedule none 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 self.task.launch_control_branches = []
277 self.task._schedule_launch_control_builds(lc_builds)
278 self.assertEqual(self._mock_push.call_count, 0)
279
280 def testScheduleLaunchControlNonvalidBoard(self):
281 """Test schedule none of launch control builds due to board check."""
282 lc_builds = {'%s_2' % self.BOARD:
283 ['git_nyc-mr2-release/shamu-userdebug/3844975',
284 'git_nyc-mr1-release/shamu-userdebug/3783920']}
285 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
286 self.task.launch_control_branches = [
287 t.lstrip() for t in lc_branches.split(',')]
288 self.task._schedule_launch_control_builds(lc_builds)
289 self.assertEqual(self._mock_push.call_count, 0)
Xixuan Wu5451a662017-10-17 10:57:40 -0700290
Po-Hsien Wang6d589732018-05-15 17:19:34 -0700291 def testScheduleLaunchControlExcludeBoard(self):
292 """Test schedule none of launch control builds due to board check."""
293 lc_builds = {self.EXCLUDE_BOARD:
294 ['git_nyc-mr2-release/shamu-userdebug/3844975',
295 'git_nyc-mr1-release/shamu-userdebug/3783920']}
296 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
297 self.task.launch_control_branches = [
298 t.lstrip() for t in lc_branches.split(',')]
299 self.task._schedule_launch_control_builds(lc_builds)
300 self.assertEqual(self._mock_push.call_count, 0)
301
Xixuan Wu5451a662017-10-17 10:57:40 -0700302
303class TaskPushTestCase(TaskBaseTestCase):
304
305 def setUp(self):
306 super(TaskPushTestCase, self).setUp()
307 mock_push = mock.patch('task_executor.push')
308 self._mock_push = mock_push.start()
309 self.addCleanup(mock_push.stop)
310
311 def testScheduleCrOSIsPushSuccessfully(self):
312 """Test IS_PUSHED is changed if some CrOS suites are scheduled."""
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700313 branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'}
Xixuan Wu5451a662017-10-17 10:57:40 -0700314 self.task.os_type = build_lib.OS_TYPE_CROS
315 self.assertTrue(self.task.schedule(
Craig Bergstrom58263d32018-04-26 14:11:35 -0600316 [], (branch_builds, []), config_reader.LabConfig(None),
317 FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700318
319 def testScheduleLaunchControlIsPushSuccessfully(self):
320 """Test IS_PUSHED is changed if some launch control suites are scheduled."""
321 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
322 'git_nyc-mr1-release/shamu-userdebug/3783920']}
323 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
324 self.task.launch_control_branches = [
325 t.lstrip() for t in lc_branches.split(',')]
326 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
327 self.assertTrue(self.task.schedule(
Craig Bergstrom58263d32018-04-26 14:11:35 -0600328 lc_builds, ([], []), config_reader.LabConfig(None), FakeCIDBClient()))
Xixuan Wu5451a662017-10-17 10:57:40 -0700329
330 def testScheduleCrosIsPushInvalidBoard(self):
331 """Test schedule no cros builds due to non-allowed board."""
Craig Bergstrom58263d32018-04-26 14:11:35 -0600332 branch_builds = (
Xixuan Wu8d2f2862018-08-28 16:48:04 -0700333 {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}, {},
Craig Bergstrom58263d32018-04-26 14:11:35 -0600334 )
Xixuan Wu5451a662017-10-17 10:57:40 -0700335 self.task.os_type = build_lib.OS_TYPE_CROS
336 self.assertFalse(self.task.schedule(
337 [], branch_builds, config_reader.LabConfig(None), FakeCIDBClient()))
338
339 def testScheduleLaunchControlIsPushInvalidBoard(self):
340 """Test schedule none of launch control builds due to board check."""
341 lc_builds = {'%s_2' % self.BOARD:
342 ['git_nyc-mr2-release/shamu-userdebug/3844975',
343 'git_nyc-mr1-release/shamu-userdebug/3783920']}
344 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
345 self.task.launch_control_branches = [
346 t.lstrip() for t in lc_branches.split(',')]
347 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
348 self.task._schedule_launch_control_builds(lc_builds)
349 self.assertFalse(self.task.schedule(
Craig Bergstrom58263d32018-04-26 14:11:35 -0600350 lc_builds, ([], []), config_reader.LabConfig(None), FakeCIDBClient()))