Exemplo n.º 1
0
 def setUp(self):
     super(DeferredCharmServiceEventsTestCase, self).setUp()
     self.tmp_dir = tempfile.mkdtemp()
     self.addCleanup(lambda: shutil.rmtree(self.tmp_dir))
     self.patch_object(deferred_events.hookenv, 'service_name')
     self.service_name.return_value = 'myapp'
     self.patch_object(deferred_events.unitdata, 'HookData')
     self.db = TestDB()
     self.HookData.return_value = TestHookData(self.db)
     self.exp_event_a = deferred_events.ServiceEvent(
         timestamp=123,
         service='svcA',
         reason='ReasonA',
         action='restart',
         policy_requestor_name='myapp',
         policy_requestor_type='charm')
     self.exp_event_b = deferred_events.ServiceEvent(timestamp=223,
                                                     service='svcB',
                                                     reason='ReasonB',
                                                     action='restart')
     self.exp_event_c = deferred_events.ServiceEvent(timestamp=323,
                                                     service='svcB',
                                                     reason='ReasonB',
                                                     action='restart')
     self.base_expect_events = [
         self.exp_event_a, self.exp_event_b, self.exp_event_c
     ]
     self.event_file_pair = []
     for index, event in enumerate(self.base_expect_events):
         event_file = '{}/{}.deferred'.format('/tmpdir', str(index))
         self.event_file_pair.append((event_file, event))
Exemplo n.º 2
0
    def test_check_restart_timestamps(self, get_service_start_time, log,
                                      clear_deferred_restarts,
                                      get_deferred_restarts):
        deferred_restarts = [
            # 'Tue 2021-02-02 10:19:55 UTC'
            deferred_events.ServiceEvent(timestamp=1612261195.0,
                                         service='svcA',
                                         reason='ReasonA',
                                         action='restart')
        ]
        get_deferred_restarts.return_value = deferred_restarts
        get_service_start_time.return_value = datetime.datetime.strptime(
            'Tue 2021-02-02 13:19:55 UTC', '%a %Y-%m-%d %H:%M:%S %Z')
        deferred_events.check_restart_timestamps()
        clear_deferred_restarts.assert_called_once_with(['svcA'])

        clear_deferred_restarts.reset_mock()
        get_service_start_time.return_value = datetime.datetime.strptime(
            'Tue 2021-02-02 10:10:55 UTC', '%a %Y-%m-%d %H:%M:%S %Z')
        deferred_events.check_restart_timestamps()
        self.assertFalse(clear_deferred_restarts.called)
        log.assert_called_once_with(
            ('Restart still required, svcA was started at 2021-02-02 10:10:55,'
             ' restart was requested after that at 2021-02-02 10:19:55'),
            level='DEBUG')
Exemplo n.º 3
0
 def test_get_deferred_restarts(self, get_deferred_events):
     test_events = copy.deepcopy(self.base_expect_events)
     test_events.append(
         deferred_events.ServiceEvent(timestamp=523,
                                      service='svcD',
                                      reason='StopReasonD',
                                      action='stop'))
     get_deferred_events.return_value = test_events
     self.assertEqual(deferred_events.get_deferred_restarts(),
                      self.base_expect_events)
Exemplo n.º 4
0
 def test_duplicate_event_files(self, _deferred_events):
     _deferred_events.return_value = self.event_file_pair
     self.assertEqual(
         deferred_events.duplicate_event_files(self.exp_event_b),
         ['/tmpdir/1.deferred', '/tmpdir/2.deferred'])
     self.assertEqual(
         deferred_events.duplicate_event_files(
             deferred_events.ServiceEvent(timestamp=223,
                                          service='svcX',
                                          reason='ReasonX',
                                          action='restart')), [])
Exemplo n.º 5
0
 def test_check_and_record_restart_request(self, is_restart_permitted,
                                           save_event, time):
     time.return_value = 123
     is_restart_permitted.return_value = False
     deferred_events.check_and_record_restart_request(
         'svcA', ['/tmp/test1.conf', '/tmp/test2.conf'])
     save_event.assert_called_once_with(
         deferred_events.ServiceEvent(
             timestamp=123,
             service='svcA',
             reason='File(s) changed: /tmp/test1.conf, /tmp/test2.conf',
             action='restart'))
Exemplo n.º 6
0
 def test_deferrable_svc_restart(self, is_restart_permitted,
                                 service_restart, save_event, time):
     time.return_value = 123
     is_restart_permitted.return_value = True
     deferred_events.deferrable_svc_restart('svcA', reason='ReasonA')
     service_restart.assert_called_once_with('svcA')
     service_restart.reset_mock()
     is_restart_permitted.return_value = False
     deferred_events.deferrable_svc_restart('svcA', reason='ReasonA')
     self.assertFalse(service_restart.called)
     save_event.assert_called_once_with(
         deferred_events.ServiceEvent(timestamp=123,
                                      service='svcA',
                                      reason='ReasonA',
                                      action='restart'))