Fix lp-test target compaints
[cascardo/ipsilon.git] / tests / tests.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import argparse
21 import inspect
22 from ipsilon.util import plugin
23 import os
24 import sys
25 import subprocess
26 import traceback
27
28
29 logger = None
30
31
32 class Tests(object):
33
34     def __init__(self):
35         p = plugin.Plugins()
36         (pathname, dummy) = os.path.split(inspect.getfile(Tests))
37         self.plugins = p.get_plugins(pathname, 'IpsilonTest')
38
39
40 def parse_args():
41     parser = argparse.ArgumentParser(description='Ipsilon Tests Environment')
42     parser.add_argument('--path', default='%s/testdir' % os.getcwd(),
43                         help="Directory in which tests are run")
44     parser.add_argument('--test', default='test1',
45                         help="The test to run")
46     parser.add_argument('--wrappers', default='auto',
47                         choices=['yes', 'no', 'auto'],
48                         help="Run the tests with socket wrappers")
49
50     return vars(parser.parse_args())
51
52
53 def try_wrappers(base, wrappers):
54     if wrappers == 'no':
55         return {}
56
57     pkgcfg = subprocess.Popen(['pkg-config', '--exists', 'socket_wrapper'])
58     pkgcfg.wait()
59     if pkgcfg.returncode != 0:
60         if wrappers == 'auto':
61             return {}
62         else:
63             raise ValueError('Socket Wrappers not available')
64
65     wrapdir = os.path.join(base, 'wrapdir')
66     os.mkdir(wrapdir)
67
68     wenv = {'LD_PRELOAD': 'libsocket_wrapper.so',
69             'SOCKET_WRAPPER_DIR': wrapdir,
70             'SOCKET_WRAPPER_DEFAULT_IFACE': '9'}
71
72     return wenv
73
74
75 if __name__ == '__main__':
76
77     args = parse_args()
78
79     tests = Tests()
80     if args['test'] not in tests.plugins:
81         print >> sys.stderr, "Unknown test [%s]" % args['test']
82         sys.exit(1)
83     test = tests.plugins[args['test']]
84
85     if not os.path.exists(args['path']):
86         os.makedirs(args['path'])
87
88     test.setup_base(args['path'], test)
89
90     env = try_wrappers(test.testdir, args['wrappers'])
91     env['PYTHONPATH'] = test.rootdir
92
93     try:
94         test.setup_servers(env)
95
96         code = test.run(env)
97         if code:
98             sys.exit(code)
99     except Exception, e:  # pylint: disable=broad-except
100         print >> sys.stderr, "Error: %s" % repr(e)
101         traceback.print_exc(None, sys.stderr)
102         sys.exit(1)
103     finally:
104         test.wait()
105
106     print "FINISHED"