Remove useless log file
[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 from datetime import datetime
22 import inspect
23 from ipsilon.util import plugin
24 import logging
25 import os
26 import sys
27 import subprocess
28 import traceback
29
30
31 logger = None
32
33
34 class Tests(object):
35
36     def __init__(self):
37         p = plugin.Plugins()
38         (pathname, dummy) = os.path.split(inspect.getfile(Tests))
39         self.plugins = p.get_plugins(pathname, 'IpsilonTest')
40
41
42 def parse_args():
43     parser = argparse.ArgumentParser(description='Ipsilon Tests Environment')
44     parser.add_argument('--path', default='%s/testdir' % os.getcwd(),
45                         help="Directory in which tests are run")
46     parser.add_argument('--test', default='test1',
47                         help="The test to run")
48     parser.add_argument('--wrappers', default='auto',
49                         choices=['yes', 'no', 'auto'],
50                         help="Run the tests with socket wrappers")
51
52     return vars(parser.parse_args())
53
54
55 def try_wrappers(base, wrappers):
56     if wrappers == 'no':
57         return {}
58
59     pkgcfg = subprocess.Popen(['pkg-config', '--exists', 'socket_wrapper'])
60     pkgcfg.wait()
61     if pkgcfg.returncode != 0:
62         if wrappers == 'auto':
63             return {}
64         else:
65             raise ValueError('Socket Wrappers not available')
66
67     wrapdir = os.path.join(base, 'wrapdir')
68     os.mkdir(wrapdir)
69
70     wenv = {'LD_PRELOAD': 'libsocket_wrapper.so',
71             'SOCKET_WRAPPER_DIR': wrapdir,
72             'SOCKET_WRAPPER_DEFAULT_IFACE': '9'}
73
74     return wenv
75
76
77 if __name__ == '__main__':
78
79     args = parse_args()
80
81     tests = Tests()
82     if args['test'] not in tests.plugins:
83         print >> sys.stderr, "Unknown test [%s]" % args['test']
84         sys.exit(1)
85     test = tests.plugins[args['test']]
86
87     if not os.path.exists(args['path']):
88         os.makedirs(args['path'])
89
90     test.setup_base(args['path'], test)
91
92     env = try_wrappers(test.testdir, args['wrappers'])
93     env['PYTHONPATH'] = test.rootdir
94
95     try:
96         test.setup_servers(env)
97
98         code = test.run(env)
99         if code:
100             sys.exit(code)
101     except Exception, e:  # pylint: disable=broad-except
102         print >> sys.stderr, "Error: %s" % repr(e)
103         traceback.print_exc(None, sys.stderr)
104         sys.exit(1)
105     finally:
106         test.wait()
107
108     print "FINISHED"