Expose testfs via QEMU's built-in TFTP server and make sure we can
communicate with it.

This will be especially useful later to test network booting in QEMU.

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 conftest.py             |  1 +
 test/py/test_network.py | 26 +++++++++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/conftest.py b/conftest.py
index ef6ca221a5db..ce5cdaf0dffa 100644
--- a/conftest.py
+++ b/conftest.py
@@ -229,6 +229,7 @@ def strategy(request, target, pytestconfig):  # noqa: 
max-complexity=30
             testfs_path = os.path.join(os.environ["LG_BUILDDIR"], "testfs")
             pytestconfig.option.qemu_fs.append(["testfs", testfs_path])
             os.makedirs(testfs_path, exist_ok=True)
+            strategy.append_qemu_args("-nic", 
f"user,id=net0,tftp={testfs_path}")
 
     for i, fs in enumerate(pytestconfig.option.qemu_fs):
         if virtio:
diff --git a/test/py/test_network.py b/test/py/test_network.py
index e76aad1cc6f6..8994782b7652 100644
--- a/test/py/test_network.py
+++ b/test/py/test_network.py
@@ -6,6 +6,7 @@ from labgrid import driver
 import socket
 import threading
 import re
+from pathlib import Path
 import warnings
 from .helper import skip_disabled
 
@@ -71,15 +72,21 @@ def tftp_conversation(barebox, barebox_interface, 
guestaddr):
         barebox.run_check("ifdown eth0")
 
 
-def test_barebox_network(barebox, barebox_config, env):
-    skip_disabled(barebox_config, "CONFIG_CMD_TFTP")
[email protected](scope="module")
+def testfs_hostname(testfs):
+    with open(Path(testfs) / "hostname", "w", encoding="utf-8") as f:
+        f.write("QEMU\n")
 
+
[email protected](scope="function")
+def barebox_interfaces(barebox, env, testfs):
     # on DUTs without network feature, this is expected to fail
     # set xfail_strict=True to enforce specifying the network feature
     # if available
     if 'network' not in env.get_target_features():
         pytest.xfail("network feature not specified")
 
+    barebox.run_check("ifdown -a")
     stdout = barebox.run_check("ifup -a")
 
     interfaces = []
@@ -94,7 +101,13 @@ def test_barebox_network(barebox, barebox_config, env):
 
     assert interfaces, "Network testing is only possible with at least one 
DHCP interface"
 
-    for iface in interfaces:
+    return interfaces
+
+
+def test_barebox_network_mock_tftp(barebox, barebox_config, 
barebox_interfaces):
+    skip_disabled(barebox_config, "CONFIG_CMD_TFTP")
+
+    for iface in barebox_interfaces:
         ifname = iface[0]
         ifaddr = iface[1]
 
@@ -115,3 +128,10 @@ def test_barebox_network(barebox, barebox_config, env):
         if not success:
             pytest.fail("Could not converse with DUT on any of the found DHCP 
interfaces!")
 
+
+def test_barebox_network_real_tftp(barebox, barebox_config,
+                                   testfs_hostname, barebox_interfaces):
+    skip_disabled(barebox_config, "CONFIG_CMD_TFTP")
+
+    barebox.run_check(f"tftp hostname", timeout=3)
+    assert barebox.run_check(f"cat hostname") == ["QEMU"]
-- 
2.47.3


Reply via email to