예제 #1
0
class StorageInitializationHandlerTests(NeoUnitTestBase):

    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.verification = InitializationHandler(self.app)
        # define some variable to simulate client and storage node
        self.master_port = 10010
        self.storage_port = 10020
        self.client_port = 11011
        self.num_partitions = 1009
        self.num_replicas = 2
        self.app.operational = False
        self.app.load_lock_dict = {}
        self.app.pt = PartitionTable(self.num_partitions, self.num_replicas)

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageInitializationHandlerTests, self)._tearDown(success)

    def getClientConnection(self):
        address = ("127.0.0.1", self.client_port)
        return self.getFakeConnection(uuid=self.getClientUUID(),
                                      address=address)

    def test_03_connectionClosed(self):
        conn = self.getClientConnection()
        self.app.listening_conn = object() # mark as running
        self.assertRaises(PrimaryFailure, self.verification.connectionClosed, conn,)
        # nothing happens
        self.checkNoPacketSent(conn)

    def test_09_answerPartitionTable(self):
        # send a table
        conn = self.getClientConnection()
        self.app.pt = PartitionTable(3, 2)
        node_1 = self.getStorageUUID()
        node_2 = self.getStorageUUID()
        node_3 = self.getStorageUUID()
        self.app.uuid = node_1
        # SN already know all nodes
        self.app.nm.createStorage(uuid=node_1)
        self.app.nm.createStorage(uuid=node_2)
        self.app.nm.createStorage(uuid=node_3)
        self.assertFalse(list(self.app.dm.getPartitionTable()))
        row_list = [(0, ((node_1, CellStates.UP_TO_DATE), (node_2, CellStates.UP_TO_DATE))),
                    (1, ((node_3, CellStates.UP_TO_DATE), (node_1, CellStates.UP_TO_DATE))),
                    (2, ((node_2, CellStates.UP_TO_DATE), (node_3, CellStates.UP_TO_DATE)))]
        self.assertFalse(self.app.pt.filled())
        # send a complete new table and ack
        self.verification.sendPartitionTable(conn, 2, row_list)
        self.assertTrue(self.app.pt.filled())
        self.assertEqual(self.app.pt.getID(), 2)
        self.assertTrue(list(self.app.dm.getPartitionTable()))
예제 #2
0
class BootstrapManagerTests(NeoUnitTestBase):

    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration()
        self.app = Application(config)
        self.bootstrap = BootstrapManager(self.app, 'main', NodeTypes.STORAGE)
        # define some variable to simulate client and storage node
        self.master_port = 10010
        self.storage_port = 10020
        self.num_partitions = 1009
        self.num_replicas = 2

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(BootstrapManagerTests, self)._tearDown(success)

    # Tests
    def testConnectionCompleted(self):
        address = ("127.0.0.1", self.master_port)
        conn = self.getFakeConnection(address=address)
        self.bootstrap.current = self.app.nm.createMaster(address=address)
        self.bootstrap.connectionCompleted(conn)
        self.checkRequestIdentification(conn)

    def testHandleNotReady(self):
        # the primary is not ready
        address = ("127.0.0.1", self.master_port)
        conn = self.getFakeConnection(address=address)
        self.bootstrap.current = self.app.nm.createMaster(address=address)
        self.bootstrap.notReady(conn, '')
        self.checkClosed(conn)
        self.checkNoPacketSent(conn)
예제 #3
0
class StorageClientHandlerTests(NeoUnitTestBase):
    def checkHandleUnexpectedPacket(self,
                                    _call,
                                    _msg_type,
                                    _listening=True,
                                    **kwargs):
        conn = self.getFakeConnection(address=("127.0.0.1", self.master_port),
                                      is_server=_listening)
        # hook
        self.operation.peerBroken = lambda c: c.peerBrokendCalled()
        self.checkUnexpectedPacketRaised(_call, conn=conn, **kwargs)

    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.app.transaction_dict = {}
        self.app.store_lock_dict = {}
        self.app.load_lock_dict = {}
        self.app.event_queue = deque()
        self.app.event_queue_dict = {}
        self.app.tm = Mock({'__contains__': True})
        # handler
        self.operation = ClientOperationHandler(self.app)
        # set pmn
        self.master_uuid = self.getMasterUUID()
        pmn = self.app.nm.getMasterList()[0]
        pmn.setUUID(self.master_uuid)
        self.app.primary_master_node = pmn
        self.master_port = 10010

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageClientHandlerTests, self)._tearDown(success)

    def _getConnection(self, uuid=None):
        return self.getFakeConnection(uuid=uuid, address=('127.0.0.1', 1000))

    def _checkTransactionsAborted(self, uuid):
        calls = self.app.tm.mockGetNamedCalls('abortFor')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(uuid)

    def test_connectionLost(self):
        uuid = self.getClientUUID()
        self.app.nm.createClient(uuid=uuid)
        conn = self._getConnection(uuid=uuid)
        self.operation.connectionClosed(conn)

    def test_18_askTransactionInformation1(self):
        # transaction does not exists
        conn = self._getConnection()
        self.app.dm = Mock({'getNumPartitions': 1})
        self.operation.askTransactionInformation(conn, INVALID_TID)
        self.checkErrorPacket(conn)

    def test_18_askTransactionInformation2(self):
        # answer
        conn = self._getConnection()
        oid_list = [self.getOID(1), self.getOID(2)]
        dm = Mock({
            "getTransaction": (oid_list, 'user', 'desc', '', False),
        })
        self.app.dm = dm
        self.operation.askTransactionInformation(conn, INVALID_TID)
        self.checkAnswerTransactionInformation(conn)

    def test_24_askObject1(self):
        # delayed response
        conn = self._getConnection()
        self.app.dm = Mock()
        self.app.tm = Mock({'loadLocked': True})
        self.app.load_lock_dict[INVALID_OID] = object()
        self.assertEqual(len(self.app.event_queue), 0)
        self.operation.askObject(conn,
                                 oid=INVALID_OID,
                                 serial=INVALID_TID,
                                 tid=INVALID_TID)
        self.assertEqual(len(self.app.event_queue), 1)
        self.checkNoPacketSent(conn)
        self.assertEqual(len(self.app.dm.mockGetNamedCalls('getObject')), 0)

    def test_24_askObject2(self):
        # invalid serial / tid / packet not found
        self.app.dm = Mock({'getObject': None})
        conn = self._getConnection()
        self.assertEqual(len(self.app.event_queue), 0)
        self.operation.askObject(conn,
                                 oid=INVALID_OID,
                                 serial=INVALID_TID,
                                 tid=INVALID_TID)
        calls = self.app.dm.mockGetNamedCalls('getObject')
        self.assertEqual(len(self.app.event_queue), 0)
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(INVALID_OID, INVALID_TID, INVALID_TID)
        self.checkErrorPacket(conn)

    def test_24_askObject3(self):
        # object found => answer
        serial = self.getNextTID()
        next_serial = self.getNextTID()
        oid = self.getOID(1)
        tid = self.getNextTID()
        H = "0" * 20
        self.app.dm = Mock(
            {'getObject': (serial, next_serial, 0, H, '', None)})
        conn = self._getConnection()
        self.assertEqual(len(self.app.event_queue), 0)
        self.operation.askObject(conn, oid=oid, serial=serial, tid=tid)
        self.assertEqual(len(self.app.event_queue), 0)
        self.checkAnswerObject(conn)

    def test_25_askTIDs1(self):
        # invalid offsets => error
        app = self.app
        app.pt = Mock()
        app.dm = Mock()
        conn = self._getConnection()
        self.checkProtocolErrorRaised(self.operation.askTIDs, conn, 1, 1, None)
        self.assertEqual(len(app.pt.mockGetNamedCalls('getCellList')), 0)
        self.assertEqual(len(app.dm.mockGetNamedCalls('getTIDList')), 0)

    def test_25_askTIDs2(self):
        # well case => answer
        conn = self._getConnection()
        self.app.pt = Mock({'getPartitions': 1})
        self.app.dm = Mock({'getTIDList': (INVALID_TID, )})
        self.operation.askTIDs(conn, 1, 2, 1)
        calls = self.app.dm.mockGetNamedCalls('getTIDList')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(1, 1, [
            1,
        ])
        self.checkAnswerTids(conn)

    def test_25_askTIDs3(self):
        # invalid partition => answer usable partitions
        conn = self._getConnection()
        cell = Mock({'getUUID': self.app.uuid})
        self.app.dm = Mock({'getTIDList': (INVALID_TID, )})
        self.app.pt = Mock({
            'getCellList': (cell, ),
            'getPartitions': 1,
            'getAssignedPartitionList': [0],
        })
        self.operation.askTIDs(conn, 1, 2, INVALID_PARTITION)
        self.assertEqual(
            len(self.app.pt.mockGetNamedCalls('getAssignedPartitionList')), 1)
        calls = self.app.dm.mockGetNamedCalls('getTIDList')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(1, 1, [0])
        self.checkAnswerTids(conn)

    def test_26_askObjectHistory1(self):
        # invalid offsets => error
        app = self.app
        app.dm = Mock()
        conn = self._getConnection()
        self.checkProtocolErrorRaised(self.operation.askObjectHistory, conn, 1,
                                      1, None)
        self.assertEqual(len(app.dm.mockGetNamedCalls('getObjectHistory')), 0)

    def test_26_askObjectHistory2(self):
        oid1, oid2 = self.getOID(1), self.getOID(2)
        # first case: empty history
        conn = self._getConnection()
        self.app.dm = Mock({'getObjectHistory': None})
        self.operation.askObjectHistory(conn, oid1, 1, 2)
        self.checkErrorPacket(conn)
        # second case: not empty history
        conn = self._getConnection()
        serial = self.getNextTID()
        self.app.dm = Mock({'getObjectHistory': [
            (
                serial,
                0,
            ),
        ]})
        self.operation.askObjectHistory(conn, oid2, 1, 2)
        self.checkAnswerObjectHistory(conn)

    def _getObject(self):
        oid = self.getOID(0)
        serial = self.getNextTID()
        data = 'DATA'
        return (oid, serial, 1, makeChecksum(data), data)

    def _checkStoreObjectCalled(self, *args):
        calls = self.app.tm.mockGetNamedCalls('storeObject')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(*args)

    def test_askStoreObject1(self):
        # no conflict => answer
        conn = self._getConnection(uuid=self.getClientUUID())
        tid = self.getNextTID()
        oid, serial, comp, checksum, data = self._getObject()
        self.operation.askStoreObject(conn, oid, serial, comp, checksum, data,
                                      None, tid, False)
        self._checkStoreObjectCalled(tid, serial, oid, comp, checksum, data,
                                     None, False)
        pconflicting, poid, pserial = self.checkAnswerStoreObject(conn,
                                                                  decode=True)
        self.assertEqual(pconflicting, 0)
        self.assertEqual(poid, oid)
        self.assertEqual(pserial, serial)

    def test_askStoreObjectWithDataTID(self):
        # same as test_askStoreObject1, but with a non-None data_tid value
        conn = self._getConnection(uuid=self.getClientUUID())
        tid = self.getNextTID()
        oid, serial, comp, checksum, data = self._getObject()
        data_tid = self.getNextTID()
        self.operation.askStoreObject(conn, oid, serial, comp, ZERO_HASH, '',
                                      data_tid, tid, False)
        self._checkStoreObjectCalled(tid, serial, oid, comp, None, None,
                                     data_tid, False)
        pconflicting, poid, pserial = self.checkAnswerStoreObject(conn,
                                                                  decode=True)
        self.assertEqual(pconflicting, 0)
        self.assertEqual(poid, oid)
        self.assertEqual(pserial, serial)

    def test_askStoreObject2(self):
        # conflict error
        conn = self._getConnection(uuid=self.getClientUUID())
        tid = self.getNextTID()
        locking_tid = self.getNextTID(tid)

        def fakeStoreObject(*args):
            raise ConflictError(locking_tid)

        self.app.tm.storeObject = fakeStoreObject
        oid, serial, comp, checksum, data = self._getObject()
        self.operation.askStoreObject(conn, oid, serial, comp, checksum, data,
                                      None, tid, False)
        pconflicting, poid, pserial = self.checkAnswerStoreObject(conn,
                                                                  decode=True)
        self.assertEqual(pconflicting, 1)
        self.assertEqual(poid, oid)
        self.assertEqual(pserial, locking_tid)

    def test_abortTransaction(self):
        conn = self._getConnection()
        tid = self.getNextTID()
        self.operation.abortTransaction(conn, tid)
        calls = self.app.tm.mockGetNamedCalls('abort')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(tid)

    def test_askObjectUndoSerial(self):
        conn = self._getConnection(uuid=self.getClientUUID())
        tid = self.getNextTID()
        ltid = self.getNextTID()
        undone_tid = self.getNextTID()
        # Keep 2 entries here, so we check findUndoTID is called only once.
        oid_list = [self.getOID(1), self.getOID(2)]
        obj2_data = []  # Marker
        self.app.tm = Mock({
            'getObjectFromTransaction': None,
        })
        self.app.dm = Mock(
            {'findUndoTID': ReturnValues((None, None, False), )})
        self.operation.askObjectUndoSerial(conn, tid, ltid, undone_tid,
                                           oid_list)
        self.checkErrorPacket(conn)

    def test_askHasLock(self):
        tid_1 = self.getNextTID()
        tid_2 = self.getNextTID()
        oid = self.getNextTID()

        def getLockingTID(oid):
            return locking_tid

        self.app.tm.getLockingTID = getLockingTID
        for locking_tid, status in (
            (None, LockState.NOT_LOCKED),
            (tid_1, LockState.GRANTED),
            (tid_2, LockState.GRANTED_TO_OTHER),
        ):
            conn = self._getConnection()
            self.operation.askHasLock(conn, tid_1, oid)
            p_oid, p_status = self.checkAnswerPacket(conn,
                                                     Packets.AnswerHasLock,
                                                     decode=True)
            self.assertEqual(oid, p_oid)
            self.assertEqual(status, p_status)
예제 #4
0
class StorageClientHandlerTests(NeoUnitTestBase):
    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.app.tm = Mock({'__contains__': True})
        # handler
        self.operation = ClientOperationHandler(self.app)
        # set pmn
        self.master_uuid = self.getMasterUUID()
        pmn = self.app.nm.getMasterList()[0]
        pmn.setUUID(self.master_uuid)
        self.app.primary_master_node = pmn

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageClientHandlerTests, self)._tearDown(success)

    def _getConnection(self, uuid=None):
        return self.getFakeConnection(uuid=uuid, address=('127.0.0.1', 1000))

    def test_18_askTransactionInformation1(self):
        # transaction does not exists
        conn = self._getConnection()
        self.app.dm = Mock({'getNumPartitions': 1})
        self.operation.askTransactionInformation(conn, INVALID_TID)
        self.checkErrorPacket(conn)

    def test_25_askTIDs1(self):
        # invalid offsets => error
        app = self.app
        app.pt = Mock()
        app.dm = Mock()
        conn = self._getConnection()
        self.checkProtocolErrorRaised(self.operation.askTIDs, conn, 1, 1, None)
        self.assertEqual(len(app.pt.mockGetNamedCalls('getCellList')), 0)
        self.assertEqual(len(app.dm.mockGetNamedCalls('getTIDList')), 0)

    def test_25_askTIDs2(self):
        # well case => answer
        conn = self._getConnection()
        self.app.pt = Mock({'getPartitions': 1})
        self.app.dm = Mock({'getTIDList': (INVALID_TID, )})
        self.operation.askTIDs(conn, 1, 2, 1)
        calls = self.app.dm.mockGetNamedCalls('getTIDList')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(1, 1, [
            1,
        ])
        self.checkAnswerPacket(conn, Packets.AnswerTIDs)

    def test_26_askObjectHistory1(self):
        # invalid offsets => error
        app = self.app
        app.dm = Mock()
        conn = self._getConnection()
        self.checkProtocolErrorRaised(self.operation.askObjectHistory, conn, 1,
                                      1, None)
        self.assertEqual(len(app.dm.mockGetNamedCalls('getObjectHistory')), 0)

    def test_askObjectUndoSerial(self):
        conn = self._getConnection(uuid=self.getClientUUID())
        tid = self.getNextTID()
        ltid = self.getNextTID()
        undone_tid = self.getNextTID()
        # Keep 2 entries here, so we check findUndoTID is called only once.
        oid_list = map(p64, (1, 2))
        self.app.tm = Mock({
            'getObjectFromTransaction': None,
        })
        self.app.dm = Mock(
            {'findUndoTID': ReturnValues((None, None, False), )})
        self.operation.askObjectUndoSerial(conn, tid, ltid, undone_tid,
                                           oid_list)
        self.checkErrorPacket(conn)
예제 #5
0
class StorageMasterHandlerTests(NeoUnitTestBase):

    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.app.transaction_dict = {}
        self.app.store_lock_dict = {}
        self.app.load_lock_dict = {}
        self.app.event_queue = deque()
        # handler
        self.operation = MasterOperationHandler(self.app)
        # set pmn
        self.master_uuid = self.getMasterUUID()
        pmn = self.app.nm.getMasterList()[0]
        pmn.setUUID(self.master_uuid)
        self.app.primary_master_node = pmn
        self.master_port = 10010

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageMasterHandlerTests, self)._tearDown(success)

    def getMasterConnection(self):
        address = ("127.0.0.1", self.master_port)
        return self.getFakeConnection(uuid=self.master_uuid, address=address)

    def test_07_connectionClosed2(self):
        # primary has closed the connection
        conn = self.getMasterConnection()
        self.app.listening_conn = object() # mark as running
        self.assertRaises(PrimaryFailure, self.operation.connectionClosed, conn)
        self.checkNoPacketSent(conn)

    def test_14_notifyPartitionChanges1(self):
        # old partition change -> do nothing
        app = self.app
        conn = self.getMasterConnection()
        app.replicator = Mock({})
        self.app.pt = Mock({'getID': 1})
        count = len(self.app.nm.getList())
        self.operation.notifyPartitionChanges(conn, 0, ())
        self.assertEqual(self.app.pt.getID(), 1)
        self.assertEqual(len(self.app.nm.getList()), count)
        calls = self.app.replicator.mockGetNamedCalls('removePartition')
        self.assertEqual(len(calls), 0)
        calls = self.app.replicator.mockGetNamedCalls('addPartition')
        self.assertEqual(len(calls), 0)

    def test_14_notifyPartitionChanges2(self):
        # cases :
        uuid1, uuid2, uuid3 = [self.getStorageUUID() for i in range(3)]
        cells = (
            (0, uuid1, CellStates.UP_TO_DATE),
            (1, uuid2, CellStates.DISCARDED),
            (2, uuid3, CellStates.OUT_OF_DATE),
        )
        # context
        conn = self.getMasterConnection()
        app = self.app
        # register nodes
        app.nm.createStorage(uuid=uuid1)
        app.nm.createStorage(uuid=uuid2)
        app.nm.createStorage(uuid=uuid3)
        ptid1, ptid2 = (1, 2)
        self.assertNotEqual(ptid1, ptid2)
        app.pt = PartitionTable(3, 1)
        app.dm = Mock({ })
        app.replicator = Mock({})
        self.operation.notifyPartitionChanges(conn, ptid2, cells)
        # ptid set
        self.assertEqual(app.pt.getID(), ptid2)
        # dm call
        calls = self.app.dm.mockGetNamedCalls('changePartitionTable')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(ptid2, cells)

    def _getConnection(self):
        return self.getFakeConnection()

    def test_askPack(self):
        self.app.dm = Mock({'pack': None})
        conn = self.getFakeConnection()
        tid = self.getNextTID()
        self.operation.askPack(conn, tid)
        calls = self.app.dm.mockGetNamedCalls('pack')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(tid, self.app.tm.updateObjectDataForPack)
        # Content has no meaning here, don't check.
        self.checkAnswerPacket(conn, Packets.AnswerPack)
class StorageInitializationHandlerTests(NeoUnitTestBase):
    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.verification = InitializationHandler(self.app)
        # define some variable to simulate client and storage node
        self.master_port = 10010
        self.storage_port = 10020
        self.client_port = 11011
        self.num_partitions = 1009
        self.num_replicas = 2
        self.app.operational = False
        self.app.load_lock_dict = {}
        self.app.pt = PartitionTable(self.num_partitions, self.num_replicas)

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageInitializationHandlerTests, self)._tearDown(success)

    def getClientConnection(self):
        address = ("127.0.0.1", self.client_port)
        return self.getFakeConnection(uuid=self.getClientUUID(),
                                      address=address)

    def test_03_connectionClosed(self):
        conn = self.getClientConnection()
        self.app.listening_conn = object()  # mark as running
        self.assertRaises(
            PrimaryFailure,
            self.verification.connectionClosed,
            conn,
        )
        # nothing happens
        self.checkNoPacketSent(conn)

    def test_09_answerPartitionTable(self):
        # send a table
        conn = self.getClientConnection()
        self.app.pt = PartitionTable(3, 2)
        node_1 = self.getStorageUUID()
        node_2 = self.getStorageUUID()
        node_3 = self.getStorageUUID()
        self.app.uuid = node_1
        # SN already know all nodes
        self.app.nm.createStorage(uuid=node_1)
        self.app.nm.createStorage(uuid=node_2)
        self.app.nm.createStorage(uuid=node_3)
        self.assertFalse(list(self.app.dm.getPartitionTable()))
        row_list = [(0, ((node_1, CellStates.UP_TO_DATE),
                         (node_2, CellStates.UP_TO_DATE))),
                    (1, ((node_3, CellStates.UP_TO_DATE),
                         (node_1, CellStates.UP_TO_DATE))),
                    (2, ((node_2, CellStates.UP_TO_DATE),
                         (node_3, CellStates.UP_TO_DATE)))]
        self.assertFalse(self.app.pt.filled())
        # send a complete new table and ack
        self.verification.sendPartitionTable(conn, 2, row_list)
        self.assertTrue(self.app.pt.filled())
        self.assertEqual(self.app.pt.getID(), 2)
        self.assertTrue(list(self.app.dm.getPartitionTable()))
예제 #7
0
class StorageAppTests(NeoUnitTestBase):

    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.app.event_queue = deque()
        self.app.event_queue_dict = {}

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageAppTests, self)._tearDown(success)

    def test_01_loadPartitionTable(self):
        self.app.dm = Mock({
            'getPartitionTable': [],
        })
        self.assertEqual(self.app.pt, None)
        num_partitions = 3
        num_replicas = 2
        self.app.pt = PartitionTable(num_partitions, num_replicas)
        self.assertFalse(self.app.pt.getNodeSet())
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertFalse(self.app.pt.hasOffset(x))

        # load an empty table
        self.app.loadPartitionTable()
        self.assertFalse(self.app.pt.getNodeSet())
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertFalse(self.app.pt.hasOffset(x))

        # add some node, will be remove when loading table
        master_uuid = self.getMasterUUID()
        master = self.app.nm.createMaster(uuid=master_uuid)
        storage_uuid = self.getStorageUUID()
        storage = self.app.nm.createStorage(uuid=storage_uuid)
        client_uuid = self.getClientUUID()

        self.app.pt.setCell(0, master, CellStates.UP_TO_DATE)
        self.app.pt.setCell(0, storage, CellStates.UP_TO_DATE)
        self.assertEqual(len(self.app.pt.getNodeSet()), 2)
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            if x == 0:
                self.assertTrue(self.app.pt.hasOffset(x))
            else:
                self.assertFalse(self.app.pt.hasOffset(x))
        # load an empty table, everything removed
        self.app.loadPartitionTable()
        self.assertFalse(self.app.pt.getNodeSet())
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertFalse(self.app.pt.hasOffset(x))

        # add some node
        self.app.pt.setCell(0, master, CellStates.UP_TO_DATE)
        self.app.pt.setCell(0, storage, CellStates.UP_TO_DATE)
        self.assertEqual(len(self.app.pt.getNodeSet()), 2)
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            if x == 0:
                self.assertTrue(self.app.pt.hasOffset(x))
            else:
                self.assertFalse(self.app.pt.hasOffset(x))
        # fill partition table
        self.app.dm = Mock({
            'getPartitionTable': [
                (0, client_uuid, CellStates.UP_TO_DATE),
                (1, client_uuid, CellStates.UP_TO_DATE),
                (1, storage_uuid, CellStates.UP_TO_DATE),
                (2, storage_uuid, CellStates.UP_TO_DATE),
                (2, master_uuid, CellStates.UP_TO_DATE),
            ],
            'getPTID': 1,
        })
        self.app.pt.clear()
        self.app.loadPartitionTable()
        self.assertTrue(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertTrue(self.app.pt.hasOffset(x))
        # check each row
        cell_list = self.app.pt.getCellList(0)
        self.assertEqual(len(cell_list), 1)
        self.assertEqual(cell_list[0].getUUID(), client_uuid)
        cell_list = self.app.pt.getCellList(1)
        self.assertEqual(len(cell_list), 2)
        self.assertTrue(cell_list[0].getUUID() in (client_uuid, storage_uuid))
        self.assertTrue(cell_list[1].getUUID() in (client_uuid, storage_uuid))
        cell_list = self.app.pt.getCellList(2)
        self.assertEqual(len(cell_list), 2)
        self.assertTrue(cell_list[0].getUUID() in (master_uuid, storage_uuid))
        self.assertTrue(cell_list[1].getUUID() in (master_uuid, storage_uuid))

    def test_02_queueEvent(self):
        self.assertEqual(len(self.app.event_queue), 0)
        msg_id = 1325136
        event = Mock({'__repr__': 'event'})
        conn = Mock({'__repr__': 'conn', 'getPeerId': msg_id})
        key = 'foo'
        self.app.queueEvent(event, conn, ("test", ), key=key)
        self.assertEqual(len(self.app.event_queue), 1)
        _key, _event, _msg_id, _conn, args = self.app.event_queue[0]
        self.assertEqual(key, _key)
        self.assertEqual(msg_id, _msg_id)
        self.assertEqual(len(args), 1)
        self.assertEqual(args[0], "test")
        self.assertRaises(AlreadyPendingError, self.app.queueEvent, event,
            conn, ("test2", ), key=key)
        self.assertEqual(len(self.app.event_queue), 1)
        self.app.queueEvent(event, conn, ("test3", ), key=key,
            raise_on_duplicate=False)
        self.assertEqual(len(self.app.event_queue), 2)

    def test_03_executeQueuedEvents(self):
        self.assertEqual(len(self.app.event_queue), 0)
        msg_id = 1325136
        msg_id_2 = 1325137
        event = Mock({'__repr__': 'event'})
        conn = Mock({'__repr__': 'conn', 'getPeerId': ReturnValues(msg_id, msg_id_2)})
        self.app.queueEvent(event, conn, ("test", ))
        self.app.executeQueuedEvents()
        self.assertEqual(len(event.mockGetNamedCalls("__call__")), 1)
        call = event.mockGetNamedCalls("__call__")[0]
        params = call.getParam(1)
        self.assertEqual(params, "test")
        params = call.kwparams
        self.assertEqual(params, {})
        calls = conn.mockGetNamedCalls("setPeerId")
        self.assertEqual(len(calls), 2)
        calls[0].checkArgs(msg_id)
        calls[1].checkArgs(msg_id_2)
예제 #8
0
class StorageAppTests(NeoUnitTestBase):
    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageAppTests, self)._tearDown(success)

    def test_01_loadPartitionTable(self):
        self.app.dm = Mock({
            'getPartitionTable': [],
        })
        self.assertEqual(self.app.pt, None)
        num_partitions = 3
        num_replicas = 2
        self.app.pt = PartitionTable(num_partitions, num_replicas)
        self.assertFalse(self.app.pt.getNodeSet())
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertFalse(self.app.pt.hasOffset(x))

        # load an empty table
        self.app.loadPartitionTable()
        self.assertFalse(self.app.pt.getNodeSet())
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertFalse(self.app.pt.hasOffset(x))

        # add some node, will be remove when loading table
        master_uuid = self.getMasterUUID()
        master = self.app.nm.createMaster(uuid=master_uuid)
        storage_uuid = self.getStorageUUID()
        storage = self.app.nm.createStorage(uuid=storage_uuid)
        client_uuid = self.getClientUUID()

        self.app.pt._setCell(0, master, CellStates.UP_TO_DATE)
        self.app.pt._setCell(0, storage, CellStates.UP_TO_DATE)
        self.assertEqual(len(self.app.pt.getNodeSet()), 2)
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            if x == 0:
                self.assertTrue(self.app.pt.hasOffset(x))
            else:
                self.assertFalse(self.app.pt.hasOffset(x))
        # load an empty table, everything removed
        self.app.loadPartitionTable()
        self.assertFalse(self.app.pt.getNodeSet())
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertFalse(self.app.pt.hasOffset(x))

        # add some node
        self.app.pt._setCell(0, master, CellStates.UP_TO_DATE)
        self.app.pt._setCell(0, storage, CellStates.UP_TO_DATE)
        self.assertEqual(len(self.app.pt.getNodeSet()), 2)
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            if x == 0:
                self.assertTrue(self.app.pt.hasOffset(x))
            else:
                self.assertFalse(self.app.pt.hasOffset(x))
        # fill partition table
        self.app.dm = Mock({
            'getPartitionTable': [
                (0, client_uuid, CellStates.UP_TO_DATE),
                (1, client_uuid, CellStates.UP_TO_DATE),
                (1, storage_uuid, CellStates.UP_TO_DATE),
                (2, storage_uuid, CellStates.UP_TO_DATE),
                (2, master_uuid, CellStates.UP_TO_DATE),
            ],
            'getPTID':
            1,
        })
        self.app.pt.clear()
        self.app.loadPartitionTable()
        self.assertTrue(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertTrue(self.app.pt.hasOffset(x))
        # check each row
        cell_list = self.app.pt.getCellList(0)
        self.assertEqual(len(cell_list), 1)
        self.assertEqual(cell_list[0].getUUID(), client_uuid)
        cell_list = self.app.pt.getCellList(1)
        self.assertEqual(len(cell_list), 2)
        self.assertTrue(cell_list[0].getUUID() in (client_uuid, storage_uuid))
        self.assertTrue(cell_list[1].getUUID() in (client_uuid, storage_uuid))
        cell_list = self.app.pt.getCellList(2)
        self.assertEqual(len(cell_list), 2)
        self.assertTrue(cell_list[0].getUUID() in (master_uuid, storage_uuid))
        self.assertTrue(cell_list[1].getUUID() in (master_uuid, storage_uuid))
예제 #9
0
class StorageMasterHandlerTests(NeoUnitTestBase):
    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.app.transaction_dict = {}
        self.app.store_lock_dict = {}
        self.app.load_lock_dict = {}
        self.app.event_queue = deque()
        # handler
        self.operation = MasterOperationHandler(self.app)
        # set pmn
        self.master_uuid = self.getMasterUUID()
        pmn = self.app.nm.getMasterList()[0]
        pmn.setUUID(self.master_uuid)
        self.app.primary_master_node = pmn
        self.master_port = 10010

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageMasterHandlerTests, self)._tearDown(success)

    def getMasterConnection(self):
        address = ("127.0.0.1", self.master_port)
        return self.getFakeConnection(uuid=self.master_uuid, address=address)

    def test_07_connectionClosed2(self):
        # primary has closed the connection
        conn = self.getMasterConnection()
        self.app.listening_conn = object()  # mark as running
        self.assertRaises(PrimaryFailure, self.operation.connectionClosed,
                          conn)
        self.checkNoPacketSent(conn)

    def test_14_notifyPartitionChanges1(self):
        # old partition change -> do nothing
        app = self.app
        conn = self.getMasterConnection()
        app.replicator = Mock({})
        self.app.pt = Mock({'getID': 1})
        count = len(self.app.nm.getList())
        self.operation.notifyPartitionChanges(conn, 0, ())
        self.assertEqual(self.app.pt.getID(), 1)
        self.assertEqual(len(self.app.nm.getList()), count)
        calls = self.app.replicator.mockGetNamedCalls('removePartition')
        self.assertEqual(len(calls), 0)
        calls = self.app.replicator.mockGetNamedCalls('addPartition')
        self.assertEqual(len(calls), 0)

    def test_14_notifyPartitionChanges2(self):
        # cases :
        uuid1, uuid2, uuid3 = [self.getStorageUUID() for i in range(3)]
        cells = (
            (0, uuid1, CellStates.UP_TO_DATE),
            (1, uuid2, CellStates.DISCARDED),
            (2, uuid3, CellStates.OUT_OF_DATE),
        )
        # context
        conn = self.getMasterConnection()
        app = self.app
        # register nodes
        app.nm.createStorage(uuid=uuid1)
        app.nm.createStorage(uuid=uuid2)
        app.nm.createStorage(uuid=uuid3)
        ptid1, ptid2 = (1, 2)
        self.assertNotEqual(ptid1, ptid2)
        app.pt = PartitionTable(3, 1)
        app.dm = Mock({})
        app.replicator = Mock({})
        self.operation.notifyPartitionChanges(conn, ptid2, cells)
        # ptid set
        self.assertEqual(app.pt.getID(), ptid2)
        # dm call
        calls = self.app.dm.mockGetNamedCalls('changePartitionTable')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(ptid2, cells)

    def _getConnection(self):
        return self.getFakeConnection()

    def test_askPack(self):
        self.app.dm = Mock({'pack': None})
        conn = self.getFakeConnection()
        tid = self.getNextTID()
        self.operation.askPack(conn, tid)
        calls = self.app.dm.mockGetNamedCalls('pack')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(tid, self.app.tm.updateObjectDataForPack)
        # Content has no meaning here, don't check.
        self.checkAnswerPacket(conn, Packets.AnswerPack)
예제 #10
0
class StorageClientHandlerTests(NeoUnitTestBase):

    def checkHandleUnexpectedPacket(self, _call, _msg_type, _listening=True, **kwargs):
        conn = self.getFakeConnection(address=("127.0.0.1", self.master_port),
                is_server=_listening)
        # hook
        self.operation.peerBroken = lambda c: c.peerBrokendCalled()
        self.checkUnexpectedPacketRaised(_call, conn=conn, **kwargs)

    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.app.transaction_dict = {}
        self.app.store_lock_dict = {}
        self.app.load_lock_dict = {}
        self.app.event_queue = deque()
        self.app.event_queue_dict = {}
        self.app.tm = Mock({'__contains__': True})
        # handler
        self.operation = ClientOperationHandler(self.app)
        # set pmn
        self.master_uuid = self.getMasterUUID()
        pmn = self.app.nm.getMasterList()[0]
        pmn.setUUID(self.master_uuid)
        self.app.primary_master_node = pmn
        self.master_port = 10010

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageClientHandlerTests, self)._tearDown(success)

    def _getConnection(self, uuid=None):
        return self.getFakeConnection(uuid=uuid, address=('127.0.0.1', 1000))

    def _checkTransactionsAborted(self, uuid):
        calls = self.app.tm.mockGetNamedCalls('abortFor')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(uuid)

    def test_connectionLost(self):
        uuid = self.getClientUUID()
        self.app.nm.createClient(uuid=uuid)
        conn = self._getConnection(uuid=uuid)
        self.operation.connectionClosed(conn)

    def test_18_askTransactionInformation1(self):
        # transaction does not exists
        conn = self._getConnection()
        self.app.dm = Mock({'getNumPartitions': 1})
        self.operation.askTransactionInformation(conn, INVALID_TID)
        self.checkErrorPacket(conn)

    def test_18_askTransactionInformation2(self):
        # answer
        conn = self._getConnection()
        oid_list = [self.getOID(1), self.getOID(2)]
        dm = Mock({ "getTransaction": (oid_list, 'user', 'desc', '', False), })
        self.app.dm = dm
        self.operation.askTransactionInformation(conn, INVALID_TID)
        self.checkAnswerTransactionInformation(conn)

    def test_24_askObject1(self):
        # delayed response
        conn = self._getConnection()
        self.app.dm = Mock()
        self.app.tm = Mock({'loadLocked': True})
        self.app.load_lock_dict[INVALID_OID] = object()
        self.assertEqual(len(self.app.event_queue), 0)
        self.operation.askObject(conn, oid=INVALID_OID,
            serial=INVALID_TID, tid=INVALID_TID)
        self.assertEqual(len(self.app.event_queue), 1)
        self.checkNoPacketSent(conn)
        self.assertEqual(len(self.app.dm.mockGetNamedCalls('getObject')), 0)

    def test_24_askObject2(self):
        # invalid serial / tid / packet not found
        self.app.dm = Mock({'getObject': None})
        conn = self._getConnection()
        self.assertEqual(len(self.app.event_queue), 0)
        self.operation.askObject(conn, oid=INVALID_OID,
            serial=INVALID_TID, tid=INVALID_TID)
        calls = self.app.dm.mockGetNamedCalls('getObject')
        self.assertEqual(len(self.app.event_queue), 0)
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(INVALID_OID, INVALID_TID, INVALID_TID)
        self.checkErrorPacket(conn)

    def test_24_askObject3(self):
        # object found => answer
        serial = self.getNextTID()
        next_serial = self.getNextTID()
        oid = self.getOID(1)
        tid = self.getNextTID()
        H = "0" * 20
        self.app.dm = Mock({'getObject': (serial, next_serial, 0, H, '', None)})
        conn = self._getConnection()
        self.assertEqual(len(self.app.event_queue), 0)
        self.operation.askObject(conn, oid=oid, serial=serial, tid=tid)
        self.assertEqual(len(self.app.event_queue), 0)
        self.checkAnswerObject(conn)

    def test_25_askTIDs1(self):
        # invalid offsets => error
        app = self.app
        app.pt = Mock()
        app.dm = Mock()
        conn = self._getConnection()
        self.checkProtocolErrorRaised(self.operation.askTIDs, conn, 1, 1, None)
        self.assertEqual(len(app.pt.mockGetNamedCalls('getCellList')), 0)
        self.assertEqual(len(app.dm.mockGetNamedCalls('getTIDList')), 0)

    def test_25_askTIDs2(self):
        # well case => answer
        conn = self._getConnection()
        self.app.pt = Mock({'getPartitions': 1})
        self.app.dm = Mock({'getTIDList': (INVALID_TID, )})
        self.operation.askTIDs(conn, 1, 2, 1)
        calls = self.app.dm.mockGetNamedCalls('getTIDList')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(1, 1, [1, ])
        self.checkAnswerTids(conn)

    def test_25_askTIDs3(self):
        # invalid partition => answer usable partitions
        conn = self._getConnection()
        cell = Mock({'getUUID':self.app.uuid})
        self.app.dm = Mock({'getTIDList': (INVALID_TID, )})
        self.app.pt = Mock({
            'getCellList': (cell, ),
            'getPartitions': 1,
            'getAssignedPartitionList': [0],
        })
        self.operation.askTIDs(conn, 1, 2, INVALID_PARTITION)
        self.assertEqual(len(self.app.pt.mockGetNamedCalls('getAssignedPartitionList')), 1)
        calls = self.app.dm.mockGetNamedCalls('getTIDList')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(1, 1, [0])
        self.checkAnswerTids(conn)

    def test_26_askObjectHistory1(self):
        # invalid offsets => error
        app = self.app
        app.dm = Mock()
        conn = self._getConnection()
        self.checkProtocolErrorRaised(self.operation.askObjectHistory, conn,
            1, 1, None)
        self.assertEqual(len(app.dm.mockGetNamedCalls('getObjectHistory')), 0)

    def test_26_askObjectHistory2(self):
        oid1, oid2 = self.getOID(1), self.getOID(2)
        # first case: empty history
        conn = self._getConnection()
        self.app.dm = Mock({'getObjectHistory': None})
        self.operation.askObjectHistory(conn, oid1, 1, 2)
        self.checkErrorPacket(conn)
        # second case: not empty history
        conn = self._getConnection()
        serial = self.getNextTID()
        self.app.dm = Mock({'getObjectHistory': [(serial, 0, ), ]})
        self.operation.askObjectHistory(conn, oid2, 1, 2)
        self.checkAnswerObjectHistory(conn)

    def _getObject(self):
        oid = self.getOID(0)
        serial = self.getNextTID()
        data = 'DATA'
        return (oid, serial, 1, makeChecksum(data), data)

    def _checkStoreObjectCalled(self, *args):
        calls = self.app.tm.mockGetNamedCalls('storeObject')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(*args)

    def test_askStoreObject1(self):
        # no conflict => answer
        conn = self._getConnection(uuid=self.getClientUUID())
        tid = self.getNextTID()
        oid, serial, comp, checksum, data = self._getObject()
        self.operation.askStoreObject(conn, oid, serial, comp, checksum,
                data, None, tid, False)
        self._checkStoreObjectCalled(tid, serial, oid, comp,
                checksum, data, None, False)
        pconflicting, poid, pserial = self.checkAnswerStoreObject(conn,
            decode=True)
        self.assertEqual(pconflicting, 0)
        self.assertEqual(poid, oid)
        self.assertEqual(pserial, serial)

    def test_askStoreObjectWithDataTID(self):
        # same as test_askStoreObject1, but with a non-None data_tid value
        conn = self._getConnection(uuid=self.getClientUUID())
        tid = self.getNextTID()
        oid, serial, comp, checksum, data = self._getObject()
        data_tid = self.getNextTID()
        self.operation.askStoreObject(conn, oid, serial, comp, ZERO_HASH,
                '', data_tid, tid, False)
        self._checkStoreObjectCalled(tid, serial, oid, comp,
                None, None, data_tid, False)
        pconflicting, poid, pserial = self.checkAnswerStoreObject(conn,
            decode=True)
        self.assertEqual(pconflicting, 0)
        self.assertEqual(poid, oid)
        self.assertEqual(pserial, serial)

    def test_askStoreObject2(self):
        # conflict error
        conn = self._getConnection(uuid=self.getClientUUID())
        tid = self.getNextTID()
        locking_tid = self.getNextTID(tid)
        def fakeStoreObject(*args):
            raise ConflictError(locking_tid)
        self.app.tm.storeObject = fakeStoreObject
        oid, serial, comp, checksum, data = self._getObject()
        self.operation.askStoreObject(conn, oid, serial, comp, checksum,
                data, None, tid, False)
        pconflicting, poid, pserial = self.checkAnswerStoreObject(conn,
            decode=True)
        self.assertEqual(pconflicting, 1)
        self.assertEqual(poid, oid)
        self.assertEqual(pserial, locking_tid)

    def test_abortTransaction(self):
        conn = self._getConnection()
        tid = self.getNextTID()
        self.operation.abortTransaction(conn, tid)
        calls = self.app.tm.mockGetNamedCalls('abort')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(tid)

    def test_askObjectUndoSerial(self):
        conn = self._getConnection(uuid=self.getClientUUID())
        tid = self.getNextTID()
        ltid = self.getNextTID()
        undone_tid = self.getNextTID()
        # Keep 2 entries here, so we check findUndoTID is called only once.
        oid_list = [self.getOID(1), self.getOID(2)]
        obj2_data = [] # Marker
        self.app.tm = Mock({
            'getObjectFromTransaction': None,
        })
        self.app.dm = Mock({
            'findUndoTID': ReturnValues((None, None, False), )
        })
        self.operation.askObjectUndoSerial(conn, tid, ltid, undone_tid, oid_list)
        self.checkErrorPacket(conn)

    def test_askHasLock(self):
        tid_1 = self.getNextTID()
        tid_2 = self.getNextTID()
        oid = self.getNextTID()
        def getLockingTID(oid):
            return locking_tid
        self.app.tm.getLockingTID = getLockingTID
        for locking_tid, status in (
                    (None, LockState.NOT_LOCKED),
                    (tid_1, LockState.GRANTED),
                    (tid_2, LockState.GRANTED_TO_OTHER),
                ):
            conn = self._getConnection()
            self.operation.askHasLock(conn, tid_1, oid)
            p_oid, p_status = self.checkAnswerPacket(conn,
                Packets.AnswerHasLock, decode=True)
            self.assertEqual(oid, p_oid)
            self.assertEqual(status, p_status)
예제 #11
0
class StorageMasterHandlerTests(NeoUnitTestBase):
    def setUp(self):
        NeoUnitTestBase.setUp(self)
        self.prepareDatabase(number=1)
        # create an application object
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        # handler
        self.operation = MasterOperationHandler(self.app)
        # set pmn
        self.master_uuid = self.getMasterUUID()
        pmn = self.app.nm.getMasterList()[0]
        pmn.setUUID(self.master_uuid)
        self.app.primary_master_node = pmn
        self.master_port = 10010

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageMasterHandlerTests, self)._tearDown(success)

    def getMasterConnection(self):
        address = ("127.0.0.1", self.master_port)
        return self.getFakeConnection(uuid=self.master_uuid, address=address)

    def test_14_notifyPartitionChanges1(self):
        # old partition change -> do nothing
        app = self.app
        conn = self.getMasterConnection()
        app.replicator = Mock({})
        self.app.pt = Mock({'getID': 1})
        count = len(self.app.nm.getList())
        self.assertRaises(ProtocolError, self.operation.notifyPartitionChanges,
                          conn, 0, 0, ())
        self.assertEqual(self.app.pt.getID(), 1)
        self.assertEqual(len(self.app.nm.getList()), count)
        calls = self.app.replicator.mockGetNamedCalls('removePartition')
        self.assertEqual(len(calls), 0)
        calls = self.app.replicator.mockGetNamedCalls('addPartition')
        self.assertEqual(len(calls), 0)

    def test_14_notifyPartitionChanges2(self):
        # cases :
        uuid1, uuid2, uuid3 = [self.getStorageUUID() for i in range(3)]
        cells = (
            (0, uuid1, CellStates.UP_TO_DATE),
            (1, uuid2, CellStates.DISCARDED),
            (2, uuid3, CellStates.OUT_OF_DATE),
        )
        # context
        conn = self.getMasterConnection()
        app = self.app
        # register nodes
        app.nm.createStorage(uuid=uuid1)
        app.nm.createStorage(uuid=uuid2)
        app.nm.createStorage(uuid=uuid3)
        app.pt = PartitionTable(3, 1)
        app.pt._id = 1
        ptid = 2
        app.dm = Mock({})
        app.replicator = Mock({})
        self.operation.notifyPartitionChanges(conn, ptid, 1, cells)
        # ptid set
        self.assertEqual(app.pt.getID(), ptid)
        # dm call
        calls = self.app.dm.mockGetNamedCalls('changePartitionTable')
        self.assertEqual(len(calls), 1)
        calls[0].checkArgs(ptid, 1, cells)
예제 #12
0
class StorageIdentificationHandlerTests(NeoUnitTestBase):
    def setUp(self):
        NeoUnitTestBase.setUp(self)
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.app.name = 'NEO'
        self.app.ready = True
        self.app.pt = PartitionTable(4, 1)
        self.identification = IdentificationHandler(self.app)

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageIdentificationHandlerTests, self)._tearDown(success)

    def test_requestIdentification1(self):
        """ nodes are rejected during election or if unknown storage """
        self.app.ready = False
        self.assertRaises(
            NotReadyError,
            self.identification.requestIdentification,
            self.getFakeConnection(),
            NodeTypes.CLIENT,
            self.getClientUUID(),
            None,
            self.app.name,
        )
        self.app.ready = True
        self.assertRaises(
            NotReadyError,
            self.identification.requestIdentification,
            self.getFakeConnection(),
            NodeTypes.STORAGE,
            self.getStorageUUID(),
            None,
            self.app.name,
        )

    def test_requestIdentification3(self):
        """ broken nodes must be rejected """
        uuid = self.getClientUUID()
        conn = self.getFakeConnection(uuid=uuid)
        node = self.app.nm.createClient(uuid=uuid)
        node.setBroken()
        self.assertRaises(
            BrokenNodeDisallowedError,
            self.identification.requestIdentification,
            conn,
            NodeTypes.CLIENT,
            uuid,
            None,
            self.app.name,
        )

    def test_requestIdentification2(self):
        """ accepted client must be connected and running """
        uuid = self.getClientUUID()
        conn = self.getFakeConnection(uuid=uuid)
        node = self.app.nm.createClient(uuid=uuid)
        master = (self.local_ip, 3000)
        self.app.master_node = Mock({
            'getAddress': master,
        })
        self.identification.requestIdentification(conn, NodeTypes.CLIENT, uuid,
                                                  None, self.app.name)
        self.assertTrue(node.isRunning())
        self.assertTrue(node.isConnected())
        self.assertEqual(node.getUUID(), uuid)
        self.assertTrue(node.getConnection() is conn)
        args = self.checkAcceptIdentification(conn, decode=True)
        node_type, address, _np, _nr, _uuid, _master, _master_list = args
        self.assertEqual(node_type, NodeTypes.STORAGE)
        self.assertEqual(address, None)
        self.assertEqual(_uuid, uuid)
        self.assertEqual(_master, master)
예제 #13
0
class StorageIdentificationHandlerTests(NeoUnitTestBase):

    def setUp(self):
        NeoUnitTestBase.setUp(self)
        config = self.getStorageConfiguration(master_number=1)
        self.app = Application(config)
        self.app.name = 'NEO'
        self.app.ready = True
        self.app.pt = PartitionTable(4, 1)
        self.identification = IdentificationHandler(self.app)

    def _tearDown(self, success):
        self.app.close()
        del self.app
        super(StorageIdentificationHandlerTests, self)._tearDown(success)

    def test_requestIdentification1(self):
        """ nodes are rejected during election or if unknown storage """
        self.app.ready = False
        self.assertRaises(
                NotReadyError,
                self.identification.requestIdentification,
                self.getFakeConnection(),
                NodeTypes.CLIENT,
                self.getClientUUID(),
                None,
                self.app.name,
        )
        self.app.ready = True
        self.assertRaises(
                NotReadyError,
                self.identification.requestIdentification,
                self.getFakeConnection(),
                NodeTypes.STORAGE,
                self.getStorageUUID(),
                None,
                self.app.name,
        )

    def test_requestIdentification3(self):
        """ broken nodes must be rejected """
        uuid = self.getClientUUID()
        conn = self.getFakeConnection(uuid=uuid)
        node = self.app.nm.createClient(uuid=uuid)
        node.setBroken()
        self.assertRaises(BrokenNodeDisallowedError,
                self.identification.requestIdentification,
                conn,
                NodeTypes.CLIENT,
                uuid,
                None,
                self.app.name,
        )

    def test_requestIdentification2(self):
        """ accepted client must be connected and running """
        uuid = self.getClientUUID()
        conn = self.getFakeConnection(uuid=uuid)
        node = self.app.nm.createClient(uuid=uuid)
        master = (self.local_ip, 3000)
        self.app.master_node = Mock({
          'getAddress': master,
        })
        self.identification.requestIdentification(conn, NodeTypes.CLIENT, uuid,
                None, self.app.name)
        self.assertTrue(node.isRunning())
        self.assertTrue(node.isConnected())
        self.assertEqual(node.getUUID(), uuid)
        self.assertTrue(node.getConnection() is conn)
        args = self.checkAcceptIdentification(conn, decode=True)
        node_type, address, _np, _nr, _uuid, _master, _master_list = args
        self.assertEqual(node_type, NodeTypes.STORAGE)
        self.assertEqual(address, None)
        self.assertEqual(_uuid, uuid)
        self.assertEqual(_master, master)