Skip to content

Commit 7254a21

Browse files
pb8obchalios
authored andcommitted
test: avoid recompiling examples for every agent
We compile the UFFD and seccomp example programs as a session fixture. If we run the tests in parallel, there is one session per worker and we end up downloading and compiling the examples for each worker (worst case). Instead, use the same approach as the Firecracker and jailer binaries. Signed-off-by: Pablo Barbáchano <[email protected]>
1 parent 8176a8a commit 7254a21

File tree

2 files changed

+23
-43
lines changed

2 files changed

+23
-43
lines changed

tests/conftest.py

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import inspect
2626
import os
27-
import platform
2827
import re
2928
import shutil
3029
import sys
@@ -196,8 +195,8 @@ def change_net_config_space_bin(test_fc_session_root_path):
196195
yield change_net_config_space_bin
197196

198197

199-
@pytest.fixture(scope="session")
200-
def bin_seccomp_paths(test_fc_session_root_path):
198+
@pytest.fixture
199+
def bin_seccomp_paths():
201200
"""Build jailers and jailed binaries to test seccomp.
202201
203202
They currently consist of:
@@ -206,48 +205,20 @@ def bin_seccomp_paths(test_fc_session_root_path):
206205
* a jailed binary that follows the seccomp rules;
207206
* a jailed binary that breaks the seccomp rules.
208207
"""
209-
seccomp_build_path = (
210-
Path(test_fc_session_root_path) / build_tools.CARGO_RELEASE_REL_PATH
211-
)
212-
release_binaries_path = seccomp_build_path / build_tools.RELEASE_BINARIES_REL_PATH
213-
214-
seccomp_examples = ["jailer", "harmless", "malicious", "panic"]
215-
216-
demos = {}
217-
218-
for example in seccomp_examples:
219-
build_tools.cargo_build(
220-
seccomp_build_path,
221-
f"--release --target {platform.machine()}-unknown-linux-musl --example seccomp_{example}",
222-
)
223-
224-
demos[f"demo_{example}"] = release_binaries_path / f"examples/seccomp_{example}"
225-
208+
demos = {
209+
f"demo_{example}": build_tools.get_example(f"seccomp_{example}")
210+
for example in ["jailer", "harmless", "malicious", "panic"]
211+
}
226212
yield demos
227213

228214

229-
@pytest.fixture(scope="session")
230-
def uffd_handler_paths(test_fc_session_root_path):
215+
@pytest.fixture
216+
def uffd_handler_paths():
231217
"""Build UFFD handler binaries."""
232-
uffd_build_path = (
233-
Path(test_fc_session_root_path) / build_tools.CARGO_RELEASE_REL_PATH
234-
)
235-
release_binaries_path = uffd_build_path / build_tools.RELEASE_BINARIES_REL_PATH
236-
237-
uffd_handlers = ["malicious", "valid"]
238-
239-
handlers = {}
240-
241-
for handler in uffd_handlers:
242-
build_tools.cargo_build(
243-
uffd_build_path,
244-
f"--release --target {platform.machine()}-unknown-linux-musl --example uffd_{handler}_handler",
245-
)
246-
247-
handlers[f"{handler}_handler"] = (
248-
release_binaries_path / f"examples/uffd_{handler}_handler"
249-
)
250-
218+
handlers = {
219+
f"{handler}_handler": build_tools.get_example(f"uffd_{handler}_handler")
220+
for handler in ["malicious", "valid"]
221+
}
251222
yield handlers
252223

253224

tests/host_tools/cargo_build.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,20 @@ def cargo_test(path, extra_args=""):
6666

6767

6868
@with_filelock
69-
def get_binary(name, *, workspace_dir=FC_WORKSPACE_DIR):
69+
def get_binary(name, *, workspace_dir=FC_WORKSPACE_DIR, example=False):
7070
"""Build a binary"""
7171
target = DEFAULT_BUILD_TARGET
7272
target_dir = workspace_dir / "build" / "cargo_target"
7373
bin_path = target_dir / target / "release" / name
74+
cmd = f"-p {name}"
75+
if example:
76+
bin_path = target_dir / target / "release" / "examples" / name
77+
cmd = f"--example {name}"
7478
if not bin_path.exists():
7579
env = {"RUSTFLAGS": get_rustflags()}
7680
cargo(
7781
"build",
78-
f"-p {name} --release --target {target}",
82+
f"--release --target {target} {cmd}",
7983
env=env,
8084
cwd=workspace_dir,
8185
)
@@ -95,6 +99,11 @@ def get_firecracker_binaries(*, workspace_dir=FC_WORKSPACE_DIR):
9599
)
96100

97101

102+
def get_example(name, *args, **kwargs):
103+
"""Build an example binary"""
104+
return get_binary(name, *args, **kwargs, example=True)
105+
106+
98107
@with_filelock
99108
def run_seccompiler_bin(bpf_path, json_path=defs.SECCOMP_JSON_DIR, basic=False):
100109
"""

0 commit comments

Comments
 (0)