cloudify.mocks.MockCloudifyContext

Here are the examples of the python api cloudify.mocks.MockCloudifyContext taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

11 Examples 7

3 Source : test_state.py
with Apache License 2.0
from cloudify-cosmo

    def test_basic(self):
        self.assertRaises(RuntimeError, current_ctx.get_ctx)
        self.assertRaises(RuntimeError, lambda: ctx.instance.id)
        value = MockCloudifyContext(node_id='1')
        current_ctx.set(value)
        self.assertEqual(value, current_ctx.get_ctx())
        self.assertEqual(value.instance.id, ctx.instance.id)
        current_ctx.clear()
        self.assertRaises(RuntimeError, current_ctx.get_ctx)
        self.assertRaises(RuntimeError, lambda: ctx.instance.id)

    def test_threads(self):

3 Source : test_script_runner.py
with Apache License 2.0
from cloudify-cosmo

    def mock_ctx(self, **kwargs):
        ctx = MockCloudifyContext(**kwargs)
        ctx.download_resource = lambda s_path, t_path: s_path
        current_ctx.set(ctx)
        return ctx

    @patch('os.chmod')

3 Source : test_script_runner.py
with Apache License 2.0
from cloudify-cosmo

    def mock_ctx(self, **kwargs):
        ctx = MockCloudifyContext(**kwargs)
        ctx.download_resource = lambda s_path, t_path: s_path
        current_ctx.set(ctx)
        return ctx

    def test_explicit_eval_without_py_extenstion(self):

0 Source : test_decorators.py
with Apache License 2.0
from cloudify-cosmo

    def test_serial_operation_install(self, list_fn):

        _ctx = MockCloudifyContext(
            node_id='test_node',
            deployment_id='test_deployment',
            index=7
        )

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'install'
        current_ctx.set(_ctx)
        _caller = Mock()

        @serial_operation()
        def run_op_install_only(ctx=_ctx, **kwargs):
            _caller()

        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='uninitialized') for n in range(0, 6)
        ]
        self.assertRaises(CloudifySerializationRetry,
                          run_op_install_only)
        self.assertFalse(_caller.called)
        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='started' if n   <   7 else 'uninitialized')
            for n in range(3, 10) if n != 7
        ]
        run_op_install_only()
        self.assertTrue(_caller.called)
        current_ctx.clear()

    @patch('cloudify.tests.mocks.mock_rest_client.'

0 Source : test_decorators.py
with Apache License 2.0
from cloudify-cosmo

    def test_serial_operation_install_wait_for_1(self, list_fn):

        _ctx = MockCloudifyContext(
            node_id='test_node',
            deployment_id='test_deployment',
            index=7
        )

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'install'
        current_ctx.set(_ctx)
        _caller = Mock()

        @serial_operation(threshold=1)
        def run_op_wait_for_1(ctx=_ctx, **kwargs):
            _caller()

        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='started' if n == 6 else 'uninitialized')
            for n in range(0, 6)]
        run_op_wait_for_1()
        self.assertTrue(_caller.called)
        current_ctx.clear()

    @patch('cloudify.tests.mocks.mock_rest_client.'

0 Source : test_decorators.py
with Apache License 2.0
from cloudify-cosmo

    def test_serial_operation_install_states(self, list_fn):

        _ctx = MockCloudifyContext(
            node_id='test_node',
            deployment_id='test_deployment',
            index=7
        )

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'install'
        current_ctx.set(_ctx)
        _caller = Mock()

        @serial_operation(states=['uninitialized'])
        def run_op_uninitialized_states(ctx=_ctx, **kwargs):
            _caller()

        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='started' if n == 6 else 'uninitialized')
            for n in range(0, 6)]
        run_op_uninitialized_states()
        self.assertTrue(_caller.called)
        current_ctx.clear()

    @patch('cloudify.tests.mocks.mock_rest_client.'

0 Source : test_decorators.py
with Apache License 2.0
from cloudify-cosmo

    def test_serial_operation_install_workflow(self, list_fn):

        _ctx = MockCloudifyContext(
            node_id='test_node',
            deployment_id='test_deployment',
            index=7
        )

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'install'
        current_ctx.set(_ctx)
        _caller = Mock()

        @serial_operation(workflows=['scale'])
        def run_op_neither(ctx=_ctx, **kwargs):
            _caller()

        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='started' if n == 6 else 'uninitialized')
            for n in range(0, 6)]
        run_op_neither()
        self.assertTrue(_caller.called)
        current_ctx.clear()

    @patch('cloudify.tests.mocks.mock_rest_client.'

0 Source : test_decorators.py
with Apache License 2.0
from cloudify-cosmo

    def test_serial_operation_uninstall(self, list_fn):

        _ctx = MockCloudifyContext(
            node_id='test_node',
            deployment_id='test_deployment',
            index=7
        )

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'uninstall'
        current_ctx.set(_ctx)
        _caller = Mock()

        @serial_operation(workflows=['uninstall'])
        def run_op_uninstall_only(ctx=_ctx, **kwargs):
            _caller()

        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='uninitialized') for n in range(1, 8) if n != 7
        ]
        self.assertRaises(CloudifySerializationRetry,
                          run_op_uninstall_only)
        self.assertFalse(_caller.called)
        current_ctx.clear()

    @patch('cloudify.tests.mocks.mock_rest_client.'

0 Source : test_decorators.py
with Apache License 2.0
from cloudify-cosmo

    def test_serial_operation_uninstall_wait_3(self, list_fn):

        _ctx = MockCloudifyContext(
            node_id='test_node',
            deployment_id='test_deployment',
            index=7
        )

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'uninstall'
        current_ctx.set(_ctx)
        _caller = Mock()

        @serial_operation(threshold=3,
                          workflows=['uninstall'])
        def run_op_wait_for_3(ctx=_ctx, **kwargs):
            _caller()

        finished3 = []
        for n in range(1, 8):
            if n == 7:
                continue
            elif n in [1, 2, 3]:
                state = 'started'
            else:
                state = 'uninitialized'
            finished3.append(
                Mock(id='test_node{x}'.format(x=n),
                     index=n,
                     state=state))
        list_fn.return_value = finished3
        self.assertRaises(CloudifySerializationRetry,
                          run_op_wait_for_3)
        self.assertFalse(_caller.called)

        # Check if we cross the serialization_type threshold,
        # then we do not trigger the retry.
        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='started' if n   <   7 else 'uninitialized')
            for n in range(3, 10) if n != 7
        ]
        run_op_wait_for_3()
        self.assertTrue(_caller.called)
        current_ctx.clear()

0 Source : test_proxy.py
with Apache License 2.0
from cloudify-cosmo

    def setUp(self, proxy_server_class):
        super(CtxProxyTestBase, self).setUp()
        self.ctx = MockCloudifyContext(node_id='instance_id', properties={
            'prop1': 'value1',
            'prop2': {
                'nested_prop1': 'nested_value1'
            },
            'prop3': [
                {'index': 0, 'value': 'value_0'},
                {'index': 1, 'value': 'value_1'},
                {'index': 2, 'value': 'value_2'}
            ],
            'prop4': {
                'key': 'value'
            }
        })
        self.ctx.stub_method = self.stub_method
        self.ctx.stub_sleep = self.stub_sleep
        self.ctx.stub_args = self.stub_args
        self.ctx.stub_attr = self.StubAttribute()
        self.server = proxy_server_class(self.ctx)
        self.start_server()

    def start_server(self):

0 Source : test_state.py
with Apache License 2.0
from cloudify-cosmo

    def test_threads(self):
        num_iterations = 1000
        num_threads = 10
        for _ in range(num_iterations):
            queues = [queue.Queue() for _ in range(num_threads)]

            def run(response_queue, value):
                try:
                    self.assertRaises(RuntimeError, current_ctx.get_ctx)
                    self.assertRaises(RuntimeError, lambda: ctx.instance.id)
                    current_ctx.set(value)
                    self.assertEqual(value, current_ctx.get_ctx())
                    self.assertEqual(value.instance.id, ctx.instance.id)
                    current_ctx.clear()
                    self.assertRaises(RuntimeError, current_ctx.get_ctx)
                    self.assertRaises(RuntimeError, lambda: ctx.instance.id)
                except Exception as e:
                    response_queue.put(e)
                else:
                    response_queue.put('ok')

            threads = []
            for index, thread_queue in enumerate(queues):
                value = MockCloudifyContext(node_id=str(index))
                threads.append(threading.Thread(target=run,
                                                args=(thread_queue, value)))

            for thread in threads:
                thread.start()

            for thread_queue in queues:
                self.assertEqual('ok', thread_queue.get())