summaryrefslogtreecommitdiffstats
path: root/tests/tests.py
blob: 469044235d1c205e7eb1cd6534ab71463942ef55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python
#
# Copyright (C) 2014  Simo Sorce <simo@redhat.com>
#
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import argparse
from datetime import datetime
import inspect
from ipsilon.util import plugin
import logging
import os
import sys
import subprocess
import traceback


logger = None


class Tests(object):

    def __init__(self):
        p = plugin.Plugins()
        (pathname, dummy) = os.path.split(inspect.getfile(Tests))
        self.plugins = p.get_plugins(pathname, 'IpsilonTest')


def parse_args():
    parser = argparse.ArgumentParser(description='Ipsilon Tests Environment')
    parser.add_argument('--path', default='%s/testdir' % os.getcwd(),
                        help="Directory in which tests are run")
    parser.add_argument('--test', default='test1',
                        help="The test to run")
    parser.add_argument('--wrappers', default='auto',
                        choices=['yes', 'no', 'auto'],
                        help="Run the tests with socket wrappers")

    return vars(parser.parse_args())


def openlogs(path, name):
    global logger  # pylint: disable=W0603
    logger = logging.getLogger()
    try:
        datestr = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
        filename = '%s/test-%s-%s.log' % (path, name, datestr)
        lh = logging.FileHandler(filename)
    except IOError, e:
        print >> sys.stderr, 'Unable to open %s (%s)' % (filename, str(e))
        lh = logging.StreamHandler(sys.stderr)
    formatter = logging.Formatter('[%(asctime)s] %(message)s')
    lh.setFormatter(formatter)
    logger.addHandler(lh)
    logger.setLevel(logging.DEBUG)


def try_wrappers(base, wrappers):
    if wrappers == 'no':
        return {}

    pkgcfg = subprocess.Popen(['pkg-config', '--exists', 'socket_wrapper'])
    pkgcfg.wait()
    if pkgcfg.returncode != 0:
        if wrappers == 'auto':
            return {}
        else:
            raise ValueError('Socket Wrappers not available')

    wrapdir = os.path.join(base, 'wrapdir')
    os.mkdir(wrapdir)

    wenv = {'LD_PRELOAD': 'libsocket_wrapper.so',
            'SOCKET_WRAPPER_DIR': wrapdir,
            'SOCKET_WRAPPER_DEFAULT_IFACE': '9'}

    return wenv


if __name__ == '__main__':

    args = parse_args()

    tests = Tests()
    if args['test'] not in tests.plugins:
        print >> sys.stderr, "Unknown test [%s]" % args['test']
        sys.exit(1)
    test = tests.plugins[args['test']]

    if not os.path.exists(args['path']):
        os.makedirs(args['path'])

    openlogs(args['path'], args['test'])

    test.setup_base(args['path'], test)

    env = try_wrappers(test.testdir, args['wrappers'])
    env['PYTHONPATH'] = test.rootdir

    try:
        test.setup_servers(env)

        code = test.run(env)
        if code:
            sys.exit(code)
    except Exception, e:  # pylint: disable=broad-except
        print >> sys.stderr, "Error: %s" % repr(e)
        traceback.print_exc(None, sys.stderr)
        sys.exit(1)
    finally:
        test.wait()

    print "FINISHED"