source: TI12-security/trunk/python/ndg_security_test/ndg/security/test/unit/__init__.py @ 5774

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg_security_test/ndg/security/test/unit/__init__.py@5774
Revision 5774, 2.9 KB checked in by pjkersha, 11 years ago (diff)

Added a wrapper class to Paste Deploy httpserver to enable automated setup and teardown of services for unit tests.

Line 
1"""NDG Security unit test package
2
3NERC Data Grid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "14/05/09"
7__copyright__ = "(C) 2009 Science and Technology Facilities Council"
8__license__ = "BSD - see LICENSE file in top-level directory"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__revision__ = '$Id: __init__.py 4840 2009-01-19 13:59:08Z pjkersha $'
11
12import unittest
13import logging
14import socket
15logging.basicConfig()
16
17import os
18from os.path import expandvars, join, dirname, abspath
19
20TEST_CONFIG_DIR = join(abspath(dirname(dirname(__file__))), 'config')
21
22mkDataDirPath = lambda file:join(TEST_CONFIG_DIR, file)
23
24from ndg.security.test.unit.wsgi import PasteDeployAppServer
25
26class BaseTestCase(unittest.TestCase):
27    '''Convenience base class from which other unit tests can extend.  Its
28    sets the generic data directory path'''
29    configDirEnvVarName = 'NDGSEC_TEST_CONFIG_DIR'
30   
31    mkDataDirPath = staticmethod(mkDataDirPath)
32   
33    def __init__(self, *arg, **kw):
34        if BaseTestCase.configDirEnvVarName not in os.environ:
35            os.environ[BaseTestCase.configDirEnvVarName] = TEST_CONFIG_DIR
36               
37        unittest.TestCase.__init__(self, *arg, **kw)
38        self.services = []
39       
40    def addService(self, cfgFilePath, port):
41        """Utility for setting up threads to run Paste HTTP based services with
42        unit tests
43       
44        @param cfgFilePath: ini file containing configuration for the service
45        @type cfgFilePath: basestring
46        @param port: port number to run the service from
47        @type port: int
48        """
49        try:
50            self.services.append(PasteDeployAppServer(cfgFilePath, port=port))
51            self.services[-1].startThread()
52           
53        except socket.error:
54            pass
55
56    def startAttributeAuthorities(self):
57        """Serve test Attribute Authorities to test against"""
58        siteACfgFilePath = mkDataDirPath(join('attributeauthority', 
59                                              'sitea', 
60                                              'site-a.ini'))
61        self.addService(siteACfgFilePath, 5000)
62       
63        siteBCfgFilePath = mkDataDirPath(join('attributeauthority',
64                                              'siteb', 
65                                              'site-b.ini'))
66        self.addService(siteBCfgFilePath, 5100)       
67       
68
69    def __del__(self):
70        """Stop any services started with the addService method"""
71        if hasattr(self, 'services'):
72            for service in self.services:
73                service.terminateThread()
74           
75def _getParentDir(depth=0, path=dirname(__file__)):
76    """
77    @type path: basestring
78    @param path: directory path from which to get parent directory, defaults
79    to dir of this module
80    @rtype: basestring
81    @return: parent directory at depth levels up from the current path
82    """
83    for i in range(depth):
84        path = dirname(path)
85    return path
86
87
Note: See TracBrowser for help on using the repository browser.