Skip to content

Commit 0288e4b

Browse files
authored
Merge pull request #1689 from Emantor/topic/managedfile-symlink
Managedfile: replace ssh usage for localhost
2 parents 7f757f0 + 18646f7 commit 0288e4b

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

labgrid/util/managedfile.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,23 @@ def sync_to_resource(self, symlink=None):
6868
f"{self.rpath}{os.path.basename(self.local_path)}"
6969
)
7070
else:
71-
conn = sshmanager.open("localhost")
7271
self.rpath = os.path.dirname(self.local_path) + "/"
7372

7473
if symlink is not None:
7574
self.logger.info("Linking")
76-
try:
77-
conn.run_check(f"test ! -e {symlink} -o -L {symlink}")
78-
except ExecutionError:
79-
raise ManagedFileError(f"Path {symlink} exists but is not a symlink.")
80-
# use short options to be compatible with busybox
81-
# --symbolic --force --no-dereference
82-
conn.run_check(f"ln -sfn {self.rpath}{os.path.basename(self.local_path)} {symlink}")
75+
if isinstance(self.resource, NetworkResource):
76+
try:
77+
conn.run_check(f"test ! -e {symlink} -o -L {symlink}")
78+
except ExecutionError:
79+
raise ManagedFileError(f"Path {symlink} exists but is not a symlink.")
80+
# use short options to be compatible with busybox
81+
# --symbolic --force --no-dereference
82+
conn.run_check(f"ln -sfn {self.rpath}{os.path.basename(self.local_path)} {symlink}")
83+
else:
84+
if os.path.exists(symlink) and not os.path.islink(symlink):
85+
raise ManagedFileError(f"Path {symlink} exists but is not a symlink.")
86+
os.symlink(f"{self.rpath}{os.path.basename(self.local_path)}", symlink)
87+
8388

8489
def _on_nfs(self, conn):
8590
if self._on_nfs_cached is not None:
@@ -131,6 +136,9 @@ def get_remote_path(self):
131136
str: path to the file on the remote host
132137
"""
133138
if isinstance(self.resource, NetworkResource):
139+
if self.rpath is None:
140+
raise ManagedFileError("sync_to_resource() needs to be called before the remote-path can be retrieved")
141+
134142
return f"{self.rpath}{os.path.basename(self.local_path)}"
135143

136144
return self.local_path

tests/test_util.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os.path
2+
import re
23
import subprocess
34
import socket
45
import atexit
@@ -14,7 +15,7 @@
1415
from labgrid.util.helper import get_free_port
1516
from labgrid.util.ssh import ForwardError, SSHConnection, sshmanager
1617
from labgrid.util.proxy import proxymanager
17-
from labgrid.util.managedfile import ManagedFile
18+
from labgrid.util.managedfile import ManagedFile, ManagedFileError
1819
from labgrid.driver.exception import ExecutionError
1920
from labgrid.resource.serialport import NetworkSerialPort
2021
from labgrid.resource.common import Resource, NetworkResource
@@ -352,6 +353,47 @@ def test_local_managedfile(target, tmpdir):
352353
assert hash == mf.get_hash()
353354
assert str(t) == mf.get_remote_path()
354355

356+
def test_network_managedfile_no_sync(target, tmpdir):
357+
res = NetworkResource(target, "test", "localhost")
358+
t = tmpdir.join("test")
359+
t.write(
360+
"""
361+
Test
362+
"""
363+
)
364+
mf = ManagedFile(t, res, detect_nfs=False)
365+
366+
expected = re.escape("sync_to_resource() needs to be called before the remote-path can be retrieved")
367+
with pytest.raises(ManagedFileError, match=expected) as e:
368+
mf.get_remote_path()
369+
370+
def test_local_managedfile_symlink(target, tmpdir):
371+
res = Resource(target, "test")
372+
t = tmpdir.join("test")
373+
t.write(
374+
"""
375+
Test
376+
"""
377+
)
378+
mf = ManagedFile(t, res, detect_nfs=False)
379+
mf.sync_to_resource(symlink=tmpdir.join("link"))
380+
381+
assert str(t) == mf.get_remote_path()
382+
assert os.path.islink(tmpdir.join("link"))
383+
384+
@pytest.mark.localsshmanager
385+
def test_remote_managedfile_symlink(target, tmpdir):
386+
res = NetworkResource(target, "test", "localhost")
387+
t = tmpdir.join("test")
388+
t.write(
389+
"""
390+
Test
391+
"""
392+
)
393+
mf = ManagedFile(t, res, detect_nfs=False)
394+
mf.sync_to_resource(symlink=tmpdir.join("link"))
395+
396+
assert os.path.islink(tmpdir.join("link"))
355397

356398
def test_find_dict():
357399
dict_a = {"a": {"a.a": {"a.a.a": "a.a.a_val"}}, "b": "b_val"}

0 commit comments

Comments
 (0)