Commit d7bfc065 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Full rewrite of node module tests, better fit to the actual implementation of

Node and NodeManager classes.


git-svn-id: https://svn.erp5.org/repos/neo/trunk@1290 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent b839b14e
...@@ -21,6 +21,11 @@ from neo import logging ...@@ -21,6 +21,11 @@ from neo import logging
from neo import protocol from neo import protocol
from neo.util import dump from neo.util import dump
# TODO:
# Node requires a manager
# Index nodes per node type, server, uuid and state
# A subclass should not tell it's own type
class Node(object): class Node(object):
"""This class represents a node.""" """This class represents a node."""
...@@ -34,6 +39,9 @@ class Node(object): ...@@ -34,6 +39,9 @@ class Node(object):
def setManager(self, manager): def setManager(self, manager):
self.manager = manager self.manager = manager
def getManager(self):
return self.manager
def getLastStateChange(self): def getLastStateChange(self):
return self.last_state_change return self.last_state_change
......
...@@ -15,259 +15,303 @@ ...@@ -15,259 +15,303 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import unittest, os import unittest
from mock import Mock from mock import Mock
from neo.protocol import RUNNING_STATE, TEMPORARILY_DOWN_STATE, DOWN_STATE, \ from neo import protocol
BROKEN_STATE, UNKNOWN_STATE, MASTER_NODE_TYPE, STORAGE_NODE_TYPE, \ from neo.protocol import RUNNING_STATE, DOWN_STATE, \
CLIENT_NODE_TYPE, INVALID_UUID UNKNOWN_STATE, MASTER_NODE_TYPE, STORAGE_NODE_TYPE, \
from neo.node import Node, MasterNode, StorageNode, ClientNode, NodeManager CLIENT_NODE_TYPE, ADMIN_NODE_TYPE
from neo.node import Node, MasterNode, StorageNode, ClientNode, AdminNode, \
NodeManager
from neo.tests import NeoTestBase from neo.tests import NeoTestBase
from time import time from time import time
class NodesTests(NeoTestBase): class NodesTests(NeoTestBase):
def setUp(self): def setUp(self):
pass self.manager = Mock()
def tearDown(self): def _updatedByServer(self, node, index=0):
pass calls = self.manager.mockGetNamedCalls('unregisterServer')
self.assertEqual(len(calls), index + 1)
self.assertEqual(calls[index].getParam(0), node)
calls = self.manager.mockGetNamedCalls('registerServer')
self.assertEqual(len(calls), index + 1)
self.assertEqual(calls[index].getParam(0), node)
def test_01_node(self): def _updatedByUUID(self, node, index=0):
# initialisation calls = self.manager.mockGetNamedCalls('unregisterUUID')
server = ("127.0.0.1", 10000) self.assertEqual(len(calls), index + 1)
self.assertEqual(calls[index].getParam(0), node)
calls = self.manager.mockGetNamedCalls('registerUUID')
self.assertEqual(len(calls), index + 1)
self.assertEqual(calls[index].getParam(0), node)
def testInit(self):
""" Check the node initialization """
server = ('127.0.0.1', 10000)
uuid = self.getNewUUID() uuid = self.getNewUUID()
node = Node(server, uuid) node = Node(server=server, uuid=uuid)
manager = Mock({'__repr__':'Fake Manager'}) self.assertEqual(node.getState(), protocol.UNKNOWN_STATE)
node.setManager(manager)
self.assertEqual(node.state, UNKNOWN_STATE)
self.assertEqual(node.server, server)
self.assertEqual(node.uuid, uuid)
self.assertTrue(node.manager is manager)
self.assertNotEqual(node.last_state_change, None)
# test getter
self.assertEqual(node.getState(), UNKNOWN_STATE)
self.assertEqual(node.getServer(), server) self.assertEqual(node.getServer(), server)
self.assertEqual(node.getUUID(), uuid) self.assertEqual(node.getUUID(), uuid)
self.assertRaises(NotImplementedError, node.getType) self.assertEqual(node.getManager(), None)
self.assertNotEqual(node.getLastStateChange(), None) self.assertTrue(time() - 1 < node.getLastStateChange() < time())
last_change = node.getLastStateChange()
self.failUnless(isinstance(last_change, float)) def testManager(self):
now = time() """ Check if the node manager is well binded to the node """
self.failUnless(last_change < now) node = Node()
# change the state self.assertEqual(node.getManager(), None)
node.setState(DOWN_STATE) node.setManager(self.manager)
self.assertEqual(node.getState(), DOWN_STATE) self.assertTrue(node.getManager() is self.manager)
# tmie of change must be updated # XXX: the manager should index the node by uuid and address
self.failUnless(node.getLastStateChange() > now)
# set new uuid def testState(self):
new_uuid = self.getNewUUID() """ Check if the last changed time is updated when state is changed """
self.assertNotEqual(new_uuid, uuid) node = Node()
node.setUUID(new_uuid) self.assertEqual(node.getState(), protocol.UNKNOWN_STATE)
self.assertEqual(node.getUUID(), new_uuid) self.assertTrue(time() - 1 < node.getLastStateChange() < time())
# set new server previous_time = node.getLastStateChange()
new_server = ("127.0.0.1", 10001) node.setState(protocol.RUNNING_STATE)
self.assertNotEqual(new_server, server) self.assertEqual(node.getState(), protocol.RUNNING_STATE)
node.setServer(new_server) self.assertTrue(previous_time < node.getLastStateChange())
self.assertEqual(node.getServer(), new_server) self.assertTrue(time() - 1 < node.getLastStateChange() < time())
# add manager
manager = Mock() def testServer(self):
node.setManager(manager) """ Check if the manager is updated when a node change it's address """
self.assertNotEqual(node.manager, None) node = Node()
# set server and uuid and check method are well called on manager self.assertEqual(node.getServer(), None)
self.assertNotEqual(node.getUUID(), uuid) node.setManager(self.manager)
node.setUUID(uuid) server = ('127.0.0.1', 10000)
self.assertEqual(node.getUUID(), uuid)
self.assertNotEqual(node.getServer(), server)
node.setServer(server) node.setServer(server)
self.assertEqual(node.getServer(), server) self._updatedByServer(node)
self.assertEqual(len(manager.mockGetNamedCalls("unregisterServer")), 1)
call = manager.mockGetNamedCalls("unregisterServer")[0] def testUUID(self):
self.assertEqual(call.getParam(0), node) """ As for Server but UUID """
self.assertEqual(len(manager.mockGetNamedCalls("registerServer")), 1) node = Node()
call = manager.mockGetNamedCalls("registerServer")[0] self.assertEqual(node.getServer(), None)
self.assertEqual(call.getParam(0), node) node.setManager(self.manager)
self.assertEqual(len(manager.mockGetNamedCalls("unregisterUUID")), 1)
call = manager.mockGetNamedCalls("unregisterUUID")[0]
self.assertEqual(call.getParam(0), node)
self.assertEqual(len(manager.mockGetNamedCalls("registerUUID")), 1)
call = manager.mockGetNamedCalls("registerUUID")[0]
self.assertEqual(call.getParam(0), node)
def test_02_master_node(self):
server = ("127.0.0.1", 10000)
uuid = self.getNewUUID() uuid = self.getNewUUID()
node = MasterNode(server, uuid) node.setUUID(uuid)
self.assertEqual(node.manager, None) self._updatedByUUID(node)
self.assertNotEqual(node.last_state_change, None)
# test getter def testTypes(self):
self.assertEqual(node.getState(), UNKNOWN_STATE) """ Check that the abstract node has no type """
self.assertEqual(node.getServer(), server) node = Node()
self.assertEqual(node.getUUID(), uuid) self.assertRaises(NotImplementedError, node.getType)
self.assertEqual(MASTER_NODE_TYPE, node.getType()) self.assertFalse(node.isStorage())
self.assertFalse(node.isMaster())
self.assertFalse(node.isClient())
self.assertFalse(node.isAdmin())
def testMaster(self):
""" Check Master sub class """
node = MasterNode()
self.assertEqual(node.getType(), protocol.MASTER_NODE_TYPE)
self.assertTrue(node.isMaster())
self.assertFalse(node.isStorage())
self.assertFalse(node.isClient())
self.assertFalse(node.isAdmin())
def testStorage(self):
""" Check Storage sub class """
node = StorageNode()
self.assertEqual(node.getType(), protocol.STORAGE_NODE_TYPE)
self.assertTrue(node.isStorage())
self.assertFalse(node.isMaster())
self.assertFalse(node.isClient())
self.assertFalse(node.isAdmin())
def testClient(self):
""" Check Client sub class """
node = ClientNode()
self.assertEqual(node.getType(), protocol.CLIENT_NODE_TYPE)
self.assertTrue(node.isClient())
self.assertFalse(node.isMaster())
self.assertFalse(node.isStorage())
self.assertFalse(node.isAdmin())
def testAdmin(self):
""" Check Admin sub class """
node = AdminNode()
self.assertEqual(node.getType(), protocol.ADMIN_NODE_TYPE)
self.assertTrue(node.isAdmin())
self.assertFalse(node.isMaster())
self.assertFalse(node.isStorage())
self.assertFalse(node.isClient())
class NodeManagerTests(NeoTestBase):
def setUp(self):
self.manager = NodeManager()
self.storage = StorageNode(('127.0.0.1', 1000), self.getNewUUID())
self.master = MasterNode(('127.0.0.1', 2000), self.getNewUUID())
self.client = ClientNode(None, self.getNewUUID())
self.admin = AdminNode(('127.0.0.1', 4000), self.getNewUUID())
def checkNodes(self, node_list):
manager = self.manager
self.assertEqual(sorted(manager.getNodeList()), sorted(node_list))
def checkMasters(self, master_list):
manager = self.manager
self.assertEqual(manager.getMasterNodeList(), master_list)
def checkStorages(self, storage_list):
manager = self.manager
self.assertEqual(manager.getStorageNodeList(), storage_list)
def checkClients(self, client_list):
manager = self.manager
self.assertEqual(manager.getClientNodeList(), client_list)
def checkByServer(self, node):
node_found = self.manager.getNodeByServer(node.getServer())
self.assertEqual(node_found, node)
def test_02_storage_node(self): def checkByUUID(self, node):
server = ("127.0.0.1", 10000) node_found = self.manager.getNodeByUUID(node.getUUID())
uuid = self.getNewUUID() self.assertEqual(node_found, node)
node = StorageNode(server, uuid)
self.assertEqual(node.manager, None)
self.assertNotEqual(node.last_state_change, None)
# test getter
self.assertEqual(node.getState(), UNKNOWN_STATE)
self.assertEqual(node.getServer(), server)
self.assertEqual(node.getUUID(), uuid)
self.assertEqual(STORAGE_NODE_TYPE, node.getType())
def test_04_client_node(self): def testInit(self):
server = ("127.0.0.1", 10000) """ Check the manager is empty when started """
manager = self.manager
self.checkNodes([])
self.checkMasters([])
self.checkStorages([])
self.checkClients([])
server = ('127.0.0.1', 10000)
self.assertEqual(manager.getNodeByServer(server), None)
self.assertEqual(manager.getNodeByServer(None), None)
uuid = self.getNewUUID() uuid = self.getNewUUID()
node = ClientNode(server, uuid) self.assertEqual(manager.getNodeByUUID(uuid), None)
self.assertEqual(node.manager, None) self.assertEqual(manager.getNodeByUUID(None), None)
self.assertNotEqual(node.last_state_change, None)
# test getter def testAdd(self):
self.assertEqual(node.getState(), UNKNOWN_STATE) """ Check if new nodes are registered in the manager """
self.assertEqual(node.getServer(), server) manager = self.manager
self.assertEqual(node.getUUID(), uuid) self.checkNodes([])
self.assertEqual(CLIENT_NODE_TYPE, node.getType()) # storage
manager.add(self.storage)
self.checkNodes([self.storage])
def test_05_node_manager(self): self.checkStorages([self.storage])
nm = NodeManager() self.checkMasters([])
self.assertEqual(len(nm.node_list), 0) self.checkClients([])
self.assertEqual(len(nm.server_dict), 0) self.checkByServer(self.storage)
self.assertEqual(len(nm.uuid_dict), 0) self.checkByUUID(self.storage)
self.assertEqual(len(nm.getNodeList()), 0) # master
self.assertEqual(len(nm.getMasterNodeList()), 0) manager.add(self.master)
self.assertEqual(len(nm.getStorageNodeList()), 0) self.checkNodes([self.storage, self.master])
self.assertEqual(len(nm.getClientNodeList()), 0) self.checkStorages([self.storage])
# Create some node and add them to node manager self.checkMasters([self.master])
# add a storage node self.checkClients([])
sn_server = ("127.0.0.1", 10000) self.checkByServer(self.master)
sn_uuid = self.getNewUUID() self.checkByUUID(self.master)
sn = StorageNode(sn_server, sn_uuid) # client
nm.add(sn) manager.add(self.client)
self.assertEqual(sn.manager, nm) self.checkNodes([self.storage, self.master, self.client])
self.assertTrue(nm.server_dict.has_key(sn_server)) self.checkStorages([self.storage])
self.assertEqual(nm.server_dict[sn_server], sn) self.checkMasters([self.master])
self.assertTrue(nm.uuid_dict.has_key(sn_uuid)) self.checkClients([self.client])
self.assertEqual(nm.uuid_dict[sn_uuid], sn) # client node has no server
self.assertEqual(len(nm.getNodeList()), 1) self.assertEqual(manager.getNodeByServer(self.client.getServer()), None)
self.assertEqual(len(nm.getMasterNodeList()), 0) self.checkByUUID(self.client)
self.assertEqual(len(nm.getClientNodeList()), 0) # admin
self.assertEqual(len(nm.getStorageNodeList()), 1) manager.add(self.admin)
sn_list = nm.getStorageNodeList() self.checkNodes([self.storage, self.master, self.client, self.admin])
self.assertEqual(sn_list[0], sn) self.checkStorages([self.storage])
# add a Master node self.checkMasters([self.master])
mn_server = ("127.0.0.1", 10001) self.checkClients([self.client])
mn_uuid = self.getNewUUID() self.checkByServer(self.admin)
mn = MasterNode(mn_server, mn_uuid) self.checkByUUID(self.admin)
nm.add(mn)
self.assertEqual(mn.manager, nm) def testClear(self):
self.assertTrue(nm.server_dict.has_key(mn_server)) """ Check that the manager clear all its content """
self.assertEqual(nm.server_dict[mn_server], mn) manager = self.manager
self.assertTrue(nm.uuid_dict.has_key(mn_uuid)) self.checkNodes([])
self.assertEqual(nm.uuid_dict[mn_uuid], mn) self.checkStorages([])
self.assertEqual(len(nm.getNodeList()), 2) self.checkMasters([])
self.assertEqual(len(nm.getMasterNodeList()), 1) self.checkClients([])
self.assertEqual(len(nm.getClientNodeList()), 0) manager.add(self.master)
self.assertEqual(len(nm.getStorageNodeList()), 1) self.checkMasters([self.master])
mn_list = nm.getMasterNodeList() manager.clear()
self.assertEqual(mn_list[0], mn) self.checkNodes([])
sn_list = nm.getStorageNodeList() self.checkMasters([])
self.assertEqual(sn_list[0], sn) manager.add(self.storage)
# add a Client node self.checkStorages([self.storage])
cn_server = ("127.0.0.1", 10002) manager.clear()
cn_uuid = self.getNewUUID() self.checkNodes([])
cn = ClientNode(cn_server, cn_uuid) self.checkStorages([])
nm.add(cn) manager.add(self.client)
self.assertEqual(cn.manager, nm) self.checkClients([self.client])
self.assertTrue(nm.server_dict.has_key(cn_server)) manager.clear()
self.assertEqual(nm.server_dict[cn_server], cn) self.checkNodes([])
self.assertTrue(nm.uuid_dict.has_key(cn_uuid)) self.checkClients([])
self.assertEqual(nm.uuid_dict[cn_uuid], cn)
self.assertEqual(len(nm.getNodeList()), 3) def testFilteredClear(self):
self.assertEqual(len(nm.getMasterNodeList()), 1) """ Check the clear filter works well """
self.assertEqual(len(nm.getClientNodeList()), 1) manager = self.manager
self.assertEqual(len(nm.getStorageNodeList()), 1) manager.add(self.master)
cn_list = nm.getClientNodeList() manager.add(self.storage)
self.assertEqual(cn_list[0], cn) manager.add(self.client)
mn_list = nm.getMasterNodeList() self.checkNodes([self.master, self.storage, self.client])
self.assertEqual(mn_list[0], mn) drop_clients = lambda node: isinstance(node, ClientNode)
sn_list = nm.getStorageNodeList() manager.clear(filter=drop_clients)
self.assertEqual(sn_list[0], sn) self.checkNodes([self.master, self.storage])
# check we can get the nodes self.checkClients([])
self.assertEqual(len(nm.server_dict), 3) drop_masters = lambda node: isinstance(node, MasterNode)
self.assertEqual(len(nm.uuid_dict), 3) manager.clear(filter=drop_masters)
node_list = nm.getNodeList() self.checkNodes([self.storage])
self.failUnless(cn in node_list) self.checkMasters([])
self.failUnless(mn in node_list) drop_storage = lambda node: node is self.storage
self.failUnless(sn in node_list) manager.clear(filter=drop_storage)
node = nm.getNodeByServer(cn_server) self.checkNodes([])
self.assertEqual(node, cn) self.checkStorages([])
node = nm.getNodeByUUID(cn_uuid)
self.assertEqual(node, cn) def testUpdate(self):
node = nm.getNodeByServer(sn_server) """ Check manager content update """
self.assertEqual(node, sn) # set up four nodes
node = nm.getNodeByUUID(sn_uuid) manager = self.manager
self.assertEqual(node, sn) manager.add(self.master)
node = nm.getNodeByServer(mn_server) manager.add(self.storage)
self.assertEqual(node, mn) manager.add(self.client)
node = nm.getNodeByUUID(mn_uuid) manager.add(self.admin)
self.assertEqual(node, mn) self.checkNodes([self.master, self.storage, self.client, self.admin])
# remove the nodes self.checkMasters([self.master])
# remove the storage node self.checkStorages([self.storage])
nm.remove(sn) self.checkClients([self.client])
self.assertFalse(nm.server_dict.has_key(sn_server)) # build changes
self.assertFalse(nm.uuid_dict.has_key(sn_uuid)) old_address = self.master.getServer()
self.assertEqual(len(nm.getNodeList()), 2) new_address = ('127.0.0.1', 2001)
self.assertEqual(len(nm.getMasterNodeList()), 1) new_uuid = self.getNewUUID()
self.assertEqual(len(nm.getClientNodeList()), 1) node_list = (
self.assertEqual(len(nm.getStorageNodeList()), 0) (CLIENT_NODE_TYPE, None, self.client.getUUID(), DOWN_STATE),
node = nm.getNodeByServer(sn_server) (MASTER_NODE_TYPE, new_address, self.master.getUUID(), RUNNING_STATE),
self.assertEqual(node, None) (STORAGE_NODE_TYPE, self.storage.getServer(), new_uuid, RUNNING_STATE),
node = nm.getNodeByUUID(sn_uuid) (ADMIN_NODE_TYPE, self.admin.getServer(), self.admin.getUUID(), UNKNOWN_STATE),
self.assertEqual(node, None) )
# remove the client node # update manager content
nm.remove(cn) manager.update(node_list)
self.assertFalse(nm.server_dict.has_key(cn_server)) # - the client gets down
self.assertFalse(nm.uuid_dict.has_key(cn_uuid)) self.checkClients([])
self.assertEqual(len(nm.getNodeList()), 1) # - master change it's address
self.assertEqual(len(nm.getMasterNodeList()), 1) self.checkMasters([self.master])
self.assertEqual(len(nm.getClientNodeList()), 0) self.assertEqual(manager.getNodeByServer(old_address), None)
self.assertEqual(len(nm.getStorageNodeList()), 0) self.master.setServer(new_address)
node = nm.getNodeByServer(cn_server) self.checkByServer(self.master)
self.assertEqual(node, None) # a new storage replaced the old one
node = nm.getNodeByUUID(cn_uuid) self.assertNotEqual(manager.getStorageNodeList(), [self.storage])
self.assertEqual(node, None) self.assertTrue(len(manager.getStorageNodeList()), 1)
# remove the master node new_storage = manager.getStorageNodeList()[0]
nm.remove(mn) self.assertEqual(new_storage.getState(), RUNNING_STATE)
self.assertFalse(nm.server_dict.has_key(mn_server)) self.assertNotEqual(new_storage, self.storage)
self.assertFalse(nm.uuid_dict.has_key(mn_uuid)) # admin is still here but in UNKNOWN_STATE
self.assertEqual(len(nm.getNodeList()), 0) self.checkNodes([self.master, self.admin, new_storage])
self.assertEqual(len(nm.getMasterNodeList()), 0) self.assertEqual(self.admin.getState(), UNKNOWN_STATE)
self.assertEqual(len(nm.getClientNodeList()), 0)
self.assertEqual(len(nm.getStorageNodeList()), 0)
node = nm.getNodeByServer(mn_server)
self.assertEqual(node, None)
node = nm.getNodeByUUID(mn_uuid)
self.assertEqual(node, None)
# test method to register uuid/server
self.assertEqual(len(nm.server_dict), 0)
nm.registerServer(cn)
self.assertEqual(len(nm.server_dict), 1)
self.assertTrue(nm.server_dict.has_key(cn_server))
self.assertEqual(len(nm.uuid_dict), 0)
nm.registerUUID(cn)
self.assertEqual(len(nm.uuid_dict), 1)
self.assertTrue(nm.uuid_dict.has_key(cn_uuid))
# test for unregister methods
self.assertEqual(len(nm.server_dict), 1)
nm.unregisterServer(cn)
self.assertEqual(len(nm.server_dict), 0)
self.assertFalse(nm.server_dict.has_key(cn_server))
self.assertEqual(len(nm.uuid_dict), 1)
nm.unregisterUUID(cn)
self.assertEqual(len(nm.uuid_dict), 0)
self.assertFalse(nm.uuid_dict.has_key(cn_uuid))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment