Exemplo n.º 1
0
    def test_execute_audit(self):
        audit_handler = continuous.ContinuousAuditHandler()
        audit_handler.execute_audit(self.audits[0], self.context)

        expected_calls = [
            mock.call(self.context,
                      self.audits[0],
                      action=objects.fields.NotificationAction.STRATEGY,
                      phase=objects.fields.NotificationPhase.START),
            mock.call(self.context,
                      self.audits[0],
                      action=objects.fields.NotificationAction.STRATEGY,
                      phase=objects.fields.NotificationPhase.END),
            mock.call(self.context,
                      self.audits[0],
                      action=objects.fields.NotificationAction.PLANNER,
                      phase=objects.fields.NotificationPhase.START),
            mock.call(self.context,
                      self.audits[0],
                      action=objects.fields.NotificationAction.PLANNER,
                      phase=objects.fields.NotificationPhase.END)
        ]

        self.assertEqual(
            expected_calls,
            self.m_audit_notifications.send_action_notification.call_args_list)
Exemplo n.º 2
0
    def test_period_audit_not_called_when_deleted(self, mock_list, mock_jobs,
                                                  m_add_job, m_engine,
                                                  m_service):
        audit_handler = continuous.ContinuousAuditHandler()
        mock_list.return_value = self.audits
        mock_jobs.return_value = mock.MagicMock()
        m_service.return_value = mock.MagicMock()
        m_engine.return_value = mock.MagicMock()
        self.audits[1].state = objects.audit.State.CANCELLED
        self.audits[0].state = objects.audit.State.SUSPENDED

        ap_jobs = [
            job.Job(mock.MagicMock(),
                    name='execute_audit',
                    func=audit_handler.execute_audit,
                    args=(self.audits[0], mock.MagicMock()),
                    kwargs={}),
            job.Job(mock.MagicMock(),
                    name='execute_audit',
                    func=audit_handler.execute_audit,
                    args=(self.audits[1], mock.MagicMock()),
                    kwargs={})
        ]
        mock_jobs.return_value = ap_jobs
        audit_handler.launch_audits_periodically()

        audit_handler.update_audit_state(self.audits[1],
                                         objects.audit.State.CANCELLED)
        audit_handler.update_audit_state(self.audits[0],
                                         objects.audit.State.SUSPENDED)
        is_inactive = audit_handler._is_audit_inactive(self.audits[1])
        self.assertTrue(is_inactive)
        is_inactive = audit_handler._is_audit_inactive(self.audits[0])
        self.assertTrue(is_inactive)
Exemplo n.º 3
0
    def test_check_audit_expired(self):
        current = datetime.datetime.utcnow()

        # start_time and end_time are None
        audit_handler = continuous.ContinuousAuditHandler()
        result = audit_handler.check_audit_expired(self.audits[0])
        self.assertFalse(result)
        self.assertIsNone(self.audits[0].start_time)
        self.assertIsNone(self.audits[0].end_time)

        # current time < start_time and end_time is None
        self.audits[0].start_time = current + datetime.timedelta(days=1)
        result = audit_handler.check_audit_expired(self.audits[0])
        self.assertTrue(result)
        self.assertIsNone(self.audits[0].end_time)

        # current time is between start_time and end_time
        self.audits[0].start_time = current - datetime.timedelta(days=1)
        self.audits[0].end_time = current + datetime.timedelta(days=1)
        result = audit_handler.check_audit_expired(self.audits[0])
        self.assertFalse(result)

        # current time > end_time
        self.audits[0].end_time = current - datetime.timedelta(days=1)
        result = audit_handler.check_audit_expired(self.audits[0])
        self.assertTrue(result)
        self.assertEqual(objects.audit.State.SUCCEEDED, self.audits[0].state)
Exemplo n.º 4
0
    def test_period_audit_not_called_when_deleted(self, mock_list, mock_jobs,
                                                  mock_add_job):
        audit_handler = continuous.ContinuousAuditHandler(mock.MagicMock())
        audits = [
            audit_objects.Audit.get_by_uuid(self.context, audit.uuid)
            for audit in self.audits
        ]
        mock_list.return_value = audits
        mock_jobs.return_value = mock.MagicMock()
        audits[1].state = audit_objects.State.CANCELLED
        calls = [
            mock.call(audit_handler.execute_audit,
                      'interval',
                      args=[mock.ANY, mock.ANY],
                      seconds=3600,
                      name='execute_audit',
                      next_run_time=mock.ANY)
        ]
        audit_handler.launch_audits_periodically()
        mock_add_job.assert_has_calls(calls)

        audit_handler.update_audit_state(audits[1],
                                         audit_objects.State.CANCELLED)
        is_inactive = audit_handler._is_audit_inactive(audits[1])
        self.assertTrue(is_inactive)
Exemplo n.º 5
0
    def test_launch_audits_periodically_with_diff_interval(
            self, mock_list, mock_jobs, m_add_job, m_remove_job, m_engine,
            m_service):
        audit_handler = continuous.ContinuousAuditHandler()
        mock_list.return_value = self.audits
        self.audits[0].next_run_time = (datetime.datetime.now() -
                                        datetime.timedelta(seconds=1800))
        m_job1 = mock.MagicMock()
        m_job1.name = 'execute_audit'
        m_audit = mock.MagicMock()
        m_audit.uuid = self.audits[0].uuid
        m_audit.interval = 60
        m_job1.args = [m_audit]
        mock_jobs.return_value = [m_job1]
        m_engine.return_value = mock.MagicMock()
        m_add_job.return_value = mock.MagicMock()

        audit_handler.launch_audits_periodically()
        m_service.assert_called()
        m_engine.assert_called()
        m_add_job.assert_called()
        mock_jobs.assert_called()
        self.assertIsNotNone(self.audits[0].next_run_time)
        self.assertIsNone(self.audits[1].next_run_time)

        audit_handler.launch_audits_periodically()
        m_remove_job.assert_called()
Exemplo n.º 6
0
 def __init__(self, messaging):
     self._messaging = messaging
     self._executor = futures.ThreadPoolExecutor(
         max_workers=CONF.watcher_decision_engine.max_workers)
     self._oneshot_handler = oneshot_handler.OneShotAuditHandler(
         self.messaging)
     self._continuous_handler = continuous_handler.ContinuousAuditHandler(
         self.messaging)
Exemplo n.º 7
0
    def test_execute_audit_with_interval_no_job(self, m_execute, m_is_inactive,
                                                m_get_jobs, m_get_engine,
                                                m_service):
        audit_handler = continuous.ContinuousAuditHandler()
        self.audits[0].next_run_time = (datetime.datetime.now() -
                                        datetime.timedelta(seconds=1800))
        m_is_inactive.return_value = True
        m_get_jobs.return_value = []

        audit_handler.execute_audit(self.audits[0], self.context)
        self.assertIsNotNone(self.audits[0].next_run_time)
Exemplo n.º 8
0
    def test_launch_audits_periodically(self, mock_list, mock_jobs, m_add_job,
                                        m_collector):
        audit_handler = continuous.ContinuousAuditHandler(mock.MagicMock())
        mock_list.return_value = self.audits
        mock_jobs.return_value = mock.MagicMock()
        m_add_job.return_value = audit_handler.execute_audit(
            self.audits[0], self.context)
        m_collector.return_value = faker.FakerModelCollector()

        audit_handler.launch_audits_periodically()
        m_add_job.assert_called()
Exemplo n.º 9
0
 def test_launch_multiply_audits_periodically(self, mock_list,
                                              mock_jobs, m_add_job):
     audit_handler = continuous.ContinuousAuditHandler(mock.MagicMock())
     mock_list.return_value = self.audits
     mock_jobs.return_value = mock.MagicMock()
     calls = [mock.call(audit_handler.execute_audit, 'interval',
                        args=[mock.ANY, mock.ANY],
                        seconds=3600,
                        name='execute_audit',
                        next_run_time=mock.ANY) for audit in self.audits]
     audit_handler.launch_audits_periodically()
     m_add_job.assert_has_calls(calls)
Exemplo n.º 10
0
    def test_launch_audits_periodically_with_invalid_cron(
            self, mock_list, mock_jobs, m_add_job, m_engine, m_service,
            mock_cron):
        audit_handler = continuous.ContinuousAuditHandler()
        mock_list.return_value = self.audits
        self.audits[0].interval = "*/5* * * *"
        mock_cron.side_effect = exception.CronFormatIsInvalid
        mock_jobs.return_value = mock.MagicMock()
        m_engine.return_value = mock.MagicMock()
        m_add_job.return_value = mock.MagicMock()

        self.assertRaises(exception.CronFormatIsInvalid,
                          audit_handler.launch_audits_periodically)
Exemplo n.º 11
0
    def test_launch_audits_periodically_with_cron(
            self, mock_list, mock_jobs, m_add_job, m_engine, m_service):
        audit_handler = continuous.ContinuousAuditHandler()
        mock_list.return_value = self.audits
        self.audits[0].interval = "*/5 * * * *"
        mock_jobs.return_value = mock.MagicMock()
        m_engine.return_value = mock.MagicMock()
        m_add_job.return_value = mock.MagicMock()

        audit_handler.launch_audits_periodically()
        m_service.assert_called()
        m_engine.assert_called()
        m_add_job.assert_called()
        mock_jobs.assert_called()
        self.assertIsNotNone(self.audits[0].next_run_time)
        self.assertIsNone(self.audits[1].next_run_time)
Exemplo n.º 12
0
    def test_launch_audits_periodically_with_interval(
            self, mock_list, mock_jobs, m_add_job, m_engine, m_service):
        audit_handler = continuous.ContinuousAuditHandler()
        mock_list.return_value = self.audits
        self.audits[0].next_run_time = (datetime.datetime.now() -
                                        datetime.timedelta(seconds=1800))
        mock_jobs.return_value = mock.MagicMock()
        m_engine.return_value = mock.MagicMock()
        m_add_job.return_value = mock.MagicMock()

        audit_handler.launch_audits_periodically()
        m_service.assert_called()
        m_engine.assert_called()
        m_add_job.assert_called()
        mock_jobs.assert_called()
        self.assertIsNotNone(self.audits[0].next_run_time)
        self.assertIsNone(self.audits[1].next_run_time)
Exemplo n.º 13
0
    def test_is_audit_inactive(self, mock_jobs):
        audit_handler = continuous.ContinuousAuditHandler()
        mock_jobs.return_value = mock.MagicMock()
        audit_handler._scheduler = mock.MagicMock()

        ap_jobs = [job.Job(mock.MagicMock(), name='execute_audit',
                           func=audit_handler.execute_audit,
                           args=(self.audits[0], mock.MagicMock()),
                           kwargs={}),
                   ]

        audit_handler.update_audit_state(self.audits[1],
                                         objects.audit.State.CANCELLED)
        mock_jobs.return_value = ap_jobs
        is_inactive = audit_handler._is_audit_inactive(self.audits[1])
        self.assertTrue(is_inactive)
        is_inactive = audit_handler._is_audit_inactive(self.audits[0])
        self.assertFalse(is_inactive)