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')
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
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 })
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]
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() })
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')
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 })
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')
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')
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
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
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 }
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' })
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
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
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
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
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']
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
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
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
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)
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)
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)
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
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()} })
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() } })
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
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)
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)
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))
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, })
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
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}
def touch(ctx, **kwargs): with update_storage(ctx) as data: data['touched_time'] = data.get('touched_time', None) data['touched_time'] = time.time()
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')
def get_state(**kwargs): with update_storage(ctx) as data: return data['machines'][ctx.instance.id] == RUNNING
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)
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)
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}
from cloudify import ctx from integration_tests_plugins.utils import update_storage with update_storage(ctx) as data: data['op3_called'] = True
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)