예제 #1
0
def saving_operation_info(ctx, op, main_node, second_node=None, **_):
    with update_storage(ctx) as data:
        invocations = data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', []
        )
        num = data.get('num', 0) + 1
        data['num'] = num

        op_info = {'operation': op, 'num': num}
        if second_node is None:
            op_info.update({
                'node': main_node.node.name,
                'id': main_node.instance.id,
                'target_ids': [r.target.instance.id
                               for r in main_node.instance.relationships]
            })
        else:
            op_info.update({
                'id': main_node.instance.id,
                'source': main_node.node.name,
                'target': second_node.node.name
            })
        invocations.append(op_info)

    client = get_rest_client()
    fail_input = client.deployments.get(ctx.deployment.id).inputs.get(
        'fail', [])
    fail_input = [i for i in fail_input if
                  i.get('workflow') == ctx.workflow_id and
                  i.get('node') == main_node.node.id and
                  i.get('operation') == ctx.operation.name]
    if fail_input:
        raise RuntimeError('TEST_EXPECTED_FAIL')
예제 #2
0
def stop(**kwargs):
    with update_storage(ctx) as data:
        ctx.logger.info('stopping machine: {0}'.format(ctx.instance.id))
        if ctx.instance.id not in data['machines']:
            raise RuntimeError('machine with id [{0}] does not exist'
                               .format(ctx.instance.id))
        data['machines'][ctx.instance.id] = NOT_RUNNING
예제 #3
0
def get_resource_operation(ctx, **kwargs):
    resource_path = get_prop('resource_path', ctx, kwargs)
    # trying to retrieve a resource
    res1 = ctx.download_resource(resource_path)
    if not res1:
        raise RuntimeError('Failed to get resource {0}'.format(resource_path))
    with open(res1, 'r') as f:
        res1_data = f.read()
    os.remove(res1)

    # trying to retrieve a resource to a specific location
    tempdir = tempfile.mkdtemp()
    try:
        filepath = os.path.join(tempdir, 'temp-resource-file')
        res2 = ctx.download_resource(resource_path, filepath)
        if not res2:
            raise RuntimeError('Failed to get resource {0} into {1}'.format(
                resource_path, filepath))
        with open(res2, 'r') as f:
            res2_data = f.read()
    finally:
        shutil.rmtree(tempdir)

    with update_storage(ctx) as data:
        data['get_resource_operation_invocation'] = data.get(
            'get_resource_operation_invocation', []
        )
        data['get_resource_operation_invocation'].append({
            'res1_data': res1_data,
            'res2_data': res2_data,
            'custom_filepath': filepath,
            'res2_path': res2
        })
예제 #4
0
def terminate(**kwargs):
    with update_storage(ctx) as data:
        ctx.logger.info('terminating machine: {0}'.format(ctx.instance.id))
        if ctx.instance.id not in data['machines']:
            raise RuntimeError('machine with id [{0}] does not exist'
                               .format(ctx.instance.id))
        del data['machines'][ctx.instance.id]
예제 #5
0
def stop(**kwargs):
    with update_storage(ctx) as data:
        ctx.logger.info('stopping machine: {0}'.format(ctx.instance.id))
        if ctx.instance.id not in data['machines']:
            raise RuntimeError('machine with id [{0}] does not exist'
                               .format(ctx.instance.id))
        data['machines'][ctx.instance.id] = NOT_RUNNING
예제 #6
0
def terminate(**kwargs):
    with update_storage(ctx) as data:
        ctx.logger.info('terminating machine: {0}'.format(ctx.instance.id))
        if ctx.instance.id not in data['machines']:
            raise RuntimeError('machine with id [{0}] does not exist'
                               .format(ctx.instance.id))
        del data['machines'][ctx.instance.id]
예제 #7
0
def make_unreachable(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['unreachable_call_order'] = data.get('unreachable_call_order', [])
        data['unreachable_call_order'].append({
            'id': ctx.instance.id,
            'time': time.time()
        })
예제 #8
0
def saving_operation_info(ctx, op, main_node, second_node=None, **_):
    with update_storage(ctx) as data:
        invocations = data.setdefault('mock_operation_invocation', [])
        num = data.get('num', 0) + 1
        data['num'] = num

        op_info = {'operation': op, 'num': num}
        if second_node is None:
            op_info.update({
                'node': main_node.node.name,
                'id': main_node.instance.id,
                'target_ids': [r.target.instance.id
                               for r in main_node.instance.relationships]
            })
        else:
            op_info.update({
                'id': main_node.instance.id,
                'source': main_node.node.name,
                'target': second_node.node.name
            })
        invocations.append(op_info)

    client = get_rest_client()
    fail_input = client.deployments.get(ctx.deployment.id).inputs.get(
        'fail', [])
    fail_input = [i for i in fail_input if
                  i.get('workflow') == ctx.workflow_id and
                  i.get('node') == main_node.node.id and
                  i.get('operation') == ctx.operation.name]
    if fail_input:
        raise RuntimeError('TEST_EXPECTED_FAIL')
예제 #9
0
def get_resource_operation(ctx, **kwargs):
    resource_path = get_prop('resource_path', ctx, kwargs)
    # trying to retrieve a resource
    res1 = ctx.download_resource(resource_path)
    if not res1:
        raise RuntimeError('Failed to get resource {0}'.format(resource_path))
    with open(res1, 'r') as f:
        res1_data = f.read()
    os.remove(res1)

    # trying to retrieve a resource to a specific location
    tempdir = tempfile.mkdtemp()
    try:
        filepath = os.path.join(tempdir, 'temp-resource-file')
        res2 = ctx.download_resource(resource_path, filepath)
        if not res2:
            raise RuntimeError('Failed to get resource {0} into {1}'.format(
                resource_path, filepath))
        with open(res2, 'r') as f:
            res2_data = f.read()
    finally:
        shutil.rmtree(tempdir)

    with update_storage(ctx) as data:
        data['get_resource_operation_invocation'] = data.get(
            'get_resource_operation_invocation', [])
        data['get_resource_operation_invocation'].append({
            'res1_data': res1_data,
            'res2_data': res2_data,
            'custom_filepath': filepath,
            'res2_path': res2
        })
예제 #10
0
def make_unreachable(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['unreachable_call_order'] = data.get('unreachable_call_order', [])
        data['unreachable_call_order'].append({
            'id': ctx.instance.id,
            'time': time.time()
        })
예제 #11
0
def restart(cloudify_agent=None, **_):
    installer = get_backend(cloudify_agent)
    worker_name = installer.agent_name
    installer.restart()
    with update_storage(ctx) as data:
        data[worker_name] = data.get(worker_name, {})
        data[worker_name]['states'] = data[worker_name].get('states', [])
        data[worker_name]['states'].append('restarted')
예제 #12
0
def configure(cloudify_agent=None, **_):
    installer = get_backend(cloudify_agent)
    installer.create()
    worker_name = installer.agent_name
    with update_storage(ctx) as data:
        data[worker_name] = data.get(worker_name, {})
        data[worker_name]['states'] = data[worker_name].get('states', [])
        data[worker_name]['states'].append('configured')
예제 #13
0
def mock_operation_get_instance_ip_from_context(ctx, **_):
    with update_storage(ctx) as data:
        data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', [])
        data['mock_operation_invocation'].append(
            (ctx.node.name, ctx.instance.host_ip))

    return True
예제 #14
0
def mock_operation_get_instance_ip(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', [])
        data['mock_operation_invocation'].append(
            (ctx.node.name, get_node_instance_ip(ctx.instance.id)))

    return True
예제 #15
0
def put_workflow_node_instance(ctx, modification, relationships):
    with update_storage(ctx) as data:
        state = data.get('state', {})
        data['state'] = state
        state[ctx.instance.id] = {
            'node_id': ctx.node.id,
            'modification': modification,
            'relationships': relationships
        }
예제 #16
0
def stop_monitor(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['monitoring_operations_invocation'] = data.get(
            'monitoring_operations_invocation', []
        )
        data['monitoring_operations_invocation'].append({
            'id': ctx.instance.id,
            'operation': 'stop_monitor'
        })
예제 #17
0
def stop_monitor(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['monitoring_operations_invocation'] = data.get(
            'monitoring_operations_invocation', []
        )
        data['monitoring_operations_invocation'].append({
            'id': ctx.instance.id,
            'operation': 'stop_monitor'
        })
예제 #18
0
def host_get_state(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['host_get_state_invocation'] = data.get(
            'host_get_state_invocation', [])
        data['host_get_state_invocation'].append(time.time())

    if len(data['host_get_state_invocation']) <= get_prop(
            'false_count', ctx, kwargs):
        return False
    return True
예제 #19
0
def mock_operation_get_instance_ip(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', []
        )
        data['mock_operation_invocation'].append((
            ctx.node.name, get_node_instance_ip(ctx.instance.id)
        ))

    return True
예제 #20
0
def mock_operation_get_instance_ip_from_context(ctx, **_):
    with update_storage(ctx) as data:
        data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', []
        )
        data['mock_operation_invocation'].append((
            ctx.node.name, ctx.instance.host_ip
        ))

    return True
예제 #21
0
def get_instance_ip_of_source_and_target(ctx, **_):
    with update_storage(ctx) as data:
        data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', [])
        data['mock_operation_invocation'].append(
            ('{}_source'.format(ctx.source.node.name),
             ctx.source.instance.host_ip))
        data['mock_operation_invocation'].append(
            ('{}_target'.format(ctx.target.node.name),
             ctx.target.instance.host_ip))
    return True
예제 #22
0
def delete(cloudify_agent=None, **_):
    installer = get_backend(cloudify_agent)
    installer.delete()
    worker_name = installer.agent_name
    with update_storage(ctx) as data:
        data[worker_name] = data.get(worker_name, {})
        data[worker_name]['states'] = data[worker_name].get('states', [])
        data[worker_name]['states'].append('deleted')

    if ctx.type == constants.NODE_INSTANCE:
        del ctx.instance.runtime_properties['cloudify_agent']
예제 #23
0
def retrieve_template(ctx, rendering_tests_demo_conf, mode,
                      property_name='rendered_resource', **_):
    if mode == 'get':
        resource = \
            ctx.get_resource_and_render(rendering_tests_demo_conf)
    else:
        resource = \
            ctx.download_resource_and_render(rendering_tests_demo_conf)

    with update_storage(ctx) as data:
        data[property_name] = resource
예제 #24
0
def start(**kwargs):
    with update_storage(ctx) as data:
        machines = data.get('machines', {})
        ctx.send_event('starting machine event')
        ctx.logger.info('cloudmock start: [node_id={0}, machines={1}]'
                        .format(ctx.instance.id, machines))
        if ctx.instance.id not in machines:
            raise NonRecoverableError('machine with id [{0}] does not exist'
                                      .format(ctx.instance.id))
        machines[ctx.instance.id] = RUNNING
        ctx.instance.runtime_properties['id'] = ctx.instance.id
예제 #25
0
def provision(**kwargs):
    with update_storage(ctx) as data:
        machines = data.get('machines', {})
        if ctx.instance.id in machines:
            raise NonRecoverableError('machine with id [{0}] already exists'
                                      .format(ctx.instance.id))
        if ctx.node.properties.get('test_ip'):
            ctx.instance.runtime_properties['ip'] = \
                ctx.node.properties['test_ip']
        machines[ctx.instance.id] = NOT_RUNNING
        data['machines'] = machines
예제 #26
0
def retry(ctx, retry_count=1, retry_after=1, **kwargs):
    with update_storage(ctx) as data:
        invocations = data.get('retry_invocations', 0)
        if invocations != ctx.operation.retry_number:
            raise NonRecoverableError(
                'invocations({0}) != ctx.operation.retry_number'
                '({1})'.format(invocations, ctx.operation.retry_number))
        data['retry_invocations'] = invocations + 1
    if ctx.operation.retry_number < retry_count:
        return ctx.operation.retry(message='Retrying operation',
                                   retry_after=retry_after)
예제 #27
0
def make_reachable(ctx, **kwargs):
    state_info = {
        'id': ctx.instance.id,
        'time': time.time(),
        'capabilities': ctx.capabilities.get_all()
    }
    ctx.logger.info('Appending to state [node_id={0}, state={1}]'.format(
        ctx.instance.id, state_info))
    with update_storage(ctx) as data:
        data['state'] = data.get('state', [])
        data['state'].append(state_info)
예제 #28
0
def retry(ctx, retry_count=1, retry_after=1, **kwargs):
    with update_storage(ctx) as data:
        invocations = data.get('retry_invocations', 0)
        if invocations != ctx.operation.retry_number:
            raise NonRecoverableError(
                'invocations({0}) != ctx.operation.retry_number'
                '({1})'.format(invocations, ctx.operation.retry_number))
        data['retry_invocations'] = invocations + 1
    if ctx.operation.retry_number < retry_count:
        return ctx.operation.retry(message='Retrying operation',
                                   retry_after=retry_after)
예제 #29
0
def make_reachable(ctx, **kwargs):
    state_info = {
        'id': ctx.instance.id,
        'time': time.time(),
        'capabilities': ctx.capabilities.get_all()
    }
    ctx.logger.info('Appending to state [node_id={0}, state={1}]'
                    .format(ctx.instance.id, state_info))
    with update_storage(ctx) as data:
        data['state'] = data.get('state', [])
        data['state'].append(state_info)
예제 #30
0
def provision(**kwargs):
    with update_storage(ctx) as data:
        machines = data.get('machines', {})
        if ctx.instance.id in machines:
            raise NonRecoverableError('machine with id [{0}] already exists'
                                      .format(ctx.instance.id))
        if ctx.node.properties.get('test_ip'):
            ctx.instance.runtime_properties['ip'] = \
                ctx.node.properties['test_ip']
        machines[ctx.instance.id] = NOT_RUNNING
        data['machines'] = machines
예제 #31
0
def start(**kwargs):
    with update_storage(ctx) as data:
        machines = data.get('machines', {})
        ctx.send_event('starting machine event')
        ctx.logger.info('cloudmock start: [node_id={0}, machines={1}]'
                        .format(ctx.instance.id, machines))
        if ctx.instance.id not in machines:
            raise NonRecoverableError('machine with id [{0}] does not exist'
                                      .format(ctx.instance.id))
        machines[ctx.instance.id] = RUNNING
        ctx.instance.runtime_properties['id'] = ctx.instance.id
예제 #32
0
def retrieve_template(ctx, rendering_tests_demo_conf, mode,
                      property_name='rendered_resource', **_):
    if mode == 'get':
        resource = \
            ctx.get_resource_and_render(rendering_tests_demo_conf)
    else:
        resource = \
            ctx.download_resource_and_render(rendering_tests_demo_conf)

    with update_storage(ctx) as data:
        data[property_name] = resource
예제 #33
0
def put_workflow_node_instance(ctx,
                               modification,
                               relationships):
    with update_storage(ctx) as data:
        state = data.get('state', {})
        data['state'] = state
        state[ctx.instance.id] = {
            'node_id': ctx.node.id,
            'modification': modification,
            'relationships': relationships
        }
예제 #34
0
def host_get_state(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['host_get_state_invocation'] = data.get(
            'host_get_state_invocation', []
        )
        data['host_get_state_invocation'].append(time.time())

    if len(data['host_get_state_invocation']) <= get_prop('false_count',
                                                          ctx,
                                                          kwargs):
        return False
    return True
예제 #35
0
def mock_operation(ctx, **kwargs):
    mockprop = get_prop('mockprop', ctx, kwargs)
    with update_storage(ctx) as data:
        data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', [])
        data['mock_operation_invocation'].append({
            'id': ctx.instance.id,
            'mockprop': mockprop,
            'properties':
            {key: value
             for (key, value) in ctx.node.properties.items()}
        })
예제 #36
0
def mock_operation(ctx, **kwargs):
    mockprop = get_prop('mockprop', ctx, kwargs)
    with update_storage(ctx) as data:
        data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', []
        )
        data['mock_operation_invocation'].append({
            'id': ctx.instance.id,
            'mockprop': mockprop,
            'properties': {
                key: value for (key, value) in ctx.node.properties.items()
            }
        })
예제 #37
0
def get_instance_ip_of_source_and_target(ctx, **_):
    with update_storage(ctx) as data:
        data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', []
        )
        data['mock_operation_invocation'].append((
            '{}_source'.format(ctx.source.node.name),
            ctx.source.instance.host_ip
        ))
        data['mock_operation_invocation'].append((
            '{}_target'.format(ctx.target.node.name),
            ctx.target.instance.host_ip
        ))
    return True
예제 #38
0
def assertion(a=None, b=None, c=None, d=None, **_):
    if ctx.type == context.NODE_INSTANCE:
        assertEqual(a, 'a_value')
        assertEqual(b, 'b_value')
        assertEqual(c, None)
        assertEqual(d, None)
    else:
        assertEqual(a, None)
        assertEqual(b, None)
        assertEqual(c, 'c_value')
        assertEqual(d, 'd_value')
    with update_storage(ctx) as data:
        invocations = data.get('invocations', [])
        data['invocations'] = invocations
        invocations.append(ctx.type)
예제 #39
0
def assertion(a=None, b=None, c=None, d=None, **_):
    if ctx.type == constants.NODE_INSTANCE:
        assertEqual(a, 'a_value')
        assertEqual(b, 'b_value')
        assertEqual(c, None)
        assertEqual(d, None)
    else:
        assertEqual(a, None)
        assertEqual(b, None)
        assertEqual(c, 'c_value')
        assertEqual(d, 'd_value')
    with update_storage(ctx) as data:
        invocations = data.get('invocations', [])
        data['invocations'] = invocations
        invocations.append(ctx.type)
예제 #40
0
def _operate_on_plugins(plugins, new_state):
    plugin_installer = get_backend()
    func = plugin_installer.install if new_state == 'installed' \
        else plugin_installer.uninstall
    for plugin in plugins:
        with update_storage(ctx) as data:
            if (new_state == 'uninstalled'
                    and 'raise_exception_on_delete' in data):
                raise Exception("Exception raised intentionally")
            func(plugin)
            plugin_name = plugin['name']
            task_target = ctx.task_target or 'local'
            data[task_target] = data.get(task_target, {})
            data[task_target][plugin_name] = \
                data[task_target].get(plugin_name, [])
            data[task_target][plugin_name].append(new_state)
        ctx.logger.info('Plugin {0} {1}'.format(plugin['name'], new_state))
예제 #41
0
def append_to_state(ctx):
    with update_storage(ctx) as data:
        data['state'] = data.get('state', [])
        data['state'].append({
            'source_id':
            ctx.source.instance.id,
            'target_id':
            ctx.target.instance.id,
            'time':
            time(),
            'source_properties':
            dict(ctx.source.node.properties),
            'source_runtime_properties':
            dict(ctx.source.instance.runtime_properties),
            'target_properties':
            ctx.target.node.properties,
            'target_runtime_properties':
            ctx.target.instance.runtime_properties,
        })
예제 #42
0
def fail(ctx, **kwargs):
    fail_count = get_prop('fail_count', ctx, kwargs, 1000000)

    with update_storage(ctx) as data:
        data['failure_invocation'] = data.get('failure_invocation', [])
        data['failure_invocation'].append(time.time())

    if len(data['failure_invocation']) > fail_count:
        return

    message = 'TEST_EXPECTED_FAIL'
    non_recoverable = get_prop('non_recoverable', ctx, kwargs, False)
    recoverable = get_prop('recoverable', ctx, kwargs, False)
    retry_after = get_prop('retry_after', ctx, kwargs)

    if non_recoverable:
        exception = NonRecoverableError(message)
    elif recoverable:
        exception = RecoverableError(message, retry_after=retry_after)
    else:
        exception = RuntimeError(message)

    raise exception
예제 #43
0
def fail(ctx, **kwargs):
    fail_count = get_prop('fail_count', ctx, kwargs, 1000000)

    with update_storage(ctx) as data:
        data['failure_invocation'] = data.get('failure_invocation', [])
        data['failure_invocation'].append(time.time())

    if len(data['failure_invocation']) > fail_count:
        return

    message = 'TEST_EXPECTED_FAIL'
    non_recoverable = get_prop('non_recoverable', ctx, kwargs, False)
    recoverable = get_prop('recoverable', ctx, kwargs, False)
    retry_after = get_prop('retry_after', ctx, kwargs)

    if non_recoverable:
        exception = NonRecoverableError(message)
    elif recoverable:
        exception = RecoverableError(message, retry_after=retry_after)
    else:
        exception = RuntimeError(message)

    raise exception
예제 #44
0
def delete(ctx, **kwargs):
    with update_storage(ctx) as data:
        data[ctx.instance.id] = data.get(ctx.instance.id, {})
        data[ctx.instance.id]['delete'] = data[ctx.instance.id].get(
            'delete', {})
        data[ctx.instance.id]['delete'] = {'target': ctx.task_target}
예제 #45
0
def touch(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['touched_time'] = data.get('touched_time', None)
        data['touched_time'] = time.time()
예제 #46
0
def delete(ctx, **kwargs):
    with update_storage(ctx) as data:
        data[ctx.instance.id] = data.get(ctx.instance.id, [])
        data[ctx.instance.id].append('delete')
예제 #47
0
def get_state(**kwargs):
    with update_storage(ctx) as data:
        return data['machines'][ctx.instance.id] == RUNNING
예제 #48
0
def saving_multiple_params_op(ctx, params, **_):
    with update_storage(ctx) as data:
        invocations = data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', []
        )
        invocations.append(params)
예제 #49
0
def append_node_state(ctx, **kwargs):
    client = get_rest_client()
    instance = client.node_instances.get(ctx.instance.id)
    with update_storage(ctx) as data:
        data['node_states'] = data.get('node_states', [])
        data['node_states'].append(instance.state)
예제 #50
0
def append_node_state(ctx, **kwargs):
    client = get_rest_client()
    instance = client.node_instances.get(ctx.instance.id)
    with update_storage(ctx) as data:
        data['node_states'] = data.get('node_states', [])
        data['node_states'].append(instance.state)
예제 #51
0
def delete(ctx, **kwargs):
    with update_storage(ctx) as data:
        data[ctx.instance.id] = data.get(ctx.instance.id, {})
        data[ctx.instance.id]['delete'] = data[
            ctx.instance.id].get('delete', {})
        data[ctx.instance.id]['delete'] = {'target': ctx.task_target}
예제 #52
0
def touch(ctx, **kwargs):
    with update_storage(ctx) as data:
        data['touched_time'] = data.get('touched_time', None)
        data['touched_time'] = time.time()
예제 #53
0
from cloudify import ctx

from integration_tests_plugins.utils import update_storage

with update_storage(ctx) as data:
    data['op3_called'] = True
예제 #54
0
from cloudify import ctx

from integration_tests_plugins.utils import update_storage


with update_storage(ctx) as data:
    data['op3_called'] = True
예제 #55
0
def saving_multiple_params_op(ctx, params, **_):
    with update_storage(ctx) as data:
        invocations = data['mock_operation_invocation'] = data.get(
            'mock_operation_invocation', [])
        invocations.append(params)
예제 #56
0
def get_state(**kwargs):
    with update_storage(ctx) as data:
        return data['machines'][ctx.instance.id] == RUNNING