Esempio n. 1
0
    def test_execution_cancellation(self):
        tracker = results_tracker.get_tracker()
        querier = tracker.get_querier('test_querymodule')
        querier._delete_state_object = mock.Mock(return_value=None)
        runners_utils.invoke_post_run = mock.Mock(return_value=None)

        # Ensure state objects are present.
        state1 = ActionExecutionState.get_by_id(ResultsTrackerTests.states['state1.yaml'].id)
        state2 = ActionExecutionState.get_by_id(ResultsTrackerTests.states['state2.yaml'].id)
        self.assertIsNotNone(state1)
        self.assertIsNotNone(state2)

        with mock.patch.object(
                querier.__class__, 'query',
                mock.MagicMock(return_value=(action_constants.LIVEACTION_STATUS_CANCELED, {}))):
            tracker._bootstrap()
            eventlet.sleep(2)

            exec_id = str(ResultsTrackerTests.states['state1.yaml'].execution_id)
            exec_db = LiveAction.get_by_id(exec_id)
            self.assertDictEqual(exec_db.result, {})

            exec_id = str(ResultsTrackerTests.states['state2.yaml'].execution_id)
            exec_db = LiveAction.get_by_id(exec_id)
            self.assertDictEqual(exec_db.result, {})

            tracker.shutdown()

        # Ensure deletes are called.
        self.assertEqual(2, querier._delete_state_object.call_count)

        # Ensure invoke_post_run is called.
        self.assertEqual(2, runners_utils.invoke_post_run.call_count)
Esempio n. 2
0
    def test_keep_state_object_on_error_at_update_result(self):
        tracker = results_tracker.get_tracker()
        querier = tracker.get_querier('test_querymodule')
        querier._delete_state_object = mock.Mock(return_value=None)
        querier.delete_state_object_on_error = False

        # Ensure state objects are present.
        state1 = ActionExecutionState.get_by_id(
            ResultsTrackerTests.states['state1.yaml'].id)
        state2 = ActionExecutionState.get_by_id(
            ResultsTrackerTests.states['state2.yaml'].id)
        self.assertIsNotNone(state1)
        self.assertIsNotNone(state2)

        with mock.patch.object(
                querier.__class__, '_update_action_results',
                mock.MagicMock(
                    side_effect=Exception('Mock update exception.'))):
            tracker._bootstrap()
            eventlet.sleep(1)

            exec_id = str(
                ResultsTrackerTests.states['state1.yaml'].execution_id)
            exec_db = LiveAction.get_by_id(exec_id)
            self.assertDictEqual(exec_db.result, {})

            exec_id = str(
                ResultsTrackerTests.states['state2.yaml'].execution_id)
            exec_db = LiveAction.get_by_id(exec_id)
            self.assertDictEqual(exec_db.result, {})

            tracker.shutdown()

        # Ensure deletes are not called.
        querier._delete_state_object.assert_not_called()
Esempio n. 3
0
    def test_keep_state_object_on_error_at_update_result(self):
        tracker = results_tracker.get_tracker()
        querier = tracker.get_querier('test_querymodule')
        querier._delete_state_object = mock.Mock(return_value=None)
        querier.delete_state_object_on_error = False

        # Ensure state objects are present.
        state1 = ActionExecutionState.get_by_id(ResultsTrackerTests.states['state1.yaml'].id)
        state2 = ActionExecutionState.get_by_id(ResultsTrackerTests.states['state2.yaml'].id)
        self.assertIsNotNone(state1)
        self.assertIsNotNone(state2)

        with mock.patch.object(
                querier.__class__, '_update_action_results',
                mock.MagicMock(side_effect=Exception('Mock update exception.'))):
            tracker._bootstrap()
            eventlet.sleep(1)

            exec_id = str(ResultsTrackerTests.states['state1.yaml'].execution_id)
            exec_db = LiveAction.get_by_id(exec_id)
            self.assertDictEqual(exec_db.result, {})

            exec_id = str(ResultsTrackerTests.states['state2.yaml'].execution_id)
            exec_db = LiveAction.get_by_id(exec_id)
            self.assertDictEqual(exec_db.result, {})

            tracker.shutdown()

        # Ensure deletes are not called.
        querier._delete_state_object.assert_not_called()
Esempio n. 4
0
def _run_worker():
    LOG.info('(PID=%s) Results tracker started.', os.getpid())
    tracker = resultstracker.get_tracker()
    try:
        tracker.start(wait=True)
    except (KeyboardInterrupt, SystemExit):
        LOG.info('(PID=%s) Results tracker stopped.', os.getpid())
        tracker.shutdown()
    except:
        return 1
    return 0
Esempio n. 5
0
def _run_worker():
    LOG.info('(PID=%s) Results tracker started.', os.getpid())
    tracker = resultstracker.get_tracker()
    try:
        tracker.start(wait=True)
    except (KeyboardInterrupt, SystemExit):
        LOG.info('(PID=%s) Results tracker stopped.', os.getpid())
        tracker.shutdown()
    except:
        return 1
    return 0
Esempio n. 6
0
 def setUp(self):
     super(ResultsTrackerTests, self).setUp()
     DbTestCase.setUpClass()
     ResultsTrackerTests.models = FIXTURES_LOADER.save_fixtures_to_db(
         fixtures_pack=FIXTURES_PACK, fixtures_dict=FIXTURES)
     ResultsTrackerTests.states = ResultsTrackerTests.models['actionstates']
     ResultsTrackerTests.liveactions = ResultsTrackerTests.models['liveactions']
     ResultsTrackerTests._update_state_models()
     tracker = results_tracker.get_tracker()
     querier = tracker.get_querier('test_querymodule')
     querier.delete_state_object_on_error = True
Esempio n. 7
0
 def setUp(self):
     super(ResultsTrackerTests, self).setUp()
     DbTestCase.setUpClass()
     ResultsTrackerTests.models = FIXTURES_LOADER.save_fixtures_to_db(
         fixtures_pack=FIXTURES_PACK, fixtures_dict=FIXTURES)
     ResultsTrackerTests.states = ResultsTrackerTests.models['actionstates']
     ResultsTrackerTests.liveactions = ResultsTrackerTests.models[
         'liveactions']
     ResultsTrackerTests._update_state_models()
     tracker = results_tracker.get_tracker()
     querier = tracker.get_querier('test_querymodule')
     querier.delete_state_object_on_error = True
 def test_bootstrap(self):
     tracker = results_tracker.get_tracker()
     tracker._bootstrap()
     eventlet.sleep(0.2)
     exec_id = str(ResultsTrackerTests.states['state1.yaml'].execution_id)
     exec_db = LiveAction.get_by_id(exec_id)
     self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                     exec_db.result)
     exec_id = str(ResultsTrackerTests.states['state2.yaml'].execution_id)
     exec_db = LiveAction.get_by_id(exec_id)
     self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                     exec_db.result)
     tracker.shutdown()
Esempio n. 9
0
    def test_query_process(self):
        tracker = results_tracker.get_tracker()
        runners_utils.invoke_post_run = mock.Mock(return_value=None)

        # Ensure state objects are present.
        state1 = ActionExecutionState.get_by_id(ResultsTrackerTests.states['state1.yaml'].id)
        state2 = ActionExecutionState.get_by_id(ResultsTrackerTests.states['state2.yaml'].id)
        self.assertIsNotNone(state1)
        self.assertIsNotNone(state2)

        # Process the state objects.
        tracker._bootstrap()
        eventlet.sleep(1)

        exec_id = str(ResultsTrackerTests.states['state1.yaml'].execution_id)
        exec_db = LiveAction.get_by_id(exec_id)
        self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                        exec_db.result)

        exec_id = str(ResultsTrackerTests.states['state2.yaml'].execution_id)
        exec_db = LiveAction.get_by_id(exec_id)
        self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                        exec_db.result)

        tracker.shutdown()

        # Ensure state objects are deleted.
        self.assertRaises(
            StackStormDBObjectNotFoundError,
            ActionExecutionState.get_by_id,
            ResultsTrackerTests.states['state1.yaml'].id
        )

        self.assertRaises(
            StackStormDBObjectNotFoundError,
            ActionExecutionState.get_by_id,
            ResultsTrackerTests.states['state2.yaml'].id
        )

        # Ensure invoke_post_run is called.
        self.assertEqual(2, runners_utils.invoke_post_run.call_count)
Esempio n. 10
0
    def test_query_process(self):
        tracker = results_tracker.get_tracker()
        querier = tracker.get_querier('test_querymodule')
        querier._invoke_post_run = mock.Mock(return_value=None)

        # Ensure state objects are present.
        state1 = ActionExecutionState.get_by_id(
            ResultsTrackerTests.states['state1.yaml'].id)
        state2 = ActionExecutionState.get_by_id(
            ResultsTrackerTests.states['state2.yaml'].id)
        self.assertIsNotNone(state1)
        self.assertIsNotNone(state2)

        # Process the state objects.
        tracker._bootstrap()
        eventlet.sleep(1)

        exec_id = str(ResultsTrackerTests.states['state1.yaml'].execution_id)
        exec_db = LiveAction.get_by_id(exec_id)
        self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                        exec_db.result)

        exec_id = str(ResultsTrackerTests.states['state2.yaml'].execution_id)
        exec_db = LiveAction.get_by_id(exec_id)
        self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                        exec_db.result)

        tracker.shutdown()

        # Ensure state objects are deleted.
        self.assertRaises(StackStormDBObjectNotFoundError,
                          ActionExecutionState.get_by_id,
                          ResultsTrackerTests.states['state1.yaml'].id)

        self.assertRaises(StackStormDBObjectNotFoundError,
                          ActionExecutionState.get_by_id,
                          ResultsTrackerTests.states['state2.yaml'].id)

        # Ensure invoke_post_run is called.
        self.assertEqual(2, querier._invoke_post_run.call_count)
Esempio n. 11
0
    def test_execution_cancellation(self):
        tracker = results_tracker.get_tracker()
        querier = tracker.get_querier('test_querymodule')
        querier._delete_state_object = mock.Mock(return_value=None)
        runners_utils.invoke_post_run = mock.Mock(return_value=None)

        # Ensure state objects are present.
        state1 = ActionExecutionState.get_by_id(
            ResultsTrackerTests.states['state1.yaml'].id)
        state2 = ActionExecutionState.get_by_id(
            ResultsTrackerTests.states['state2.yaml'].id)
        self.assertIsNotNone(state1)
        self.assertIsNotNone(state2)

        with mock.patch.object(
                querier.__class__, 'query',
                mock.MagicMock(
                    return_value=(action_constants.LIVEACTION_STATUS_CANCELED,
                                  {}))):
            tracker._bootstrap()
            eventlet.sleep(2)

            exec_id = str(
                ResultsTrackerTests.states['state1.yaml'].execution_id)
            exec_db = LiveAction.get_by_id(exec_id)
            self.assertDictEqual(exec_db.result, {})

            exec_id = str(
                ResultsTrackerTests.states['state2.yaml'].execution_id)
            exec_db = LiveAction.get_by_id(exec_id)
            self.assertDictEqual(exec_db.result, {})

            tracker.shutdown()

        # Ensure deletes are called.
        self.assertEqual(2, querier._delete_state_object.call_count)

        # Ensure invoke_post_run is called.
        self.assertEqual(2, runners_utils.invoke_post_run.call_count)
Esempio n. 12
0
    def test_action_deleted(self):
        tracker = results_tracker.get_tracker()
        action_db_utils.get_runnertype_by_name = mock.Mock(return_value=None)

        # Ensure state objects are present.
        state1 = ActionExecutionState.get_by_id(
            ResultsTrackerTests.states['state1.yaml'].id)
        state2 = ActionExecutionState.get_by_id(
            ResultsTrackerTests.states['state2.yaml'].id)
        self.assertIsNotNone(state1)
        self.assertIsNotNone(state2)

        # Process the state objects.
        tracker._bootstrap()
        eventlet.sleep(1)

        exec_id = str(ResultsTrackerTests.states['state1.yaml'].execution_id)
        exec_db = LiveAction.get_by_id(exec_id)
        self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                        exec_db.result)

        exec_id = str(ResultsTrackerTests.states['state2.yaml'].execution_id)
        exec_db = LiveAction.get_by_id(exec_id)
        self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                        exec_db.result)

        tracker.shutdown()

        # Ensure state objects are deleted.
        self.assertRaises(StackStormDBObjectNotFoundError,
                          ActionExecutionState.get_by_id,
                          ResultsTrackerTests.states['state1.yaml'].id)

        self.assertRaises(StackStormDBObjectNotFoundError,
                          ActionExecutionState.get_by_id,
                          ResultsTrackerTests.states['state2.yaml'].id)

        # Ensure get_runnertype_by_name in invoke_post_run is not called.
        action_db_utils.get_runnertype_by_name.assert_not_called()
Esempio n. 13
0
 def _get_tracker(self):
     from st2actions.resultstracker import resultstracker
     tracker = resultstracker.get_tracker()
     return tracker
Esempio n. 14
0
 def test_querier_started(self):
     tracker = results_tracker.get_tracker()
     querier = tracker.get_querier('test_querymodule')
     eventlet.sleep(0.1)
     self.assertTrue(querier.is_started(),
                     'querier must have been started.')
Esempio n. 15
0
 def test_start_shutdown(self):
     tracker = results_tracker.get_tracker()
     tracker.start()
     eventlet.sleep(0.1)
     tracker.shutdown()
Esempio n. 16
0
 def test_get_querier(self):
     tracker = results_tracker.get_tracker()
     self.assertEqual(tracker.get_querier('this_module_aint_exist'), None)
     self.assertTrue(tracker.get_querier('test_querymodule') is not None)
Esempio n. 17
0
 def test_start_shutdown(self):
     tracker = results_tracker.get_tracker()
     tracker.start()
     eventlet.sleep(0.1)
     tracker.shutdown()
Esempio n. 18
0
 def test_get_querier(self):
     tracker = results_tracker.get_tracker()
     self.assertEqual(tracker.get_querier('this_module_aint_exist'), None)
     self.assertTrue(tracker.get_querier('tests.resources.test_querymodule') is not None)
Esempio n. 19
0
 def test_querier_started(self):
     tracker = results_tracker.get_tracker()
     querier = tracker.get_querier('tests.resources.test_querymodule')
     eventlet.sleep(0.1)
     self.assertTrue(querier.is_started(), 'querier must have been started.')
Esempio n. 20
0
 def _get_tracker(self):
     from st2actions.resultstracker import resultstracker
     tracker = resultstracker.get_tracker()
     return tracker