source: TI05-delivery/trunk/test/test_bbftpd.py @ 974

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/test/test_bbftpd.py@974
Revision 974, 2.0 KB checked in by spascoe, 14 years ago (diff)

Started test framework. One unit test exists to start and stop the server.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2"""
3Tests for the bbftp daemon.
4
5@author: Stephen Pascoe
6@version: $Id$
7"""
8
9import unittest
10import os, signal, time, syslog
11from glob import glob
12
13
14class ExtendedAPITestCase(unittest.TestCase):
15    """Test the basic bbftp private authentication API with stub extensions.
16    """
17
18    def setUp(self):
19        self._getPaths()
20        # Check bbftpd isn't running
21        if self._getServerPids():
22            raise RuntimeError, 'A bbftpd process is already running'
23
24        # We want to mark the beginning of this test case in syslog
25        syslog.openlog('test_bbftpd.py')
26        syslog.syslog(syslog.LOG_INFO, 'Starting ExtendedAPITestCase')
27       
28    def tearDown(self):
29        syslog.syslog(syslog.LOG_INFO, 'Ended ExtendedAPITestCase')
30        syslog.closelog()
31
32    def testStartup(self):
33        try:
34            self._startServer()
35            pid = self._getServerPids()[0]
36            self._stopServer(pid)
37        except Exception, e:
38            self.fail(e)
39
40    #------------------------------------------------------------------------------
41           
42    def _getPaths(self):
43        """Find the server executable and any other important files.
44        """
45        self.HOME = os.getenv('NDG_DELIVERY_HOME', os.curdir)
46        try:
47            self.BBFTPD = glob('%s/src/bbftp-server*/bbftpd/bbftpd' % self.HOME)[0]
48        except IndexError:
49            raise RuntimeError, 'Cannot find bbftpd executable'
50
51    def _startServer(self):
52        os.system('%s -b -l INFORMATION' % self.BBFTPD)
53
54    def _getServerPids(self):
55        pids = os.popen('ps -C bbftpd -o %p --no-headers').readlines()
56        return [int(x.strip()) for x in pids]
57
58    def _stopServer(self, pid):
59        os.kill(pid, signal.SIGTERM)
60        # Wait upto 5 seconds for the server to stop
61        for x in range(10):
62            if pid not in self._getServerPids():
63                return
64            time.sleep(0.5)
65        raise RuntimeError, 'Failed to stop server'
66
67
68if __name__ == '__main__':
69    unittest.main()
Note: See TracBrowser for help on using the repository browser.