[py-svn] r34518 - in py/dist/py/test/rsession: . testing

fijal at codespeak.net fijal at codespeak.net
Sun Nov 12 00:27:06 CET 2006


Author: fijal
Date: Sun Nov 12 00:27:04 2006
New Revision: 34518

Modified:
   py/dist/py/test/rsession/hostmanage.py
   py/dist/py/test/rsession/rsession.py
   py/dist/py/test/rsession/testing/test_rsession.py
Log:
Major refactoring of rsession rsync attempt. Now you can specify multiple times the same host and control how many times you would like to rsync. And test for the above ;-)


Modified: py/dist/py/test/rsession/hostmanage.py
==============================================================================
--- py/dist/py/test/rsession/hostmanage.py	(original)
+++ py/dist/py/test/rsession/hostmanage.py	Sun Nov 12 00:27:04 2006
@@ -29,13 +29,13 @@
 
 def prepare_gateway(sshosts, relpath, rsync_roots, optimise_localhost, remote_python, pkgdir):
     hosts = []
-    for host in sshosts:
+    for num, host in enumerate(sshosts):
         if host != 'localhost' or not optimise_localhost:
             if isinstance(relpath, str):
                 assert not os.path.isabs(relpath), relpath
                 remoterootpath = relpath
             else:
-                remoterootpath = relpath[host]
+                remoterootpath = relpath[(num, host)]
             # XXX: because of NFS we do create different directories
             #      otherwise, .pyc files overlap
             remoterootpath += "-" + host
@@ -51,46 +51,53 @@
                 else:
                     gw = py.execnet.SshGateway(host, remotepython=remote_python)
             
-            hosts.append((host, gw, remoterootpath))
+            hosts.append((num, host, gw, remoterootpath))
         else:
             if remote_python is None:
                 gw = py.execnet.PopenGateway()
             else:
                 gw = py.execnet.PopenGateway(remotepython=remote_python)
             gw.sshaddress = 'localhost'
-            hosts.append((host, gw, str(pkgdir.dirpath())))
+            hosts.append((num, host, gw, str(pkgdir.dirpath())))
     return hosts
 
+# XXX: Options has grown a bit too much, but most of them is just for tests
 def init_hosts(reporter, sshhosts, relpath, pkgdir, rsync_roots=None, \
-               remote_python=None, remote_options={}, optimise_localhost=True):
+               remote_python=None, remote_options={}, optimise_localhost=True,\
+               do_sync=True):
     assert pkgdir.join("__init__.py").check(), (
             "%s probably wrong" %(pkgdir,))
     assert relpath, relpath
     
-    nodes = []
     exc_info = [None]
     hosts = prepare_gateway(sshhosts, relpath, rsync_roots, optimise_localhost,
         remote_python, pkgdir)
     
     # rsyncing
-    if optimise_localhost:
-        rsynced = {'localhost':True}
-    else:
-        rsynced = {}
-
-    rsync = HostRSync(rsync_roots)
-    for host, gw, remoterootpath in hosts:
-        if host in rsynced:
+    rsynced = {}
+
+    if do_sync:
+        rsync = HostRSync(rsync_roots)
+    for num, host, gw, remoterootpath in hosts:
+        if (host, remoterootpath) in rsynced or (host == 'localhost' \
+            and optimise_localhost):
             continue
-        rsynced[host] = True
+        rsynced[(host, remoterootpath)] = True
         def done(host=host):
             reporter(report.HostReady(host))
         reporter(report.HostRSyncing(host, remoterootpath))
-        rsync.add_target(gw, remoterootpath, done)
+        if do_sync:
+            rsync.add_target(gw, remoterootpath, done)
+    if not do_sync:
+        return # for testing only
     rsync.send(pkgdir.dirpath())
 
     # hosts ready
-    for host, gw, remoterootpath in hosts:
+    return setup_nodes(hosts, pkgdir, remote_options, reporter)
+
+def setup_nodes(hosts, pkgdir, remote_options, reporter):
+    nodes = []
+    for num, host, gw, remoterootpath in hosts:
         ch = setup_slave(gw, os.path.join(remoterootpath, pkgdir.basename), 
             remote_options)
         nodes.append(MasterNode(ch, reporter))

Modified: py/dist/py/test/rsession/rsession.py
==============================================================================
--- py/dist/py/test/rsession/rsession.py	(original)
+++ py/dist/py/test/rsession/rsession.py	Sun Nov 12 00:27:04 2006
@@ -106,6 +106,19 @@
                 reporter(report.FailedTryiter(excinfo, item))
     reporterror = staticmethod(reporterror)
 
+def parse_directories(sshhosts):
+    # dictionary containing directories for hosts
+    # XXX: should be class with some info like key, etc. in future
+    directories = {}
+    for num, host in enumerate(sshhosts):
+        m = re.match("^(.*?):(.*)$", host)
+        if m:
+            directories[(num, m.group(1))] = m.group(2)
+            sshhosts[num] = m.group(1)
+        else:
+            directories[(num, host)] = "pytestcache"
+    return directories
+
 class RSession(AbstractSession):
     """ Remote version of session
     """
@@ -114,16 +127,7 @@
         if not args: 
             args = [py.path.local()]
         sshhosts = self.config.getinitialvalue("disthosts")
-        # dictionary containing directories for hosts
-        # XXX: should be class with some info like key, etc. in future
-        directories = {}
-        for num, host in enumerate(sshhosts):
-            m = re.match("^(.*?):(.*)$", host)
-            if m:
-                directories[m.group(1)] = m.group(2)
-                sshhosts[num] = m.group(1)
-            else:
-                directories[host] = "pytestcache"
+        directories = parse_directories(sshhosts)
         try:
             rsync_roots = self.config.getinitialvalue("distrsync_roots")
         except:
@@ -155,8 +159,6 @@
                     yield y 
         
         itemgenerator = itemgen() 
-        #assert 0, "\n".join([",".join(x.listnames()) for x in 
-        #    list(itemgenerator)])
         dispatch_loop(nodes, itemgenerator, checkfun)
         teardown_hosts(reporter, [node.channel for node in nodes], nodes, 
             exitfirst=self.config.option.exitfirst)

Modified: py/dist/py/test/rsession/testing/test_rsession.py
==============================================================================
--- py/dist/py/test/rsession/testing/test_rsession.py	(original)
+++ py/dist/py/test/rsession/testing/test_rsession.py	Sun Nov 12 00:27:04 2006
@@ -4,7 +4,7 @@
 
 import py
 from py.__.test.rsession import report
-from py.__.test.rsession.rsession import RSession
+from py.__.test.rsession.rsession import RSession, parse_directories
 from py.__.test.rsession.hostmanage import init_hosts, teardown_hosts
 from py.__.test.rsession.testing.test_slave import funcfail_spec,\
     funcpass_spec, funcskip_spec, funcprint_spec, funcprintfail_spec, \
@@ -270,3 +270,39 @@
         assert len(passed) == 2 * len(nodes)
         assert len(skipped) == 0
         assert len(events) == len(passed)
+
+class TestDirectories(object):
+    def test_simple_parse(self):
+        sshhosts = ['h1', 'h2', 'h3']
+        dirs = parse_directories(sshhosts)
+        assert len(set(dirs.values())) == 1
+        assert sorted(dirs.keys()) == [(0, 'h1'), (1, 'h2'), (2, 'h3')]
+    
+    def test_sophisticated_parse(self):
+        sshhosts = ['a at h1:/tmp', 'h2:tmp', 'h3']
+        dirs = parse_directories(sshhosts)
+        assert sorted(dirs.values()) == ['/tmp', 'pytestcache', 'tmp']
+    
+    def test_parse_multiple_hosts(self):
+        hosts = ['h1', 'h1', 'h1:/tmp']
+        dirs = parse_directories(hosts)
+        assert dirs == {(0, 'h1'): 'pytestcache', (1, 'h1'): 'pytestcache', 
+            (2, 'h1'):'/tmp'}
+
+class TestInithosts(object):
+    def test_inithosts(self):
+        testevents = []
+        hosts = ['h1:/tmp', 'h1:/tmp', 'h1:/other', 'h2', 'h2:home']
+        dirs = parse_directories(hosts)
+        init_hosts(testevents.append, hosts, dirs, pkgdir, do_sync=False)
+        events = [i for i in testevents if isinstance(i, report.HostRSyncing)]
+        assert len(events) == 4
+        assert events[0].hostname == 'h1'
+        assert events[0].remoterootpath == '/tmp-h1'
+        assert events[1].hostname == 'h1'
+        assert events[1].remoterootpath == '/other-h1'
+        assert events[2].hostname == 'h2'
+        assert events[2].remoterootpath == 'pytestcache-h2'
+        assert events[3].hostname == 'h2'
+        assert events[3].remoterootpath == 'home-h2'
+        



More information about the pytest-commit mailing list