2007-02-04 22:05:01 +08:00
|
|
|
|
|
|
|
""" RSync filter test
|
|
|
|
"""
|
|
|
|
|
|
|
|
import py
|
2007-02-07 03:06:57 +08:00
|
|
|
from py.__.test.rsession.hostmanage import HostRSync, HostInfo, HostManager
|
|
|
|
from py.__.test.rsession.hostmanage import gethomedir, getpath_relto_home
|
2007-02-06 07:53:29 +08:00
|
|
|
from py.__.test.rsession import repevent
|
2007-02-04 22:05:01 +08:00
|
|
|
|
|
|
|
class DirSetup:
|
|
|
|
def setup_method(self, method):
|
|
|
|
name = "%s.%s" %(self.__class__.__name__, method.func_name)
|
|
|
|
self.tmpdir = py.test.ensuretemp(name)
|
|
|
|
self.source = self.tmpdir.ensure("source", dir=1)
|
|
|
|
self.dest = self.tmpdir.join("dest")
|
|
|
|
|
2007-02-07 03:06:57 +08:00
|
|
|
class TestHostInfo(DirSetup):
|
|
|
|
def _gethostinfo(self, relpath=""):
|
|
|
|
exampledir = self.tmpdir.join("gethostinfo")
|
|
|
|
if relpath:
|
|
|
|
exampledir = exampledir.join(relpath)
|
|
|
|
assert not exampledir.check()
|
|
|
|
hostinfo = HostInfo("localhost:%s" % exampledir)
|
|
|
|
return hostinfo
|
|
|
|
|
2007-02-04 22:05:01 +08:00
|
|
|
def test_defaultpath(self):
|
|
|
|
x = HostInfo("localhost")
|
|
|
|
assert x.hostname == "localhost"
|
|
|
|
assert x.relpath == "pytestcache-" + x.hostname
|
|
|
|
|
|
|
|
def test_path(self):
|
|
|
|
x = HostInfo("localhost:/tmp")
|
|
|
|
assert x.relpath == "/tmp"
|
|
|
|
assert x.hostname == "localhost"
|
|
|
|
|
|
|
|
def test_hostid(self):
|
|
|
|
x = HostInfo("localhost")
|
|
|
|
y = HostInfo("localhost")
|
|
|
|
assert x.hostid != y.hostid
|
|
|
|
x = HostInfo("localhost:/tmp")
|
|
|
|
y = HostInfo("localhost")
|
|
|
|
assert x.hostid != y.hostid
|
|
|
|
|
|
|
|
def test_non_existing_hosts(self):
|
|
|
|
host = HostInfo("alskdjalsdkjasldkajlsd")
|
|
|
|
py.test.raises((py.process.cmdexec.Error, IOError, EOFError),
|
|
|
|
host.initgateway)
|
|
|
|
|
2007-02-06 06:46:31 +08:00
|
|
|
def test_remote_has_homedir_as_currentdir(self):
|
2007-02-07 03:06:57 +08:00
|
|
|
host = self._gethostinfo()
|
2007-02-06 06:46:31 +08:00
|
|
|
old = py.path.local.get_temproot().chdir()
|
|
|
|
try:
|
|
|
|
host.initgateway()
|
2007-02-07 03:06:57 +08:00
|
|
|
channel = host.gw.remote_exec(py.code.Source(
|
|
|
|
gethomedir, """
|
2007-02-06 06:46:31 +08:00
|
|
|
import os
|
2007-02-07 03:06:57 +08:00
|
|
|
homedir = gethomedir()
|
|
|
|
curdir = os.getcwd()
|
|
|
|
channel.send((curdir, homedir))
|
|
|
|
"""))
|
|
|
|
remote_curdir, remote_homedir = channel.receive()
|
|
|
|
assert remote_curdir == remote_homedir
|
2007-02-06 06:46:31 +08:00
|
|
|
finally:
|
|
|
|
old.chdir()
|
|
|
|
|
2007-02-04 22:05:01 +08:00
|
|
|
def test_initgateway_localhost_relpath(self):
|
2007-02-07 03:06:57 +08:00
|
|
|
host = HostInfo("localhost:somedir")
|
|
|
|
host.initgateway()
|
|
|
|
assert host.gw
|
2007-02-04 22:05:01 +08:00
|
|
|
try:
|
2007-02-05 08:36:00 +08:00
|
|
|
homedir = py.path.local._gethomedir()
|
2007-02-07 03:06:57 +08:00
|
|
|
expected = homedir.join("somedir")
|
|
|
|
assert host.gw_remotepath == str(expected)
|
2007-02-04 22:05:01 +08:00
|
|
|
finally:
|
2007-02-07 03:06:57 +08:00
|
|
|
host.gw.exit()
|
2007-02-04 22:05:01 +08:00
|
|
|
|
|
|
|
def test_initgateway_ssh_and_remotepath(self):
|
|
|
|
option = py.test.config.option
|
|
|
|
if option.sshtarget is None:
|
|
|
|
py.test.skip("no known ssh target, use -S to set one")
|
2007-02-07 03:06:57 +08:00
|
|
|
host = HostInfo("%s" % (option.sshtarget, ))
|
|
|
|
# this test should be careful to not write/rsync anything
|
|
|
|
# as the remotepath is the default location
|
|
|
|
# and may be used in the real world
|
|
|
|
host.initgateway()
|
|
|
|
assert host.gw
|
|
|
|
assert host.gw_remotepath.endswith(host.relpath)
|
|
|
|
channel = host.gw.remote_exec("""
|
2007-02-04 22:05:01 +08:00
|
|
|
import os
|
|
|
|
homedir = os.environ['HOME']
|
|
|
|
relpath = channel.receive()
|
|
|
|
path = os.path.join(homedir, relpath)
|
|
|
|
channel.send(path)
|
|
|
|
""")
|
2007-02-07 03:06:57 +08:00
|
|
|
channel.send(host.relpath)
|
2007-02-04 22:05:01 +08:00
|
|
|
res = channel.receive()
|
2007-02-07 03:06:57 +08:00
|
|
|
assert res == host.gw_remotepath
|
2007-02-04 22:05:01 +08:00
|
|
|
|
|
|
|
class TestSyncing(DirSetup):
|
2007-02-07 03:06:57 +08:00
|
|
|
def _gethostinfo(self):
|
|
|
|
hostinfo = HostInfo("localhost:%s" % self.dest)
|
|
|
|
return hostinfo
|
|
|
|
|
2007-02-04 22:05:01 +08:00
|
|
|
def test_hrsync_filter(self):
|
|
|
|
self.source.ensure("dir", "file.txt")
|
|
|
|
self.source.ensure(".svn", "entries")
|
|
|
|
self.source.ensure(".somedotfile", "moreentries")
|
|
|
|
self.source.ensure("somedir", "editfile~")
|
2007-02-08 23:31:38 +08:00
|
|
|
syncer = HostRSync(self.source)
|
2007-02-04 22:05:01 +08:00
|
|
|
l = list(self.source.visit(rec=syncer.filter,
|
|
|
|
fil=syncer.filter))
|
|
|
|
assert len(l) == 3
|
|
|
|
basenames = [x.basename for x in l]
|
|
|
|
assert 'dir' in basenames
|
|
|
|
assert 'file.txt' in basenames
|
|
|
|
assert 'somedir' in basenames
|
|
|
|
|
|
|
|
def test_hrsync_one_host(self):
|
2007-02-07 03:06:57 +08:00
|
|
|
h1 = self._gethostinfo()
|
2007-02-04 22:05:01 +08:00
|
|
|
finished = []
|
2007-02-08 23:31:38 +08:00
|
|
|
rsync = HostRSync(self.source)
|
2007-02-04 22:05:01 +08:00
|
|
|
h1.initgateway()
|
|
|
|
rsync.add_target_host(h1)
|
|
|
|
self.source.join("hello.py").write("world")
|
2007-02-08 23:31:38 +08:00
|
|
|
rsync.send()
|
2007-02-04 22:05:01 +08:00
|
|
|
assert self.dest.join("hello.py").check()
|
|
|
|
|
|
|
|
def test_hrsync_same_host_twice(self):
|
2007-02-07 03:06:57 +08:00
|
|
|
h1 = self._gethostinfo()
|
|
|
|
h2 = self._gethostinfo()
|
2007-02-04 22:05:01 +08:00
|
|
|
finished = []
|
2007-02-08 23:31:38 +08:00
|
|
|
rsync = HostRSync(self.source)
|
2007-02-04 22:05:01 +08:00
|
|
|
l = []
|
|
|
|
h1.initgateway()
|
|
|
|
res1 = rsync.add_target_host(h1)
|
|
|
|
assert res1
|
|
|
|
res2 = rsync.add_target_host(h2)
|
|
|
|
assert not res2
|
|
|
|
|
|
|
|
class TestHostManager(DirSetup):
|
2007-02-05 08:14:11 +08:00
|
|
|
def test_hostmanager_custom_hosts(self):
|
|
|
|
config = py.test.config._reparse([self.source])
|
|
|
|
hm = HostManager(config, hosts=[1,2,3])
|
|
|
|
assert hm.hosts == [1,2,3]
|
|
|
|
|
2007-02-04 22:05:01 +08:00
|
|
|
def test_hostmanager_init_rsync_topdir(self):
|
|
|
|
dir2 = self.source.ensure("dir1", "dir2", dir=1)
|
|
|
|
dir2.ensure("hello")
|
|
|
|
config = py.test.config._reparse([self.source])
|
2007-02-05 08:14:11 +08:00
|
|
|
hm = HostManager(config,
|
|
|
|
hosts=[HostInfo("localhost:" + str(self.dest))])
|
2007-02-04 22:05:01 +08:00
|
|
|
events = []
|
|
|
|
hm.init_rsync(reporter=events.append)
|
|
|
|
assert self.dest.join("dir1").check()
|
|
|
|
assert self.dest.join("dir1", "dir2").check()
|
|
|
|
assert self.dest.join("dir1", "dir2", 'hello').check()
|
|
|
|
|
|
|
|
def test_hostmanager_init_rsync_rsync_roots(self):
|
|
|
|
dir2 = self.source.ensure("dir1", "dir2", dir=1)
|
2007-02-06 07:21:33 +08:00
|
|
|
self.source.ensure("dir1", "somefile", dir=1)
|
2007-02-04 22:05:01 +08:00
|
|
|
dir2.ensure("hello")
|
|
|
|
self.source.ensure("bogusdir", "file")
|
|
|
|
self.source.join("conftest.py").write(py.code.Source("""
|
|
|
|
dist_rsync_roots = ['dir1/dir2']
|
|
|
|
"""))
|
|
|
|
config = py.test.config._reparse([self.source])
|
2007-02-05 08:14:11 +08:00
|
|
|
hm = HostManager(config,
|
|
|
|
hosts=[HostInfo("localhost:" + str(self.dest))])
|
2007-02-04 22:05:01 +08:00
|
|
|
events = []
|
|
|
|
hm.init_rsync(reporter=events.append)
|
|
|
|
assert self.dest.join("dir1").check()
|
|
|
|
assert self.dest.join("dir1", "dir2").check()
|
|
|
|
assert self.dest.join("dir1", "dir2", 'hello').check()
|
|
|
|
assert not self.dest.join("bogus").check()
|
2007-02-06 07:21:33 +08:00
|
|
|
assert not self.dest.join("dir1", "somefile").check()
|
2007-02-05 09:14:17 +08:00
|
|
|
|
|
|
|
def test_hostmanager_rsync_ignore(self):
|
|
|
|
dir2 = self.source.ensure("dir1", "dir2", dir=1)
|
|
|
|
dir5 = self.source.ensure("dir5", "dir6", "bogus")
|
|
|
|
dirf = self.source.ensure("dir5", "file")
|
|
|
|
dir2.ensure("hello")
|
|
|
|
self.source.join("conftest.py").write(py.code.Source("""
|
|
|
|
dist_rsync_ignore = ['dir1/dir2', 'dir5/dir6']
|
|
|
|
"""))
|
|
|
|
config = py.test.config._reparse([self.source])
|
|
|
|
hm = HostManager(config,
|
|
|
|
hosts=[HostInfo("localhost:" + str(self.dest))])
|
|
|
|
events = []
|
|
|
|
hm.init_rsync(reporter=events.append)
|
|
|
|
assert self.dest.join("dir1").check()
|
|
|
|
assert not self.dest.join("dir1", "dir2").check()
|
|
|
|
assert self.dest.join("dir5","file").check()
|
|
|
|
assert not self.dest.join("dir6").check()
|
2007-02-06 06:46:31 +08:00
|
|
|
|
2007-02-07 03:06:57 +08:00
|
|
|
def test_getpath_relto_home():
|
|
|
|
x = getpath_relto_home("hello")
|
|
|
|
assert x == py.path.local._gethomedir().join("hello")
|
|
|
|
|