blob: 83bd3d0244e72abf623d1a4da81503c0aa2a50a0 [file] [log] [blame]
Xixuan Wuabbaa4c2017-08-23 17:31:49 -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 config reader unittests."""
6
7import unittest
8
9import config_reader
10import constants
11import mock
12import task
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070013import tot_manager
14
15
16class FakeTotMilestoneManager(tot_manager.TotMilestoneManager):
17
18 def __init__(self, is_sanity):
19 self.is_sanity = is_sanity
20 self.storage_client = None
21 self.tot = self._tot_milestone()
Xixuan Wuabbaa4c2017-08-23 17:31:49 -070022
23
24class BaseTaskConfigReaderTestCase(unittest.TestCase):
25
26 _EVENT_TYPE = 'nightly'
27 _PRIORITY = config_reader.EVENT_CLASSES[_EVENT_TYPE].PRIORITY
28 _TIMEOUT = config_reader.EVENT_CLASSES[_EVENT_TYPE].TIMEOUT
29 _TASK_NAME = 'fake_task'
30 _SUITE = 'fake_suite'
31 _BRANCH_SPEC = 'tot-1'
32 _POOL = 'bvt'
33 _NUM = 2
34 _BOARD = 'link'
35
36 def setUp(self):
37 self.config = config_reader.ConfigReader(None)
38 self.config.add_section(self._TASK_NAME)
39 self.config.set(self._TASK_NAME, 'suite', self._SUITE)
40 self.config.set(self._TASK_NAME, 'branch_specs', self._BRANCH_SPEC)
41 self.config.set(self._TASK_NAME, 'run_on', self._EVENT_TYPE)
42 self.config.set(self._TASK_NAME, 'pool', self._POOL)
43 self.config.set(self._TASK_NAME, 'num', '%d' % self._NUM)
44 self.config.set(self._TASK_NAME, 'boards', self._BOARD)
45
46 tot_patcher = mock.patch('tot_manager.TotMilestoneManager')
47 self._tot = tot_patcher.start()
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070048 self._tot.return_value = FakeTotMilestoneManager(True)
Xixuan Wuabbaa4c2017-08-23 17:31:49 -070049 self.addCleanup(tot_patcher.stop)
50
51
52class TaskCreationTestCase(BaseTaskConfigReaderTestCase):
53
54 def testCreateTaskFromConfigNonexistentSection(self):
55 """Ensure a Task CANNOT be built without suite, and raise error."""
56 task_config = config_reader.TaskConfig(self.config)
57 self.assertRaises(config_reader.MalformedConfigEntryError,
58 task_config._create_task_from_config_section,
59 'non-existent section')
60
61 def testCreateTaskFromConfigNotAllowedHeader(self):
62 """Ensure a Task CANNOT be built without suite, and raise error."""
63 self.config.set(self._TASK_NAME, 'non-valid-header', 'test')
64 task_config = config_reader.TaskConfig(self.config)
65 self.assertRaises(config_reader.MalformedConfigEntryError,
66 task_config._create_task_from_config_section,
67 self._TASK_NAME)
68
69 def testGetTaskByKeyword(self):
70 """Ensure a Task can be built from a correct config."""
71 task_config = config_reader.TaskConfig(self.config)
72 tasks = task_config.get_tasks_by_keyword(self._EVENT_TYPE)
73 self.assertEqual(len(tasks), 1)
74 new_task = tasks[0]
75 self.assertEqual(new_task.name, self._TASK_NAME)
76 self.assertEqual(new_task.suite, self._SUITE)
77 self.assertEqual(new_task.branch_specs, [self._BRANCH_SPEC])
78 self.assertEqual(new_task.pool, self._POOL)
79 self.assertEqual(new_task.num, self._NUM)
80 self.assertEqual(new_task.boards, task.split_str_to_list(self._BOARD))
81 self.assertEqual(new_task.priority, self._PRIORITY)
82 self.assertEqual(new_task.timeout, self._TIMEOUT)
83
84 def testGetTaskByKeywordCheckFileBug(self):
85 """Ensure a Task can be built from a correct config."""
86 task_config = config_reader.TaskConfig(self.config)
87 tasks = task_config.get_tasks_by_keyword(self._EVENT_TYPE)
88 self.assertFalse(tasks[0].file_bugs)
89
90 def testGetTaskByKeywordNoRunonNoRaise(self):
91 """Ensure a Task CANNOT be built without run_on, but not raise error."""
92 self.config.remove_option(self._TASK_NAME, 'run_on')
93 task_config = config_reader.TaskConfig(self.config)
94 try:
95 tasks = task_config.get_tasks_by_keyword(self._EVENT_TYPE)
96 self.assertEqual(len(tasks), 0)
97 except config_reader.MalformedConfigEntryError:
98 self.fail('task_config should not raise error without run_on!')
99
100 def testGetTaskByKeywordNoSuiteNoRaise(self):
101 """Ensure a Task CANNOT be built without suite, but not raise error.
102
103 This is to ensure a malformed task won't affect other tasks' reading.
104 """
105 self.config.remove_option(self._TASK_NAME, 'suite')
106 task_config = config_reader.TaskConfig(self.config)
107 try:
108 tasks = task_config.get_tasks_by_keyword(self._EVENT_TYPE)
109 self.assertEqual(len(tasks), 0)
110 except config_reader.MalformedConfigEntry:
111 self.fail('task_config should not raise error without suite!')
112
113 def testGetTaskByKeywordNoBranch(self):
114 """Ensure a Task can be built without branch_specs."""
115 self.config.remove_option(self._TASK_NAME, 'branch_specs')
116 task_config = config_reader.TaskConfig(self.config)
117 tasks = task_config.get_tasks_by_keyword(self._EVENT_TYPE)
118 self.assertEqual(tasks[0].branch_specs, [])
119
120 def testGetTaskByKeywordNoNum(self):
121 """Ensure a Task can be built without num."""
122 self.config.remove_option(self._TASK_NAME, 'num')
123 task_config = config_reader.TaskConfig(self.config)
124 tasks = task_config.get_tasks_by_keyword(self._EVENT_TYPE)
125 self.assertIsNone(tasks[0].num)
126
127 def testSetPriority(self):
128 """Ensure priority can be set by user."""
129 priority = constants.Priorities.DEFAULT
130 self.config.set(self._TASK_NAME, 'priority', str(priority))
131 task_config = config_reader.TaskConfig(self.config)
132 tasks = task_config.get_tasks_by_keyword(self._EVENT_TYPE)
133 self.assertEqual(tasks[0].priority, priority)
134
135 def testSetTimeout(self):
136 """Ensure timeout can be set by user."""
137 timeout = 1000
138 self.config.set(self._TASK_NAME, 'timeout', str(timeout))
139 task_config = config_reader.TaskConfig(self.config)
140 tasks = task_config.get_tasks_by_keyword(self._EVENT_TYPE)
141 self.assertEqual(tasks[0].timeout, timeout)
142
143
144class TaskInfoCheckTestCase(BaseTaskConfigReaderTestCase):
145
146 def testNonIntNum(self):
147 """Ensure a Task's num is a Integer."""
148 self.config.set(self._TASK_NAME, 'num', 'non_int')
149 task_config = config_reader.TaskConfig(self.config)
150 self.assertRaises(config_reader.MalformedConfigEntryError,
151 task_config._read_task_section,
152 self._TASK_NAME)
153
154 def testNonIntHour(self):
155 """Ensure a Task's hour is a Integer."""
156 self.config.set(self._TASK_NAME, 'hour', 'non_int')
157 task_config = config_reader.TaskConfig(self.config)
158 self.assertRaises(config_reader.MalformedConfigEntryError,
159 task_config._read_task_section,
160 self._TASK_NAME)
161
162 def testNonIntDay(self):
163 """Ensure a Task's hour is a Integer."""
164 self.config.set(self._TASK_NAME, 'day', 'non_int')
165 task_config = config_reader.TaskConfig(self.config)
166 self.assertRaises(config_reader.MalformedConfigEntryError,
167 task_config._read_task_section,
168 self._TASK_NAME)
169
170 def testNoSuite(self):
171 """Ensure a Task's suite exists."""
172 self.config.remove_option(self._TASK_NAME, 'suite')
173 task_config = config_reader.TaskConfig(self.config)
174 task_info = task_config._read_task_section(self._TASK_NAME)
175 self.assertRaises(config_reader.MalformedConfigEntryError,
176 task_config._validate_task_section,
177 task_info)
178
179 def testInvalidIntHour(self):
180 """Ensure a Task's hour is in 0~23."""
181 self.config.set(self._TASK_NAME, 'hour', '24')
182 task_config = config_reader.TaskConfig(self.config)
183 task_info = task_config._read_task_section(self._TASK_NAME)
184 self.assertRaises(config_reader.MalformedConfigEntryError,
185 task_config._validate_task_section,
186 task_info)
187
188 def testNoHourForNonNightlyEvent(self):
189 """Ensure a non-nightly Task cannot specify hour."""
190 self.config.set(self._TASK_NAME, 'hour', '10')
191 self.config.set(self._TASK_NAME, 'run_on', 'new_build')
192 task_config = config_reader.TaskConfig(self.config)
193 task_info = task_config._read_task_section(self._TASK_NAME)
194 self.assertRaises(config_reader.MalformedConfigEntryError,
195 task_config._validate_task_section,
196 task_info)
197
198 def testInvalidIntDay(self):
199 """Ensure a Task's day is in 0~6."""
200 self.config.set(self._TASK_NAME, 'day', '10')
201 self.config.set(self._TASK_NAME, 'run_on', 'weekly')
202 task_config = config_reader.TaskConfig(self.config)
203 task_info = task_config._read_task_section(self._TASK_NAME)
204 self.assertRaises(config_reader.MalformedConfigEntryError,
205 task_config._validate_task_section,
206 task_info)
207
208 def testNoDayForNonWeeklyEvent(self):
209 """Ensure a non-weekly Task cannot specify day."""
210 self.config.set(self._TASK_NAME, 'day', '1')
211 self.config.set(self._TASK_NAME, 'run_on', 'nightly')
212 task_config = config_reader.TaskConfig(self.config)
213 task_info = task_config._read_task_section(self._TASK_NAME)
214 self.assertRaises(config_reader.MalformedConfigEntryError,
215 task_config._validate_task_section,
216 task_info)
217
218 def testInValidOSType(self):
219 """Ensure a Task's os_type is valid."""
220 self.config.set(self._TASK_NAME, 'os_type', 'invalid')
221 task_config = config_reader.TaskConfig(self.config)
222 task_info = task_config._read_task_section(self._TASK_NAME)
223 self.assertRaises(config_reader.MalformedConfigEntryError,
224 task_config._check_os_type,
225 task_info)
226
227 def testNoLaunchControlForCrOS(self):
228 """Ensure a CrOS Task doesn't have launch control settings."""
229 self.config.set(self._TASK_NAME, 'os_type', 'CrOS')
230 self.config.set(self._TASK_NAME, 'branches', 'shamu-userdebug')
231 task_config = config_reader.TaskConfig(self.config)
232 task_info = task_config._read_task_section(self._TASK_NAME)
233 self.assertRaises(config_reader.MalformedConfigEntryError,
234 task_config._check_os_type,
235 task_info)
236
237 def testLaunchControlForNonCrOS(self):
238 """Ensure an android Task has launch control settings."""
239 self.config.set(self._TASK_NAME, 'os_type', 'android')
240 task_config = config_reader.TaskConfig(self.config)
241 task_info = task_config._read_task_section(self._TASK_NAME)
242 self.assertRaises(config_reader.MalformedConfigEntryError,
243 task_config._check_os_type,
244 task_info)
245
246 def testGetTestbedCount(self):
247 """Ensure testbed count can be successfully parsed."""
248 task_config = config_reader.TaskConfig(self.config)
249 self.assertEqual(task_config._get_testbed_dut_count('sharu-2'), 2)
250
251 def testNoTestbedForCrOS(self):
252 """Ensure CrOS task won't specify testbed parameters."""
253 self.config.set(self._TASK_NAME, 'boards', 'sharu-2')
254 task_config = config_reader.TaskConfig(self.config)
255 task_info = task_config._read_task_section(self._TASK_NAME)
256 self.assertRaises(config_reader.MalformedConfigEntryError,
257 task_config._check_testbed_parameter,
258 task_info)
259
260
261class LabReaderTestCase(unittest.TestCase):
262
263 def setUp(self):
264 self.config = config_reader.ConfigReader(None)
265 self.config.add_section(config_reader.ANDROID_BOARD)
266 self.config.add_section(config_reader.RELEASED_RO_BUILDS)
267
268 def testGetAndroidBoardList(self):
269 """Ensure android board list can be correctly fetched."""
270 self.config.set(config_reader.ANDROID_BOARD, 'board_list',
271 'abox_edge,android-angler-1,angler-6')
272 lab_config = config_reader.LabConfig(self.config)
273 self.assertEqual(lab_config.get_android_board_list(),
274 set(['abox_edge', 'android-angler', 'angler']))
275
276 def testGetInvalidAndroidBoardListEntry(self):
277 """Ensure ValueError is raised if no valid board list entry."""
278 self.config.set(config_reader.ANDROID_BOARD, 'invalid_board_list',
279 'abox_edge,android-angler-1,angler-6')
280 lab_config = config_reader.LabConfig(self.config)
281 self.assertRaises(ValueError, lab_config.get_android_board_list)
282
283 def testGetEmptyAndroidBoardListEntry(self):
284 """Ensure ValueError is raised if no android board list is found."""
285 lab_config = config_reader.LabConfig(self.config)
286 self.assertRaises(ValueError, lab_config.get_android_board_list)
287
288 def testGetFirmwareROBuildList(self):
289 """Ensure firmware ro build can be successfully found."""
290 self.config.set(config_reader.RELEASED_RO_BUILDS, 'link', 'firmware')
291 lab_config = config_reader.LabConfig(self.config)
292 self.assertEqual(lab_config.get_firmware_ro_build_list('link'),
293 'firmware')
294
295 def testGetEmptyFirmwareROBuildList(self):
296 """Ensure ValueError is raised if no firmware ro build is found."""
297 lab_config = config_reader.LabConfig(self.config)
298 self.assertRaises(ValueError,
299 lab_config.get_firmware_ro_build_list, 'link')
300
301
302if __name__ == '__main__':
303 unittest.main()