blob: 743fce3f511fa9261e73ca6a83c4bb85f2a1990f [file] [log] [blame]
Xixuan Wu40998892017-08-29 14:32:26 -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 trigger_receiver unittests."""
6
7import datetime
Craig Bergstrom58263d32018-04-26 14:11:35 -06008import logging
Xixuan Wu40998892017-08-29 14:32:26 -07009import os
10import unittest
11
12import cloud_sql_client
13import config_reader
14import datastore_client
15import file_getter
16import mock
Craig Bergstrom58263d32018-04-26 14:11:35 -060017import task_executor
Xixuan Wu40998892017-08-29 14:32:26 -070018import time_converter
19import trigger_receiver
20
Craig Bergstrom58263d32018-04-26 14:11:35 -060021from google.appengine.api import taskqueue
Xixuan Wu40998892017-08-29 14:32:26 -070022from google.appengine.ext import ndb
23from google.appengine.ext import testbed
24
25# Ensure that SUITE_SCHEDULER_CONFIG_FILE is read only once.
26_SUITE_CONFIG_READER = config_reader.ConfigReader(
Xixuan Wu26d06e02017-09-20 14:50:28 -070027 file_getter.TEST_SUITE_SCHEDULER_CONFIG_FILE)
Xixuan Wu40998892017-08-29 14:32:26 -070028
29
30def now_generator(start_time, interval_min=30, last_days=7):
31 """A datetime.datetime.now generator.
32
33 The generator will generate 'now' from start_time till start_time+last_days
34 for every interval_min.
35
36 Args:
37 start_time: A datetime.datetime object representing the initial value of
38 the 'now'.
39 interval_min: The interval minutes between current 'now' and next 'now.
40 last_days: Representing how many days this generator will last.
41
42 Yields:
43 a datetime.datetime object to mock the current time.
44 """
45 cur_time = start_time
46 end_time = start_time + datetime.timedelta(days=last_days)
47 while cur_time < end_time:
48 yield cur_time
49 cur_time += datetime.timedelta(minutes=interval_min)
50
51
Xixuan Wu40998892017-08-29 14:32:26 -070052def _should_schedule_nightly_task(last_now, now):
53 """Check whether nightly task should be scheduled.
54
55 A nightly task should be schduled when next hour is coming.
56
57 Args:
58 last_now: the last time to check if nightly task should be scheduled
59 or not.
60 now: the current time to check if nightly task should be scheduled.
61
62 Returns:
63 a boolean indicating whether there will be nightly tasks scheduled.
64 """
65 if last_now is not None and last_now.hour != now.hour:
66 return True
67
68 return False
69
70
71def _should_schedule_weekly_task(last_now, now, weekly_time_info):
72 """Check whether weekly task should be scheduled.
73
74 A weekly task should be schduled when it comes to the default weekly tasks
75 scheduling hour in next day.
76
77 Args:
78 last_now: the last time to check if weekly task should be scheduled
79 or not.
80 now: the current time to check if weekly task should be scheduled.
81 weekly_time_info: the default weekly tasks scheduling time info.
82
83 Returns:
84 a boolean indicating whether there will be weekly tasks scheduled.
85 """
86 if (last_now is not None and last_now.hour != now.hour and
87 now.hour == weekly_time_info.hour):
88 return True
89
90 return False
91
92
93def _should_schedule_new_build_task(last_now, now):
94 """Check whether weekly task should be scheduled.
95
96 A new_build task should be schduled when there're new builds between last
97 check and this check.
98
99 Args:
100 last_now: the last time to check if new_build task should be scheduled.
101 now: the current time to check if new_build task should be scheduled.
102
103 Returns:
104 a boolean indicating whether there will be new_build tasks scheduled.
105 """
106 if last_now is not None and last_now != now:
107 return True
108
109 return False
110
111
112class FakeCIDBClient(object):
113 """Mock cloud_sql_client.CIDBClient."""
114
115 def get_passed_builds_since_date(self, since_date):
116 """Mock cloud_sql_client.CIDBClient.get_passed_builds_since_date."""
117 del since_date # unused
118 return [cloud_sql_client.BuildInfo('link', '62', '9868.0.0',
119 'link-release')]
120
121 def get_latest_passed_builds(self, build_config):
122 """Mock cloud_sql_client.CIDBClient.get_latest_passed_builds."""
123 del build_config # unused
124 return cloud_sql_client.BuildInfo('link', '62', '9868.0.0', build_config)
125
Craig Bergstrom58263d32018-04-26 14:11:35 -0600126 def get_relaxed_pased_builds_since_date(self, since_date):
127 """Mock cloud_sql_client.CIDBClient.get_relaxed_pased_builds_since_date."""
128 del since_date # unused
129 return [cloud_sql_client.BuildInfo('grunt', '63', '9968.0.0',
130 'grunt-release')]
131
Xixuan Wu40998892017-08-29 14:32:26 -0700132
133class FakeAndroidBuildRestClient(object):
134 """Mock rest_client.AndroidBuildRestClient."""
135
136 def get_latest_build_id(self, branch, target):
137 """Mock rest_client.AndroidBuildRestClient.get_latest_build_id."""
138 del branch, target # unused
139 return '100'
140
141
142class FakeLabConfig(object):
143 """Mock rest_client.AndroidBuildRestClient."""
144
145 def get_android_board_list(self):
146 """Mock config_reader.LabConfig.get_android_board_list."""
147 return ('android-angler', 'android-bullhead')
148
Xixuan Wu6fb16272017-10-19 13:16:00 -0700149 def get_cros_board_list(self):
150 """Mock config_reader.LabConfig.get_android_board_list."""
Craig Bergstrom58263d32018-04-26 14:11:35 -0600151 return ('grunt', 'link', 'peppy', 'daisy')
Xixuan Wu6fb16272017-10-19 13:16:00 -0700152
Xixuan Wu40998892017-08-29 14:32:26 -0700153 def get_firmware_ro_build_list(self, release_board):
154 """Mock config_reader.LabConfig.get_firmware_ro_build_list."""
155 del release_board # unused
156 return 'firmware1,firmware2'
157
C Shapiro7f24a002017-12-05 14:25:09 -0700158 def get_cros_model_map(self):
159 """Mock config_reader.LabConfig.get_cros_model_map."""
160 return {}
161
Xixuan Wu40998892017-08-29 14:32:26 -0700162
Xixuan Wu008ee832017-10-12 16:59:34 -0700163class TriggerReceiverBaseTestCase(unittest.TestCase):
Xixuan Wu40998892017-08-29 14:32:26 -0700164
165 def setUp(self):
166 self.testbed = testbed.Testbed()
167 self.testbed.activate()
168 self.addCleanup(self.testbed.deactivate)
169
170 self.testbed.init_datastore_v3_stub()
171 self.testbed.init_memcache_stub()
172 ndb.get_context().clear_cache()
173 self.testbed.init_taskqueue_stub(
174 root_path=os.path.join(os.path.dirname(__file__)))
175 self.taskqueue_stub = self.testbed.get_stub(
176 testbed.TASKQUEUE_SERVICE_NAME)
177
178 mock_cidb_client = mock.patch('cloud_sql_client.CIDBClient')
179 self._mock_cidb_client = mock_cidb_client.start()
180 self.addCleanup(mock_cidb_client.stop)
181
182 mock_android_client = mock.patch('rest_client.AndroidBuildRestClient')
183 self._mock_android_client = mock_android_client.start()
184 self.addCleanup(mock_android_client.stop)
185
Xixuan Wu40998892017-08-29 14:32:26 -0700186 mock_lab_config = mock.patch('config_reader.LabConfig')
187 self._mock_lab_config = mock_lab_config.start()
188 self.addCleanup(mock_lab_config.stop)
189
190 mock_utc_now = mock.patch('time_converter.utc_now')
191 self._mock_utc_now = mock_utc_now.start()
192 self.addCleanup(mock_utc_now.stop)
193
194 self._mock_cidb_client.return_value = FakeCIDBClient()
195 self._mock_android_client.return_value = FakeAndroidBuildRestClient()
Xixuan Wu40998892017-08-29 14:32:26 -0700196 self._mock_lab_config.return_value = FakeLabConfig()
197
Xixuan Wu008ee832017-10-12 16:59:34 -0700198
199class TriggerReceiverFakeConfigTestCase(TriggerReceiverBaseTestCase):
200
201 _TEST_PST_HOUR = 20
202 _TEST_PST_DAY = 4 # Friday
203 _TEST_EVENT_PST_HOUR = 13
204
205 _FAKE_NIGHTLY_TASK_NAME = 'fake_nightly_task'
206 _FAKE_WEEKLY_TASK_NAME = 'fake_weekly_task'
207
208 def setUp(self):
209 super(TriggerReceiverFakeConfigTestCase, self).setUp()
210
211 self.fake_config = config_reader.ConfigReader(None)
212 self._add_nightly_tasks(self.fake_config)
213 self._add_weekly_tasks(self.fake_config)
214
215 self.fake_config_with_settings = config_reader.ConfigReader(None)
216 self._add_weekly_tasks(self.fake_config_with_settings)
217 self._add_weekly_params(self.fake_config_with_settings)
218
219 mock_config_reader = mock.patch('config_reader.ConfigReader')
220 self._mock_config_reader = mock_config_reader.start()
221 self.addCleanup(mock_config_reader.stop)
222
223 def _add_nightly_tasks(self, fake_config):
224 fake_config.add_section(self._FAKE_NIGHTLY_TASK_NAME)
225 fake_config.set(self._FAKE_NIGHTLY_TASK_NAME, 'suite', 'fake_suite')
226 fake_config.set(self._FAKE_NIGHTLY_TASK_NAME, 'run_on', 'nightly')
227 fake_config.set(self._FAKE_NIGHTLY_TASK_NAME, 'hour',
228 str(self._TEST_PST_HOUR))
229
230 def _add_weekly_tasks(self, fake_config):
231 fake_config.add_section(self._FAKE_WEEKLY_TASK_NAME)
232 fake_config.set(self._FAKE_WEEKLY_TASK_NAME, 'suite', 'fake_suite')
233 fake_config.set(self._FAKE_WEEKLY_TASK_NAME, 'run_on', 'weekly')
234 fake_config.set(self._FAKE_WEEKLY_TASK_NAME, 'day', str(self._TEST_PST_DAY))
235
236 def _add_weekly_params(self, fake_config):
237 weekly_section_name = config_reader.EVENT_CLASSES['weekly'].section_name()
238 fake_config.add_section(weekly_section_name)
239 fake_config.set(weekly_section_name, 'hour', str(self._TEST_EVENT_PST_HOUR))
240
241 def testInitializeTriggerReceiverWithNightlyEvent(self):
242 """Test nightly event can be handled on right hour."""
243 # A task with hour=20 should be scheduled at 20:00 in PST everyday, which
244 # is 3:00/4:00 in UTC everyday.
245 self._mock_config_reader.return_value = self.fake_config
246 given_utc_hour = time_converter.convert_time_info(
247 time_converter.TimeInfo(None, self._TEST_PST_HOUR)).hour
248 utc_now = datetime.datetime(2017, 8, 6, given_utc_hour,
249 tzinfo=time_converter.UTC_TZ)
250 last_exec_client = datastore_client.LastExecutionRecordStore()
251 last_exec_client.set_last_execute_time(
252 'nightly', utc_now - datetime.timedelta(hours=1))
253 self._mock_utc_now.return_value = utc_now
254
255 suite_trigger = trigger_receiver.TriggerReceiver()
256 suite_trigger.cron()
257 self.assertTrue(suite_trigger.events['nightly'].should_handle)
258 self.assertEqual(len(suite_trigger.event_results['nightly']), 1)
259 self.assertEqual(suite_trigger.event_results['nightly'][0],
260 self._FAKE_NIGHTLY_TASK_NAME)
261
262 def testInitializeTriggerReceiverWithWeeklyEventWithoutEventHour(self):
263 """Test weekly event without event settings can be handled on right day."""
264 # A task with day=4 (Friday) and default event_hour (23) should be
265 # scheduled at Friday 23:00 in PST, which is Saturday 6:00 or 7:00 in UTC.
266 self._mock_config_reader.return_value = self.fake_config
267 given_utc_hour = time_converter.convert_time_info(
268 time_converter.TimeInfo(
269 self._TEST_PST_DAY,
270 config_reader.EVENT_CLASSES['weekly'].DEFAULT_PST_HOUR)).hour
271 utc_now = datetime.datetime(2017, 10, 14, given_utc_hour,
272 tzinfo=time_converter.UTC_TZ)
273 last_exec_client = datastore_client.LastExecutionRecordStore()
274 last_exec_client.set_last_execute_time(
275 'weekly', utc_now - datetime.timedelta(days=1))
276 self._mock_utc_now.return_value = utc_now
277
278 suite_trigger = trigger_receiver.TriggerReceiver()
279 suite_trigger.cron()
280 self.assertTrue(suite_trigger.events['weekly'].should_handle)
281 self.assertEqual(len(suite_trigger.event_results['weekly']), 1)
282 self.assertEqual(suite_trigger.event_results['weekly'][0],
283 self._FAKE_WEEKLY_TASK_NAME)
284
285 def testInitializeTriggerReceiverWithWeeklyEventWithEventHour(self):
286 """Test weekly event with event settings can be handled on right day."""
287 # A task with day=4 (Friday) and event_hour=13 should be scheduled at
288 # Friday 13:00 in PST, which is Friday 20:00 or 21:00 in UTC.
289 self._mock_config_reader.return_value = self.fake_config_with_settings
290 given_utc_time_info = time_converter.convert_time_info(
291 time_converter.TimeInfo(self._TEST_PST_DAY, self._TEST_EVENT_PST_HOUR))
292
293 # Set the current time as a Friday 20:00 or 21:00 in UTC.
294 utc_now = datetime.datetime(2017, 10, 13, given_utc_time_info.hour,
295 tzinfo=time_converter.UTC_TZ)
296 last_exec_client = datastore_client.LastExecutionRecordStore()
297 last_exec_client.set_last_execute_time(
298 'weekly', utc_now - datetime.timedelta(days=1))
299 self._mock_utc_now.return_value = utc_now
300
301 suite_trigger = trigger_receiver.TriggerReceiver()
302 suite_trigger.cron()
303 self.assertTrue(suite_trigger.events['weekly'].should_handle)
304 self.assertEqual(len(suite_trigger.event_results['weekly']), 1)
305 self.assertEqual(suite_trigger.event_results['weekly'][0],
306 self._FAKE_WEEKLY_TASK_NAME)
307
308
Craig Bergstrom58263d32018-04-26 14:11:35 -0600309class TriggerReceiverFakeBuildTestCase(TriggerReceiverBaseTestCase):
310 """Test the new_build functionality."""
311
312 _TEST_PST_HOUR = 20
313 _TEST_PST_DAY = 4 # Friday
314
315 def setUp(self):
316 """Set up for a test."""
317 super(TriggerReceiverFakeBuildTestCase, self).setUp()
318
319 # Construct a fake config so that we don't depend on the
320 # production configs not changing.
321 self.fake_config = config_reader.ConfigReader(None)
322 self._add_build_tasks(self.fake_config)
323 mock_config_reader = mock.patch('config_reader.ConfigReader')
324 self._mock_config_reader = mock_config_reader.start()
325 self.addCleanup(mock_config_reader.stop)
326 self._mock_config_reader.return_value = self.fake_config
327
328 def _add_build_tasks(self, fake_config):
329 """Add the build tasks to the fake config."""
330 fsnbt_name = 'FakeStrictNewBuildTask'
331 fake_config.add_section(fsnbt_name)
332 fake_config.set(fsnbt_name, 'run_on', 'new_build')
333 fake_config.set(fsnbt_name, 'suite', 'fake_suite_base')
334 frnbt_name = 'FakeRelaxedNewBuildTask'
335 fake_config.add_section(frnbt_name)
336 fake_config.set(frnbt_name, 'run_on', 'new_build')
337 fake_config.set(frnbt_name, 'suite', 'fake_suite_relaxed')
338 fake_config.set(frnbt_name, 'only_hwtest_sanity_required', 'True')
339
340 def testNewBuild(self):
341 """Test the new_build functionality."""
342 self._mock_utc_now.return_value = datetime.datetime.now(
343 time_converter.UTC_TZ)
344
345 # Fake out the TotMilestoneManager so we don't hit the discovery api.
346 # This will keep the test more hermetic.
347 mock_tot_mgr = mock.patch('config_reader.tot_manager.TotMilestoneManager')
348 mock_tot_mgr.start()
349 self.addCleanup(mock_tot_mgr.stop)
350
351 given_utc_hour = time_converter.convert_time_info(
352 time_converter.TimeInfo(None, self._TEST_PST_HOUR)).hour
353 utc_now = datetime.datetime(2017, 8, 6, given_utc_hour,
354 tzinfo=time_converter.UTC_TZ)
355 last_exec_client = datastore_client.LastExecutionRecordStore()
356 last_exec_client.set_last_execute_time(
357 'nightly', utc_now - datetime.timedelta(hours=1))
358 self._mock_utc_now.return_value = utc_now
359 start_time = datetime.datetime.now(time_converter.UTC_TZ)
360
361 queue = taskqueue.Queue(task_executor.SUITES_QUEUE)
362
363 # Run the trigger once. On the first run, it skips out
364 # early because it doesn't know when the last time it ran.
365 # This isn't really what this test is validating, but we're
366 # working around the current implementation.
367 self._mock_utc_now.return_value = start_time
368 suite_trigger = trigger_receiver.TriggerReceiver()
369 suite_trigger.cron()
370 # Validate the assumption that it skipped out early and didn't
371 # enqueue anything because if this assumption changes, the
372 # test probably needs to be fixed.
373 self.assertEqual(len(suite_trigger.event_results), 0)
374 tasks = queue.lease_tasks(3600, 10, deadline=0.5)
375 self.assertEqual(len(tasks), 0)
376
377 # Run again, this time it'll notice that it hasn't run in 30
378 # minutes and should enqueue some events.
379 self._mock_utc_now.return_value = (
380 start_time + datetime.timedelta(minutes=30))
381 suite_trigger = trigger_receiver.TriggerReceiver()
382 suite_trigger.cron()
383
384 # Validate that the expected tests got kicked off.
385 logging.info('event_results: %s',
386 suite_trigger.event_results)
387 self.assertEqual(len(suite_trigger.event_results), 1)
388 self.assertEqual(suite_trigger.event_results['new_build'],
389 ['FakeStrictNewBuildTask',
390 'FakeRelaxedNewBuildTask'])
391
392 # Now, make sure the expected tests get queued up based on the
393 # input we provided.
394 tasks = queue.lease_tasks(3600, 10, deadline=0.5)
395 logging.info('tasks: %s', tasks)
396 self.assertEqual(len(tasks), 2)
397
398 # If this starts breaking, you'll want to look at this.
399 logging.info('payload0: %s', tasks[0].payload)
400 logging.info('payload1: %s', tasks[1].payload)
401
402 # The number of builds that matched the base (success) Task.
403 count_base = 0
404 # The number of builds that matched the relaxed Task.
405 count_relaxed = 0
406 for task in tasks:
407 if 'fake_suite_base' in task.payload:
408 # Make sure it matched the expected build only.
409 self.assertIn('link-release', task.payload)
410 self.assertNotIn('grunt-release', task.payload)
411 count_base += 1
412
413 if 'fake_suite_relaxed' in task.payload:
414 self.assertIn('grunt-release', task.payload)
415 self.assertNotIn('link_release', task.payload)
416 count_relaxed += 1
417
418 # Make each case matched precisely one event.
419 self.assertEqual(1, count_base)
420 self.assertEqual(1, count_relaxed)
421
422
Xixuan Wu008ee832017-10-12 16:59:34 -0700423class TriggerReceiverRealConfigTestCase(TriggerReceiverBaseTestCase):
424
425 def setUp(self):
426 super(TriggerReceiverRealConfigTestCase, self).setUp()
427 mock_config_reader = mock.patch('config_reader.ConfigReader')
428 self._mock_config_reader = mock_config_reader.start()
429 self.addCleanup(mock_config_reader.stop)
430 self._mock_config_reader.return_value = _SUITE_CONFIG_READER
431
432 def _get_ground_truth_task_list_from_config(self):
433 """Get the ground truth of to-be-scheduled task list from config file."""
434 self._mock_utc_now.return_value = datetime.datetime.now(
435 time_converter.UTC_TZ)
436 task_config = config_reader.TaskConfig(_SUITE_CONFIG_READER)
437 tasks = {}
438 for keyword, klass in config_reader.EVENT_CLASSES.iteritems():
439 new_event = klass(
440 task_config.get_event_setting(klass.section_name()), None)
441 new_event.set_task_list(
442 task_config.get_tasks_by_keyword(klass.KEYWORD)['tasks'])
443 tasks[keyword] = new_event.task_list
444
445 return tasks
446
Xixuan Wu40998892017-08-29 14:32:26 -0700447 def testCronWithoutLastExec(self):
448 """Test the first round of cron can be successfully executed."""
449 self._mock_utc_now.return_value = datetime.datetime.now(
450 time_converter.UTC_TZ)
451 suite_trigger = trigger_receiver.TriggerReceiver()
452 suite_trigger.cron()
453 self.assertFalse(suite_trigger.events['nightly'].should_handle)
454 self.assertFalse(suite_trigger.events['weekly'].should_handle)
455 self.assertFalse(suite_trigger.events['new_build'].should_handle)
456
457 self.assertEqual(suite_trigger.event_results, {})
458
459 def testCronTriggerNightly(self):
460 """Test nightly event is read with available nightly last_exec_time."""
461 utc_now = datetime.datetime.now(time_converter.UTC_TZ)
462 last_exec_client = datastore_client.LastExecutionRecordStore()
463 last_exec_client.set_last_execute_time(
464 'nightly', utc_now - datetime.timedelta(hours=1))
465 self._mock_utc_now.return_value = utc_now
466 suite_trigger = trigger_receiver.TriggerReceiver()
467 self.assertTrue(suite_trigger.events['nightly'].should_handle)
468 self.assertFalse(suite_trigger.events['weekly'].should_handle)
469 self.assertFalse(suite_trigger.events['new_build'].should_handle)
470
Xixuan Wu33179672017-09-12 11:44:04 -0700471 def testCronTriggerNightlyOutdated(self):
472 """Test nightly event is read with available nightly last_exec_time."""
473 utc_now = datetime.datetime.now(time_converter.UTC_TZ)
474 last_exec_client = datastore_client.LastExecutionRecordStore()
475 last_exec_client.set_last_execute_time(
476 'nightly', utc_now - datetime.timedelta(days=3))
477 self._mock_utc_now.return_value = utc_now
478 suite_trigger = trigger_receiver.TriggerReceiver()
479 self.assertFalse(suite_trigger.events['nightly'].should_handle)
480
481 def testCronTriggerWeeklyOutdated(self):
482 """Test weekly event is read with available weekly last_exec_time."""
483 utc_now = datetime.datetime.now(time_converter.UTC_TZ)
484 last_exec_client = datastore_client.LastExecutionRecordStore()
485 last_exec_client.set_last_execute_time(
486 'weekly', utc_now - datetime.timedelta(days=8))
487 self._mock_utc_now.return_value = utc_now
488 suite_trigger = trigger_receiver.TriggerReceiver()
489 self.assertFalse(suite_trigger.events['weekly'].should_handle)
490
Xixuan Wu40998892017-08-29 14:32:26 -0700491 def testCronForWeeks(self):
492 """Ensure cron job can be successfully scheduled for several weeks."""
Xixuan Wu008ee832017-10-12 16:59:34 -0700493 all_tasks = self._get_ground_truth_task_list_from_config()
494 nightly_time_info = time_converter.convert_time_info(
Xixuan Wu40998892017-08-29 14:32:26 -0700495 time_converter.TimeInfo(
496 config_reader.EVENT_CLASSES['nightly'].DEFAULT_PST_DAY,
497 config_reader.EVENT_CLASSES['nightly'].DEFAULT_PST_HOUR))
Xixuan Wu008ee832017-10-12 16:59:34 -0700498 weekly_time_info = time_converter.convert_time_info(
Xixuan Wu40998892017-08-29 14:32:26 -0700499 time_converter.TimeInfo(
500 config_reader.EVENT_CLASSES['weekly'].DEFAULT_PST_DAY,
501 config_reader.EVENT_CLASSES['weekly'].DEFAULT_PST_HOUR))
502 last_now = None
503
504 for now in now_generator(datetime.datetime.now(time_converter.UTC_TZ)):
505 self._mock_utc_now.return_value = now
506 suite_trigger = trigger_receiver.TriggerReceiver()
Xixuan Wu5451a662017-10-17 10:57:40 -0700507 with mock.patch('task.Task.schedule', return_value=True):
508 suite_trigger.cron()
Xixuan Wu40998892017-08-29 14:32:26 -0700509
Xixuan Wu40998892017-08-29 14:32:26 -0700510 should_scheduled_nightly_tasks = [
511 t.name for t in all_tasks['nightly']
512 if (t.hour is not None and t.hour == now.hour) or
513 (t.hour is None and now.hour == nightly_time_info.hour)]
Xixuan Wu008ee832017-10-12 16:59:34 -0700514
Xixuan Wu40998892017-08-29 14:32:26 -0700515 if (_should_schedule_nightly_task(last_now, now) and
516 should_scheduled_nightly_tasks):
517 self.assertEqual(suite_trigger.event_results['nightly'],
518 should_scheduled_nightly_tasks)
519 else:
520 self.assertNotIn('nightly', suite_trigger.event_results.keys())
521
522 # Verify weekly tasks
523 should_scheduled_weekly_tasks = [
524 t.name for t in all_tasks['weekly']
525 if (t.day is not None and now.weekday() == t.day) or
526 (t.day is None and now.weekday() == weekly_time_info.weekday)]
527 if (_should_schedule_weekly_task(last_now, now, weekly_time_info) and
528 should_scheduled_weekly_tasks):
529 self.assertEqual(suite_trigger.event_results['weekly'],
530 should_scheduled_weekly_tasks)
531 else:
532 self.assertNotIn('weekly', suite_trigger.event_results.keys())
533
534 # Verify new_build tasks
535 should_scheduled_new_build_tasks = [
536 t.name for t in all_tasks['new_build']]
537 if (_should_schedule_new_build_task(last_now, now) and
538 should_scheduled_new_build_tasks):
539 self.assertEqual(suite_trigger.event_results['new_build'],
540 should_scheduled_new_build_tasks)
541 else:
542 self.assertNotIn('new_build', suite_trigger.event_results.keys())
543
544 last_now = now
545
546
547if __name__ == '__main__':
548 unittest.main()