Commit 4035fe5c authored by Nicolas Wavrant's avatar Nicolas Wavrant

test_equeue: improve test code

Keep log file for debugging and use cleanup function to be sure
to correctly stop services
parent 0c69fca2
...@@ -22,20 +22,20 @@ class TestEqueue(unittest.TestCase): ...@@ -22,20 +22,20 @@ class TestEqueue(unittest.TestCase):
def setUp(self): def setUp(self):
cwd = os.getcwd() cwd = os.getcwd()
( (
self.logfile,
self.database, self.database,
self.takeover_triggered_file_path, self.takeover_triggered_file_path,
self.lockfile, self.lockfile,
self.socket, self.socket,
self.testfile, self.testfile,
) = self.file_list = map(os.path.join, ( ) = self.file_list = [
'equeue.log',
'equeue.db', 'equeue.db',
'takeover.txt', 'takeover.txt',
'equeue.lock', 'equeue.lock',
'equeue.sock', 'equeue.sock',
'testfile.txt', 'testfile.txt',
)) ]
# Do not delete log files, as it may contain interesting information for debugging
self.logfile = 'equeue.log'
self.options = Options( self.options = Options(
logfile=self.logfile, logfile=self.logfile,
loglevel='INFO', loglevel='INFO',
...@@ -58,6 +58,7 @@ class TestEqueue(unittest.TestCase): ...@@ -58,6 +58,7 @@ class TestEqueue(unittest.TestCase):
self.equeue_server = EqueueServer(self.socket, equeue_options=self.options) self.equeue_server = EqueueServer(self.socket, equeue_options=self.options)
server_process = multiprocessing.Process(target=self.equeue_server.serve_forever) server_process = multiprocessing.Process(target=self.equeue_server.serve_forever)
server_process.start() server_process.start()
self.addCleanup(server_process.terminate)
self.assertTrue(os.path.exists(self.options.database)) self.assertTrue(os.path.exists(self.options.database))
self.assertTrue(os.path.exists(self.socket)) self.assertTrue(os.path.exists(self.socket))
...@@ -88,12 +89,12 @@ class TestEqueue(unittest.TestCase): ...@@ -88,12 +89,12 @@ class TestEqueue(unittest.TestCase):
with open(self.testfile, 'r') as fd: with open(self.testfile, 'r') as fd:
self.assertEqual(fd.read(), '2\n') self.assertEqual(fd.read(), '2\n')
server_process.terminate()
def test_doNotRunACommandIfItHasAlreadyRun(self): def test_doNotRunACommandIfItHasAlreadyRun(self):
self.equeue_server = EqueueServer(self.socket, equeue_options=self.options) self.equeue_server = EqueueServer(self.socket, equeue_options=self.options)
server_process = multiprocessing.Process(target=self.equeue_server.serve_forever) server_process = multiprocessing.Process(target=self.equeue_server.serve_forever)
server_process.start() server_process.start()
self.addCleanup(server_process.terminate)
self.assertTrue(os.path.exists(self.options.database)) self.assertTrue(os.path.exists(self.options.database))
self.assertTrue(os.path.exists(self.socket)) self.assertTrue(os.path.exists(self.socket))
...@@ -123,12 +124,12 @@ class TestEqueue(unittest.TestCase): ...@@ -123,12 +124,12 @@ class TestEqueue(unittest.TestCase):
with open(self.testfile, 'r') as fd: with open(self.testfile, 'r') as fd:
self.assertEqual(fd.read(), '1\n') self.assertEqual(fd.read(), '1\n')
server_process.terminate()
def test_doNothingIfTakeoverHasBeenTriggered(self): def test_doNothingIfTakeoverHasBeenTriggered(self):
self.equeue_server = EqueueServer(self.socket, equeue_options=self.options) self.equeue_server = EqueueServer(self.socket, equeue_options=self.options)
server_process = multiprocessing.Process(target=self.equeue_server.serve_forever) server_process = multiprocessing.Process(target=self.equeue_server.serve_forever)
server_process.start() server_process.start()
self.addCleanup(server_process.terminate)
self.assertTrue(os.path.exists(self.options.database)) self.assertTrue(os.path.exists(self.options.database))
self.assertTrue(os.path.exists(self.socket)) self.assertTrue(os.path.exists(self.socket))
...@@ -152,5 +153,3 @@ class TestEqueue(unittest.TestCase): ...@@ -152,5 +153,3 @@ class TestEqueue(unittest.TestCase):
time.sleep(2) time.sleep(2)
self.assertFalse(os.path.exists(self.testfile)) self.assertFalse(os.path.exists(self.testfile))
server_process.terminate()
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