Пример #1
0
    def __init__(self,
                 host,
                 port,
                 callbacks=None,
                 proto=None,
                 node_name=None,
                 server_node_name=None,
                 *args,
                 **kwargs):
        super(TwistedCalvinTransportClient, self).__init__(host,
                                                           port,
                                                           callbacks=callbacks)
        self._jid = host
        self._token = port
        self._proto = proto
        self._uri = proto._uri
        self._factory = None
        self._node_name = node_name
        self._server_node_name = server_node_name
        self._runtime_credentials = None
        self._is_connected = False
        if proto:
            proto.callback_register('connected', CalvinCB(self._connected))
            proto.callback_register('disconnected',
                                    CalvinCB(self._disconnected))
            proto.callback_register('data', CalvinCB(self._data))

        self._callbacks = callbacks
        self.sendConnected()
Пример #2
0
    def init(self,
             address,
             port,
             connection_type,
             mode="raw",
             delimiter="\r\n"):
        self.connection = None
        self.data = b""
        if connection_type == "TCP":
            connection_factory = client_connection.TCPClientProtocolFactory(
                mode=mode,
                delimiter=delimiter,
                server_node_name=None,
                callbacks={'data_received': [CalvinCB(self._data_received)]})
        elif connection_type == "UDP":
            connection_factory = client_connection.UDPClientProtocolFactory(
                callbacks={'data_received': [CalvinCB(self._data_received)]})

        connection_factory.callback_register(
            'connected', CalvinCB(self._connected, connection_factory))
        connection_factory.callback_register('disconnected',
                                             CalvinCB(self._disconnected))
        connection_factory.callback_register(
            'connection_failed',
            CalvinCB(self._connection_failed, connection_factory))
        connection_factory.connect(address, port)
    def __init__(self, rt_id, remote_uri, callbacks, transport, proto=None):
        """docstring for __init__"""
        super(CalvinTransport, self).__init__(rt_id,
                                              remote_uri,
                                              callbacks=callbacks)

        self._rt_id = rt_id
        self._remote_rt_id = None
        self._coder = None
        self._transport = transport(self._uri.hostname,
                                    self._uri.port,
                                    callbacks,
                                    proto=proto)
        self._rtt = 2000  # Init rt in ms

        # TODO: This should be incoming param
        self._verify_client = lambda x: True

        self._incoming = proto is not None
        if self._incoming:
            # TODO: Set timeout
            # Incomming connection timeout if no join
            self._transport.callback_register("disconnected",
                                              CalvinCB(self._disconnected))
            self._transport.callback_register("data",
                                              CalvinCB(self._data_received))
Пример #4
0
    def test_application_functions(self):
        self.q = Queue.Queue()

        def cb(key, value):
            self.q.put({"key": key, "value": value})

        application = appmanager.Application(
            calvinuuid.uuid('APP'), "test_app",
            [calvinuuid.uuid('ACTOR'),
             calvinuuid.uuid('ACTOR')], calvinuuid.uuid("NODE"), None)

        self.storage.add_application(application, cb=CalvinCB(cb))
        yield wait_for(self.q.empty, condition=lambda x: not x())
        value = self.q.get(timeout=.001)
        # print value

        self.storage.get_application(application.id, cb=CalvinCB(func=cb))
        yield wait_for(self.q.empty, condition=lambda x: not x())
        value = self.q.get(timeout=.001)
        # print value
        assert value["key"] == application.id and value["value"][
            "name"] == application.name

        self.storage.delete_application(application.id, cb=CalvinCB(func=cb))
        yield wait_for(self.q.empty, condition=lambda x: not x())
        value = self.q.get(timeout=.001)
        assert application.id not in self.storage.localstore
Пример #5
0
    def __init__(self, host, port, callbacks=None, proto=None, node_name=None, server_node_name=None, *args, **kwargs):
        super(TwistedCalvinTransport, self).__init__(host, port, callbacks=callbacks)
        self._host_ip = host
        self._host_port = port
        self._proto = proto
        self._factory = None
        self._node_name = node_name
        self._server_node_name=server_node_name
        self._runtime_credentials = None

        # Server created us already have a proto
        if proto:
            proto.callback_register('connected', CalvinCB(self._connected))
            proto.callback_register('disconnected', CalvinCB(self._disconnected))
            proto.callback_register('data', CalvinCB(self._data))

        self._callbacks = callbacks

        #If TLS is chosen, ensure that a node_name and a server_node_name are set
        runtime_to_runtime_security = _conf.get("security","runtime_to_runtime_security")
        if (runtime_to_runtime_security=="tls"):
            if self._node_name==None or self._server_node_name==None:
                _log.error("For TLS, both node_name and server_node_name must be given as input"
                                "\n\tself._node_name={}"
                                "\n\tself._server_node_name={}".format(self._node_name, self._server_node_name))
                raise Exception("For TLS, both node_name and server_node_name must be given as input")
Пример #6
0
 def get_callbacks(self):
     return {
         'peer_disconnected': [CalvinCB(self._peer_disconnected)],
         'peer_connection_failed': [CalvinCB(self._connection_failed)],
         'join_failed': [CalvinCB(self._join_failed)],
         'peer_connected': [CalvinCB(self._peer_connected)]
     }
Пример #7
0
    def test_node_functions(self):
        self.q = Queue.Queue()

        def cb(key, value):
            self.q.put({"key": key, "value": value})

        node = calvin.tests.TestNode(["127.0.0.1:5000"])
        self.storage.add_node(node)
        self.storage.get_node(node_id=node.id, cb=CalvinCB(func=cb))
        yield wait_for(self.q.empty, condition=lambda x: not x())
        value = self.q.get(timeout=.001)
        assert value["key"] == node.id and value["value"] == {
            u'attributes': {
                u'indexed_public': [],
                u'public': {}
            },
            u'control_uris': [u'127.0.0.1:5000'],
            'uris': node.uris
        }

        self.storage.delete_node(node, cb=CalvinCB(func=cb))
        yield wait_for(self.q.empty, condition=lambda x: not x())
        value = self.q.get(timeout=.001)
        assert value
        assert node.id not in self.storage.localstore
Пример #8
0
    def test_late_start(self):
        self.q = Queue.Queue()

        def cb(key, value):
            self.q.put({"key": key, "value": value})

        def started_cb(started):
            self.q.put(started)

        self.storage = storage.Storage()
        self.storage.set("test", "1", 1, None)
        self.storage.set("test", "2", 2, None)
        self.storage.set("test", "3", 3, None)

        assert "test1" in self.storage.localstore
        assert "test2" in self.storage.localstore
        assert "test3" in self.storage.localstore

        yield threads.defer_to_thread(self.storage.start, CalvinCB(started_cb))
        yield threads.defer_to_thread(time.sleep, 2)
        value = self.q.get(timeout=0.2)
        assert value

        assert "test1" not in self.storage.localstore
        assert "test2" not in self.storage.localstore
        assert "test3" not in self.storage.localstore

        yield threads.defer_to_thread(self.storage.get, "test", "3",
                                      CalvinCB(func=cb))
        value = self.q.get(timeout=0.2)
        assert value["value"] == 3

        yield threads.defer_to_thread(self.storage.stop)
Пример #9
0
    def test_application_functions(self):
        self.q = Queue.Queue()

        def cb(key, value):
            self.q.put({"key": key, "value": value})

        application = appmanager.Application(
            calvinuuid.uuid('APP'), "test_app",
            [calvinuuid.uuid('ACTOR'),
             calvinuuid.uuid('ACTOR')], calvinuuid.uuid("NODE"), None)

        self.storage.add_application(application, cb=CalvinCB(cb))
        yield wait_for(self.q.empty, condition=lambda x: not x())
        value = self.q.get(timeout=.001)
        assert isinstance(value["value"], calvinresponse.CalvinResponse
                          ) and value["value"] == calvinresponse.OK

        self.storage.get_application(application.id, cb=CalvinCB(cb))
        yield wait_for(self.q.empty, condition=lambda x: not x())
        value = self.q.get(timeout=.001)
        assert value["value"]["name"] == application.name

        self.storage.delete_application(application.id, cb=CalvinCB(cb))
        yield wait_for(self.q.empty, condition=lambda x: not x())
        value = self.q.get(timeout=.001)
        assert isinstance(value["value"], calvinresponse.CalvinResponse
                          ) and value["value"] == calvinresponse.OK

        self.storage.get_application(application.id, cb=CalvinCB(cb))
        yield wait_for(self.q.empty, condition=lambda x: not x())
        value = self.q.get(timeout=.001)
        assert isinstance(value["value"], calvinresponse.CalvinResponse
                          ) and value["value"] == calvinresponse.NOT_FOUND
Пример #10
0
 def destroy_with_disconnect(self,
                             actor_id,
                             terminate=DISCONNECT.TERMINATE,
                             callback=None):
     if actor_id not in self.actors:
         self._actor_not_found(actor_id)
     if terminate == DISCONNECT.EXHAUST:
         actor = self.actors[actor_id]
         actor.exhaust(
             CalvinCB(self._destroy_with_disconnect_exhausted,
                      actor_id=actor_id,
                      terminate=terminate,
                      callback=callback))
         # Exhaust first disconnects all inports then all outports after inports exhausted
         self.node.pm.disconnect(callback=CalvinCB(
             self._destroy_with_disconnect_in_cb,
             terminate=terminate,
             callback=callback),
                                 actor_id=actor_id,
                                 port_dir="in",
                                 terminate=terminate)
     else:
         self.node.pm.disconnect(callback=CalvinCB(
             self._destroy_with_disconnect_cb, callback=callback),
                                 actor_id=actor_id,
                                 terminate=terminate)
Пример #11
0
 def test_append_multi_getconcat(self, setup):
     """ Test that appending multiple values returns OK and get_concat returns the unique values (set) in a list """
     self.nodes = setup.get("nodes")
     storage_types = self.verify_started()
     for stype in storage_types:
         for i in [("aa", "aa"), ("bb", 2, "ff"), (True, ""), (False, True)
                   ]:  # values must be hashable (i.e. not list or dict)
             key = str(i)
             for y in range(3):
                 self.done = False
                 self.get_ans = None
                 self.nodes[stype][y].storage.append(
                     "test4-", key + str(y), i, CalvinCB(self.cb))
                 yield wait_for(self._test_done, timeout=10)
                 # Verify response is CalvinResponse object with OK status
                 print "append response", self.get_ans, stype, key, y
                 assert isinstance(self.get_ans,
                                   calvinresponse.CalvinResponse
                                   ) and self.get_ans == calvinresponse.OK
                 for x in range(3):
                     if stype == "notstarted" and x != y:
                         # Not started storage is never connected to other nodes storage
                         continue
                     self.done = False
                     self.get_ans = None
                     self.nodes[stype][x].storage.get_concat(
                         "test4-", key + str(y), CalvinCB(self.cb))
                     yield wait_for(self._test_done, timeout=10)
                     print "get_concat response", self.get_ans, stype, key, x
                     # Verify we read what is written
                     assert set(self.get_ans) == set(i)
Пример #12
0
 def test_set_get(self, setup):
     """ Test that set returns OK and get returns value set """
     self.nodes = setup.get("nodes")
     storage_types = self.verify_started()
     for stype in storage_types:
         for i in [
                 "aa", "bb", "", None, True, False, 0, 1, 2, [0, 1], {
                     "xx": 10
                 }
         ]:
             key = str(i)
             for y in range(3):
                 self.done = False
                 self.get_ans = None
                 self.nodes[stype][y].storage.set("test1-", key + str(y), i,
                                                  CalvinCB(self.cb))
                 yield wait_for(self._test_done, timeout=10)
                 # Verify response is CalvinResponse object with OK status
                 print "set response", self.get_ans, stype, key, y
                 assert isinstance(self.get_ans,
                                   calvinresponse.CalvinResponse
                                   ) and self.get_ans == calvinresponse.OK
                 for x in range(3):
                     if stype == "notstarted" and x != y:
                         # Not started storage is never connected to other nodes storage
                         continue
                     self.done = False
                     self.get_ans = None
                     self.nodes[stype][x].storage.get(
                         "test1-", key + str(y), CalvinCB(self.cb))
                     yield wait_for(self._test_done, timeout=10)
                     print "get response", self.get_ans, stype, key, x
                     # Verify we read what is written
                     assert self.get_ans == i
Пример #13
0
    def start(self):
        # These should come from us
        self._transport.callback_register('server_started', CalvinCB(self._started))
        self._transport.callback_register('server_stopped', CalvinCB(self._stopped))
        self._transport.callback_register('client_connected', CalvinCB(self._client_connected))

        # Start the server
        self._port = self._transport.start()
Пример #14
0
 def get_callbacks(self):
     return {
         'server_started': [CalvinCB(self._server_started)],
         'server_stopped': [CalvinCB(self._server_stopped)],
         'join_failed': [CalvinCB(self._join_failed)],
         'peer_disconnected': [CalvinCB(self._peer_disconnected)],
         'peer_connected': [CalvinCB(self._peer_connected)]
     }
Пример #15
0
 def __init__(self, node, actor):
     _log.info("Started HTTPClientHandler")
     self._actor = actor
     self._node = node
     callbacks = {'receive-headers': [CalvinCB(self._receive_headers)],
                  'receive-body': [CalvinCB(self._receive_body)]}
     self._client = http_client.HTTPClient(callbacks)
     self._requests = {}
Пример #16
0
 def _got_link(self, master_id, org_cb):
     _log.debug("_got_link %s, %s" % (master_id, org_cb))
     self.master_id = master_id
     self.tunnel = self.node.proto.tunnel_new(self.master_id, 'storage', {})
     self.tunnel.register_tunnel_down(
         CalvinCB(self.tunnel_down, org_cb=org_cb))
     self.tunnel.register_tunnel_up(CalvinCB(self.tunnel_up, org_cb=org_cb))
     self.tunnel.register_recv(self.tunnel_recv_handler)
Пример #17
0
    def connect(self, status=None, port_meta=None):
        # TODO: status & port_meta unused

        tunnel = None
        if self.peer_port_meta.node_id not in self.token_tunnel.tunnels.iterkeys(
        ):
            # No tunnel to peer, get one first
            _log.analyze(self.node.id,
                         "+ GET TUNNEL",
                         self.peer_port_meta,
                         peer_node_id=self.peer_port_meta.node_id)
            tunnel = self.node.proto.tunnel_new(self.peer_port_meta.node_id,
                                                'token', {})
            tunnel.register_tunnel_down(
                CalvinCB(self.token_tunnel.tunnel_down, tunnel))
            tunnel.register_tunnel_up(
                CalvinCB(self.token_tunnel.tunnel_up, tunnel))
            tunnel.register_recv(
                CalvinCB(self.token_tunnel.tunnel_recv_handler, tunnel))
            self.token_tunnel.tunnels[self.peer_port_meta.node_id] = tunnel
        else:
            tunnel = self.token_tunnel.tunnels[self.peer_port_meta.node_id]

        if tunnel.status == CalvinTunnel.STATUS.PENDING:
            if self.peer_port_meta.node_id not in self.token_tunnel.pending_tunnels:
                self.token_tunnel.pending_tunnels[
                    self.peer_port_meta.node_id] = []
            # call _connect_via_tunnel when we get the response of the tunnel
            self.token_tunnel.pending_tunnels[
                self.peer_port_meta.node_id].append(
                    CalvinCB(self._connect_via_tunnel))
            return
        elif tunnel.status == CalvinTunnel.STATUS.TERMINATED:
            # TODO should we retry at this level?
            if self.callback:
                self.callback(status=response.CalvinResponse(
                    response.INTERNAL_ERROR),
                              actor_id=self.port.owner.id,
                              port_name=self.port.name,
                              port_id=self.port.id,
                              peer_node_id=self.peer_port_meta.node_id,
                              peer_actor_id=self.peer_port_meta.actor_id,
                              peer_port_name=self.peer_port_meta.port_name,
                              peer_port_id=self.peer_port_meta.port_id)
            return

        _log.analyze(
            self.node.id,
            "+ HAD TUNNEL", {
                'local_port':
                self.port,
                'peer_port':
                self.peer_port_meta,
                'tunnel_status':
                self.token_tunnel.tunnels[self.peer_port_meta.node_id].status
            },
            peer_node_id=self.peer_port_meta.node_id)
        self._connect_via_tunnel(status=response.CalvinResponse(True))
Пример #18
0
 def test_append_delete_append_get_concat(self, setup):
     """ Test that append returns OK, delete return OK and get returns second append """
     self.nodes = setup.get("nodes")
     storage_types = self.verify_started()
     for stype in storage_types:
         for i in ["aa"]:
             for y in range(3):
                 key = "t3" + str(i)
                 self.done = False
                 self.get_ans = None
                 self.nodes[stype][y].storage.append(
                     "test7-", key + str(y), [i], CalvinCB(self.cb))
                 yield wait_for(self._test_done,
                                timeout=10,
                                test_part="append" + key + str(y))
                 # Verify response is CalvinResponse object with OK status
                 assert isinstance(self.get_ans,
                                   calvinresponse.CalvinResponse
                                   ) and self.get_ans == calvinresponse.OK
                 print "append response", self.get_ans, stype, i, y
                 self.done = False
                 self.get_ans = None
                 self.nodes[stype][y].storage.delete(
                     "test7-", key + str(y), CalvinCB(self.cb))
                 yield wait_for(self._test_done,
                                timeout=10,
                                test_part="delete" + key + str(y))
                 # Verify response is CalvinResponse object with OK status
                 assert isinstance(self.get_ans,
                                   calvinresponse.CalvinResponse
                                   ) and self.get_ans == calvinresponse.OK
                 print "delete response", self.get_ans, stype, i, y
                 self.done = False
                 self.get_ans = None
                 self.nodes[stype][y].storage.append(
                     "test7-", key + str(y), [i + "2"], CalvinCB(self.cb))
                 yield wait_for(self._test_done,
                                timeout=10,
                                test_part="append2" + key + str(y))
                 # Verify response is CalvinResponse object with OK status
                 assert isinstance(self.get_ans,
                                   calvinresponse.CalvinResponse
                                   ) and self.get_ans == calvinresponse.OK
                 print "append2 response", self.get_ans, stype, i, y
                 for x in range(3):
                     if stype == "notstarted" and x != y:
                         # Not started storage is never connected to other nodes storage
                         continue
                     self.done = False
                     self.get_ans = None
                     self.nodes[stype][x].storage.get_concat(
                         "test7-", key + str(y), CalvinCB(self.cb))
                     yield wait_for(self._test_done,
                                    timeout=10,
                                    test_part="get_concat" + key + str(y))
                     print "get_concat response", self.get_ans, stype, i, x
                     # Verify the response is second appended value
                     assert self.get_ans == [i + "2"]
Пример #19
0
    def get_authorization_decision(self,
                                   callback,
                                   actor_id=None,
                                   requires=None,
                                   element_dict=None,
                                   decision_from_migration=None):
        """Get authorization decision using the authorization procedure specified in config."""
        #        _log.debug("Security: get_authorization_decision:\n\tcallback={}\n\tactor_id={}\n\trequires={}\n\telement_dict={}\n\tdecision_from_migration={}".format(callback, actor_id, requires, element_dict, decision_from_migration))
        if decision_from_migration:
            try:
                _log.debug("Authorization decision from migration")
                # Decode JSON Web Token, which contains the authorization response.
                decode_jwt(
                    decision_from_migration["jwt"],
                    decision_from_migration["cert_name"], self.node, actor_id,
                    CalvinCB(self._get_authorization_decision_jwt_decoded_cb,
                             callback=callback))
                return
            except Exception as e:
                _log.error("Security: JWT decoding error - %s" % str(e))
                self._return_authorization_decision("indeterminate", [],
                                                    callback)
                return
        else:
            request = {}
            request["subject"] = self.get_subject_attributes()
            if element_dict is not None:
                request["subject"].update(element_dict)
            request["resource"] = {"node_id": self.node.id}
            if requires is not None:
                request["action"] = {"requires": requires}
            _log.debug("Security: authorization request: %s" % request)

            # Check if the authorization server is local (the runtime itself) or external.
            if 'authorization' in self.sec_conf and 'procedure' in self.sec_conf[
                    'authorization']:
                if self.sec_conf['authorization']['procedure'] == "external":
                    if not HAS_JWT:
                        _log.error(
                            "Security: Install JWT to use external server as authorization method.\n"
                            + "Note: NO AUTHORIZATION USED")
                        return False
                    _log.debug(
                        "Security: external authorization method chosen")
                    self.authorize_using_external_server(
                        request, callback, actor_id)
                else:
                    _log.debug("Security: local authorization method chosen")
                    # Authorize access using a local Policy Decision Point (PDP).
                    self.node.authorization.pdp.authorize(
                        request,
                        CalvinCB(self._handle_local_authorization_response,
                                 callback=callback))
            else:
                _log.error(
                    "This should have already been caught, raise exception")
                raise ("No authorization procedure configured")
Пример #20
0
 def tunnel_request_handles(self, tunnel):
     """ Incoming tunnel request for token transport """
     # TODO check if we want a tunnel first
     self.tunnels[tunnel.peer_node_id] = tunnel
     tunnel.register_tunnel_down(CalvinCB(self.tunnel_down, tunnel))
     tunnel.register_tunnel_up(CalvinCB(self.tunnel_up, tunnel))
     tunnel.register_recv(CalvinCB(self.tunnel_recv_handler, tunnel))
     # We accept it by returning True
     return True
Пример #21
0
 def __init__(self, config):
     super(KubeCtl, self).__init__()
     self._config = config
     self._api_base = str(config["api-base"])
     if not self._api_base.endswith("/"):
         self._api_base += "/"
     self._httpclient = http_client.HTTPClient({'receive_headers' : [CalvinCB(self._receive_headers)],
                                                'receive-body': [CalvinCB(self._receive_body)]})
     self._requests = {}
Пример #22
0
def handle_actor_migrate(self, handle, connection, match, data, hdr):
    """
    POST /actor/{actor-id}/migrate
    Migrate actor to (other) node, either explicit node_id or by updated requirements
    Body: {"peer_node_id": <node-id>}
    Alternative body:
    Body:
    {
        "requirements": [ {"op": "<matching rule name>",
                          "kwargs": {<rule param key>: <rule param value>, ...},
                          "type": "+" or "-" for set intersection or set removal, respectively
                          }, ...
                        ],
        "extend": True or False  # defaults to False, i.e. replace current requirements
        "move": True or False  # defaults to False, i.e. when possible stay on the current node
    }

    For further details about requirements see application deploy.
    Response status code: OK, BAD_REQUEST, INTERNAL_ERROR or NOT_FOUND
    Response: none
    """
    status = calvinresponse.OK
    actor_id = match.group(1)
    if 'peer_node_id' in data:
        if actor_id in self.node.am.list_actors():
            try:
                self.node.am.migrate(actor_id,
                                     data['peer_node_id'],
                                     callback=CalvinCB(self.actor_migrate_cb,
                                                       handle, connection))
            except:
                _log.exception("Migration failed")
                status = calvinresponse.INTERNAL_ERROR
        else:
            self.node.storage.get_actor(
                actor_id,
                CalvinCB(func=self.handle_actor_migrate_lookup_peer_cb,
                         handle=handle,
                         connection=connection,
                         actor_id=actor_id,
                         peer_node_id=data['peer_node_id']))
    elif 'requirements' in data:
        try:
            self.node.am.update_requirements(
                match.group(1),
                data['requirements'],
                extend=data['extend'] if 'extend' in data else False,
                move=data['move'] if 'move' in data else False,
                callback=CalvinCB(self.actor_migrate_cb, handle, connection))
        except:
            _log.exception("Migration failed")
            status = calvinresponse.INTERNAL_ERROR
    else:
        status = calvinresponse.BAD_REQUEST

    if status != calvinresponse.OK:
        self.send_response(handle, connection, None, status=status)
Пример #23
0
 def get_concat(self, key, cb=None):
     """
         Gets a value from the storage
     """
     _log.debug("SQL get_concat %s" % (key,))
     key_sql = key.replace("'", r"\'")
     d = self.dbpool.runQuery(QUERY_GETCONCAT.format(keystr=key_sql))
     d.addCallbacks(CalvinCB(self._getconcat_cb, cb=cb, key=key), CalvinCB(self._getconcat_fail_cb, cb=cb, key=key))
     _log.debug("SQL get_concat %s requested" % (key,))
Пример #24
0
 def _start_link_cb(self, status, uri, peer_node_id):
     if status == "NACK":
         return
     # Got link set up tunnel
     master_id = peer_node_id
     self.tunnel = self.calvincontrol.node.proto.tunnel_new(master_id, 'control', {})
     self.tunnel.register_tunnel_down(CalvinCB(self.tunnel_down))
     self.tunnel.register_tunnel_up(CalvinCB(self.tunnel_up))
     self.tunnel.register_recv(self.tunnel_recv_handler)
Пример #25
0
    def connect(self, timeout=10):
        if self._transport.is_connected():
            raise Exception("Transport already connected")

        self._transport.callback_register("connected", CalvinCB(self._send_join))
        self._transport.callback_register("disconnected", CalvinCB(self._disconnected))
        self._transport.callback_register("data", CalvinCB(self._data_recieved))
        # TODO: set timeout
        self._transport.join()
Пример #26
0
 def test_add_remove_get_index(self, setup):
     """ Test that add_index returns OK, remove_index returns OK and get_index returns appended value in list for index hierarchies """
     self.nodes = setup.get("nodes")
     storage_types = self.verify_started()
     for stype in storage_types:
         for y in range(3):
             for i in [(["aa", "bb", "", "dd"], ["xx", "kk", "ll"], "+"),
                       (["aa", "bb", "", "ee"], ["yy", "kk", "ll"], "+"),
                       (["aa", "bb"], ["zz", "mm", "oo"], "+"),
                       (["aa", "ff", "", "dd"], ["xx"], "+"),
                       (["aa", "bb", "", "dd"], ["kk", "ll"], "-"),
                       (["aa", "bb", "", "ee"], ["yy", "kk", "ll"], "-"),
                       (["aa", "bb"], ["oo"], "-"),
                       (["aa", "gg", "", "dd"], ["xx"], "-")]:
                 self.done = False
                 self.get_ans = None
                 index = copy.copy(i[0])
                 index[0] = "test2" + index[0] + str(y)
                 # root_prefix_level is default 2 hence 2 first are combined
                 if i[2] == "+":
                     self.nodes[stype][y].storage.add_index(index,
                                                            i[1],
                                                            cb=CalvinCB(
                                                                self.cb2))
                 else:
                     self.nodes[stype][y].storage.remove_index(
                         index, i[1], cb=CalvinCB(self.cb2))
                 yield wait_for(self._test_done, timeout=10)
                 # Verify response is CalvinResponse object with OK status
                 print "add" if i[
                     2] == "+" else "remove", " response", self.get_ans, stype, index, y
                 assert isinstance(self.get_ans,
                                   calvinresponse.CalvinResponse
                                   ) and self.get_ans == calvinresponse.OK
             for x in range(3):
                 if stype == "notstarted" and x != y:
                     # Not started storage is never connected to other nodes storage
                     continue
                 for i in [(["aa"], []), (["gg"], []), (["aa", "gg"], []),
                           (["aa", "bb"], ["xx", "zz", "mm"]),
                           (["aa", "bb", ""], ["xx"]), (["aa",
                                                         "ff"], ["xx"]),
                           (["aa", "bb", "", "dd"], ["xx"]),
                           (["aa", "bb", "", "ee"], []),
                           (["aa", "ff", "", "dd"], ["xx"])]:
                     self.done = False
                     self.get_ans = None
                     index = copy.copy(i[0])
                     index[0] = "test2" + index[0] + str(y)
                     self.nodes[stype][x].storage.get_index(index,
                                                            cb=CalvinCB(
                                                                self.cb2))
                     yield wait_for(self._test_done, timeout=10)
                     print "get_index response", self.get_ans, stype, index, x
                     # Verify we read what is written if too short prefix or not existing we should get [].
                     assert set(self.get_ans) == set(i[1])
Пример #27
0
def test_calvin_cb_group():
    func1 = Mock()
    func2 = Mock()
    cb1 = CalvinCB(func1)
    cb2 = CalvinCB(func2)
    cbs = CalvinCBGroup([cb1, cb2])
    cbs(1, 2, a=3)

    func1.assert_called_with(1, 2, a=3)
    func2.assert_called_with(1, 2, a=3)
Пример #28
0
 def tunnel_request_handles(self, tunnel):
     """ Incoming tunnel request for storage proxy server"""
     # TODO check if we want a tunnel first
     _log.analyze(self.node.id, "+ SERVER", {'tunnel_id': tunnel.id})
     self.tunnel[tunnel.peer_node_id] = tunnel
     tunnel.register_tunnel_down(CalvinCB(self.tunnel_down, tunnel))
     tunnel.register_tunnel_up(CalvinCB(self.tunnel_up, tunnel))
     tunnel.register_recv(CalvinCB(self.tunnel_recv_handler, tunnel))
     # We accept it by returning True
     return True
Пример #29
0
 def peersetup(self, peers, cb=None):
     """ Sets up a RT to RT communication channel, only needed if the peer can't be found in storage.
         peers: a list of peer uris, e.g. ["calvinip://127.0.0.1:5001"]
     """
     _log.debug("peersetup(%s)" % (peers))
     peers_copy = peers[:]
     peer_node_ids = {}
     self.network.join(peers,
         callback=CalvinCB(self.logging_callback, preamble="peersetup cb") if cb is None else
                  CalvinCB(self.peersetup_collect_cb, peers=peers_copy, peer_node_ids=peer_node_ids, org_cb=cb))
Пример #30
0
 def _remove_cb(self, result, **kwargs):
     _log.debug("SQL remove OK")
     cb = kwargs.pop('cb', None)
     key = kwargs.pop('key', None)
     _log.debug("SQL remove OK %s" % str(result))
     if cb is not None:
         if key is None:
             async.DelayedCall(0, CalvinCB(cb, calvinresponse.CalvinResponse(status=True)))
         else:
             async.DelayedCall(0, CalvinCB(cb, key, calvinresponse.CalvinResponse(status=True)))
Пример #31
0
def test_calvin_cb_class():
    func = Mock()
    cb = CalvinCB(func)

    func2 = Mock()
    cb2 = CalvinCB(func2)

    cb = CalvinCBClass(callbacks={"f": [cb]})
    assert "f" in cb.callback_valid_names()

    cb.callback_register("f2", cb2)
    assert "f2" in cb.callback_valid_names()

    cb.callback_unregister(cb2.id)
    assert "f2" not in cb.callback_valid_names()

    cb._callback_execute("f", 1, 2, a=3)
    func.assert_called_with(1, 2, a=3)
Пример #32
0
def test_kwargs_update():
    func = Mock()
    cb = CalvinCB(func, 1, b=2)
    cb.kwargs_update(c=3)
    cb()
    func.assert_called_with(1, b=2, c=3)
Пример #33
0
def test_args_update():
    func = Mock()
    cb = CalvinCB(func, 1, 2)
    cb.args_append(3)
    cb()
    func.assert_called_with(1, 2, 3)
def test_calvin_cb_class():
    func = Mock()
    cb = CalvinCB(func)

    func2 = Mock()
    cb2 = CalvinCB(func2)

    cb = CalvinCBClass(callbacks={'f': [cb]})
    assert 'f' in cb.callback_valid_names()

    cb.callback_register('f2', cb2)
    assert 'f2' in cb.callback_valid_names()

    cb.callback_unregister(cb2._id)
    assert 'f2' not in cb.callback_valid_names()

    cb._callback_execute('f', 1, 2, a=3)
    func.assert_called_with(1, 2, a=3)