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

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

Added TestCase? that checks private_auth initialisation and finalisation routines
happen.

  • 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
12import re
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    def testStartupWithSyslog(self):
41        try:
42            self._startServer()
43            pid = self._getServerPids()[0]
44            self._stopServer(pid)
45
46            lines = self._readSyslog()
47            self.assert_(len(lines) == 2)
48            self.assert_(re.search('initialising private auth', lines[0]))
49            self.assert_(re.search('finalising private auth', lines[1]))
50
51        except Exception, e:
52            self.fail(e)
53           
54
55    #------------------------------------------------------------------------------
56           
57    def _getPaths(self):
58        """Find the server executable and any other important files.
59        """
60        self.HOME = os.getenv('NDG_DELIVERY_HOME', os.curdir)
61        try:
62            self.BBFTPD = glob('%s/src/bbftp-server*/bbftpd/bbftpd' % self.HOME)[0]
63        except IndexError:
64            raise RuntimeError, 'Cannot find bbftpd executable'
65
66    def _startServer(self):
67        os.system('%s -b -l INFORMATION' % self.BBFTPD)
68
69    def _getServerPids(self):
70        pids = os.popen('ps -C bbftpd -o %p --no-headers').readlines()
71        return [int(x.strip()) for x in pids]
72
73    def _stopServer(self, pid):
74        os.kill(pid, signal.SIGTERM)
75        # Wait upto 5 seconds for the server to stop
76        for x in range(10):
77            if pid not in self._getServerPids():
78                return
79            time.sleep(0.5)
80        raise RuntimeError, 'Failed to stop server'
81
82    def _readSyslog(self, logfile="/var/log/messages"):
83        """Get all bbftpd messages from syslog for this test case.
84
85        @note: This requires read access to the logfile
86        """
87
88        # Read the logfile into a buffer
89        log = open(logfile).readlines()
90
91        # Find the latest testcase marker
92        start_i = 0
93        for i in xrange(len(log)-1, -1, -1):
94            if re.search('test_bbftpd.py: Starting ExtendedAPITestCase', log[i]):
95                start_i = i+1
96                break
97        if not start_i:
98            raise RuntimeError, "Can't find test case entry in syslog"
99
100        filtered_log = []
101        for line in log[start_i:]:
102            if re.search(r'test_bbftpd.py:|bbftpd .*:', line):
103                filtered_log.append(line)
104
105        return filtered_log
106
107
108
109if __name__ == '__main__':
110    unittest.main()
Note: See TracBrowser for help on using the repository browser.