Example #1
0
def test_response_join():
    def _callback(response, ):
        assert response.request.command == 'join'
        assert not response.error
        assert response.body is not None
        assert response.seq == 1

        _body = response.body

        assert isinstance(
            _body,
            dict,
        )
        assert 'Num' in _body
        assert _body.get('Num') == 1

    _body = dict(
        Existing=('127.0.0.1:7901', ),
        Replay=True,
    )
    _client = serf.Client(connection_class=JoinFakeConnectionNormal, )
    _client.join(**_body).add_callback(_callback, ).request()

    _client = serf.Client(connection_class=JoinFakeConnectionMangled, )
    _client.join(**_body).add_callback(_callback, ).request()
Example #2
0
def test_response_query():
    _client = serf.Client(connection_class=QueryFakeConnection, )

    def _callback(response, ):
        assert response.request.command == 'query'
        assert not response.error
        assert response.is_success
        assert response.body is not None
        assert response.seq == 1
        assert response.body == QueryFakeConnection.responses[len(_responses)]

        _responses.append(response)
        return

    _body = dict(
        FilterNodes=[
            "node0",
            "node1",
        ],
        FilterTags={"role": ".*"},
        RequestAck=True,
        Timeout=0,
        Name='response-me',
        Payload='this is payload of `response-me` query event',
    )

    _responses = list()

    _client.query(**_body).add_callback(_callback, ).watch()
Example #3
0
def test_response_query():
    _client = serf.Client(connection_class=QueryFakeConnection, )

    def _callback(response, ):
        assert response.request.command == 'query'
        assert not response.error
        assert response.is_success
        assert response.body is not None
        assert response.seq == 1

        return

    _body = dict(
        FilterNodes=[
            "node0",
            "node1",
        ],
        FilterTags={"role": ".*"},
        RequestAck=True,
        Timeout=0,
        Name='response-me',
        Payload='this is payload of `response-me` query event',
    )

    _responses_all = _client.query(**_body).add_callback(_callback, ).watch()

    _responses = list()
    for i in _responses_all:
        if i.request.command not in ('query', ):
            continue

        _responses.append(i, )

    for _n, i in enumerate(QueryFakeConnection.responses):
        assert _responses[_n].body == i
Example #4
0
def test_response_stream_stop_receive_data():
    _client = serf.Client(connection_class=StreamFakeConnection, )

    _responses = list()

    def _callback(response, ):
        assert response.request.command == 'stream'
        assert not response.error
        assert response.is_success
        assert response.body is not None
        assert response.seq == 1

        assert response.body.get('LTime') in StreamFakeConnection.events

        if response.body.get('LTime') in (7, ):
            raise _exceptions.StopReceiveData

        _responses.append(response, )

        return

    _body = dict(Type='*', )

    _client.stream(**_body).add_callback(_callback, ).watch()
    assert len(_responses) == 2

    _event_ids = StreamFakeConnection.events.keys()
    _event_ids.sort()

    for _n, i in enumerate(_event_ids[:len(_responses)]):
        assert _responses[_n].body == StreamFakeConnection.events[i]

    return
def test_support_context_manager () :
    _client = serf.Client(connection_class=HandshakeFakeConnection, )
    with _client :
        assert not _client._conn.disconnected

        _client.handshake()
        _client.request()

    assert _client._conn.disconnected
Example #6
0
def test_fake_connection():
    _client = serf.Client(connection_class=FakeConnection, )
    assert _client._conn.connection is not None
    assert not _client._conn.disconnected

    assert isinstance(
        _client._conn.connection,
        FakeSocket,
    )
Example #7
0
def test_hosts_argument_simple():
    _host = (
        '192.168.100.1',
        7374,
    )
    _client = serf.Client('%s:%s' % _host, )

    assert len(_client._conn._members) == 1
    assert _client._conn._members[0][:2] == _host
Example #8
0
def test_hosts_argument_with_auth_token():
    _host = 'serf://192.168.100.1:7374?AuthKey=this-is-token,serf://192.168.100.2:7373?AuthKey=this-is-another-token'

    _client = serf.Client(_host, )
    assert 'AuthKey' in _client._conn._members[0][2]
    assert _client._conn._members[0][2]['AuthKey'] == 'this-is-token'

    assert 'AuthKey' in _client._conn._members[1][2]
    assert _client._conn._members[1][2]['AuthKey'] == 'this-is-another-token'
Example #9
0
def test_hosts_argument_url_format():
    _host = (
        'serf',
        '192.168.100.1',
        7374,
    )
    _client = serf.Client('%s://%s:%s' % _host, )

    assert len(_client._conn._members) == 1
    assert _client._conn._members[0][:2] == _host[1:]
Example #10
0
def test_default_hosts():
    _client = serf.Client()

    assert _client._conn._members == [
        (
            serf.constant.DEFAULT_HOST,
            serf.constant.DEFAULT_PORT,
            dict(),
        ),
    ]
Example #11
0
def test_response_handshake():
    _client = serf.Client(connection_class=HandshakeFakeConnection, )

    def _callback(response, ):
        assert response.request.command == 'handshake'
        assert not response.error
        assert response.is_success
        assert response.body is None
        assert response.seq == 0

    _client.handshake().add_callback(_callback, ).request()
Example #12
0
def test_response_members_got_error():
    _client = serf.Client(connection_class=MembersFakeConnectionGotError, )

    def _callback(response, ):
        assert response.request.command == 'members'
        assert response.error
        assert not response.is_success
        assert response.body is None
        assert response.seq == 1

    _client.members().add_callback(_callback, ).request()
def test_response_force_leave():
    _client = serf.Client(connection_class=ForceLeaveFakeConnection, )

    def _callback(response, ):
        assert response.request.command == 'force_leave'
        assert not response.error
        assert response.is_success
        assert response.body is None
        assert response.seq == 1

    _body = dict(Node='node0', )
    _client.force_leave(**_body).add_callback(_callback, ).request()
Example #14
0
    def del_tag(self, tag_names_list):
        resp = None

        try:
            client = serf.Client("%s:%d" % (self.rpc_address, self.rpc_port))
            client.connect()
            client.tags(DeleteTags=tag_names_list)
            resp = client.request()
            client.disconnect()
        except serf._exceptions.ConnectionError:
            print "Connection error"

        return resp
Example #15
0
def test_got_error():
    def _callback(response, ):
        assert response.request.command == 'join'
        assert response.error
        assert not response.is_success
        assert response.body is None
        assert response.seq == 1

    _body = dict(
        Existing=('127.0.0.1:7901', ),
        Replay=True,
    )
    _client = serf.Client(connection_class=JoinFakeConnectionGotError, )
    _client.join(**_body).add_callback(_callback, ).request()
Example #16
0
def test_response_handshake_default_callback():
    _client = serf.Client(connection_class=HandshakeFakeConnection, )
    assert not _client.is_handshaked

    def _callback(response, ):
        assert response.request.command == 'handshake'
        assert not response.error
        assert response.is_success
        assert response.body is None
        assert response.seq == 0

    _client.handshake().add_callback(_callback, ).force_leave(
        Node='unknown-node0', ).request()

    assert _client.is_handshaked
Example #17
0
    def get_members(self):
        # Retrieve informations from all the serf memebrs.
        # This is done through the RPC members call.

        resp = None

        try:
            client = serf.Client("%s:%d" % (self.rpc_address, self.rpc_port))
            client.connect()
            client.members()
            resp = client.request(timeout=5)
            client.disconnect()
        except serf._exceptions.ConnectionError:
            print "Connection error"

        return resp
Example #18
0
def test_response_handshake_and_auth():
    _auth_key = uuid.uuid1().hex
    _host = 'serf://127.0.0.1:7373?AuthKey=%s' % _auth_key
    _client = serf.Client(
        _host,
        connection_class=HandshakeFakeConnection,
    )

    def _callback(response, ):
        assert response.request.command == 'handshake'
        assert not response.error
        assert response.is_success
        assert response.body is None
        assert response.seq == 0

    _client.handshake().add_callback(_callback, ).request()
Example #19
0
def test_response_auth_success():
    _client = serf.Client(connection_class=AuthFakeConnectionSuccess, )

    def _callback(response, ):
        assert response.request.command == 'auth'
        assert not response.error
        assert response.is_success
        assert response.body is None
        assert response.seq == 1

    _body = dict(AuthKey='this-is-valid-authkey', )

    assert not _client.is_authed

    _client.auth(**_body).add_callback(_callback, ).request()

    assert _client.is_authed
Example #20
0
def test_response_event () :
    _client = serf.Client(connection_class=EventFakeConnection, )

    def _callback (response, ) :
        assert response.request.command == 'event'
        assert not response.error
        assert response.is_success
        assert response.body is None
        assert response.seq == 1


    _body = dict(
            Name='anonymous-event',
            Payload='payload',
        )

    _client.event(**_body).add_callback(_callback, ).request()
Example #21
0
def test_response_query_invalid_ack():
    _client = serf.Client(connection_class=QueryFakeConnectionInvalidAck, )

    _body = dict(
        RequestAck=True,
        Timeout=0,
        Name='response-me',
        Payload='this is payload of `response-me` query event',
    )

    _responses_all = _client.query(**_body).watch()

    for i in _responses_all:
        if i.request.command not in ('query', ):
            continue

        if i.body.get('Type', ) in ('ack', ):
            assert i.body.get('From', ) != 'node0'
Example #22
0
def test_response_auth_failed():
    _client = serf.Client(connection_class=AuthFakeConnectionFailed, )

    def _callback(response, ):
        assert response.request.command == 'auth'
        assert response.error
        assert not response.is_success
        assert response.body is None
        assert response.seq == 1

    _body = dict(AuthKey='this-is-bad-authkey', )

    assert not _client.is_authed

    with pytest.raises(serf.AuthenticationError, ):
        _client.auth(**_body).add_callback(_callback, ).request()

    assert not _client.is_authed
Example #23
0
def test_hosts_argument_missing_port():
    _hosts_parsed = [
        ('127.0.0.1', ),
        ('192.168.0.22', ),
    ]
    _hosts = ','.join(
        map(
            lambda x: ':'.join(map(
                str,
                x,
            ), ),
            _hosts_parsed,
        ), )

    _client = serf.Client(hosts=_hosts, )
    for n, i in enumerate(_client._conn._members, ):
        assert i[0] == _hosts_parsed[n][0]
        assert i[1] == serf.constant.DEFAULT_PORT
Example #24
0
def test_response_members():
    _client = serf.Client(connection_class=MembersFakeConnection, )

    def _callback(response, ):
        assert response.request.command == 'members'
        assert not response.error
        assert response.is_success
        assert response.body is not None
        assert response.seq == 1

        _members = response.body.get('Members', )

        assert 'Members' in response.body
        assert len(_members, ) == 2

        # Addr
        assert _members[0].get('Addr') == [127, 0, 0, 1]
        assert _members[1].get('Addr') == [127, 0, 0, 1]

        # Name
        assert _members[0].get('Name') == 'node0'
        assert _members[1].get('Name') == 'node1'

        # Port
        assert _members[0].get('Port') == 7900
        assert _members[1].get('Port') == 7901

        # Tags
        assert isinstance(
            _members[0].get('Tags'),
            dict,
        )
        assert isinstance(
            _members[1].get('Tags'),
            dict,
        )
        assert _members[0].get('Tags') == {'role': 'test-server'}

        # Status
        assert _members[0].get('Status') == 'alive'
        assert _members[1].get('Status') == 'leaving'

    _client.members().add_callback(_callback, ).request()
def test_response_monitor():
    _client = serf.Client(connection_class=MonitorFakeConnection, )

    logs = list(MonitorFakeConnection.logs[:], )

    def _callback(response, ):
        assert response.request.command == 'monitor'
        assert not response.error
        assert response.is_success
        assert response.body is not None
        assert response.seq == 1

        _log = response.body
        _log_received = logs.pop(0, )
        assert _log == _log_received

    _body = dict(LogLevel='DEBUG', )

    _client.monitor(**_body).add_callback(_callback, ).watch()
Example #26
0
def test_response_auth_failed():
    """
    when the body first received.
    """

    _client = serf.Client(connection_class=FakeConnectionAbnormal, )

    def _callback(response, ):
        assert response.request.command == 'event'
        assert not response.error
        assert response.is_success
        assert response.body is None
        assert response.seq == 1

    _body = dict(
        Name='anonymous-event',
        Payload='payload',
    )

    _client.event(**_body).add_callback(_callback, ).request()
Example #27
0
def test_implicit_authentication_with_host_url_success():
    def _callback(response, ):
        assert response.request.command == 'force_leave'
        assert not response.error
        assert response.is_success
        assert response.body is None
        assert response.seq == 2

    _body = dict(Node='node0', )

    _auth_key = 'this-is-valid-authkey'
    _client = serf.Client(
        'serf://127.0.0.1:7373?AuthKey=%s' % _auth_key,
        connection_class=AuthFakeConnectionForceLeaveSuccess,
    )

    assert not _client.is_authed

    _client.force_leave(**_body).add_callback(_callback, ).request()

    assert _client.is_authed
Example #28
0
    def listen_for_member_update_events(self, ch_dbfile):
        print "Database file: %s" % (ch_dbfile, )

        members_updated = False
        while True:

            # Write the db file based on the current members channels tags
            sleep_time = 5

            self.client = serf.Client("%s:%d" %
                                      (self.rpc_address, self.rpc_port),
                                      auto_reconnect=True)

            while not members_updated:
                try:
                    self.client.connect()

                    self.ch_dbfile = ch_dbfile
                    if self.update_db_from_members():
                        members_updated = True
                    else:
                        time.sleep(sleep_time)
                except serf._exceptions.ConnectionError:
                    print "Client connection error (sleep %d)" % (sleep_time, )
                    time.sleep(sleep_time)

            try:
                # Register callback for memebr update events
                # Todo: handle sigint
                self.client.stream(Type="member-update").add_callback(
                    self.member_update_event_callback).request(timeout=120)
                self.client.disconnect()
                members_updated = False
            except serf._exceptions.ConnectionError:
                print "Client connection error (sleep %d)" % (sleep_time, )
                time.sleep(sleep_time)
            except KeyboardInterrupt:
                print "Disconnection from RPC deamon"
                self.client.disconnect()
                return
Example #29
0
def test_hosts_argument():
    _hosts_parsed = [
        (
            '127.0.0.1',
            7373,
        ),
        (
            '192.168.0.22',
            7300,
        ),
    ]
    _hosts = ','.join(
        map(
            lambda x: ':'.join(map(
                str,
                x,
            ), ),
            _hosts_parsed,
        ), )

    _client = serf.Client(hosts=_hosts, )
    assert map(lambda x: x[:2], _client._conn._members) == _hosts_parsed
def test_response_stream():
    _client = serf.Client(connection_class=StreamFakeConnection, )

    def _callback(response, ):
        assert response.request.command == 'stream'
        assert not response.error
        assert response.is_success
        assert response.body is not None
        assert response.seq == 1

        assert response.body.get('LTime') in StreamFakeConnection.events

        _event_received = response.body
        _event_sent = StreamFakeConnection.events.get(
            _event_received.get('LTime'), )

        assert _event_sent.get('Name') == _event_received.get('Name')
        assert _event_sent.get('Payload') == _event_received.get('Payload')
        assert _event_sent.get('Event') == _event_received.get('Event')

    _body = dict(Type='*', )

    _client.stream(**_body).add_callback(_callback, ).watch()