Skip to content

Commit cb14007

Browse files
committed
refactor(conformance): isolate policy draft assertions
Keep the expected draft type, pure assertion helper, and its three unit tests together in a dedicated scenario module before reorganizing the conformance suite. Preserve the policy behavior and assertion checks. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 3d591af commit cb14007

3 files changed

Lines changed: 79 additions & 70 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Pure assertions for a single scoped mechanistic policy draft.
5+
6+
pub(super) struct ExpectedDraft<'a> {
7+
pub(super) rule: &'a str,
8+
pub(super) endpoint: &'a str,
9+
pub(super) binary: &'a str,
10+
}
11+
12+
pub(super) fn assert_mechanistic_draft(
13+
output: &str,
14+
expected: &ExpectedDraft<'_>,
15+
) -> Result<(), String> {
16+
let fields = output.lines().map(str::trim).collect::<Vec<_>>();
17+
let field = |name: &str| {
18+
fields
19+
.iter()
20+
.find_map(|line| line.strip_prefix(name).map(str::trim))
21+
};
22+
let endpoints = format!("{} [L4]", expected.endpoint);
23+
if fields
24+
.iter()
25+
.filter(|line| line.starts_with("Chunk:"))
26+
.count()
27+
!= 1
28+
|| !matches!(field("Status:"), Some("pending" | "approved"))
29+
|| field("Rule:") != Some(expected.rule)
30+
|| field("Binary:") != Some(expected.binary)
31+
|| field("Binaries:") != Some(expected.binary)
32+
|| field("Endpoints:") != Some(endpoints.as_str())
33+
|| !field("Rationale:").is_some_and(|value| value.contains(expected.endpoint))
34+
{
35+
return Err(format!(
36+
"expected one pending or approved L4 mechanistic draft scoped to {} and {}",
37+
expected.binary, expected.endpoint
38+
));
39+
}
40+
Ok(())
41+
}
42+
43+
#[cfg(test)]
44+
mod tests {
45+
use super::{ExpectedDraft, assert_mechanistic_draft};
46+
47+
const EXPECTED: ExpectedDraft<'static> = ExpectedDraft {
48+
rule: "allow_pypi_org_80",
49+
endpoint: "pypi.org:80",
50+
binary: "/usr/bin/bash",
51+
};
52+
53+
fn draft(binary: &str) -> String {
54+
format!(
55+
"Chunk: id\nStatus: pending\nRule: allow_pypi_org_80\nBinary: {binary}\nRationale: Allow {binary} to connect to pypi.org:80 (HTTP).\nEndpoints: pypi.org:80 [L4]\nBinaries: {binary}\n"
56+
)
57+
}
58+
59+
#[test]
60+
fn draft_assertion_accepts_a_hostname_scoped_draft() {
61+
assert!(assert_mechanistic_draft(&draft("/usr/bin/bash"), &EXPECTED).is_ok());
62+
}
63+
64+
#[test]
65+
fn draft_assertion_rejects_unrelated_binary() {
66+
assert!(assert_mechanistic_draft(&draft("/usr/bin/sh"), &EXPECTED).is_err());
67+
}
68+
69+
#[test]
70+
fn draft_assertion_rejects_fields_spread_across_drafts() {
71+
let drafts = format!(
72+
"{}Chunk: other\nStatus: pending\nRule: allow_1_1_1_1_443\n",
73+
draft("/usr/bin/bash")
74+
);
75+
assert!(assert_mechanistic_draft(&drafts, &EXPECTED).is_err());
76+
}
77+
}

‎crates/openshell-conformance/src/scenarios/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
//! Portable conformance scenarios exercised by the Cargo test suite.
55
6+
mod draft_assertion;
67
mod file_transfer;
78
mod policy_behavior;
89
mod sandbox_lifecycle;

‎crates/openshell-conformance/src/scenarios/policy_behavior.rs‎

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use serde_json::Value;
1111
use tempfile::NamedTempFile;
1212
use tokio::time::sleep;
1313

14+
use super::draft_assertion::{ExpectedDraft, assert_mechanistic_draft};
1415
use crate::{OpenShellRunner, Scenario, ScenarioFuture};
1516

1617
const CREATE_TIMEOUT: Duration = Duration::from_mins(10);
@@ -413,12 +414,6 @@ fn run_new_hostname_proposal(runner: &mut OpenShellRunner) -> ScenarioFuture<'_>
413414
}
414415

415416
/// The single L4 mechanistic draft expected for one denied endpoint.
416-
struct ExpectedDraft<'a> {
417-
rule: &'a str,
418-
endpoint: &'a str,
419-
binary: &'a str,
420-
}
421-
422417
/// Poll the reviewer inbox until a draft appears, tolerating transient read
423418
/// failures, then require that it is the single expected draft.
424419
async fn await_mechanistic_draft(
@@ -458,34 +453,6 @@ async fn await_mechanistic_draft(
458453
}
459454
}
460455

461-
fn assert_mechanistic_draft(output: &str, expected: &ExpectedDraft<'_>) -> Result<(), String> {
462-
let fields = output.lines().map(str::trim).collect::<Vec<_>>();
463-
let field = |name: &str| {
464-
fields
465-
.iter()
466-
.find_map(|line| line.strip_prefix(name).map(str::trim))
467-
};
468-
let endpoints = format!("{} [L4]", expected.endpoint);
469-
if fields
470-
.iter()
471-
.filter(|line| line.starts_with("Chunk:"))
472-
.count()
473-
!= 1
474-
|| !matches!(field("Status:"), Some("pending" | "approved"))
475-
|| field("Rule:") != Some(expected.rule)
476-
|| field("Binary:") != Some(expected.binary)
477-
|| field("Binaries:") != Some(expected.binary)
478-
|| field("Endpoints:") != Some(endpoints.as_str())
479-
|| !field("Rationale:").is_some_and(|value| value.contains(expected.endpoint))
480-
{
481-
return Err(format!(
482-
"expected one pending or approved L4 mechanistic draft scoped to {} and {}",
483-
expected.binary, expected.endpoint
484-
));
485-
}
486-
Ok(())
487-
}
488-
489456
async fn create_sandbox(
490457
runner: &mut OpenShellRunner,
491458
name: &str,
@@ -541,39 +508,3 @@ async fn create_sandbox(
541508
sleep(POLL_INTERVAL).await;
542509
}
543510
}
544-
545-
#[cfg(test)]
546-
mod tests {
547-
use super::{ExpectedDraft, assert_mechanistic_draft};
548-
549-
const EXPECTED: ExpectedDraft<'static> = ExpectedDraft {
550-
rule: "allow_pypi_org_80",
551-
endpoint: "pypi.org:80",
552-
binary: "/usr/bin/bash",
553-
};
554-
555-
fn draft(binary: &str) -> String {
556-
format!(
557-
"Chunk: id\nStatus: pending\nRule: allow_pypi_org_80\nBinary: {binary}\nRationale: Allow {binary} to connect to pypi.org:80 (HTTP).\nEndpoints: pypi.org:80 [L4]\nBinaries: {binary}\n"
558-
)
559-
}
560-
561-
#[test]
562-
fn draft_assertion_accepts_a_hostname_scoped_draft() {
563-
assert!(assert_mechanistic_draft(&draft("/usr/bin/bash"), &EXPECTED).is_ok());
564-
}
565-
566-
#[test]
567-
fn draft_assertion_rejects_unrelated_binary() {
568-
assert!(assert_mechanistic_draft(&draft("/usr/bin/sh"), &EXPECTED).is_err());
569-
}
570-
571-
#[test]
572-
fn draft_assertion_rejects_fields_spread_across_drafts() {
573-
let drafts = format!(
574-
"{}Chunk: other\nStatus: pending\nRule: allow_1_1_1_1_443\n",
575-
draft("/usr/bin/bash")
576-
);
577-
assert!(assert_mechanistic_draft(&drafts, &EXPECTED).is_err());
578-
}
579-
}

0 commit comments

Comments
 (0)