6
from py.__.test.rsession.hostmanage import HostRSync, HostInfo, HostManager
7
from py.__.test.rsession.hostmanage import sethomedir, gethomedir, getpath_relto_home
8
from py.__.test.rsession import repevent
11
def setup_method(self, method):
12
name = "%s.%s" %(self.__class__.__name__, method.func_name)
13
self.tmpdir = py.test.ensuretemp(name)
14
self.source = self.tmpdir.ensure("source", dir=1)
15
self.dest = self.tmpdir.join("dest")
17
class TestHostInfo(DirSetup):
18
def _gethostinfo(self, relpath=""):
19
exampledir = self.tmpdir.join("gethostinfo")
21
exampledir = exampledir.join(relpath)
22
assert not exampledir.check()
23
hostinfo = HostInfo("localhost:%s" % exampledir)
26
def test_defaultpath(self):
27
x = HostInfo("localhost:")
28
assert x.hostname == "localhost"
31
def test_addrel(self):
32
host = HostInfo("localhost:", addrel="whatever")
33
assert host.inplacelocal
34
assert not host.relpath
35
host = HostInfo("localhost:/tmp", addrel="base")
36
assert host.relpath == "/tmp/base"
37
host = HostInfo("localhost:tmp", addrel="base2")
38
assert host.relpath == "tmp/base2"
41
x = HostInfo("localhost:/tmp")
42
assert x.relpath == "/tmp"
43
assert x.hostname == "localhost"
44
assert not x.inplacelocal
46
def test_hostid(self):
47
x = HostInfo("localhost:")
48
y = HostInfo("localhost:")
49
assert x.hostid != y.hostid
50
x = HostInfo("localhost:/tmp")
51
y = HostInfo("localhost:")
52
assert x.hostid != y.hostid
54
def test_non_existing_hosts(self):
55
host = HostInfo("alskdjalsdkjasldkajlsd")
56
py.test.raises((py.process.cmdexec.Error, IOError, EOFError),
59
def test_remote_has_homedir_as_currentdir(self):
60
host = self._gethostinfo()
61
old = py.path.local.get_temproot().chdir()
64
channel = host.gw.remote_exec(py.code.Source(
67
homedir = gethomedir()
69
channel.send((curdir, homedir))
71
remote_curdir, remote_homedir = channel.receive()
72
assert remote_curdir == remote_homedir
76
def test_initgateway_localhost_relpath(self):
77
host = HostInfo("localhost:somedir")
81
homedir = py.path.local._gethomedir()
82
expected = homedir.join("somedir")
83
assert host.gw_remotepath == str(expected)
87
def test_initgateway_ssh_and_remotepath(self):
88
option = py.test.config.option
89
if option.sshtarget is None:
90
py.test.skip("no known ssh target, use -S to set one")
91
host = HostInfo("%s" % (option.sshtarget, ))
92
# this test should be careful to not write/rsync anything
93
# as the remotepath is the default location
94
# and may be used in the real world
97
assert host.gw_remotepath.endswith(host.relpath)
98
channel = host.gw.remote_exec("""
100
homedir = os.environ['HOME']
101
relpath = channel.receive()
102
path = os.path.join(homedir, relpath)
105
channel.send(host.relpath)
106
res = channel.receive()
107
assert res == host.gw_remotepath
109
class TestSyncing(DirSetup):
110
def _gethostinfo(self):
111
hostinfo = HostInfo("localhost:%s" % self.dest)
114
def test_hrsync_filter(self):
115
self.source.ensure("dir", "file.txt")
116
self.source.ensure(".svn", "entries")
117
self.source.ensure(".somedotfile", "moreentries")
118
self.source.ensure("somedir", "editfile~")
119
syncer = HostRSync(self.source)
120
l = list(self.source.visit(rec=syncer.filter,
123
basenames = [x.basename for x in l]
124
assert 'dir' in basenames
125
assert 'file.txt' in basenames
126
assert 'somedir' in basenames
128
def test_hrsync_localhost_inplace(self):
129
h1 = HostInfo("localhost")
131
rsync = HostRSync(self.source)
133
rsync.add_target_host(h1, reporter=events.append)
135
l = [x for x in events
136
if isinstance(x, repevent.HostRSyncing)]
140
assert ev.root == ev.remotepath
141
l = [x for x in events
142
if isinstance(x, repevent.HostRSyncRootReady)]
145
assert ev.root == self.source
148
def test_hrsync_one_host(self):
149
h1 = self._gethostinfo()
151
rsync = HostRSync(self.source)
153
rsync.add_target_host(h1)
154
self.source.join("hello.py").write("world")
156
assert self.dest.join("hello.py").check()
158
def test_hrsync_same_host_twice(self):
159
h1 = self._gethostinfo()
160
h2 = self._gethostinfo()
162
rsync = HostRSync(self.source)
166
res1 = rsync.add_target_host(h1)
168
res2 = rsync.add_target_host(h2)
171
class TestHostManager(DirSetup):
172
def gethostmanager(self, dist_hosts, dist_rsync_roots=None):
173
l = ["dist_hosts = %r" % dist_hosts]
175
l.append("dist_rsync_roots = %r" % dist_rsync_roots)
176
self.source.join("conftest.py").write("\n".join(l))
177
config = py.test.config._reparse([self.source])
178
assert config.topdir == self.source
179
hm = HostManager(config)
183
def test_hostmanager_custom_hosts(self):
184
config = py.test.config._reparse([self.source])
185
hm = HostManager(config, hosts=[1,2,3])
186
assert hm.hosts == [1,2,3]
188
def test_hostmanager_init_rsync_topdir(self):
189
dir2 = self.source.ensure("dir1", "dir2", dir=1)
191
hm = self.gethostmanager(
192
dist_hosts = ["localhost:%s" % self.dest]
194
assert hm.config.topdir == self.source
195
hm.init_rsync([].append)
196
dest = self.dest.join(self.source.basename)
197
assert dest.join("dir1").check()
198
assert dest.join("dir1", "dir2").check()
199
assert dest.join("dir1", "dir2", 'hello').check()
201
def test_hostmanager_init_rsync_topdir_explicit(self):
202
dir2 = self.source.ensure("dir1", "dir2", dir=1)
204
hm = self.gethostmanager(
205
dist_hosts = ["localhost:%s" % self.dest],
206
dist_rsync_roots = [str(self.source)]
208
assert hm.config.topdir == self.source
209
hm.init_rsync([].append)
210
dest = self.dest.join(self.source.basename)
211
assert dest.join("dir1").check()
212
assert dest.join("dir1", "dir2").check()
213
assert dest.join("dir1", "dir2", 'hello').check()
215
def test_hostmanager_init_rsync_roots(self):
216
dir2 = self.source.ensure("dir1", "dir2", dir=1)
217
self.source.ensure("dir1", "somefile", dir=1)
219
self.source.ensure("bogusdir", "file")
220
self.source.join("conftest.py").write(py.code.Source("""
221
dist_rsync_roots = ['dir1/dir2']
223
config = py.test.config._reparse([self.source])
224
hm = HostManager(config,
225
hosts=[HostInfo("localhost:" + str(self.dest))])
227
hm.init_rsync(reporter=events.append)
228
assert self.dest.join("dir2").check()
229
assert not self.dest.join("dir1").check()
230
assert not self.dest.join("bogus").check()
232
def test_hostmanager_rsync_ignore(self):
233
dir2 = self.source.ensure("dir1", "dir2", dir=1)
234
dir5 = self.source.ensure("dir5", "dir6", "bogus")
235
dirf = self.source.ensure("dir5", "file")
237
self.source.join("conftest.py").write(py.code.Source("""
238
dist_rsync_ignore = ['dir1/dir2', 'dir5/dir6']
240
config = py.test.config._reparse([self.source])
241
hm = HostManager(config,
242
hosts=[HostInfo("localhost:" + str(self.dest))])
245
hm.init_rsync(reporter=events.append)
246
assert self.dest.join("dir1").check()
247
assert not self.dest.join("dir1", "dir2").check()
248
assert self.dest.join("dir5","file").check()
249
assert not self.dest.join("dir6").check()
251
def test_hostmanage_optimise_localhost(self):
252
hosts = [HostInfo("localhost") for i in range(3)]
253
config = py.test.config._reparse([self.source])
254
hm = HostManager(config, hosts=hosts)
256
hm.init_rsync(events.append)
258
assert host.inplacelocal
259
assert host.gw_remotepath is None
260
assert not host.relpath
263
def XXXtest_ssh_rsync_samehost_twice(self):
264
#XXX we have no easy way to have a temp directory remotely!
265
option = py.test.config.option
266
if option.sshtarget is None:
267
py.test.skip("no known ssh target, use -S to set one")
268
host1 = HostInfo("%s" % (option.sshtarget, ))
269
host2 = HostInfo("%s" % (option.sshtarget, ))
270
hm = HostManager(config, hosts=[host1, host2])
272
hm.init_rsync(events.append)
276
def test_getpath_relto_home():
277
x = getpath_relto_home("hello")
278
assert x == py.path.local._gethomedir().join("hello")
279
x = getpath_relto_home(".")
280
assert x == py.path.local._gethomedir()
282
def test_sethomedir():
283
old = py.path.local.get_temproot().chdir()
286
curdir = py.path.local()
290
assert py.path.local._gethomedir() == curdir