Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 1 | # 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.""" |
| 6 | |
| 7 | import re |
| 8 | import unittest |
| 9 | |
Xixuan Wu | 5451a66 | 2017-10-17 10:57:40 -0700 | [diff] [blame] | 10 | import build_lib |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 11 | import cloud_sql_client |
| 12 | import config_reader |
| 13 | import mock |
| 14 | import MySQLdb |
| 15 | import task |
| 16 | import tot_manager |
| 17 | |
| 18 | |
| 19 | class FakeTotMilestoneManager(tot_manager.TotMilestoneManager): |
| 20 | """Mock class for tot_manager.TotMilestoneManager.""" |
| 21 | |
| 22 | def __init__(self, is_sanity): |
| 23 | self.is_sanity = is_sanity |
| 24 | self.storage_client = None |
| 25 | self.tot = self._tot_milestone() |
| 26 | |
| 27 | |
| 28 | class FakeCIDBClient(object): |
| 29 | """Mock class for cloud_sql_client.CIDBClient.""" |
| 30 | |
| 31 | def __init__(self, success=True): |
| 32 | self.success = success |
| 33 | |
| 34 | def get_latest_passed_builds(self, build_config): |
| 35 | if not self.success: |
| 36 | raise MySQLdb.OperationalError('Failed to connect to db') |
| 37 | |
| 38 | return cloud_sql_client.BuildInfo(board=build_config.split('-')[0], |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 39 | model=None, |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 40 | milestone=TaskTestCase.MILESTONE, |
| 41 | platform=TaskTestCase.PLATFORM, |
| 42 | build_config=build_config) |
| 43 | |
| 44 | |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 45 | class FakeLabConfig(object): |
| 46 | """Mock class for cloud_sql_client.CIDBClient.""" |
| 47 | |
| 48 | def get_skylab_board_list(self): |
| 49 | return [] |
| 50 | |
| 51 | def get_skylab_suite_list(self): |
| 52 | return [] |
| 53 | |
| 54 | def get_cros_model_map(self): |
| 55 | return {'coral': ['robo', 'nasher', 'lava']} |
| 56 | |
| 57 | |
Xixuan Wu | 5451a66 | 2017-10-17 10:57:40 -0700 | [diff] [blame] | 58 | class TaskBaseTestCase(unittest.TestCase): |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 59 | |
| 60 | BOARD = 'fake_board' |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 61 | UNIBUILD_BOARD = 'coral' |
Po-Hsien Wang | 6d58973 | 2018-05-15 17:19:34 -0700 | [diff] [blame] | 62 | EXCLUDE_BOARD = 'exclude_fake_board' |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 63 | NAME = 'fake_name' |
| 64 | SUITE = 'fake_suite' |
| 65 | POOL = 'fake_pool' |
| 66 | PLATFORM = '6182.0.0-rc2' |
| 67 | MILESTONE = '30' |
| 68 | NUM = 1 |
| 69 | PRIORITY = 50 |
| 70 | TIMEOUT = 600 |
| 71 | |
| 72 | def setUp(self): |
| 73 | tot_patcher = mock.patch('tot_manager.TotMilestoneManager') |
| 74 | self._mock_tot = tot_patcher.start() |
| 75 | self.addCleanup(tot_patcher.stop) |
| 76 | # Can't pass in False since storage_client is not mocked. |
| 77 | self._mock_tot.return_value = FakeTotMilestoneManager(True) |
| 78 | self.task = task.Task(config_reader.TaskInfo( |
| 79 | name=self.NAME, |
| 80 | suite=self.SUITE, |
| 81 | branch_specs=[], |
| 82 | pool=self.POOL, |
| 83 | num=self.NUM, |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 84 | boards=','.join([self.BOARD, self.UNIBUILD_BOARD]), |
Po-Hsien Wang | 6d58973 | 2018-05-15 17:19:34 -0700 | [diff] [blame] | 85 | exclude_boards=self.EXCLUDE_BOARD, |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 86 | priority=self.PRIORITY, |
| 87 | timeout=self.TIMEOUT)) |
| 88 | |
Xixuan Wu | 5451a66 | 2017-10-17 10:57:40 -0700 | [diff] [blame] | 89 | |
| 90 | class TaskTestCase(TaskBaseTestCase): |
| 91 | |
| 92 | def setUp(self): |
| 93 | super(TaskTestCase, self).setUp() |
| 94 | |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 95 | mock_push = mock.patch('task.Task._push_suite') |
| 96 | self._mock_push = mock_push.start() |
| 97 | self.addCleanup(mock_push.stop) |
| 98 | |
| 99 | def testSetSpecCompareInfoEqual(self): |
| 100 | """Test compare info setting for specs that equals to a milestone.""" |
| 101 | self.task.branch_specs = re.split(r'\s*,\s*', '==tot-2') |
| 102 | self.task._set_spec_compare_info() |
| 103 | self.assertTrue(self.task._version_equal_constraint) |
| 104 | self.assertFalse(self.task._version_gte_constraint) |
| 105 | self.assertFalse(self.task._version_lte_constraint) |
| 106 | self.assertEqual(self.task._bare_branches, []) |
| 107 | |
| 108 | def testSetSpecCompareInfoLess(self): |
| 109 | """Test compare info setting for specs that is less than a milestone.""" |
| 110 | self.task.branch_specs = re.split(r'\s*,\s*', '<=tot') |
| 111 | self.task._set_spec_compare_info() |
| 112 | self.assertFalse(self.task._version_equal_constraint) |
| 113 | self.assertFalse(self.task._version_gte_constraint) |
| 114 | self.assertTrue(self.task._version_lte_constraint) |
| 115 | self.assertEqual(self.task._bare_branches, []) |
| 116 | |
| 117 | def testSetSpecCompareInfoGreater(self): |
| 118 | """Test compare info setting for specs that is greater than a milestone.""" |
| 119 | self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2') |
| 120 | self.task._set_spec_compare_info() |
| 121 | self.assertFalse(self.task._version_equal_constraint) |
| 122 | self.assertTrue(self.task._version_gte_constraint) |
| 123 | self.assertFalse(self.task._version_lte_constraint) |
| 124 | self.assertEqual(self.task._bare_branches, []) |
| 125 | |
| 126 | def testFitsSpecEqual(self): |
| 127 | """Test milestone check for specs that equals to a milestone.""" |
| 128 | self.task.branch_specs = re.split(r'\s*,\s*', '==tot-1') |
| 129 | self.task._set_spec_compare_info() |
| 130 | self.assertFalse(self.task._fits_spec('40')) |
| 131 | self.assertTrue(self.task._fits_spec('39')) |
| 132 | self.assertFalse(self.task._fits_spec('38')) |
| 133 | |
| 134 | def testFitsSpecLess(self): |
| 135 | """Test milestone check for specs that is less than a milestone.""" |
| 136 | self.task.branch_specs = re.split(r'\s*,\s*', '<=tot-1') |
| 137 | self.task._set_spec_compare_info() |
| 138 | self.assertFalse(self.task._fits_spec('40')) |
| 139 | self.assertTrue(self.task._fits_spec('39')) |
| 140 | self.assertTrue(self.task._fits_spec('38')) |
| 141 | |
| 142 | def testFitsSpecGreater(self): |
| 143 | """Test milestone check for specs that is greater than a milestone.""" |
| 144 | self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2') |
| 145 | self.task._set_spec_compare_info() |
| 146 | self.assertTrue(self.task._fits_spec('39')) |
| 147 | self.assertTrue(self.task._fits_spec('38')) |
| 148 | self.assertFalse(self.task._fits_spec('37')) |
| 149 | |
| 150 | def testGetFirmwareFromLabConfig(self): |
| 151 | """Test get firmware from lab config successfully.""" |
| 152 | with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list', |
| 153 | return_value='build1,build2'): |
| 154 | firmware = self.task._get_firmware_build( |
| 155 | 'released_ro_2', self.BOARD, config_reader.LabConfig(None), None) |
| 156 | self.assertEqual(firmware, 'build2') |
| 157 | |
| 158 | def testGetFirmwareFromLabConfigOutofIndex(self): |
| 159 | """Test get firmware from lab config when firmware index is invalid.""" |
| 160 | with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list', |
| 161 | return_value='build1,build2'): |
| 162 | self.assertRaises(ValueError, |
| 163 | self.task._get_latest_firmware_build_from_lab_config, |
| 164 | 'released_ro_3', |
| 165 | self.BOARD, |
| 166 | config_reader.LabConfig(None)) |
| 167 | self.assertIsNone(self.task._get_firmware_build( |
| 168 | 'released_ro_3', self.BOARD, config_reader.LabConfig(None), None)) |
| 169 | |
| 170 | def testGetFirmwareFromDB(self): |
| 171 | """Test get firmware from DB successfully.""" |
| 172 | firmware = self.task._get_firmware_build( |
| 173 | 'firmware', self.BOARD, None, FakeCIDBClient()) |
| 174 | self.assertEqual( |
| 175 | firmware, |
| 176 | '%s-firmware/R%s-%s' % (self.BOARD, self.MILESTONE, self.PLATFORM)) |
| 177 | |
| 178 | def testGetFirmwareFromDBConnectionError(self): |
| 179 | """Test get firmware from DB when DB connection is failed.""" |
| 180 | self.assertIsNone(self.task._get_firmware_build( |
| 181 | 'firmware', self.BOARD, None, FakeCIDBClient(success=False))) |
| 182 | |
| 183 | def testScheduleCrosSuccessfully(self): |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 184 | """test schedule cros builds successfully.""" |
| 185 | branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'} |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 186 | self.task._schedule_cros_builds(branch_builds, |
| 187 | config_reader.LabConfig(None), |
| 188 | FakeCIDBClient()) |
| 189 | self.assertEqual(self._mock_push.call_count, 1) |
| 190 | |
| 191 | def testScheduleCrosNonvalidBoard(self): |
| 192 | """Test schedule no cros builds due to non-allowed board.""" |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 193 | branch_builds = {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'} |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 194 | self.task._schedule_cros_builds(branch_builds, |
| 195 | config_reader.LabConfig(None), |
| 196 | FakeCIDBClient()) |
| 197 | self.assertEqual(self._mock_push.call_count, 0) |
| 198 | |
Po-Hsien Wang | 6d58973 | 2018-05-15 17:19:34 -0700 | [diff] [blame] | 199 | def testScheduleCrosExcludeBoard(self): |
| 200 | """Test schedule no cros builds due to board is excluded.""" |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 201 | branch_builds = {(self.EXCLUDE_BOARD, None, 'release', '56'): '0000.00.00'} |
Po-Hsien Wang | 6d58973 | 2018-05-15 17:19:34 -0700 | [diff] [blame] | 202 | self.task._schedule_cros_builds(branch_builds, |
| 203 | config_reader.LabConfig(None), |
| 204 | FakeCIDBClient()) |
| 205 | self.assertEqual(self._mock_push.call_count, 0) |
| 206 | |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 207 | def testScheduleCrosSuccessfullyWithSpecifiedModel(self): |
| 208 | """Test schedule unibuild with specific model.""" |
| 209 | self.task.board = self.UNIBUILD_BOARD |
| 210 | branch_builds = {(self.UNIBUILD_BOARD, 'robo', 'release', '56'): |
| 211 | '0000.00.00', |
| 212 | (self.UNIBUILD_BOARD, 'lava', 'release', '56'): |
| 213 | '0000.00.00'} |
| 214 | self.task._schedule_cros_builds(branch_builds, |
| 215 | FakeLabConfig(), |
| 216 | FakeCIDBClient()) |
| 217 | self.assertEqual(self._mock_push.call_count, 2) |
| 218 | |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 219 | def testScheduleCrosNonValidSpec(self): |
| 220 | """Test schedule no cros builds due to non-allowed branch milestone.""" |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 221 | branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'} |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 222 | # Only run tasks whose milestone = tot (R40) |
| 223 | self.task.branch_specs = re.split(r'\s*,\s*', '==tot') |
| 224 | self.task._set_spec_compare_info() |
| 225 | self.task._schedule_cros_builds(branch_builds, |
| 226 | config_reader.LabConfig(None), |
| 227 | FakeCIDBClient()) |
| 228 | self.assertEqual(self._mock_push.call_count, 0) |
| 229 | |
| 230 | def testScheduleCrosSuccessfullyWithValidFirmware(self): |
| 231 | """Test schedule cros builds successfully with valid firmware.""" |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 232 | branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'} |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 233 | self.task.firmware_ro_build_spec = 'firmware' |
| 234 | self.task._schedule_cros_builds(branch_builds, |
| 235 | config_reader.LabConfig(None), |
| 236 | FakeCIDBClient()) |
| 237 | self.assertEqual(self._mock_push.call_count, 1) |
| 238 | |
| 239 | def testScheduleCrosWithNonValidFirmware(self): |
| 240 | """Test schedule no cros builds due to non-existent firmware.""" |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 241 | branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'} |
Xixuan Wu | 0c76d5b | 2017-08-30 16:40:17 -0700 | [diff] [blame] | 242 | self.task.firmware_ro_build_spec = 'firmware' |
| 243 | self.task._schedule_cros_builds(branch_builds, |
| 244 | config_reader.LabConfig(None), |
| 245 | FakeCIDBClient(success=False)) |
| 246 | self.assertEqual(self._mock_push.call_count, 0) |
| 247 | |
| 248 | def testScheduleLaunchControlWithFullBranches(self): |
| 249 | """Test schedule all launch control builds successfully.""" |
| 250 | lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975', |
| 251 | 'git_nyc-mr1-release/shamu-userdebug/3783920']} |
| 252 | lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release' |
| 253 | self.task.launch_control_branches = [ |
| 254 | t.lstrip() for t in lc_branches.split(',')] |
| 255 | self.task._schedule_launch_control_builds(lc_builds) |
| 256 | self.assertEqual(self._mock_push.call_count, 2) |
| 257 | |
| 258 | def testScheduleLaunchControlWithPartlyBranches(self): |
| 259 | """Test schedule part of launch control builds due to branch check.""" |
| 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' |
| 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, 1) |
| 267 | |
| 268 | def testScheduleLaunchControlWithNoBranches(self): |
| 269 | """Test schedule none 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 | self.task.launch_control_branches = [] |
| 273 | self.task._schedule_launch_control_builds(lc_builds) |
| 274 | self.assertEqual(self._mock_push.call_count, 0) |
| 275 | |
| 276 | def testScheduleLaunchControlNonvalidBoard(self): |
| 277 | """Test schedule none of launch control builds due to board check.""" |
| 278 | lc_builds = {'%s_2' % self.BOARD: |
| 279 | ['git_nyc-mr2-release/shamu-userdebug/3844975', |
| 280 | 'git_nyc-mr1-release/shamu-userdebug/3783920']} |
| 281 | lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-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, 0) |
Xixuan Wu | 5451a66 | 2017-10-17 10:57:40 -0700 | [diff] [blame] | 286 | |
Po-Hsien Wang | 6d58973 | 2018-05-15 17:19:34 -0700 | [diff] [blame] | 287 | def testScheduleLaunchControlExcludeBoard(self): |
| 288 | """Test schedule none of launch control builds due to board check.""" |
| 289 | lc_builds = {self.EXCLUDE_BOARD: |
| 290 | ['git_nyc-mr2-release/shamu-userdebug/3844975', |
| 291 | 'git_nyc-mr1-release/shamu-userdebug/3783920']} |
| 292 | lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release' |
| 293 | self.task.launch_control_branches = [ |
| 294 | t.lstrip() for t in lc_branches.split(',')] |
| 295 | self.task._schedule_launch_control_builds(lc_builds) |
| 296 | self.assertEqual(self._mock_push.call_count, 0) |
| 297 | |
Xixuan Wu | 5451a66 | 2017-10-17 10:57:40 -0700 | [diff] [blame] | 298 | |
| 299 | class TaskPushTestCase(TaskBaseTestCase): |
| 300 | |
| 301 | def setUp(self): |
| 302 | super(TaskPushTestCase, self).setUp() |
| 303 | mock_push = mock.patch('task_executor.push') |
| 304 | self._mock_push = mock_push.start() |
| 305 | self.addCleanup(mock_push.stop) |
| 306 | |
| 307 | def testScheduleCrOSIsPushSuccessfully(self): |
| 308 | """Test IS_PUSHED is changed if some CrOS suites are scheduled.""" |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 309 | branch_builds = {(self.BOARD, None, 'release', '56'): '0000.00.00'} |
Xixuan Wu | 5451a66 | 2017-10-17 10:57:40 -0700 | [diff] [blame] | 310 | self.task.os_type = build_lib.OS_TYPE_CROS |
| 311 | self.assertTrue(self.task.schedule( |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 312 | [], (branch_builds, []), config_reader.LabConfig(None), |
| 313 | FakeCIDBClient())) |
Xixuan Wu | 5451a66 | 2017-10-17 10:57:40 -0700 | [diff] [blame] | 314 | |
| 315 | def testScheduleLaunchControlIsPushSuccessfully(self): |
| 316 | """Test IS_PUSHED is changed if some launch control suites are scheduled.""" |
| 317 | lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975', |
| 318 | 'git_nyc-mr1-release/shamu-userdebug/3783920']} |
| 319 | lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release' |
| 320 | self.task.launch_control_branches = [ |
| 321 | t.lstrip() for t in lc_branches.split(',')] |
| 322 | self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL |
| 323 | self.assertTrue(self.task.schedule( |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 324 | lc_builds, ([], []), config_reader.LabConfig(None), FakeCIDBClient())) |
Xixuan Wu | 5451a66 | 2017-10-17 10:57:40 -0700 | [diff] [blame] | 325 | |
| 326 | def testScheduleCrosIsPushInvalidBoard(self): |
| 327 | """Test schedule no cros builds due to non-allowed board.""" |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 328 | branch_builds = ( |
Xixuan Wu | 8d2f286 | 2018-08-28 16:48:04 -0700 | [diff] [blame^] | 329 | {('%s_2' % self.BOARD, None, 'release', '56'): '0000.00.00'}, {}, |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 330 | ) |
Xixuan Wu | 5451a66 | 2017-10-17 10:57:40 -0700 | [diff] [blame] | 331 | self.task.os_type = build_lib.OS_TYPE_CROS |
| 332 | self.assertFalse(self.task.schedule( |
| 333 | [], branch_builds, config_reader.LabConfig(None), FakeCIDBClient())) |
| 334 | |
| 335 | def testScheduleLaunchControlIsPushInvalidBoard(self): |
| 336 | """Test schedule none of launch control builds due to board check.""" |
| 337 | lc_builds = {'%s_2' % self.BOARD: |
| 338 | ['git_nyc-mr2-release/shamu-userdebug/3844975', |
| 339 | 'git_nyc-mr1-release/shamu-userdebug/3783920']} |
| 340 | lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release' |
| 341 | self.task.launch_control_branches = [ |
| 342 | t.lstrip() for t in lc_branches.split(',')] |
| 343 | self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL |
| 344 | self.task._schedule_launch_control_builds(lc_builds) |
| 345 | self.assertFalse(self.task.schedule( |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 346 | lc_builds, ([], []), config_reader.LabConfig(None), FakeCIDBClient())) |