2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
""" Remote session base class
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import py
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
|
|
|
|
from py.__.test.rsession import report
|
|
|
|
from py.__.test.rsession.master import \
|
|
|
|
setup_slave, MasterNode, dispatch_loop, itemgen, randomgen
|
2007-01-25 00:46:46 +08:00
|
|
|
from py.__.test.rsession.hostmanage import HostInfo, HostOptions, HostManager
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
from py.__.test.rsession.local import local_loop, plain_runner, apigen_runner,\
|
2007-01-25 00:46:46 +08:00
|
|
|
box_runner
|
2007-01-24 22:24:01 +08:00
|
|
|
from py.__.test.rsession.reporter import LocalReporter, RemoteReporter
|
|
|
|
|
|
|
|
class AbstractSession(object):
|
|
|
|
"""
|
|
|
|
An abstract session executes collectors/items through a runner.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def __init__(self, config, optimise_localhost=True):
|
|
|
|
self.config = config
|
|
|
|
self.optimise_localhost = optimise_localhost
|
|
|
|
|
|
|
|
def getpkgdir(path):
|
|
|
|
path = py.path.local(path)
|
|
|
|
pkgpath = path.pypkgpath()
|
|
|
|
if pkgpath is None:
|
|
|
|
pkgpath = path
|
|
|
|
return pkgpath
|
|
|
|
getpkgdir = staticmethod(getpkgdir)
|
|
|
|
|
|
|
|
def init_reporter(self, reporter, sshhosts, reporter_class, arg=""):
|
2007-01-25 00:46:46 +08:00
|
|
|
""" This initialises so called `reporter` class, which will
|
|
|
|
handle all event presenting to user. Does not get called
|
|
|
|
if main received custom reporter
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
startserverflag = self.config.option.startserver
|
|
|
|
restflag = self.config.option.restreport
|
|
|
|
|
|
|
|
if startserverflag and reporter is None:
|
|
|
|
from py.__.test.rsession.web import start_server, exported_methods
|
2007-01-25 00:46:46 +08:00
|
|
|
if self.config.option.runbrowser:
|
|
|
|
from socket import INADDR_ANY
|
|
|
|
port = INADDR_ANY # pick a random port when starting browser
|
|
|
|
else:
|
|
|
|
port = 8000 # stick to a fixed port otherwise
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
reporter = exported_methods.report
|
2007-01-25 00:46:46 +08:00
|
|
|
httpd = start_server(server_address = ('', port))
|
|
|
|
port = httpd.server_port
|
2007-01-24 22:24:01 +08:00
|
|
|
if self.config.option.runbrowser:
|
2007-01-25 00:46:46 +08:00
|
|
|
import webbrowser, thread
|
|
|
|
# webbrowser.open() may block until the browser finishes or not
|
|
|
|
url = "http://localhost:%d" % (port,)
|
|
|
|
thread.start_new_thread(webbrowser.open, (url,))
|
2007-01-24 22:24:01 +08:00
|
|
|
elif reporter is None:
|
|
|
|
if restflag:
|
|
|
|
from py.__.test.rsession.rest import RestReporter
|
|
|
|
reporter_class = RestReporter
|
|
|
|
if arg:
|
2007-01-26 19:49:59 +08:00
|
|
|
reporter_instance = reporter_class(self.config, sshhosts)
|
2007-01-24 22:24:01 +08:00
|
|
|
else:
|
|
|
|
reporter_instance = reporter_class(self.config, sshhosts)
|
|
|
|
reporter = reporter_instance.report
|
|
|
|
else:
|
|
|
|
startserverflag = False
|
|
|
|
|
|
|
|
return reporter, startserverflag
|
|
|
|
|
|
|
|
def reporterror(reporter, data):
|
|
|
|
excinfo, item = data
|
|
|
|
if excinfo is None:
|
|
|
|
reporter(report.ItemStart(item))
|
|
|
|
elif excinfo.type is py.test.Item.Skipped:
|
|
|
|
reporter(report.SkippedTryiter(excinfo, item))
|
|
|
|
else:
|
|
|
|
reporter(report.FailedTryiter(excinfo, item))
|
|
|
|
reporterror = staticmethod(reporterror)
|
|
|
|
|
|
|
|
def kill_server(self, startserverflag):
|
2007-01-25 00:46:46 +08:00
|
|
|
""" Kill web server
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
if startserverflag:
|
|
|
|
from py.__.test.rsession.web import kill_server
|
|
|
|
kill_server()
|
|
|
|
|
|
|
|
def wrap_reporter(self, reporter):
|
|
|
|
""" We wrap reporter around, which makes it possible to us to track
|
|
|
|
number of failures
|
|
|
|
"""
|
|
|
|
self.was_failure = False
|
|
|
|
def new_reporter(event):
|
|
|
|
if isinstance(event, report.ReceivedItemOutcome) and \
|
|
|
|
not event.outcome.passed and \
|
|
|
|
not event.outcome.skipped:
|
|
|
|
self.was_failure = True
|
|
|
|
return reporter(event)
|
|
|
|
checkfun = lambda : self.config.option.exitfirst and \
|
|
|
|
self.was_failure
|
|
|
|
# for tests
|
|
|
|
self.checkfun = checkfun
|
|
|
|
return new_reporter, checkfun
|
|
|
|
|
|
|
|
def parse_directories(sshhosts):
|
2007-01-25 00:46:46 +08:00
|
|
|
""" Parse sshadresses of hosts to have distinct hostname/hostdir
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
directories = {}
|
|
|
|
for host in sshhosts:
|
|
|
|
m = re.match("^(.*?):(.*)$", host.hostname)
|
|
|
|
if m:
|
|
|
|
host.hostname = m.group(1)
|
|
|
|
host.relpath = m.group(2) + "-" + host.hostname
|
|
|
|
else:
|
|
|
|
host.relpath = "pytestcache-%s" % host.hostname
|
|
|
|
|
|
|
|
class RSession(AbstractSession):
|
|
|
|
""" Remote version of session
|
|
|
|
"""
|
2007-01-25 05:05:33 +08:00
|
|
|
def fixoptions(self):
|
|
|
|
config = self.config
|
|
|
|
try:
|
|
|
|
config.getvalue('disthosts')
|
|
|
|
except KeyError:
|
|
|
|
print "You're trying to run RSession without disthosts specified"
|
|
|
|
print "you need to specify it in your conftest.py (ie. ~/conftest.py)"
|
|
|
|
print "for example:"
|
|
|
|
print " disthosts = ['localhost'] * 4 # for 3 processors"
|
|
|
|
print " - or -"
|
|
|
|
print " disthosts = ['you@some.remote.com'] # for remote testing"
|
|
|
|
print " # with your remote ssh account"
|
|
|
|
print "http://codespeak.net/py/current/doc/test.html#automated-distributed-testing"
|
|
|
|
print " for more info..."
|
|
|
|
raise SystemExit
|
|
|
|
|
2007-01-24 22:24:01 +08:00
|
|
|
def main(self, reporter=None):
|
|
|
|
""" main loop for running tests. """
|
2007-01-25 00:46:46 +08:00
|
|
|
args = self.config.args
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
sshhosts, remotepython, rsync_roots = self.read_distributed_config()
|
|
|
|
reporter, startserverflag = self.init_reporter(reporter,
|
|
|
|
sshhosts, RemoteReporter)
|
|
|
|
reporter, checkfun = self.wrap_reporter(reporter)
|
|
|
|
|
|
|
|
reporter(report.TestStarted(sshhosts))
|
|
|
|
|
|
|
|
done_dict = {}
|
2007-01-25 00:46:46 +08:00
|
|
|
hostopts = HostOptions(rsync_roots=rsync_roots,
|
|
|
|
remote_python=remotepython,
|
|
|
|
optimise_localhost=self.optimise_localhost)
|
2007-01-26 19:49:59 +08:00
|
|
|
hostmanager = HostManager(sshhosts, self.config, hostopts)
|
2007-01-24 22:24:01 +08:00
|
|
|
try:
|
2007-01-25 00:46:46 +08:00
|
|
|
nodes = hostmanager.init_hosts(reporter, done_dict)
|
|
|
|
reporter(report.RsyncFinished())
|
|
|
|
try:
|
2007-01-26 19:49:59 +08:00
|
|
|
self.dispatch_tests(nodes, reporter, checkfun, done_dict)
|
2007-01-25 00:46:46 +08:00
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
print >>sys.stderr, "C-c pressed waiting for gateways to teardown..."
|
|
|
|
channels = [node.channel for node in nodes]
|
|
|
|
hostmanager.kill_channels(channels)
|
|
|
|
hostmanager.teardown_gateways(reporter, channels)
|
|
|
|
print >>sys.stderr, "... Done"
|
|
|
|
raise
|
|
|
|
|
|
|
|
channels = [node.channel for node in nodes]
|
|
|
|
hostmanager.teardown_hosts(reporter, channels, nodes,
|
2007-01-24 22:24:01 +08:00
|
|
|
exitfirst=self.config.option.exitfirst)
|
2007-01-25 00:46:46 +08:00
|
|
|
reporter(report.Nodes(nodes))
|
|
|
|
retval = reporter(report.TestFinished())
|
|
|
|
self.kill_server(startserverflag)
|
|
|
|
return retval
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
reporter(report.InterruptedExecution())
|
|
|
|
self.kill_server(startserverflag)
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
reporter(report.CrashedExecution())
|
|
|
|
self.kill_server(startserverflag)
|
|
|
|
raise
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def read_distributed_config(self):
|
2007-01-25 00:46:46 +08:00
|
|
|
""" Read from conftest file the configuration of distributed testing
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
try:
|
|
|
|
rsync_roots = self.config.getvalue("distrsync_roots")
|
|
|
|
except:
|
|
|
|
rsync_roots = None # all files and directories in the pkgdir
|
|
|
|
sshhosts = [HostInfo(i) for i in
|
|
|
|
self.config.getvalue("disthosts")]
|
|
|
|
parse_directories(sshhosts)
|
2007-01-25 00:46:46 +08:00
|
|
|
remotepython = self.config.getvalue("dist_remotepython")
|
2007-01-24 22:24:01 +08:00
|
|
|
return sshhosts, remotepython, rsync_roots
|
|
|
|
|
2007-01-26 19:49:59 +08:00
|
|
|
def dispatch_tests(self, nodes, reporter, checkfun, done_dict):
|
|
|
|
colitems = self.config.getcolitems()
|
2007-01-24 22:24:01 +08:00
|
|
|
keyword = self.config.option.keyword
|
|
|
|
itemgenerator = itemgen(colitems, reporter, keyword, self.reporterror)
|
|
|
|
|
|
|
|
all_tests = dispatch_loop(nodes, itemgenerator, checkfun)
|
|
|
|
#if all_tests:
|
|
|
|
# todo = {}
|
|
|
|
# for key, value in all_tests.items():
|
|
|
|
# if key not in done_dict:
|
|
|
|
# todo[key] = True
|
|
|
|
# rg = randomgen(todo, done_dict)
|
|
|
|
# dispatch_loop(nodes, rg, lambda:False, max_tasks_per_node=1)
|
|
|
|
|
|
|
|
|
|
|
|
class LSession(AbstractSession):
|
|
|
|
""" Local version of session
|
|
|
|
"""
|
|
|
|
def main(self, reporter=None, runner=None):
|
|
|
|
# check out if used options makes any sense
|
2007-01-25 00:46:46 +08:00
|
|
|
args = self.config.args
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
sshhosts = [HostInfo('localhost')] # this is just an info to reporter
|
|
|
|
|
|
|
|
if not self.config.option.nomagic:
|
|
|
|
py.magic.invoke(assertion=1)
|
|
|
|
|
|
|
|
reporter, startserverflag = self.init_reporter(reporter,
|
|
|
|
sshhosts, LocalReporter, args[0])
|
|
|
|
reporter, checkfun = self.wrap_reporter(reporter)
|
|
|
|
|
|
|
|
reporter(report.TestStarted(sshhosts))
|
|
|
|
pkgdir = self.getpkgdir(args[0])
|
2007-01-26 19:49:59 +08:00
|
|
|
colitems = self.config.getcolitems()
|
2007-01-24 22:24:01 +08:00
|
|
|
reporter(report.RsyncFinished())
|
|
|
|
|
|
|
|
if runner is None:
|
|
|
|
runner = self.init_runner(pkgdir)
|
|
|
|
|
|
|
|
keyword = self.config.option.keyword
|
|
|
|
|
|
|
|
itemgenerator = itemgen(colitems, reporter, keyword, self.reporterror)
|
|
|
|
local_loop(self, reporter, itemgenerator, checkfun, self.config, runner=runner)
|
|
|
|
|
|
|
|
retval = reporter(report.TestFinished())
|
|
|
|
self.kill_server(startserverflag)
|
|
|
|
|
|
|
|
if not self.config.option.nomagic:
|
|
|
|
py.magic.revoke(assertion=1)
|
|
|
|
|
|
|
|
self.write_docs(pkgdir)
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def write_docs(self, pkgdir):
|
|
|
|
if self.config.option.apigen:
|
|
|
|
from py.__.apigen.tracer.docstorage import DocStorageAccessor
|
|
|
|
apigen = py.path.local(self.config.option.apigen).pyimport()
|
2007-01-25 23:23:18 +08:00
|
|
|
if not hasattr(apigen, 'build'):
|
|
|
|
raise NotImplementedError("Provided script does not seem "
|
|
|
|
"to contain build function")
|
2007-01-24 22:24:01 +08:00
|
|
|
print >>sys.stderr, 'building documentation'
|
|
|
|
capture = py.io.OutErrCapture()
|
|
|
|
try:
|
2007-01-26 19:49:59 +08:00
|
|
|
try:
|
|
|
|
apigen.build(pkgdir, DocStorageAccessor(self.docstorage))
|
|
|
|
except AttributeError:
|
|
|
|
import traceback
|
|
|
|
exc, e, tb = sys.exc_info()
|
|
|
|
print '%s - %s' % (exc, e)
|
|
|
|
print ''.join(traceback.format_tb(tb))
|
|
|
|
del tb
|
|
|
|
print '-' * 79
|
|
|
|
raise NotImplementedError("Provided script does not seem "
|
|
|
|
"to contain build function")
|
2007-01-24 22:24:01 +08:00
|
|
|
finally:
|
|
|
|
capture.reset()
|
|
|
|
|
|
|
|
def init_runner(self, pkgdir):
|
|
|
|
if self.config.option.apigen:
|
|
|
|
from py.__.apigen.tracer.tracer import Tracer, DocStorage
|
|
|
|
module = py
|
|
|
|
try:
|
|
|
|
apigen = py.path.local(self.config.option.apigen).pyimport()
|
|
|
|
items = apigen.get_documentable_items(pkgdir)
|
|
|
|
if isinstance(items, dict):
|
|
|
|
self.docstorage = DocStorage().from_dict(items)
|
|
|
|
else:
|
|
|
|
self.docstorage = DocStorage().from_pkg(items)
|
|
|
|
except ImportError:
|
2007-01-26 19:49:59 +08:00
|
|
|
import traceback
|
|
|
|
exc, e, tb = sys.exc_info()
|
|
|
|
print '%s - %s' % (exc, e)
|
|
|
|
print ''.join(traceback.format_tb(tb))
|
|
|
|
del tb
|
|
|
|
print '-' * 79
|
2007-01-24 22:24:01 +08:00
|
|
|
raise ImportError("Provided script cannot be imported")
|
|
|
|
except (ValueError, AttributeError):
|
2007-01-26 19:49:59 +08:00
|
|
|
import traceback
|
|
|
|
exc, e, tb = sys.exc_info()
|
|
|
|
print '%s - %s' % (exc, e)
|
|
|
|
print ''.join(traceback.format_tb(tb))
|
|
|
|
del tb
|
|
|
|
print '-' * 79
|
2007-01-24 22:24:01 +08:00
|
|
|
raise NotImplementedError("Provided script does not seem "
|
|
|
|
"to contain get_documentable_items")
|
|
|
|
self.tracer = Tracer(self.docstorage)
|
|
|
|
return apigen_runner
|
|
|
|
else:
|
2007-01-25 00:46:46 +08:00
|
|
|
if (self.config.getvalue('dist_boxing') or self.config.option.boxing)\
|
|
|
|
and not self.config.option.nocapture:
|
|
|
|
return box_runner
|
|
|
|
return plain_runner
|
|
|
|
|