Exemplo n.º 1
0
    def test_clean_children(self):
        from pipeline.engine.core.data import del_object

        mock_snapshot = ProcessSnapshot(
            data={
                '_pipeline_stack': Stack(),
                '_children': ['1', '2', '3'],
                '_root_pipeline': IdentifyObject(),
                '_subprocess_stack': Stack([])
            })
        mock_snapshot.clean_children = MagicMock()
        mock_snapshot.save = MagicMock()

        process = PipelineProcess.objects.create()
        process.snapshot = mock_snapshot

        process.clean_children()
        del_object.assert_has_calls([
            mock.call(process._context_key('1')),
            mock.call(process._data_key('1')),
            mock.call(process._context_key('2')),
            mock.call(process._data_key('2')),
            mock.call(process._context_key('3')),
            mock.call(process._data_key('3')),
        ])
        mock_snapshot.clean_children.assert_called()
        mock_snapshot.save.assert_called()
Exemplo n.º 2
0
    def test_destroy_all(self):
        mock_snapshot = ProcessSnapshot(
            data={
                '_pipeline_stack': Stack(),
                '_children': [],
                '_root_pipeline': PipelineObject(),
                '_subprocess_stack': Stack([])
            }
        )
        process = PipelineProcess.objects.create()
        process.snapshot = mock_snapshot
        process.is_alive = False
        process.destroy_all()
        process.destroy.assert_not_called()

        process.is_alive = True
        process.destroy_all()
        process.destroy.assert_called()
        process.destroy.reset_mock()

        mock_snapshot.data['_children'] = [1, 2, 3]

        child_1 = Object()
        child_1.children = []
        child_1.destroy = MagicMock()
        child_1.is_alive = True
        child_2 = Object()
        child_2.children = []
        child_2.destroy = MagicMock()
        child_2.is_alive = False
        child_3 = Object()
        child_3.children = [1]
        child_3.destroy = MagicMock()
        child_3.is_alive = True

        def get_child(id):
            return {
                1: child_1,
                2: child_2,
                3: child_3
            }[id]

        with mock.patch(PIPELINE_PROCESS_GET, get_child):
            process.destroy_all()
            child_1.destroy.assert_called()
            child_2.destroy.assert_not_called()
            child_3.destroy.assert_called()
            self.assertEqual(child_1.destroy.call_count, 2)
Exemplo n.º 3
0
    def test_exit_gracefully(self):
        mock_snapshot = ProcessSnapshot(
            data={
                '_pipeline_stack': Stack(),
                '_children': ['1', '2', '3'],
                '_root_pipeline': PipelineObject(),
                '_subprocess_stack': Stack([])
            })

        process = PipelineProcess.objects.create()
        process.snapshot = mock_snapshot
        process.sleep = MagicMock()
        e = Exception('test')

        process.current_node_id = uniqid()
        process.exit_gracefully(e)
        Status.objects.fail.assert_called_with(process.current_node_id,
                                               ex_data=traceback.format_exc(e))
        Status.objects.raw_fail.assert_not_called()
        process.sleep.assert_called_with(adjust_status=True)

        Status.objects.fail.reset_mock()
        process.sleep.reset_mock()

        # when stack is not empty
        mock_snapshot.data['_pipeline_stack'] = Stack([PipelineObject()])
        process.current_node_id = uniqid()
        process.exit_gracefully(e)
        Status.objects.fail.assert_called_with(process.current_node_id,
                                               ex_data=traceback.format_exc(e))
        Status.objects.raw_fail.assert_not_called()
        process.sleep.assert_called_with(adjust_status=True)

        Status.objects.fail.reset_mock()
        process.sleep.reset_mock()

        # when current_node is none
        top_pipeline = PipelineObject()
        top_pipeline.node = MagicMock(return_value=None)
        mock_snapshot.data['_pipeline_stack'] = Stack([top_pipeline])
        process.current_node_id = uniqid()
        process.exit_gracefully(e)
        Status.objects.fail.assert_not_called()
        Status.objects.raw_fail.assert_called_with(
            process.current_node_id, ex_data=traceback.format_exc(e))
        process.sleep.assert_called_with(adjust_status=True)
Exemplo n.º 4
0
    def test_in_subprocess__false(self):
        snapshot = ProcessSnapshot(data={
            '_pipeline_stack': Stack([1]),
        })
        process = PipelineProcess()
        process.snapshot = snapshot

        self.assertFalse(process.in_subprocess)
Exemplo n.º 5
0
    def test_revoke_subprocess(self):
        mock_snapshot = ProcessSnapshot(
            data={
                '_pipeline_stack': Stack(),
                '_children': [],
                '_root_pipeline': PipelineObject(),
                '_subprocess_stack': Stack([1, 2, 3, 4])
            }
        )

        process = PipelineProcess.objects.create(id=uniqid())
        process.snapshot = mock_snapshot
        process.sleep = MagicMock()

        process.revoke_subprocess()
        Status.objects.batch_transit.assert_called_with(id_list=[1, 2, 3, 4], state=states.REVOKED)

        child_1 = Object()
        child_2 = Object()
        child_3 = Object()
        child_1.revoke_subprocess = MagicMock()
        child_2.revoke_subprocess = MagicMock()
        child_3.revoke_subprocess = MagicMock()

        def get_child(id):
            return {
                1: child_1,
                2: child_2,
                3: child_3
            }[id]

        mock_snapshot.data['_children'] = [1, 2, 3]

        with mock.patch(PIPELINE_PROCESS_GET, get_child):
            process.revoke_subprocess()
            Status.objects.batch_transit.assert_called_with(id_list=[1, 2, 3, 4], state=states.REVOKED)
            child_1.revoke_subprocess.assert_called()
            child_2.revoke_subprocess.assert_called()
            child_3.revoke_subprocess.assert_called()

        # test when subprocess_stack and children return None
        process = PipelineProcess.objects.create(id=uniqid())
        self.assertIsNone(process.subprocess_stack)
        self.assertIsNone(process.children)
        process.revoke_subprocess()
Exemplo n.º 6
0
    def test_sync_with_children(self):
        outputs = {'output_key': 'output_value'}
        variables = {'variable_key': 'varaiable_value'}

        process = PipelineProcess.objects.create()
        context = Object()
        context.update_global_var = MagicMock()
        context.sync_change = MagicMock()

        data = Object()
        data.update_outputs = MagicMock()

        mock_snapshot = ProcessSnapshot(
            data={
                '_pipeline_stack':
                Stack([PipelineObject(context=context, data=data)]),
                '_children': [1, 2, 3, 4],
                '_root_pipeline':
                IdentifyObject(),
                '_subprocess_stack':
                Stack([])
            })
        process.snapshot = mock_snapshot
        process.clean_children = MagicMock()

        def return_none(*args, **kwargs):
            return None

        def return_mock(id):
            if id.endswith('data'):
                return DataObject(outputs=outputs)
            if id.endswith('context'):
                return ContextObject(variables=variables)

        with mock.patch(PIPELINE_ENGINE_CORE_DATA_GET_OBJECT, return_none):
            self.assertRaises(exceptions.ChildDataSyncError,
                              process.sync_with_children)

        with mock.patch(PIPELINE_ENGINE_CORE_DATA_GET_OBJECT, return_mock):
            process.sync_with_children()
            context.sync_change.assert_called()
            data.update_outputs.assert_called_with(outputs)
            process.clean_children.assert_called()