From 775fcbb10999f404e0388faf3f8d811babdfc5fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=A9gory=20Wisniewski?= <gregory@nexedi.com>
Date: Fri, 7 Aug 2009 14:36:09 +0000
Subject: [PATCH] Only one temporary directory is used for the test runner,
 each test method have it's own directory to easily find related log files.
 The NeoFunctionalTest class is added to provide the getTempDirectory() that
 return the path for the current test method. Master functional test case now
 have the neo attribute, all test cases inherit from NeoFunctionalTest.

git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@1246 71dcc9de-d417-0410-9af5-da40c76e7ee4
---
 neo/tests/functional/__init__.py         | 32 +++++++--
 neo/tests/functional/testCluster.py      | 13 ++--
 neo/tests/functional/testImportExport.py |  9 ++-
 neo/tests/functional/testMaster.py       | 82 +++++++++++++-----------
 neo/tests/functional/testStorage.py      |  5 +-
 neo/tests/functional/testZODB.py         |  4 +-
 6 files changed, 89 insertions(+), 56 deletions(-)

diff --git a/neo/tests/functional/__init__.py b/neo/tests/functional/__init__.py
index 3cfdd9a1..88cac753 100644
--- a/neo/tests/functional/__init__.py
+++ b/neo/tests/functional/__init__.py
@@ -24,6 +24,7 @@ import ZODB
 import signal
 import random
 import MySQLdb
+import unittest
 import tempfile
 import traceback
 
@@ -157,7 +158,7 @@ class NEOCluster(object):
                  partitions=1, replicas=0, port_base=10000,
                  db_user='neo', db_password='neo',
                  db_super_user='root', db_super_password=None,
-                 cleanup_on_delete=False):
+                 cleanup_on_delete=False, temp_dir=None):
         self.cleanup_on_delete = cleanup_on_delete
         self.uuid_set = set()
         self.db_super_user = db_super_user
@@ -167,10 +168,12 @@ class NEOCluster(object):
         self.db_list = db_list
         self.process_dict = {}
         self.last_port = port_base
-        self.temp_dir = temp_dir = tempfile.mkdtemp(prefix='neo_')
-        print 'Using temp directory %r.' % (temp_dir, )
-        self.config_file_path = config_file_path = os.path.join(temp_dir, 'neo.conf')
-        config_file = open(config_file_path, 'w')
+        if temp_dir is None:
+            temp_dir = tempfile.mkdtemp(prefix='neo_')
+            print 'Using temp directory %r.' % (temp_dir, )
+        self.temp_dir = temp_dir
+        self.config_file_path = os.path.join(temp_dir, 'neo.conf')
+        config_file = open(self.config_file_path, 'w')
         neo_admin_port = self.__allocatePort()
         self.cluster_name = cluster_name = 'neo_%s' % (random.randint(0, 100), )
         master_node_dict = {}
@@ -468,3 +471,22 @@ class NEOCluster(object):
         if self.cleanup_on_delete:
             os.removedirs(self.temp_dir)
 
+
+class NEOFunctionalTest(unittest.TestCase):
+
+    def getTempDirectory(self):
+        # get the current temp directory or a new one
+        temp_dir = os.environ.get('TEMP', None)
+        if temp_dir is None:
+            temp_dir = tempfile.mkdtemp(prefix='neo_')
+        # build the full path based on test case and current test method
+        test_case_name = self.__class__.__name__
+        test_method_name = self._TestCase__testMethodName
+        temp_dir = os.path.join(temp_dir, test_case_name)
+        temp_dir = os.path.join(temp_dir, test_method_name)
+        # build the path if needed
+        if not os.path.exists(temp_dir):
+            os.makedirs(temp_dir)
+        return temp_dir
+        
+
diff --git a/neo/tests/functional/testCluster.py b/neo/tests/functional/testCluster.py
index 6a884528..5df025b7 100644
--- a/neo/tests/functional/testCluster.py
+++ b/neo/tests/functional/testCluster.py
@@ -16,13 +16,14 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 import unittest
-from neo.tests.functional import NEOCluster
+from neo.tests.functional import NEOCluster, NEOFunctionalTest
 from neo import protocol
 
-class ClusterTests(unittest.TestCase):
+class ClusterTests(NEOFunctionalTest):
 
     def testClusterBreaks(self):
-        neo = NEOCluster(['test_neo1'], port_base=20000, master_node_count=1)
+        neo = NEOCluster(['test_neo1'], port_base=20000, master_node_count=1,
+                temp_dir=self.getTempDirectory())
         neoctl = neo.getNEOCTL()
         neo.setupDB()
         neo.start()
@@ -35,7 +36,8 @@ class ClusterTests(unittest.TestCase):
 
     def testClusterBreaksWithTwoNodes(self):
         neo = NEOCluster(['test_neo1', 'test_neo2'], port_base=20000,
-                         partitions=2, master_node_count=1)
+                 partitions=2, master_node_count=1,
+                 temp_dir=self.getTempDirectory())
         neoctl = neo.getNEOCTL()
         neo.setupDB()
         neo.start()
@@ -48,7 +50,8 @@ class ClusterTests(unittest.TestCase):
 
     def testClusterDoesntBreakWithTwoNodesOneReplica(self):
         neo = NEOCluster(['test_neo1', 'test_neo2'], port_base=20000,
-                         partitions=2, replicas=1, master_node_count=1)
+                         partitions=2, replicas=1, master_node_count=1,
+                         temp_dir=self.getTempDirectory())
         neoctl = neo.getNEOCTL()
         neo.setupDB()
         neo.start()
diff --git a/neo/tests/functional/testImportExport.py b/neo/tests/functional/testImportExport.py
index 8d7666a1..7b46188e 100644
--- a/neo/tests/functional/testImportExport.py
+++ b/neo/tests/functional/testImportExport.py
@@ -24,7 +24,7 @@ import ZODB
 from ZODB.FileStorage import FileStorage
 from Persistence import Persistent
 
-from neo.tests.functional import NEOCluster
+from neo.tests.functional import NEOCluster, NEOFunctionalTest
 from neo.client.Storage import Storage as NEOStorage
 
 
@@ -42,14 +42,13 @@ class Tree(Persistent):
         self.left = Tree(depth)
 
 
-class ImportExportTests(unittest.TestCase):
+class ImportExportTests(NEOFunctionalTest):
 
     def setUp(self):
-        self.temp_dir = tempfile.mkdtemp(prefix='neo_import_export_')
-        print "using the temp directory %s" % self.temp_dir
         # create a neo cluster
         databases = ['test_neo1', 'test_neo2']
-        self.neo = NEOCluster(databases, port_base=20000, master_node_count=2)
+        self.neo = NEOCluster(databases, port_base=20000, master_node_count=2,
+                temp_dir=self.getTempDirectory())
         self.neo.setupDB()
 
     def tearDown(self):
diff --git a/neo/tests/functional/testMaster.py b/neo/tests/functional/testMaster.py
index 7886c4e0..7d706978 100644
--- a/neo/tests/functional/testMaster.py
+++ b/neo/tests/functional/testMaster.py
@@ -17,115 +17,123 @@
 
 from time import sleep, time
 import unittest
-from neo.tests.functional import NEOCluster
+from neo.tests.functional import NEOCluster, NEOFunctionalTest
 from neo.neoctl.neoctl import NotReadyException
 from neo import protocol
 from neo.util import dump
 
 MASTER_NODE_COUNT = 3
 
-neo = NEOCluster([], port_base=20000, master_node_count=MASTER_NODE_COUNT)
 
-class MasterTests(unittest.TestCase):
+class MasterTests(NEOFunctionalTest):
 
     def setUp(self):
-        neo.stop()
-        neo.start()
-        self.storage = neo.getZODBStorage()
-        self.neoctl = neo.getNEOCTL()
+        self.neo = NEOCluster([], port_base=20000, 
+                master_node_count=MASTER_NODE_COUNT,
+                temp_dir=self.getTempDirectory())
+        self.neo.stop()
+        self.neo.start()
+        self.storage = self.neo.getZODBStorage()
+        self.neoctl = self.neo.getNEOCTL()
 
     def tearDown(self):
-        neo.stop()
+        self.neo.stop()
 
     def testStoppingSecondaryMaster(self):
         # Wait for masters to stabilize
-        neo.expectAllMasters(MASTER_NODE_COUNT)
+        self.neo.expectAllMasters(MASTER_NODE_COUNT)
 
         # Kill
-        killed_uuid_list = neo.killSecondaryMaster()
+        killed_uuid_list = self.neo.killSecondaryMaster()
         # Test sanity check.
         self.assertEqual(len(killed_uuid_list), 1)
         uuid = killed_uuid_list[0]
         # Check node state has changed.
-        neo.expectMasterState(uuid, None)
+        self.neo.expectMasterState(uuid, None)
 
     def testStoppingPrimaryMasterWithTwoSecondaries(self):
         # Wait for masters to stabilize
-        neo.expectAllMasters(MASTER_NODE_COUNT)
+        self.neo.expectAllMasters(MASTER_NODE_COUNT)
 
         # Kill
-        killed_uuid_list = neo.killPrimaryMaster()
+        killed_uuid_list = self.neo.killPrimaryMaster()
         # Test sanity check.
         self.assertEqual(len(killed_uuid_list), 1)
         uuid = killed_uuid_list[0]
         # Check the state of the primary we just killed
-        neo.expectMasterState(uuid, (None, protocol.UNKNOWN_STATE))
-        self.assertEqual(neo.getPrimaryMaster(), None)
+        self.neo.expectMasterState(uuid, (None, protocol.UNKNOWN_STATE))
+        self.assertEqual(self.neo.getPrimaryMaster(), None)
         # Check that a primary master arised.
-        neo.expectPrimaryMaster(timeout=10)
+        self.neo.expectPrimaryMaster(timeout=10)
         # Check that the uuid really changed.
-        new_uuid = neo.getPrimaryMaster()
+        new_uuid = self.neo.getPrimaryMaster()
         self.assertNotEqual(new_uuid, uuid)
 
     def testStoppingPrimaryMasterWithOneSecondary(self):
-        neo.expectAllMasters(MASTER_NODE_COUNT, state=protocol.RUNNING_STATE)
+        self.neo.expectAllMasters(MASTER_NODE_COUNT, 
+                state=protocol.RUNNING_STATE)
 
         # Kill one secondary master.
-        killed_uuid_list = neo.killSecondaryMaster()
+        killed_uuid_list = self.neo.killSecondaryMaster()
         # Test sanity checks.
         self.assertEqual(len(killed_uuid_list), 1)
-        neo.expectMasterState(killed_uuid_list[0], None)
-        self.assertEqual(len(neo.getMasterNodeList()), 2)
+        self.neo.expectMasterState(killed_uuid_list[0], None)
+        self.assertEqual(len(self.neo.getMasterNodeList()), 2)
 
-        killed_uuid_list = neo.killPrimaryMaster()
+        killed_uuid_list = self.neo.killPrimaryMaster()
         # Test sanity check.
         self.assertEqual(len(killed_uuid_list), 1)
         uuid = killed_uuid_list[0]
         # Check the state of the primary we just killed
-        neo.expectMasterState(uuid, (None, protocol.UNKNOWN_STATE))
-        self.assertEqual(neo.getPrimaryMaster(), None)
+        self.neo.expectMasterState(uuid, (None, protocol.UNKNOWN_STATE))
+        self.assertEqual(self.neo.getPrimaryMaster(), None)
         # Check that a primary master arised.
-        neo.expectPrimaryMaster(timeout=10)
+        self.neo.expectPrimaryMaster(timeout=10)
         # Check that the uuid really changed.
-        new_uuid = neo.getPrimaryMaster()
+        new_uuid = self.neo.getPrimaryMaster()
         self.assertNotEqual(new_uuid, uuid)
 
     def testMasterSequentialStart(self):
-        neo.expectAllMasters(MASTER_NODE_COUNT, state=protocol.RUNNING_STATE)
-        master_list = neo.getMasterProcessList()
+        self.neo.expectAllMasters(MASTER_NODE_COUNT, 
+                state=protocol.RUNNING_STATE)
+        master_list = self.neo.getMasterProcessList()
 
         # Stop the cluster (so we can start processes manually)
-        neo.killMasters()
+        self.neo.killMasters()
 
         # Start the first master.
         first_master = master_list[0]
         first_master.start()
         first_master_uuid = first_master.getUUID()
         # Check that the master node we started elected itself.
-        neo.expectPrimaryMaster(first_master_uuid, timeout=60)
+        self.neo.expectPrimaryMaster(first_master_uuid, timeout=60)
         # Check that no other node is known as running.
-        self.assertEqual(len(neo.getMasterNodeList(
+        self.assertEqual(len(self.neo.getMasterNodeList(
             state=protocol.RUNNING_STATE)), 1)
 
         # Start a second master.
         second_master = master_list[1]
         # Check that the second master is known as being down.
-        self.assertEqual(neo.getMasterNodeState(second_master.getUUID()), None)
+        self.assertEqual(self.neo.getMasterNodeState(second_master.getUUID()), 
+                None)
         second_master.start()
         # Check that the second master is running under his known UUID.
-        neo.expectMasterState(second_master.getUUID(), protocol.RUNNING_STATE)
+        self.neo.expectMasterState(second_master.getUUID(), 
+                protocol.RUNNING_STATE)
         # Check that the primary master didn't change.
-        self.assertEqual(neo.getPrimaryMaster(), first_master_uuid)
+        self.assertEqual(self.neo.getPrimaryMaster(), first_master_uuid)
 
         # Start a third master.
         third_master = master_list[2]
         # Check that the third master is known as being down.
-        self.assertEqual(neo.getMasterNodeState(third_master.getUUID()), None)
+        self.assertEqual(self.neo.getMasterNodeState(third_master.getUUID()), 
+                None)
         third_master.start()
         # Check that the third master is running under his known UUID.
-        neo.expectMasterState(third_master.getUUID(), protocol.RUNNING_STATE)
+        self.neo.expectMasterState(third_master.getUUID(), 
+                protocol.RUNNING_STATE)
         # Check that the primary master didn't change.
-        self.assertEqual(neo.getPrimaryMaster(), first_master_uuid)
+        self.assertEqual(self.neo.getPrimaryMaster(), first_master_uuid)
 
 def test_suite():
     return unittest.makeSuite(MasterTests)
diff --git a/neo/tests/functional/testStorage.py b/neo/tests/functional/testStorage.py
index 62358c2c..ccee44c3 100644
--- a/neo/tests/functional/testStorage.py
+++ b/neo/tests/functional/testStorage.py
@@ -20,7 +20,7 @@ import unittest
 import transaction
 from Persistence import Persistent
 
-from neo.tests.functional import NEOCluster
+from neo.tests.functional import NEOCluster, NEOFunctionalTest
 from neo.client.Storage import Storage as NEOStorage
 from neo import protocol
 
@@ -32,7 +32,7 @@ class PObject(Persistent):
 
 OBJECT_NUMBER = 100
 
-class StorageTests(unittest.TestCase):
+class StorageTests(NEOFunctionalTest):
 
     def setUp(self):
         self.neo = None
@@ -52,6 +52,7 @@ class StorageTests(unittest.TestCase):
             port_base=20000, 
             master_node_count=2,
             partitions=10, replicas=replicas,
+            temp_dir=self.getTempDirectory(),
         )
         self.neo.setupDB()
         # too many pending storage nodes requested
diff --git a/neo/tests/functional/testZODB.py b/neo/tests/functional/testZODB.py
index e9d6ed94..57990cfd 100644
--- a/neo/tests/functional/testZODB.py
+++ b/neo/tests/functional/testZODB.py
@@ -23,7 +23,7 @@ from persistent import Persistent
 from persistent.mapping import PersistentMapping
 import transaction
 
-from neo.tests.functional import NEOCluster
+from neo.tests.functional import NEOCluster, NEOFunctionalTest
 
 class P(Persistent):
     pass
@@ -42,7 +42,7 @@ neo = NEOCluster(['test_neo1'],
                  partitions=1, replicas=0, port_base=20000,
                  master_node_count=1)
 
-class ZODBTests(unittest.TestCase):
+class ZODBTests(NEOFunctionalTest):
 
     def setUp(self):
         neo.stop()
-- 
2.30.9