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