Skip to content

Commit bc6de2e

Browse files
committed
util/managedfile: error on unsynced retrieval
For the managedfile, if it is remote, the rpath will only be available once the resource has been synced to the remote host. Raise an error if retrieval is attempted before synchronization. Signed-off-by: Rouven Czerwinski <[email protected]>
1 parent e43d093 commit bc6de2e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

labgrid/util/managedfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ def get_remote_path(self):
131131
str: path to the file on the remote host
132132
"""
133133
if isinstance(self.resource, NetworkResource):
134+
if self.rpath is None:
135+
raise ManagedFileError("sync_to_resource() needs to be called before the remote-path can be retrieved")
136+
134137
return f"{self.rpath}{os.path.basename(self.local_path)}"
135138

136139
return self.local_path

tests/test_util.py

Lines changed: 18 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,22 @@ 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+
import hashlib
358+
359+
res = NetworkResource(target, "localhost", "test")
360+
t = tmpdir.join("test")
361+
t.write(
362+
"""
363+
Test
364+
"""
365+
)
366+
mf = ManagedFile(t, res, detect_nfs=False)
367+
368+
expected = re.escape("sync_to_resource() needs to be called before the remote-path can be retrieved")
369+
with pytest.raises(ManagedFileError, match=expected) as e:
370+
mf.get_remote_path()
371+
355372

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

0 commit comments

Comments
 (0)