runner 12 KB
Newer Older
1
#! /usr/bin/env python
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# Copyright (C) 2009  Nexedi SA
# 
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

19
import traceback
20
import unittest
21
import tempfile
22 23
import logging
import time
Grégory Wisniewski's avatar
Grégory Wisniewski committed
24
import sys
25
import neo
26
import os
27

28 29
from neo.tests.benchmark import BenchmarkRunner

30 31
# list of test modules
# each of them have to import its TestCase classes
32
UNIT_TEST_MODULES = [ 
33 34 35 36 37 38 39
    # generic parts
    'neo.tests.testBootstrap',
    'neo.tests.testConnection',
    'neo.tests.testEvent',
    'neo.tests.testHandler',
    'neo.tests.testNodes',
    'neo.tests.testProtocol',
40
    'neo.tests.testDispatcher',
41
    'neo.tests.testUtil',
42 43 44 45 46 47
    'neo.tests.testPT',
    # master application
    'neo.tests.master.testClientHandler',
    'neo.tests.master.testElectionHandler',
    'neo.tests.master.testMasterApp',
    'neo.tests.master.testMasterPT',
48
    'neo.tests.master.testRecovery',
49
    'neo.tests.master.testStorageHandler',
50
    'neo.tests.master.testVerification',
51
    'neo.tests.master.testTransactions',
52 53 54 55 56 57 58
    # storage application
    'neo.tests.storage.testClientHandler',
    'neo.tests.storage.testInitializationHandler',
    'neo.tests.storage.testMasterHandler',
    'neo.tests.storage.testStorageApp',
    'neo.tests.storage.testStorageHandler',
    'neo.tests.storage.testStorageMySQLdb',
59
    'neo.tests.storage.testStorageBTree',
60
    'neo.tests.storage.testVerificationHandler',
61
    'neo.tests.storage.testIdentificationHandler',
62
    'neo.tests.storage.testTransactions',
63 64
    'neo.tests.storage.testReplicationHandler',
    'neo.tests.storage.testReplicator',
65
    'neo.tests.storage.testReplication',
66 67
    # client application
    'neo.tests.client.testClientApp',
68 69
    'neo.tests.client.testMasterHandler',
    'neo.tests.client.testStorageHandler',
70
    'neo.tests.client.testConnectionPool',
71 72
]

73
FUNC_TEST_MODULES = [
74
    'neo.tests.functional.testMaster',
75
    'neo.tests.functional.testClient',
76
    'neo.tests.functional.testCluster',
77
    'neo.tests.functional.testStorage',
78 79
]

80
ZODB_TEST_MODULES = [
81 82 83 84 85
    ('neo.tests.zodb.testBasic', 'check'),
    ('neo.tests.zodb.testConflict', 'check'),
    ('neo.tests.zodb.testHistory', 'check'),
    ('neo.tests.zodb.testIterator', 'check'),
    ('neo.tests.zodb.testMT', 'check'),
86
    ('neo.tests.zodb.testPack', 'check'),
87 88 89
    ('neo.tests.zodb.testPersistent', 'check'),
    ('neo.tests.zodb.testReadOnly', 'check'),
    ('neo.tests.zodb.testRevision', 'check'),
90
    #('neo.tests.zodb.testRecovery', 'check'),
91 92 93
    ('neo.tests.zodb.testSynchronization', 'check'),
    # ('neo.tests.zodb.testVersion', 'check'),
    ('neo.tests.zodb.testUndo', 'check'),
94 95 96
    ('neo.tests.zodb.testZODB', 'check'),
]

97
# configuration 
98 99
ATTACH_LOG = False # for ZODB test, only the client side is logged
LOG_FILE = 'neo.log' 
100 101

# override logging configuration to send all messages to a file
102 103
for logger_name in ('NEO', 'CLIENT'):
    neo.setupLog(logger_name, filename=LOG_FILE)
104

105
class NeoTestRunner(unittest.TestResult):
106 107
    """ Custom result class to build report with statistics per module """

108
    def __init__(self, title, masters, storages, replicas, partitions):
109
        unittest.TestResult.__init__(self)
110
        self._title = title
111
        self.modulesStats = {}
112
        self.failedImports = {}
113
        self.lastStart = None
114 115 116 117
        self.masters = masters
        self.storages = storages
        self.replicas = replicas
        self.partitions = partitions
118 119
        self.temp_directory = tempfile.mkdtemp(prefix='neo_')
        os.environ['TEMP'] = self.temp_directory
120 121 122 123
        os.environ['ZODB_MASTERS'] = str(masters)
        os.environ['ZODB_STORAGES'] = str(storages)
        os.environ['ZODB_REPLICAS'] = str(replicas)
        os.environ['ZODB_PARTITIONS'] = str(partitions)
124
        print "Base directory : %s" % (self.temp_directory, )
125

126
    def run(self, name, modules):
127
        print '\n', name
128 129 130
        suite = unittest.TestSuite()
        loader = unittest.defaultTestLoader
        for test_module in modules:
131 132 133 134 135 136
            # load prefix if supplied
            if isinstance(test_module, tuple):
                test_module, prefix = test_module
                loader.testMethodPrefix = prefix
            else:
                loader.testMethodPrefix = 'test'
137 138 139
            try:
                test_module = __import__(test_module, globals(), locals(), ['*'])
            except ImportError, err:
140
                self.failedImports[test_module] = err
141
                print "Import of %s failed : %s" % (test_module, err)
142
                traceback.print_exc()
143 144 145 146
                continue
            suite.addTests(loader.loadTestsFromModule(test_module))
        suite.run(self)

147
    class ModuleStats(object):
148
        run = 0
149 150 151 152 153 154
        errors = 0
        success = 0
        failures = 0
        time = 0.0

    def _getModuleStats(self, test):
155 156
        module = test.__class__.__module__
        module = tuple(module.split('.'))
157 158 159 160 161 162 163 164 165 166 167
        try:
            return self.modulesStats[module] 
        except KeyError:
            self.modulesStats[module] = self.ModuleStats()
            return self.modulesStats[module]

    def _updateTimer(self, stats):
        stats.time += time.time() - self.lastStart

    def startTest(self, test):
        unittest.TestResult.startTest(self, test)
168
        logging.info(" * TEST %s", test)
169 170
        stats = self._getModuleStats(test)
        stats.run += 1
171 172 173
        self.lastStart = time.time()

    def addSuccess(self, test):
174
        print "OK"
175 176 177 178 179 180
        unittest.TestResult.addSuccess(self, test)
        stats = self._getModuleStats(test)
        stats.success += 1
        self._updateTimer(stats)

    def addError(self, test, err):
181
        print "ERROR"
182 183 184 185 186 187
        unittest.TestResult.addError(self, test, err)
        stats = self._getModuleStats(test)
        stats.errors += 1
        self._updateTimer(stats)

    def addFailure(self, test, err):
188
        print "FAIL"
189 190 191 192 193
        unittest.TestResult.addFailure(self, test, err)
        stats = self._getModuleStats(test)
        stats.failures += 1
        self._updateTimer(stats)

194
    def _buildSummary(self, add_status):
195
        success = self.testsRun - len(self.errors) - len(self.failures)
196
        add_status('Directory', self.temp_directory)
197 198 199 200
        add_status('Masters', self.masters)
        add_status('Storages', self.storages)
        add_status('Replicas', self.replicas)
        add_status('Partitions', self.partitions)
201 202
        add_status('Status', '%.3f%%' % (success * 100.0 / self.testsRun))
        # visual
203 204 205 206 207
        header       = "%25s |   run   | success |  errors |  fails  |   time   \n" % 'Test Module'
        separator    = "%25s-+---------+---------+---------+---------+----------\n" % ('-' * 25)
        format       = "%25s |   %3s   |   %3s   |   %3s   |   %3s   | %6.2fs   \n"
        group_f      = "%25s |         |         |         |         |          \n" 
        # header
208
        s = ' ' * 30 + ' NEO TESTS REPORT'
209
        s += '\n'
210 211 212 213 214 215 216 217 218 219 220 221
        s += '\n' + header + separator
        group = None
        t_success = 0
        # for each test case
        for k, v in sorted(self.modulesStats.items()):
            # display group below its content
            _group = '.'.join(k[:-1])
            if group is None:
                group = _group
            if _group != group:
                s += separator + group_f % group + separator
                group = _group
222 223 224 225 226 227 228
            # test case stats
            t_success += v.success
            run, success = v.run or '.', v.success or '.'
            errors, failures = v.errors or '.', v.failures or '.'
            name = k[-1].lstrip('test')
            args = (name, run, success, errors, failures, v.time)
            s += format % args
229 230 231
        # the last group
        s += separator  + group_f % group + separator
        # the final summary
232
        errors, failures = len(self.errors) or '.', len(self.failures) or '.'
233 234
        args = ("Summary", self.testsRun, t_success, errors, failures, self.time)
        s += format % args + separator + '\n'
235 236 237
        return s

    def _buildErrors(self):
238
        s = ''
239
        test_formatter = lambda t: t.id()
240 241 242
        if len(self.errors):
            s += '\nERRORS:\n'
            for test, trace in self.errors:
243
                s += "%s\n" % test_formatter(test)
244
                s += "-------------------------------------------------------------\n"
245
                s += trace
246
                s += "-------------------------------------------------------------\n"
247 248 249 250
                s += '\n'
        if len(self.failures):
            s += '\nFAILURES:\n'
            for test, trace in self.failures:
251
                s += "%s\n" % test_formatter(test)
252
                s += "-------------------------------------------------------------\n"
253
                s += trace
254
                s += "-------------------------------------------------------------\n"
255 256 257
                s += '\n'
        return s

258 259 260 261 262 263 264 265 266
    def _buildWarnings(self):
        s = '\n'
        if self.failedImports:
            s += 'Failed imports :\n'
            for module, err in self.failedImports.items():
                s += '%s:\n%s' % (module, err)
        s += '\n'
        return s

267
    def buildReport(self, add_status):
268
        self.time = sum([s.time for s in self.modulesStats.values()])
269
        self.subject = "%s Tests, %s Errors, %s Failures" % (
270
            self.testsRun, len(self.errors), len(self.failures))
271 272 273 274 275 276 277 278 279 280 281 282
        summary = self._buildSummary(add_status)
        errors = self._buildErrors()
        warnings = self._buildWarnings()
        report = '\n'.join([summary, errors, warnings])
        return (self.subject, report)

class TestRunner(BenchmarkRunner):

    def add_options(self, parser):
        parser.add_option('-f', '--functional', action='store_true')
        parser.add_option('-u', '--unit', action='store_true')
        parser.add_option('-z', '--zodb', action='store_true')
283 284 285 286
        parser.add_option('', '--zodb-masters')
        parser.add_option('', '--zodb-storages')
        parser.add_option('', '--zodb-replicas')
        parser.add_option('', '--zodb-partitions')
287 288 289 290 291 292 293 294

    def load_options(self, options, args):
        if not (options.unit or options.functional or options.zodb or args):
            sys.exit('Nothing to run, please give one of -f, -u, -z')
        return dict(
            unit = options.unit,
            functional = options.functional,
            zodb = options.zodb,
295 296 297 298
            zodb_masters = int(options.zodb_masters or 1),
            zodb_storages = int(options.zodb_storages or 1),
            zodb_replicas = int(options.zodb_replicas or 0),
            zodb_partitions = int(options.zodb_partitions or 1),
299
        )
300

301 302 303
    def start(self):
        config = self._config
        # run requested tests
304 305 306 307 308 309 310
        runner = NeoTestRunner(
            title=config.title or 'Neo',
            masters=config.zodb_masters,
            storages=config.zodb_storages,
            replicas=config.zodb_replicas,
            partitions=config.zodb_partitions,
        )
311 312 313 314 315 316 317 318 319 320 321 322 323
        try:
            if config.unit:
                runner.run('Unit tests', UNIT_TEST_MODULES)
            if config.functional:
                runner.run('Functional tests', FUNC_TEST_MODULES)
            if config.zodb:
                runner.run('ZODB tests', ZODB_TEST_MODULES)
        except KeyboardInterrupt:
            config['mail_to'] = None
            traceback.print_exc()
        # build report
        self._successful = runner.wasSuccessful()
        return runner.buildReport(self.add_status)
324 325

if __name__ == "__main__":
326 327 328
    runner = TestRunner()
    runner.run()
    if not runner.was_successful():
329 330
        sys.exit(1)
    sys.exit(0)