Exemple #1
0
def test_payload_get_path():
    expected = 'RESULT'
    payload = Payload({'d': {'v': expected}})

    assert get_path(payload, 'data/value') == expected
    assert get_path(payload, 'data|value', delimiter='|') == expected
    assert get_path(payload, 'data/missing', default='DEFAULT') == 'DEFAULT'

    with pytest.raises(KeyError):
        get_path(payload, 'data/missing')
def test_api_action_transactions(read_json, registry):
    transport = Payload(read_json('transport.json'))
    service_name = 'foo'
    service_version = '1.0'
    service_action = 'foo'
    params = [Param('dummy', value=123)]
    action = Action(
        **{
            'action': service_action,
            'params': [],
            'transport': transport,
            'component': None,
            'path': '/path/to/file.py',
            'name': service_name,
            'version': service_version,
            'framework_version': '1.0.0',
        })

    # Clear transport transactions
    assert transport.path_exists('transactions')
    del transport[FIELD_MAPPINGS['transactions']]
    assert not transport.path_exists('transactions')

    tr_params = [{
        PARAM['name']: 'dummy',
        PARAM['value']: 123,
        PARAM['type']: TYPE_INTEGER
    }]
    actions = ('action-1', 'action-2')

    cases = {
        'commit': action.commit,
        'rollback': action.rollback,
        'complete': action.complete,
    }

    # Check all transaction types
    for type, register in cases.items():
        # Register 2 transaction actions for current type
        for name in actions:
            assert register(name, params=params) == action

        path = 'transactions/{}'.format(type)
        assert transport.path_exists(path)
        transactions = transport.get(path)
        assert isinstance(transactions, list)
        for tr in transactions:
            assert isinstance(tr, dict)
            assert get_path(tr, 'name', default='NO') == service_name
            assert get_path(tr, 'version', default='NO') == service_version
            assert get_path(tr, 'action', default='NO') in actions
            assert get_path(tr, 'caller',
                            default='NO') == action.get_action_name()
            assert get_path(tr, 'params', default='NO') == tr_params
Exemple #3
0
def test_payload_set_path():
    expected = 'RESULT'
    payload = Payload({'d': {'v': 1}})

    assert not path_exists(payload, 'data/missing')
    assert set_path(payload, 'data/missing', expected) == payload
    assert get_path(payload, 'data/missing', default='B') == expected
def test_api_action_call_remote(read_json, registry):
    service_name = 'foo'
    service_version = '1.0'

    # Check that registry does not have mappings
    assert not registry.has_mappings
    # Add an empty test action to mappings
    registry.update_registry({
        service_name: {
            service_version: {
                FIELD_MAPPINGS['files']: True,
                FIELD_MAPPINGS['actions']: {
                    'test': {}
                },
            },
        },
    })

    transport = Payload(read_json('transport.json'))
    calls_path = 'calls/{}/{}'.format(nomap(service_name), service_version)
    action = Action(
        **{
            'action': 'test',
            'params': [],
            'transport': transport,
            'component': None,
            'path': '/path/to/file.py',
            'name': service_name,
            'version': service_version,
            'framework_version': '1.0.0',
        })

    # Clear transport calls
    assert transport.path_exists('calls')
    del transport[FIELD_MAPPINGS['calls']]
    assert not transport.path_exists('calls')

    # Prepare call arguments
    params = [Param('dummy', value=123)]
    c_addr = '87.65.43.21:4321'
    c_name = 'foo'
    c_version = '1.1'
    c_action = 'bar'
    c_params = [{
        PARAM['name']: 'dummy',
        PARAM['value']: 123,
        PARAM['type']: TYPE_INTEGER
    }]

    # Make a remotr call
    kwargs = {
        'address': c_addr,
        'service': c_name,
        'version': c_version,
        'action': c_action,
        'params': params,
        'timeout': 2.0,
    }
    assert action.remote_call(**kwargs) == action
    assert transport.path_exists(calls_path)
    calls = transport.get(calls_path)
    assert isinstance(calls, list)
    assert len(calls) == 1
    call = calls[0]
    assert isinstance(call, dict)
    assert get_path(call, 'gateway', default='NO') == 'ktp://{}'.format(c_addr)
    assert get_path(call, 'name', default='NO') == c_name
    assert get_path(call, 'version', default='NO') == c_version
    assert get_path(call, 'action', default='NO') == c_action
    assert get_path(call, 'params', default='NO') == c_params

    # Make a call and add files
    files_path = '|'.join([
        'files',
        transport.get('meta/gateway')[1],
        nomap(c_name),
        c_version,
        nomap(c_action),
    ])
    kwargs['files'] = [action.new_file('download', '/tmp/file.ext')]
    assert action.remote_call(**kwargs) == action
    tr_files = transport.get(files_path, delimiter='|')
    assert isinstance(tr_files, list)
    assert len(tr_files) == 1
    assert tr_files[0] == {
        FIELD_MAPPINGS['name']: 'download',
        FIELD_MAPPINGS['token']: '',
        FIELD_MAPPINGS['filename']: 'file.ext',
        FIELD_MAPPINGS['size']: 0,
        FIELD_MAPPINGS['mime']: 'text/plain',
        FIELD_MAPPINGS['path']: 'file:///tmp/file.ext',
    }

    # Set file server mappings to False and try to call with local files
    registry.update_registry({
        service_name: {
            service_version: {
                FIELD_MAPPINGS['files']: False,
                FIELD_MAPPINGS['actions']: {
                    'test': {}
                },
            },
        },
    })

    # TODO: Figure out why existing action does not see new mappungs.
    #       Action should read the mapping values from previous statement.
    action = Action(
        **{
            'action': 'test',
            'params': [],
            'transport': transport,
            'component': None,
            'path': '/path/to/file.py',
            'name': service_name,
            'version': service_version,
            'framework_version': '1.0.0',
        })

    with pytest.raises(NoFileServerError):
        action.remote_call(**kwargs)