Xixuan Wu | 197b065 | 2017-08-28 15:46:25 -0700 | [diff] [blame] | 1 | # 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 build_event unittests.""" |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame^] | 6 | # pylint: disable=g-missing-super-call |
Xixuan Wu | 197b065 | 2017-08-28 15:46:25 -0700 | [diff] [blame] | 7 | |
| 8 | import datetime |
| 9 | import unittest |
| 10 | |
| 11 | import build_event |
| 12 | import config_reader |
| 13 | import datastore_client |
| 14 | import mock |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame^] | 15 | import task_config_reader |
Xixuan Wu | 197b065 | 2017-08-28 15:46:25 -0700 | [diff] [blame] | 16 | import time_converter |
| 17 | |
| 18 | from google.appengine.ext import ndb |
| 19 | from google.appengine.ext import testbed |
| 20 | |
| 21 | |
| 22 | class BuildEventTestCase(unittest.TestCase): |
| 23 | |
| 24 | _KLASS = build_event.NewBuild |
| 25 | |
| 26 | def setUp(self): |
| 27 | mock_utc_now = mock.patch('time_converter.utc_now') |
| 28 | self._mock_utc_now = mock_utc_now.start() |
| 29 | self.addCleanup(mock_utc_now.stop) |
| 30 | |
| 31 | self.testbed = testbed.Testbed() |
| 32 | self.testbed.activate() |
| 33 | self.addCleanup(self.testbed.deactivate) |
| 34 | self.testbed.init_datastore_v3_stub() |
| 35 | self.testbed.init_memcache_stub() |
| 36 | ndb.get_context().clear_cache() |
| 37 | |
| 38 | self.config = config_reader.ConfigReader(None) |
| 39 | self.utc_now = datetime.datetime(2017, 8, 1, 2, 15, |
| 40 | tzinfo=time_converter.UTC_TZ) |
| 41 | self._mock_utc_now.return_value = self.utc_now |
| 42 | |
| 43 | def testInitBuildEventWithoutLastExec(self): |
| 44 | """Test that init a Build Event without last_exec_utc in datastore.""" |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame^] | 45 | task_config = task_config_reader.TaskConfig(self.config) |
Xixuan Wu | 197b065 | 2017-08-28 15:46:25 -0700 | [diff] [blame] | 46 | last_exec_client = datastore_client.LastExecutionRecordStore() |
| 47 | event = self._KLASS( |
| 48 | task_config.get_event_setting(self._KLASS.section_name()), |
| 49 | last_exec_client.get_last_execute_time(self._KLASS.KEYWORD)) |
| 50 | self.assertEqual(event.target_exec_utc, self.utc_now) |
| 51 | self.assertEqual(event.last_exec_utc, self.utc_now) |
| 52 | self.assertEqual(event.since_date, self.utc_now) |
| 53 | self.assertFalse(event.always_handle) |
| 54 | self.assertFalse(event.should_handle) |
| 55 | |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 56 | def testInitBuildEventWithoutLastExecWithGAETesting(self): |
| 57 | """Test BuildEvent without last exec will be handled in testing on GAE.""" |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame^] | 58 | task_config = task_config_reader.TaskConfig(self.config) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 59 | last_exec_client = datastore_client.LastExecutionRecordStore() |
| 60 | |
| 61 | with mock.patch('global_config.GAE_TESTING', return_value=True): |
| 62 | event = self._KLASS( |
| 63 | task_config.get_event_setting(self._KLASS.section_name()), |
| 64 | last_exec_client.get_last_execute_time(self._KLASS.KEYWORD)) |
| 65 | self.assertEqual(event.target_exec_utc, self.utc_now) |
| 66 | self.assertEqual(event.last_exec_utc, self.utc_now) |
| 67 | self.assertEqual(event.since_date, self.utc_now) |
| 68 | self.assertFalse(event.always_handle) |
| 69 | self.assertTrue(event.should_handle) |
| 70 | |
Xixuan Wu | 197b065 | 2017-08-28 15:46:25 -0700 | [diff] [blame] | 71 | def testInitBuildEventWithValidLastExec(self): |
| 72 | """Test that init a Build Event with a valid last_exec_utc in datastore.""" |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame^] | 73 | task_config = task_config_reader.TaskConfig(self.config) |
Xixuan Wu | 197b065 | 2017-08-28 15:46:25 -0700 | [diff] [blame] | 74 | last_exec_utc = self.utc_now - datetime.timedelta( |
| 75 | hours=self._KLASS.LAST_EXEC_INTERVAL-1) |
| 76 | last_exec_client = datastore_client.LastExecutionRecordStore() |
| 77 | last_exec_client.set_last_execute_time(self._KLASS.KEYWORD, last_exec_utc) |
| 78 | event = self._KLASS( |
| 79 | task_config.get_event_setting(self._KLASS.section_name()), |
| 80 | last_exec_client.get_last_execute_time(self._KLASS.KEYWORD)) |
| 81 | self.assertEqual(event.target_exec_utc, self.utc_now) |
| 82 | self.assertEqual(event.last_exec_utc, last_exec_utc) |
| 83 | self.assertEqual(event.since_date, last_exec_utc) |
| 84 | self.assertFalse(event.always_handle) |
| 85 | self.assertTrue(event.should_handle) |
| 86 | |
| 87 | def testInitBuildEventWithOutdatedLastExec(self): |
| 88 | """Test that init a BuildEvent with a non-valid last_exec_utc.""" |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame^] | 89 | task_config = task_config_reader.TaskConfig(self.config) |
Xixuan Wu | 197b065 | 2017-08-28 15:46:25 -0700 | [diff] [blame] | 90 | last_exec_utc = self.utc_now - datetime.timedelta( |
| 91 | hours=self._KLASS.LAST_EXEC_INTERVAL+1) |
| 92 | last_exec_client = datastore_client.LastExecutionRecordStore() |
| 93 | last_exec_client.set_last_execute_time(self._KLASS.KEYWORD, last_exec_utc) |
| 94 | event = self._KLASS( |
| 95 | task_config.get_event_setting(self._KLASS.section_name()), |
| 96 | last_exec_client.get_last_execute_time(self._KLASS.KEYWORD)) |
| 97 | self.assertEqual(event.target_exec_utc, self.utc_now) |
| 98 | self.assertEqual(event.last_exec_utc, self.utc_now) |
| 99 | self.assertEqual(event.since_date, self.utc_now) |
| 100 | self.assertFalse(event.always_handle) |
| 101 | self.assertFalse(event.should_handle) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 102 | |
| 103 | def testInitBuildEventWithOutdatedLastExecWithGAETesting(self): |
| 104 | """Test BuildEvent with old last exec will be handled in testing on GAE.""" |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame^] | 105 | task_config = task_config_reader.TaskConfig(self.config) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 106 | last_exec_utc = self.utc_now - datetime.timedelta( |
| 107 | hours=self._KLASS.LAST_EXEC_INTERVAL+1) |
| 108 | last_exec_client = datastore_client.LastExecutionRecordStore() |
| 109 | last_exec_client.set_last_execute_time(self._KLASS.KEYWORD, last_exec_utc) |
| 110 | |
| 111 | with mock.patch('global_config.GAE_TESTING', return_value=True): |
| 112 | event = self._KLASS( |
| 113 | task_config.get_event_setting(self._KLASS.section_name()), |
| 114 | last_exec_client.get_last_execute_time(self._KLASS.KEYWORD)) |
| 115 | self.assertEqual(event.target_exec_utc, self.utc_now) |
| 116 | self.assertEqual(event.last_exec_utc, self.utc_now) |
| 117 | self.assertEqual(event.since_date, self.utc_now) |
| 118 | self.assertFalse(event.always_handle) |
| 119 | self.assertTrue(event.should_handle) |