Skip to content

Commit 5b5b333

Browse files
ppennaCopilot
andcommitted
tests: check the CLI's behavior instead of its old output (SL3.11a)
Two of the checks that run the nvx-rs that NVX_RS names still compared its output with the words that it inherited from nvx.py. The refusal of an egress policy that is not an object had to read "egress policy must be an object", the message of the Python code that nvx-rs replaced in #597. The help of nvx-rs run had to state "alpine 128" and the other guests' defaults, as nvx.py's help did before #902. Neither wording is a behavior: a reworded message or help failed the checks, while a refusal with another code, or a run that booted a guest with another memory default, passed them. The egress check now reads the refusal through nvx_rs as the scenarios do: its stable code, unsupported, and a message of one line without that code. The memory check now runs nvx-rs run --dry-run for each guest, through nvx_rs.command_line, on placeholder artifacts, and reads the --memory of the OpenVMM command that a run would launch: the memory that guests.py gives test-microvm. nvx_rs.query's docstring no longer says that nvx.py prints the message of the Python code that nvx-rs replaced. On Windows, with nvx-rs built from this tree, both checks passed. With the message and the help reworded, the old checks failed and the new ones passed; with the refusal's code changed to io and Alpine's default memory to 256 MiB, the old checks passed and the new ones failed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 14ca267 commit 5b5b333

2 files changed

Lines changed: 50 additions & 19 deletions

File tree

‎scripts/nvx_tools/nvx_rs.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,8 @@ def query(arguments: Sequence[str]) -> object:
389389
"""Run `nvx-rs --root ROOT ARGUMENTS`, and return the JSON that it prints.
390390
391391
A failure raises NvxRsError with the code and the message of the error
392-
that nvx-rs reported, which nvx.py prints after `error: ` as it printed the
393-
message of the Python code that nvx-rs replaced.
392+
that nvx-rs reported, which nvx.py prints after `error: `. Callers tell
393+
failures apart by the code, since the message may change.
394394
"""
395395
command = command_line(arguments)
396396
try:

‎scripts/test_nvx_tools.py‎

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,21 +2453,49 @@ def test_usage_documents_every_option_of_nvx_rs(self):
24532453
@unittest.skipUnless(
24542454
os.environ.get("NVX_RS"), "set NVX_RS to an nvx-rs executable to run"
24552455
)
2456-
def test_run_help_states_each_guest_memory_default(self):
2456+
def test_run_boots_each_guest_with_its_default_memory(self):
24572457
# nvx-rs run boots each guest with the memory that guests.py gives
2458-
# test-microvm, and its help says so.
2459-
completed = subprocess.run(
2460-
[os.environ["NVX_RS"], "run", "--help"],
2461-
capture_output=True,
2462-
text=True,
2463-
timeout=60,
2464-
check=True,
2465-
)
2466-
help_text = " ".join(completed.stdout.split())
2467-
for name in guests.GUEST_NAMES:
2468-
default = guests.guest_descriptor(name).default_memory_mib
2469-
with self.subTest(guest=name):
2470-
self.assertIn(f"{name} {default}", help_text)
2458+
# test-microvm, as the OpenVMM command of its dry run shows. A dry run
2459+
# launches nothing, so placeholders stand in for the artifacts.
2460+
with tempfile.TemporaryDirectory() as temporary:
2461+
root = Path(temporary).resolve()
2462+
build_dir = BuildConstants.BUILD_DIR.relative_to(BuildConstants.REPO_ROOT)
2463+
for relative in (
2464+
common.openvmm_binary_path().relative_to(BuildConstants.REPO_ROOT),
2465+
build_dir / KernelBuildConstants.BINARY_NAME,
2466+
*(
2467+
build_dir / guests.guest_descriptor(name).initramfs_name
2468+
for name in guests.GUEST_NAMES
2469+
),
2470+
):
2471+
(root / relative).parent.mkdir(parents=True, exist_ok=True)
2472+
(root / relative).write_bytes(b"placeholder\n")
2473+
with patch.dict(os.environ):
2474+
os.environ.pop("NVX_OPENVMM", None)
2475+
for name in guests.GUEST_NAMES:
2476+
default = guests.guest_descriptor(name).default_memory_mib
2477+
with self.subTest(guest=name):
2478+
completed = subprocess.run(
2479+
nvx_rs.command_line(
2480+
("run", "--guest", name, "--dry-run"), root=root
2481+
),
2482+
stdin=subprocess.DEVNULL,
2483+
capture_output=True,
2484+
text=True,
2485+
timeout=60,
2486+
check=True,
2487+
)
2488+
command = completed.stdout.splitlines()[0]
2489+
self.assertTrue(command.startswith(">> "), command)
2490+
words = command.split()
2491+
self.assertEqual(
2492+
[
2493+
value
2494+
for option, value in itertools.pairwise(words)
2495+
if option == "--memory"
2496+
],
2497+
[f"{default}M"],
2498+
)
24712499

24722500
def test_egress_policies_compile_to_the_rules_that_nvx_rs_prints(self):
24732501
with patch.object(
@@ -2522,11 +2550,14 @@ def test_the_real_nvx_rs_prints_what_nvx_py_reads(self):
25222550
allow=("192.0.2.0/25",), deny=("198.51.100.0/24:icmp",)
25232551
),
25242552
)
2553+
# nvx-rs refuses a policy that is not an object with the stable
2554+
# code of its error, and nvx.py reads the message, one line
2555+
# without that code, whatever its wording.
25252556
policy.write_text("[]", encoding="utf-8")
2526-
with self.assertRaisesRegex(
2527-
common.ScriptError, "^egress policy must be an object$"
2528-
):
2557+
with self.assertRaises(nvx_rs.NvxRsError) as refused:
25292558
nvx_rs.compile_egress_policy(policy)
2559+
self.assertEqual(refused.exception.code, "unsupported")
2560+
self.assertRegex(str(refused.exception), r"\A(?!error\[)[^\r\n]+\Z")
25302561
# A caller may pass a path as a command line gave it, which may
25312562
# be relative and start with "-".
25322563
(root / "-policy.json").write_text(

0 commit comments

Comments
 (0)