2007-01-24 22:24:01 +08:00
|
|
|
""" master code and test dispatching for
|
|
|
|
making 1-n master -> slave connection
|
|
|
|
and test it locally.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import time, threading
|
|
|
|
import py, sys
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
py.test.skip("rsession is unsupported on Windows.")
|
|
|
|
|
2007-02-05 07:12:12 +08:00
|
|
|
from py.__.test.rsession.master import dispatch_loop, MasterNode
|
2007-02-04 22:05:01 +08:00
|
|
|
from py.__.test.rsession.slave import setup_slave
|
2007-08-27 17:02:50 +08:00
|
|
|
from py.__.test.outcome import ReprOutcome, SerializableOutcome
|
|
|
|
from py.__.test import repevent
|
2007-01-24 22:24:01 +08:00
|
|
|
from py.__.test.rsession.hostmanage import HostInfo
|
|
|
|
|
|
|
|
def setup_module(mod):
|
|
|
|
# bind an empty config
|
2007-02-04 22:05:01 +08:00
|
|
|
mod.tmpdir = tmpdir = py.test.ensuretemp(mod.__name__)
|
|
|
|
# to avoid rsyncing
|
|
|
|
config = py.test.config._reparse([tmpdir])
|
2007-02-12 01:08:13 +08:00
|
|
|
config.option.dist_taskspernode = 10
|
2007-02-04 22:05:01 +08:00
|
|
|
mod.rootcol = config._getcollector(tmpdir)
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
class DummyGateway(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.host = HostInfo("localhost")
|
|
|
|
|
|
|
|
class DummyChannel(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.sent = []
|
|
|
|
self.gateway = DummyGateway()
|
|
|
|
|
|
|
|
def setcallback(self, func):
|
|
|
|
self.callback = func
|
|
|
|
|
|
|
|
def send(self, item):
|
|
|
|
assert py.std.marshal.dumps(item)
|
|
|
|
self.sent.append(item)
|
|
|
|
|
2007-02-08 02:44:49 +08:00
|
|
|
class NonWorkingChannel(object):
|
|
|
|
def setcallback(self, func):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def send(self, item):
|
|
|
|
raise IOError
|
|
|
|
|
|
|
|
def _getremoteerror(self):
|
|
|
|
return "blah"
|
|
|
|
|
2007-02-14 03:21:53 +08:00
|
|
|
class Item(py.test.collect.Item):
|
2007-02-04 02:00:04 +08:00
|
|
|
def _get_collector_trail(self):
|
2007-01-27 05:46:11 +08:00
|
|
|
return (self.name,)
|
|
|
|
|
2007-01-24 22:24:01 +08:00
|
|
|
def test_masternode():
|
|
|
|
try:
|
|
|
|
raise ValueError()
|
|
|
|
except ValueError:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
|
|
|
|
ch = DummyChannel()
|
|
|
|
reportlist = []
|
2007-02-05 07:12:12 +08:00
|
|
|
mnode = MasterNode(ch, reportlist.append)
|
2007-01-27 05:46:11 +08:00
|
|
|
mnode.send(Item("ok"))
|
|
|
|
mnode.send(Item("notok"))
|
2007-08-27 17:02:50 +08:00
|
|
|
ch.callback(SerializableOutcome().make_repr())
|
|
|
|
ch.callback(SerializableOutcome(excinfo=excinfo).make_repr())
|
2007-01-24 22:24:01 +08:00
|
|
|
assert len(reportlist) == 4
|
|
|
|
received = [i for i in reportlist
|
2007-02-05 07:34:23 +08:00
|
|
|
if isinstance(i, repevent.ReceivedItemOutcome)]
|
2007-01-24 22:24:01 +08:00
|
|
|
assert received[0].outcome.passed
|
|
|
|
assert not received[1].outcome.passed
|
|
|
|
|
2007-02-08 02:44:49 +08:00
|
|
|
def test_masternode_nonworking_channel():
|
|
|
|
ch = NonWorkingChannel()
|
|
|
|
reportlist = []
|
|
|
|
mnode = MasterNode(ch, reportlist.append)
|
|
|
|
cap = py.io.StdCaptureFD()
|
|
|
|
py.test.raises(IOError, 'mnode.send(Item("ok"))')
|
|
|
|
out, err = cap.reset()
|
|
|
|
assert out.find("blah") != -1
|
|
|
|
|
2007-02-05 07:12:12 +08:00
|
|
|
def test_sending_two_noes():
|
|
|
|
# XXX fijal: this test previously tested that the second
|
|
|
|
# item result would not get send. why? did i miss
|
|
|
|
# something?
|
|
|
|
#
|
2007-01-24 22:24:01 +08:00
|
|
|
ch = DummyChannel()
|
|
|
|
reportlist = []
|
2007-02-05 07:12:12 +08:00
|
|
|
mnode = MasterNode(ch, reportlist.append)
|
2007-01-27 05:46:11 +08:00
|
|
|
mnode.send(Item("ok"))
|
|
|
|
mnode.send(Item("ok"))
|
2007-08-27 17:02:50 +08:00
|
|
|
ch.callback(SerializableOutcome().make_repr())
|
|
|
|
ch.callback(SerializableOutcome().make_repr())
|
2007-02-05 07:12:12 +08:00
|
|
|
assert len(reportlist) == 4
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def test_outcome_repr():
|
2008-01-25 23:54:04 +08:00
|
|
|
out = ReprOutcome(SerializableOutcome(skipped="xxx").make_repr())
|
2007-01-24 22:24:01 +08:00
|
|
|
s = repr(out)
|
|
|
|
assert s.lower().find("skip") != -1
|
|
|
|
|
|
|
|
class DummyMasterNode(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.pending = []
|
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
self.pending.append(data)
|
|
|
|
|
|
|
|
def test_dispatch_loop():
|
|
|
|
masternodes = [DummyMasterNode(), DummyMasterNode()]
|
|
|
|
itemgenerator = iter(range(100))
|
|
|
|
shouldstop = lambda : False
|
|
|
|
def waiter():
|
|
|
|
for node in masternodes:
|
|
|
|
node.pending.pop()
|
|
|
|
dispatch_loop(masternodes, itemgenerator, shouldstop, waiter=waiter)
|
|
|
|
|
2007-02-04 22:05:01 +08:00
|
|
|
class TestSlave:
|
|
|
|
def setup_class(cls):
|
|
|
|
cls.tmpdir = tmpdir = py.test.ensuretemp(cls.__name__)
|
2007-02-04 23:42:55 +08:00
|
|
|
cls.pkgpath = pkgpath = tmpdir.join("slavetestpkg")
|
2007-02-04 22:05:01 +08:00
|
|
|
pkgpath.ensure("__init__.py")
|
|
|
|
pkgpath.join("test_something.py").write(py.code.Source("""
|
|
|
|
def funcpass():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def funcfail():
|
|
|
|
raise AssertionError("hello world")
|
|
|
|
"""))
|
|
|
|
cls.config = py.test.config._reparse([tmpdir])
|
|
|
|
assert cls.config.topdir == tmpdir
|
|
|
|
cls.rootcol = cls.config._getcollector(tmpdir)
|
|
|
|
|
|
|
|
def _gettrail(self, *names):
|
|
|
|
item = self.rootcol._getitembynames(names)
|
|
|
|
return self.config.get_collector_trail(item)
|
|
|
|
|
|
|
|
def test_slave_running(self):
|
|
|
|
py.test.skip("XXX test broken, needs refactoring")
|
|
|
|
def simple_report(event):
|
2007-02-05 07:34:23 +08:00
|
|
|
if not isinstance(event, repevent.ReceivedItemOutcome):
|
2007-02-04 22:05:01 +08:00
|
|
|
return
|
|
|
|
item = event.item
|
|
|
|
if item.code.name == 'funcpass':
|
|
|
|
assert event.outcome.passed
|
|
|
|
else:
|
|
|
|
assert not event.outcome.passed
|
|
|
|
|
|
|
|
def open_gw():
|
|
|
|
gw = py.execnet.PopenGateway()
|
2008-01-25 23:54:04 +08:00
|
|
|
host = HostInfo("localhost")
|
|
|
|
host.gw_remotepath = ''
|
|
|
|
host.gw = gw
|
|
|
|
#gw.host.gw = gw
|
2007-02-04 22:05:01 +08:00
|
|
|
config = py.test.config._reparse([tmpdir])
|
2008-01-25 23:54:04 +08:00
|
|
|
channel = setup_slave(host, config)
|
|
|
|
mn = MasterNode(channel, simple_report)
|
2007-02-04 22:05:01 +08:00
|
|
|
return mn
|
|
|
|
|
|
|
|
master_nodes = [open_gw(), open_gw(), open_gw()]
|
2008-01-25 23:54:04 +08:00
|
|
|
funcpass_item = self.xxx
|
2007-02-04 22:05:01 +08:00
|
|
|
funcfail_item = rootcol._getitembynames(funcfail_spec)
|
|
|
|
itemgenerator = iter([funcfail_item] +
|
|
|
|
[funcpass_item] * 5 + [funcfail_item] * 5)
|
|
|
|
shouldstop = lambda : False
|
|
|
|
dispatch_loop(master_nodes, itemgenerator, shouldstop)
|
2007-01-24 22:24:01 +08:00
|
|
|
|
2007-01-25 00:46:46 +08:00
|
|
|
def test_slave_running_interrupted():
|
2007-02-04 22:05:01 +08:00
|
|
|
py.test.skip("XXX test broken, needs refactoring")
|
2007-01-25 00:46:46 +08:00
|
|
|
#def simple_report(event):
|
2007-02-05 07:34:23 +08:00
|
|
|
# if not isinstance(event, repevent.ReceivedItemOutcome):
|
2007-01-25 00:46:46 +08:00
|
|
|
# return
|
|
|
|
# item = event.item
|
|
|
|
# if item.code.name == 'funcpass':
|
|
|
|
# assert event.outcome.passed
|
|
|
|
# else:
|
|
|
|
# assert not event.outcome.passed
|
|
|
|
reports = []
|
|
|
|
|
|
|
|
def open_gw():
|
|
|
|
gw = py.execnet.PopenGateway()
|
|
|
|
gw.host = HostInfo("localhost")
|
|
|
|
gw.host.gw = gw
|
2007-02-04 22:05:01 +08:00
|
|
|
config = py.test.config._reparse([tmpdir])
|
2007-02-06 18:31:08 +08:00
|
|
|
channel = setup_slave(gw.host, config)
|
2007-01-25 00:46:46 +08:00
|
|
|
mn = MasterNode(channel, reports.append, {})
|
|
|
|
return mn, gw, channel
|
|
|
|
|
|
|
|
mn, gw, channel = open_gw()
|
2007-01-26 19:49:59 +08:00
|
|
|
rootcol = py.test.collect.Directory(pkgdir)
|
2007-02-04 02:29:24 +08:00
|
|
|
funchang_item = rootcol._getitembynames(funchang_spec)
|
2007-01-25 00:46:46 +08:00
|
|
|
mn.send(funchang_item)
|
|
|
|
mn.send(StopIteration)
|
|
|
|
# XXX: We have to wait here a bit to make sure that it really did happen
|
|
|
|
channel.waitclose(2)
|
|
|
|
|