diff --git a/test/unit/test_runtime.py b/test/unit/test_runtime.py index 6dc22d95..0a8824c5 100644 --- a/test/unit/test_runtime.py +++ b/test/unit/test_runtime.py @@ -118,6 +118,22 @@ def test_pre_run_docker(tmp_path): ) +def test_pre_run_docker_default_volume(tmp_path): + runtime = Runtime.select("docker")(tmp_path) + runtime.pre_run(tmp_path) + wrapper = (tmp_path / "docker").read_text(encoding="utf-8") + assert str(tmp_path / "dispatcher" / "tmp") in wrapper + + +def test_pre_run_docker_custom_volume(tmp_path): + custom_volume = tmp_path / "custom" / "downloads" + runtime = Runtime.select("docker")(tmp_path) + runtime.pre_run(tmp_path, volume=custom_volume) + wrapper = (tmp_path / "docker").read_text(encoding="utf-8") + assert str(custom_volume) in wrapper + assert str(tmp_path / "dispatcher" / "tmp") not in wrapper + + def test_pre_run_null(tmp_path): runtime = Runtime.select("null")(tmp_path) runtime.pre_run(None) @@ -135,6 +151,30 @@ def test_pre_run_podman(mocker, tmp_path): run.assert_called_once() +def test_pre_run_podman_default_volume(mocker, tmp_path): + (tmp_path / "podman.sock").touch() + mocker.patch("subprocess.Popen") + mocker.patch("subprocess.run") + + runtime = Runtime.select("podman")(tmp_path) + runtime.pre_run(tmp_path) + wrapper = (tmp_path / "docker").read_text(encoding="utf-8") + assert str(tmp_path / "dispatcher" / "tmp") in wrapper + + +def test_pre_run_podman_custom_volume(mocker, tmp_path): + (tmp_path / "podman.sock").touch() + mocker.patch("subprocess.Popen") + mocker.patch("subprocess.run") + + custom_volume = tmp_path / "custom" / "downloads" + runtime = Runtime.select("podman")(tmp_path) + runtime.pre_run(tmp_path, volume=custom_volume) + wrapper = (tmp_path / "docker").read_text(encoding="utf-8") + assert str(custom_volume) in wrapper + assert str(tmp_path / "dispatcher" / "tmp") not in wrapper + + def test_pre_run_podman_errors(mocker, tmp_path): popen = mocker.patch("subprocess.Popen") run = mocker.patch("subprocess.run") diff --git a/tuxrun/__main__.py b/tuxrun/__main__.py index 6ce970eb..250d7be1 100644 --- a/tuxrun/__main__.py +++ b/tuxrun/__main__.py @@ -355,14 +355,17 @@ def handler(*_): if job.device.flag_use_pre_run_cmd or job.qemu_image or options.device_dict: LOG.debug("Pre run command") if options.device_dict: + options.dispatcher_download_dir.mkdir(parents=True, exist_ok=True) runtime.bind(options.dispatcher_download_dir, Path("/srv/tftp")) runtime.bind( options.dispatcher_download_dir, options.dispatcher_download_dir ) + volume = options.dispatcher_download_dir else: runtime.bind(tmpdir / "dispatcher" / "tmp", options.dispatcher_download_dir) (tmpdir / "dispatcher" / "tmp").mkdir() - runtime.pre_run(tmpdir) + volume = tmpdir / "dispatcher" / "tmp" + runtime.pre_run(tmpdir, volume=volume) # Build the lava-run arguments list args = [ diff --git a/tuxrun/runtimes.py b/tuxrun/runtimes.py index e52418d6..c615b9ed 100644 --- a/tuxrun/runtimes.py +++ b/tuxrun/runtimes.py @@ -53,7 +53,7 @@ def image(self, image): def name(self, name): self.__name__ = name - def pre_run(self, tmpdir): + def pre_run(self, tmpdir, volume=None): pass def post_run(self): @@ -112,6 +112,12 @@ class ContainerRuntime(Runtime): _use_host_network = False _skip_http_server = False + @staticmethod + def _resolve_volume(tmpdir, volume): + if volume is None: + return tmpdir / "dispatcher" / "tmp" + return volume + def __init__(self, dispatcher_download_dir): super().__init__(dispatcher_download_dir) self.bind("/boot", ro=True) @@ -173,14 +179,14 @@ class DockerRuntime(ContainerRuntime): binary = "docker" prefix = ["docker", "run", "--rm", "--hostname", "tuxrun"] - def pre_run(self, tmpdir): - # Render and bind the docker wrapper + def pre_run(self, tmpdir, volume=None): + volume = self._resolve_volume(tmpdir, volume) wrap = ( wrappers() .get_template("docker.jinja2") .render( runtime="docker", - volume=str(tmpdir / "dispatcher" / "tmp"), + volume=str(volume), dispatcher_download_dir=self.dispatcher_download_dir, ) ) @@ -199,8 +205,8 @@ class PodmanRuntime(ContainerRuntime): prefix = ["podman", "run", "--log-driver=none", "--rm", "--hostname", "tuxrun"] network = None - def pre_run(self, tmpdir): - # Render and bind the docker wrapper + def pre_run(self, tmpdir, volume=None): + volume = self._resolve_volume(tmpdir, volume) self.network = os.path.basename(tmpdir) subprocess.run(["podman", "network", "create", self.network]) if self.qemu_image is None: @@ -210,7 +216,7 @@ def pre_run(self, tmpdir): .get_template("docker.jinja2") .render( runtime="podman", - volume=str(tmpdir / "dispatcher" / "tmp"), + volume=str(volume), network=self.network, dispatcher_download_dir=self.dispatcher_download_dir, )