Skip to content

Commit dcabc6a

Browse files
committed
[container_bench] Refactor remote access and platform builders for cross-platform support
Signed-off-by: Jan Rodák <[email protected]>
1 parent 2a1e1ec commit dcabc6a

File tree

9 files changed

+170
-210
lines changed

9 files changed

+170
-210
lines changed

projects/container_bench/testing/config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ test:
209209
# - custom
210210
runtime: # Linux only
211211
- crun
212-
- krun
213212
- runc
214213
capture_metrics:
215214
enabled: true

projects/container_bench/testing/platform_builders.py

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABC, abstractmethod
22
from config_manager import ConfigManager
3+
import shlex
34

45

56
class PlatformCommandBuilder(ABC):
@@ -11,6 +12,38 @@ def build_env_command(self, env_dict):
1112
def build_service_start_script(self, service_name, start_command, binary_path):
1213
pass
1314

15+
@abstractmethod
16+
def build_chdir_command(self, chdir):
17+
pass
18+
19+
@abstractmethod
20+
def build_rm_command(self, file_path, recursive=False):
21+
pass
22+
23+
@abstractmethod
24+
def build_mkdir_command(self, path):
25+
pass
26+
27+
@abstractmethod
28+
def build_exists_command(self, path):
29+
pass
30+
31+
@abstractmethod
32+
def get_shell_command(self):
33+
pass
34+
35+
@abstractmethod
36+
def build_entrypoint_script(self, env_cmd, chdir_cmd, cmd, verbose):
37+
pass
38+
39+
@abstractmethod
40+
def check_exists_result(self, ret):
41+
pass
42+
43+
44+
def escape_powershell_single_quote(value):
45+
return str(value).replace("'", "''")
46+
1447

1548
class WindowsCommandBuilder(PlatformCommandBuilder):
1649
def build_env_command(self, env_dict):
@@ -21,8 +54,7 @@ def build_env_command(self, env_dict):
2154
for k, v in env_dict.items():
2255
if v is None or v == "":
2356
continue
24-
env_commands.append(f"$env:{k}='{v}'")
25-
57+
env_commands.append(f"$env:{escape_powershell_single_quote(k)}='{escape_powershell_single_quote(v)}'")
2658
return "; ".join(env_commands) + ";"
2759

2860
def build_service_start_script(self, service_name, start_command, binary_path):
@@ -66,18 +98,92 @@ def build_service_start_script(self, service_name, start_command, binary_path):
6698
Remove-Item "$env:USERPROFILE\\start_{service_name}.ps1" -Force -ErrorAction SilentlyContinue
6799
"""
68100

101+
def build_chdir_command(self, chdir):
102+
if chdir is None:
103+
return "Set-Location $env:USERPROFILE"
104+
return f"Set-Location '{escape_powershell_single_quote(chdir)}'"
105+
106+
def build_rm_command(self, file_path, recursive=False):
107+
flags = "-Force -ErrorAction SilentlyContinue"
108+
if recursive:
109+
flags += " -Recurse"
110+
return f"Remove-Item '{escape_powershell_single_quote(str(file_path))}' {flags}"
111+
112+
def build_mkdir_command(self, path):
113+
return f"New-Item -ItemType Directory -Path '{escape_powershell_single_quote(str(path))}' -Force"
114+
115+
def build_exists_command(self, path):
116+
return f"Test-Path '{escape_powershell_single_quote(str(path))}'"
117+
118+
def get_shell_command(self):
119+
return "powershell.exe -Command -"
120+
121+
def build_entrypoint_script(self, env_cmd, chdir_cmd, cmd, verbose):
122+
env_section = f"{env_cmd}\n" if env_cmd else ""
123+
script = f"""
124+
$ErrorActionPreference = "Stop"
125+
126+
{env_section}{chdir_cmd}
127+
128+
{cmd}
129+
"""
130+
if verbose:
131+
script = f"$VerbosePreference = 'Continue'\n{script}"
132+
return script
133+
134+
def check_exists_result(self, ret):
135+
return ret.stdout and ret.stdout.strip().lower() == "true"
136+
69137

70138
class UnixCommandBuilder(PlatformCommandBuilder):
71139
def build_env_command(self, env_dict):
72140
if not env_dict:
73141
return ""
74142

75-
env_values = " ".join(f"'{k}={v}'" for k, v in env_dict.items() if v is not None and v != "")
76-
return f"env {env_values}"
143+
env_values = " ".join(f"{k}={shlex.quote(str(v))}" for k, v in env_dict.items() if v is not None and v != "")
144+
return f"export {env_values}\n"
77145

78146
def build_service_start_script(self, service_name, start_command, binary_path) -> str:
79147
return start_command
80148

149+
def build_chdir_command(self, chdir):
150+
if chdir is None:
151+
return "cd $HOME"
152+
return f"cd '{shlex.quote(str(chdir))}'"
153+
154+
def build_rm_command(self, file_path, recursive=False):
155+
flag = "-rf" if recursive else "-f"
156+
return f"rm {flag} {shlex.quote(str(file_path))}"
157+
158+
def build_mkdir_command(self, path):
159+
return f"mkdir -p {shlex.quote(str(path))}"
160+
161+
def build_exists_command(self, path):
162+
return f"test -e {shlex.quote(str(path))}"
163+
164+
def get_shell_command(self):
165+
return "bash"
166+
167+
def build_entrypoint_script(self, env_cmd, chdir_cmd, cmd, verbose):
168+
script = f"""
169+
set -o pipefail
170+
set -o errexit
171+
set -o nounset
172+
set -o errtrace
173+
174+
{env_cmd}
175+
176+
{chdir_cmd}
177+
178+
{cmd}
179+
"""
180+
if verbose:
181+
script = f"set -x\n{script}"
182+
return script
183+
184+
def check_exists_result(self, ret):
185+
return ret.returncode == 0
186+
81187

82188
class PlatformFactory:
83189
@staticmethod

projects/container_bench/testing/prepare.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import shlex
32

43
from container_engine import PodmanMachine, ContainerEngine, DockerDesktopMachine
54
from config_manager import ConfigManager
@@ -8,19 +7,6 @@
87
import utils
98

109

11-
def remove_remote_file(base_work_dir, file_path, recursive=False):
12-
if ConfigManager.is_windows():
13-
flags = "-Force -ErrorAction SilentlyContinue"
14-
if recursive:
15-
flags += " -Recurse"
16-
cmd = f"Remove-Item '{file_path}' {flags}"
17-
else:
18-
flag = "-rf" if recursive else "-f"
19-
cmd = f"rm {flag} {shlex.quote(str(file_path))}"
20-
21-
remote_access.run_with_ansible_ssh_conf(base_work_dir, cmd)
22-
23-
2410
def cleanup():
2511
cleanup_config = ConfigManager.get_extended_cleanup_config()
2612

@@ -30,15 +16,15 @@ def cleanup():
3016
exec_time_script = utils.get_benchmark_script_path(base_work_dir)
3117
if remote_access.exists(exec_time_script):
3218
logging.info(f"Removing {exec_time_script} ...")
33-
remove_remote_file(base_work_dir, exec_time_script)
19+
remote_access.remove_remote_file(base_work_dir, exec_time_script)
3420

3521
if cleanup_config['files_venv']:
3622
logging.info("Cleaning up virtual environment")
3723
base_work_dir = remote_access.prepare()
3824
venv_path = utils.get_benchmark_script_path(base_work_dir).parent / ".venv"
3925
if remote_access.exists(venv_path):
4026
logging.info(f"Removing {venv_path} ...")
41-
remove_remote_file(base_work_dir, venv_path, recursive=True)
27+
remote_access.remove_remote_file(base_work_dir, venv_path, recursive=True)
4228

4329
try:
4430
cleanup_podman_platform()

0 commit comments

Comments
 (0)