blob: fe5f2a3323348631d9f02dc550391d2a069c2027 [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."""
6
7import re
8import unittest
9
Xixuan Wu5451a662017-10-17 10:57:40 -070010import build_lib
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070011import cloud_sql_client
12import config_reader
13import mock
14import MySQLdb
15import task
16import tot_manager
17
18
19class 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
28class 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],
39 milestone=TaskTestCase.MILESTONE,
40 platform=TaskTestCase.PLATFORM,
41 build_config=build_config)
42
43
Xixuan Wu5451a662017-10-17 10:57:40 -070044class TaskBaseTestCase(unittest.TestCase):
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070045
46 BOARD = 'fake_board'
47 NAME = 'fake_name'
48 SUITE = 'fake_suite'
49 POOL = 'fake_pool'
50 PLATFORM = '6182.0.0-rc2'
51 MILESTONE = '30'
52 NUM = 1
53 PRIORITY = 50
54 TIMEOUT = 600
55
56 def setUp(self):
57 tot_patcher = mock.patch('tot_manager.TotMilestoneManager')
58 self._mock_tot = tot_patcher.start()
59 self.addCleanup(tot_patcher.stop)
60 # Can't pass in False since storage_client is not mocked.
61 self._mock_tot.return_value = FakeTotMilestoneManager(True)
62 self.task = task.Task(config_reader.TaskInfo(
63 name=self.NAME,
64 suite=self.SUITE,
65 branch_specs=[],
66 pool=self.POOL,
67 num=self.NUM,
68 boards=self.BOARD,
69 priority=self.PRIORITY,
70 timeout=self.TIMEOUT))
71
Xixuan Wu5451a662017-10-17 10:57:40 -070072
73class TaskTestCase(TaskBaseTestCase):
74
75 def setUp(self):
76 super(TaskTestCase, self).setUp()
77
Xixuan Wu0c76d5b2017-08-30 16:40:17 -070078 mock_push = mock.patch('task.Task._push_suite')
79 self._mock_push = mock_push.start()
80 self.addCleanup(mock_push.stop)
81
82 def testSetSpecCompareInfoEqual(self):
83 """Test compare info setting for specs that equals to a milestone."""
84 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-2')
85 self.task._set_spec_compare_info()
86 self.assertTrue(self.task._version_equal_constraint)
87 self.assertFalse(self.task._version_gte_constraint)
88 self.assertFalse(self.task._version_lte_constraint)
89 self.assertEqual(self.task._bare_branches, [])
90
91 def testSetSpecCompareInfoLess(self):
92 """Test compare info setting for specs that is less than a milestone."""
93 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot')
94 self.task._set_spec_compare_info()
95 self.assertFalse(self.task._version_equal_constraint)
96 self.assertFalse(self.task._version_gte_constraint)
97 self.assertTrue(self.task._version_lte_constraint)
98 self.assertEqual(self.task._bare_branches, [])
99
100 def testSetSpecCompareInfoGreater(self):
101 """Test compare info setting for specs that is greater than a milestone."""
102 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
103 self.task._set_spec_compare_info()
104 self.assertFalse(self.task._version_equal_constraint)
105 self.assertTrue(self.task._version_gte_constraint)
106 self.assertFalse(self.task._version_lte_constraint)
107 self.assertEqual(self.task._bare_branches, [])
108
109 def testFitsSpecEqual(self):
110 """Test milestone check for specs that equals to a milestone."""
111 self.task.branch_specs = re.split(r'\s*,\s*', '==tot-1')
112 self.task._set_spec_compare_info()
113 self.assertFalse(self.task._fits_spec('40'))
114 self.assertTrue(self.task._fits_spec('39'))
115 self.assertFalse(self.task._fits_spec('38'))
116
117 def testFitsSpecLess(self):
118 """Test milestone check for specs that is less than a milestone."""
119 self.task.branch_specs = re.split(r'\s*,\s*', '<=tot-1')
120 self.task._set_spec_compare_info()
121 self.assertFalse(self.task._fits_spec('40'))
122 self.assertTrue(self.task._fits_spec('39'))
123 self.assertTrue(self.task._fits_spec('38'))
124
125 def testFitsSpecGreater(self):
126 """Test milestone check for specs that is greater than a milestone."""
127 self.task.branch_specs = re.split(r'\s*,\s*', '>=tot-2')
128 self.task._set_spec_compare_info()
129 self.assertTrue(self.task._fits_spec('39'))
130 self.assertTrue(self.task._fits_spec('38'))
131 self.assertFalse(self.task._fits_spec('37'))
132
133 def testGetFirmwareFromLabConfig(self):
134 """Test get firmware from lab config successfully."""
135 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
136 return_value='build1,build2'):
137 firmware = self.task._get_firmware_build(
138 'released_ro_2', self.BOARD, config_reader.LabConfig(None), None)
139 self.assertEqual(firmware, 'build2')
140
141 def testGetFirmwareFromLabConfigOutofIndex(self):
142 """Test get firmware from lab config when firmware index is invalid."""
143 with mock.patch('config_reader.LabConfig.get_firmware_ro_build_list',
144 return_value='build1,build2'):
145 self.assertRaises(ValueError,
146 self.task._get_latest_firmware_build_from_lab_config,
147 'released_ro_3',
148 self.BOARD,
149 config_reader.LabConfig(None))
150 self.assertIsNone(self.task._get_firmware_build(
151 'released_ro_3', self.BOARD, config_reader.LabConfig(None), None))
152
153 def testGetFirmwareFromDB(self):
154 """Test get firmware from DB successfully."""
155 firmware = self.task._get_firmware_build(
156 'firmware', self.BOARD, None, FakeCIDBClient())
157 self.assertEqual(
158 firmware,
159 '%s-firmware/R%s-%s' % (self.BOARD, self.MILESTONE, self.PLATFORM))
160
161 def testGetFirmwareFromDBConnectionError(self):
162 """Test get firmware from DB when DB connection is failed."""
163 self.assertIsNone(self.task._get_firmware_build(
164 'firmware', self.BOARD, None, FakeCIDBClient(success=False)))
165
166 def testScheduleCrosSuccessfully(self):
167 """Test schedule cros builds successfully."""
168 branch_builds = {(self.BOARD, 'release', '56'): '0000.00.00'}
169 self.task._schedule_cros_builds(branch_builds,
170 config_reader.LabConfig(None),
171 FakeCIDBClient())
172 self.assertEqual(self._mock_push.call_count, 1)
173
174 def testScheduleCrosNonvalidBoard(self):
175 """Test schedule no cros builds due to non-allowed board."""
176 branch_builds = {('%s_2' % self.BOARD, 'release', '56'): '0000.00.00'}
177 self.task._schedule_cros_builds(branch_builds,
178 config_reader.LabConfig(None),
179 FakeCIDBClient())
180 self.assertEqual(self._mock_push.call_count, 0)
181
182 def testScheduleCrosNonValidSpec(self):
183 """Test schedule no cros builds due to non-allowed branch milestone."""
184 branch_builds = {(self.BOARD, 'release', '56'): '0000.00.00'}
185 # Only run tasks whose milestone = tot (R40)
186 self.task.branch_specs = re.split(r'\s*,\s*', '==tot')
187 self.task._set_spec_compare_info()
188 self.task._schedule_cros_builds(branch_builds,
189 config_reader.LabConfig(None),
190 FakeCIDBClient())
191 self.assertEqual(self._mock_push.call_count, 0)
192
193 def testScheduleCrosSuccessfullyWithValidFirmware(self):
194 """Test schedule cros builds successfully with valid firmware."""
195 branch_builds = {(self.BOARD, 'release', '56'): '0000.00.00'}
196 self.task.firmware_ro_build_spec = 'firmware'
197 self.task._schedule_cros_builds(branch_builds,
198 config_reader.LabConfig(None),
199 FakeCIDBClient())
200 self.assertEqual(self._mock_push.call_count, 1)
201
202 def testScheduleCrosWithNonValidFirmware(self):
203 """Test schedule no cros builds due to non-existent firmware."""
204 branch_builds = {(self.BOARD, 'release', '56'): '0000.00.00'}
205 self.task.firmware_ro_build_spec = 'firmware'
206 self.task._schedule_cros_builds(branch_builds,
207 config_reader.LabConfig(None),
208 FakeCIDBClient(success=False))
209 self.assertEqual(self._mock_push.call_count, 0)
210
211 def testScheduleLaunchControlWithFullBranches(self):
212 """Test schedule all launch control builds successfully."""
213 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
214 'git_nyc-mr1-release/shamu-userdebug/3783920']}
215 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
216 self.task.launch_control_branches = [
217 t.lstrip() for t in lc_branches.split(',')]
218 self.task._schedule_launch_control_builds(lc_builds)
219 self.assertEqual(self._mock_push.call_count, 2)
220
221 def testScheduleLaunchControlWithPartlyBranches(self):
222 """Test schedule part of launch control builds due to branch check."""
223 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
224 'git_nyc-mr1-release/shamu-userdebug/3783920']}
225 lc_branches = 'git_nyc-mr1-release'
226 self.task.launch_control_branches = [
227 t.lstrip() for t in lc_branches.split(',')]
228 self.task._schedule_launch_control_builds(lc_builds)
229 self.assertEqual(self._mock_push.call_count, 1)
230
231 def testScheduleLaunchControlWithNoBranches(self):
232 """Test schedule none of launch control builds due to branch check."""
233 lc_builds = {self.BOARD: ['git_nyc-mr2-release/shamu-userdebug/3844975',
234 'git_nyc-mr1-release/shamu-userdebug/3783920']}
235 self.task.launch_control_branches = []
236 self.task._schedule_launch_control_builds(lc_builds)
237 self.assertEqual(self._mock_push.call_count, 0)
238
239 def testScheduleLaunchControlNonvalidBoard(self):
240 """Test schedule none of launch control builds due to board check."""
241 lc_builds = {'%s_2' % self.BOARD:
242 ['git_nyc-mr2-release/shamu-userdebug/3844975',
243 'git_nyc-mr1-release/shamu-userdebug/3783920']}
244 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
245 self.task.launch_control_branches = [
246 t.lstrip() for t in lc_branches.split(',')]
247 self.task._schedule_launch_control_builds(lc_builds)
248 self.assertEqual(self._mock_push.call_count, 0)
Xixuan Wu5451a662017-10-17 10:57:40 -0700249
250
251class TaskPushTestCase(TaskBaseTestCase):
252
253 def setUp(self):
254 super(TaskPushTestCase, self).setUp()
255 mock_push = mock.patch('task_executor.push')
256 self._mock_push = mock_push.start()
257 self.addCleanup(mock_push.stop)
258
259 def testScheduleCrOSIsPushSuccessfully(self):
260 """Test IS_PUSHED is changed if some CrOS suites are scheduled."""
261 branch_builds = {(self.BOARD, 'release', '56'): '0000.00.00'}
262 self.task.os_type = build_lib.OS_TYPE_CROS
263 self.assertTrue(self.task.schedule(
264 [], branch_builds, config_reader.LabConfig(None), FakeCIDBClient()))
265
266 def testScheduleLaunchControlIsPushSuccessfully(self):
267 """Test IS_PUSHED is changed if some launch control suites are scheduled."""
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.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
274 self.assertTrue(self.task.schedule(
275 lc_builds, [], config_reader.LabConfig(None), FakeCIDBClient()))
276
277 def testScheduleCrosIsPushInvalidBoard(self):
278 """Test schedule no cros builds due to non-allowed board."""
279 branch_builds = {('%s_2' % self.BOARD, 'release', '56'): '0000.00.00'}
280 self.task.os_type = build_lib.OS_TYPE_CROS
281 self.assertFalse(self.task.schedule(
282 [], branch_builds, config_reader.LabConfig(None), FakeCIDBClient()))
283
284 def testScheduleLaunchControlIsPushInvalidBoard(self):
285 """Test schedule none of launch control builds due to board check."""
286 lc_builds = {'%s_2' % self.BOARD:
287 ['git_nyc-mr2-release/shamu-userdebug/3844975',
288 'git_nyc-mr1-release/shamu-userdebug/3783920']}
289 lc_branches = 'git_nyc-mr1-release,git_nyc-mr2-release'
290 self.task.launch_control_branches = [
291 t.lstrip() for t in lc_branches.split(',')]
292 self.task.os_type = build_lib.OS_TYPES_LAUNCH_CONTROL
293 self.task._schedule_launch_control_builds(lc_builds)
294 self.assertFalse(self.task.schedule(
295 lc_builds, [], config_reader.LabConfig(None), FakeCIDBClient()))