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