Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 8 additions & 42 deletions apps/rocm/src/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,14 @@ pub(crate) fn unique_suffix() -> String {
)
}

/// Case count for a property test: `PROPTEST_CASES` (proptest's own knob,
/// which an explicit `with_cases` would otherwise override) when it is set and
/// above `default`, otherwise `default`.
///
/// Never below `default`: the counts are chosen so a property catches the
/// defects it targets with near certainty, and some of those are hit by a
/// small share of cases, so a lower count would pass with them present. `0`
/// would pass vacuously.
pub(crate) fn proptest_cases(default: u32) -> u32 {
case_count_from_env("PROPTEST_CASES", default)
}

/// Case count read from `var`, raising `default` but never lowering it, for
/// the same reason as [`proptest_cases`]. The uninstall properties use it with
/// their own knob, `ROCM_DELETION_PROP_CASES`. A value that is ignored,
/// because it is lower or does not parse, is reported on stderr.
pub(crate) fn case_count_from_env(var: &str, default: u32) -> u32 {
let requested = std::env::var(var).ok();
let cases = raised_case_count(requested.as_deref(), default);
if let Some(requested) = requested
&& requested.parse::<u32>().ok() != Some(cases)
{
eprintln!("{var}={requested} ignored: this property runs at least {default} cases");
}
cases
}

/// The pure half of [`case_count_from_env`].
fn raised_case_count(requested: Option<&str>, default: u32) -> u32 {
requested
.and_then(|value| value.parse().ok())
.map_or(default, |requested: u32| requested.max(default))
}
// The case-count rule for property tests that set their own count, shared with
// `rocm-core`, its integration tests and the Lemonade engine rather than copied.
#[path = "../../../crates/rocm-core/tests/common/proptest_cases.rs"]
mod proptest_cases;
pub(crate) use proptest_cases::proptest_cases;
// Only the Unix-only uninstall properties read their own knob.
#[cfg(unix)]
pub(crate) use proptest_cases::case_count_from_env;

/// Split `bytes` at the given (unsorted, possibly duplicated) offsets, each
/// taken modulo `bytes.len() + 1`.
Expand Down Expand Up @@ -475,13 +450,4 @@ mod tests {
(0..64).map(|_| unique_suffix()).collect();
assert_eq!(suffixes.len(), 64, "two calls must never share a suffix");
}

#[test]
fn case_counts_are_raised_but_never_lowered() {
assert_eq!(raised_case_count(None, 256), 256);
assert_eq!(raised_case_count(Some("4096"), 256), 4096);
assert_eq!(raised_case_count(Some("16"), 256), 256);
assert_eq!(raised_case_count(Some("0"), 256), 256);
assert_eq!(raised_case_count(Some("lots"), 256), 256);
}
}
5 changes: 4 additions & 1 deletion crates/rocm-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ ureq = { version = "2.12", features = ["native-certs"] }
#
# Also used by `disk_space`, for the same reason: its unit formatting, margin
# arithmetic and mount selection are small total functions whose contracts are
# easy to state over ALL inputs and awkward to cover with examples.
# easy to state over ALL inputs and awkward to cover with examples. And by the
# integration suites under `tests/` (download digest gate, signature
# verification, model selection and readiness), whose generators aim at near
# misses no example list enumerates.
# Test-only, so it adds nothing to any shipped binary.
proptest = { version = "1", default-features = false, features = ["std"] }
# Archive-extraction property tests (`src/archive_props.rs`) build hostile
Expand Down
7 changes: 4 additions & 3 deletions crates/rocm-core/src/archive_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,10 @@ pub fn run_property(
// `PROPTEST_CASES` (proptest's own knob, which an explicit case count
// would otherwise override) scales every property up for a longer hunt.
// Never down: the reach floors below are percentages, which a handful of
// cases cannot meet. The same rule as rocm-core's `proptest_cases`, kept
// inline because this file is also `#[path]`-included into `rocm` and the
// Lemonade engine, where `crate::test_support` is a different module.
// cases cannot meet. The same rule as `tests/common/proptest_cases.rs`,
// kept inline because this file is also `#[path]`-included into `rocm` and
// the Lemonade engine, and a `#[path]` inside it would resolve relative to
// each includer.
let requested = std::env::var("PROPTEST_CASES").ok();
let cases = match requested.as_deref().map(str::parse::<u32>) {
Some(Ok(requested)) if requested >= cases => requested,
Expand Down
10 changes: 7 additions & 3 deletions crates/rocm-core/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ use crate::disk_space;
/// retry, so this is one initial try plus two retries.
pub const DOWNLOAD_MAX_ATTEMPTS: u32 = 3;

/// Chunk size for the streaming copy. Fixed, so peak memory is independent of
/// the artifact size — the whole point of streaming a multi-gigabyte tarball.
const DOWNLOAD_CHUNK_BYTES: usize = 64 * 1024;
/// Chunk size for the streaming copy.
///
/// Fixed, so peak memory is independent of the artifact size — the whole point
/// of streaming a multi-gigabyte tarball. Public so the digest gate's tests can
/// size a body that crosses a chunk boundary from this value rather than from a
/// copy of it.
pub const DOWNLOAD_CHUNK_BYTES: usize = 64 * 1024;

/// Exponential backoff between download attempts.
///
Expand Down
4 changes: 2 additions & 2 deletions crates/rocm-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ pub use disk_space::{
mount_for_path, on_same_mount, warn_if_low_space, with_margin,
};
pub use download::{
Backoff, DOWNLOAD_MAX_ATTEMPTS, DownloadOutcome, DownloadRequest, download_file_streaming,
download_file_streaming_with_progress, download_file_to_path,
Backoff, DOWNLOAD_CHUNK_BYTES, DOWNLOAD_MAX_ATTEMPTS, DownloadOutcome, DownloadRequest,
download_file_streaming, download_file_streaming_with_progress, download_file_to_path,
download_file_to_path_with_progress,
};
pub use endpoint_key::{endpoint_api_key_has_forbidden_chars, generate_endpoint_api_key};
Expand Down
45 changes: 5 additions & 40 deletions crates/rocm-core/src/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,37 +179,11 @@ pub(crate) fn audit_messages(records: &[AuditEventRecord]) -> Vec<&str> {
records.iter().map(|r| r.message.as_str()).collect()
}

/// Case count for a property test that sets its own count: `PROPTEST_CASES`
/// (proptest's own knob, which an explicit count would otherwise override)
/// when it is set and above `default`, otherwise `default`.
///
/// Never below `default`: the counts are chosen so a property catches the
/// defects it targets with near certainty, and some of those are hit by a
/// small share of cases, so a lower count would pass with them present. `0`
/// would pass vacuously. A value that is ignored, because it is lower or does
/// not parse, is reported on stderr. `archive_props::run_property` applies the
/// same rule with its own copy, since other crates include that file by
/// `#[path]`.
pub(crate) fn proptest_cases(default: u32) -> u32 {
let requested = std::env::var("PROPTEST_CASES").ok();
let cases = raised_case_count(requested.as_deref(), default);
if let Some(requested) = requested
&& requested.parse::<u32>().ok() != Some(cases)
{
eprintln!(
"PROPTEST_CASES={requested} ignored: this property runs at least {default} cases"
);
}
cases
}

/// The pure half of [`proptest_cases`]: `requested` raises `default` and never
/// lowers it.
fn raised_case_count(requested: Option<&str>, default: u32) -> u32 {
requested
.and_then(|value| value.parse().ok())
.map_or(default, |requested: u32| requested.max(default))
}
// The case-count rule for property tests that set their own count, shared with
// `rocm`, the integration tests and the Lemonade engine rather than copied.
#[path = "../tests/common/proptest_cases.rs"]
mod proptest_cases;
pub(crate) use proptest_cases::proptest_cases;

#[cfg(test)]
mod tests {
Expand All @@ -219,13 +193,4 @@ mod tests {
fn temp_app_paths_gives_each_call_its_own_root_even_for_one_label() {
assert_each_call_gets_its_own_root(|| temp_app_paths("same-label").0);
}

#[test]
fn proptest_cases_are_raised_but_never_lowered() {
assert_eq!(raised_case_count(None, 256), 256);
assert_eq!(raised_case_count(Some("4096"), 256), 4096);
assert_eq!(raised_case_count(Some("16"), 256), 256);
assert_eq!(raised_case_count(Some("0"), 256), 256);
assert_eq!(raised_case_count(Some("lots"), 256), 256);
}
}
108 changes: 108 additions & 0 deletions crates/rocm-core/tests/common/loopback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
//
// SPDX-License-Identifier: MIT

//! A loopback HTTP server for the integration tests that drive real network
//! code: it answers every request with one fixed `200 OK` until it is dropped.

use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;

/// Serves one fixed response on an ephemeral loopback port, to any number of
/// requests, until dropped.
///
/// Dropping it never hangs: it wakes the accept loop with a connection of its
/// own before joining, so a client that never connected cannot leave the
/// server thread blocked in `accept`.
pub struct LoopbackServer {
port: u16,
stopping: Arc<AtomicBool>,
served: Arc<AtomicUsize>,
handle: Option<thread::JoinHandle<()>>,
}

impl LoopbackServer {
pub fn start(content_type: Option<&str>, body: Vec<u8>) -> Self {
let listener = TcpListener::bind("127.0.0.1:0").expect("bind loopback");
let port = listener.local_addr().expect("local addr").port();
let stopping = Arc::new(AtomicBool::new(false));
let served = Arc::new(AtomicUsize::new(0));
let content_type = content_type
.map(|value| format!("Content-Type: {value}\r\n"))
.unwrap_or_default();
let handle = thread::spawn({
let stopping = Arc::clone(&stopping);
let served = Arc::clone(&served);
move || {
for stream in listener.incoming() {
if stopping.load(Ordering::SeqCst) {
break;
}
let Ok(mut stream) = stream else { continue };
// A client that connects and never sends cannot hold the
// accept loop, and with it `Drop`, forever.
let _ = stream.set_read_timeout(Some(Duration::from_secs(10)));
read_request_head(&mut stream);
// Counted before the response is written, so a client that
// has its answer always sees its request counted.
served.fetch_add(1, Ordering::SeqCst);
let _ = write!(
stream,
"HTTP/1.1 200 OK\r\n{content_type}Content-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
);
let _ = stream.write_all(&body);
let _ = stream.flush();
}
}
});
Self {
port,
stopping,
served,
handle: Some(handle),
}
}

pub fn url(&self) -> String {
format!("http://127.0.0.1:{}", self.port)
}

/// Requests answered so far. A test that expects a verdict about the
/// response checks this is non-zero, so a request that never reached the
/// server cannot pass as one the code under test rejected.
pub fn requests_served(&self) -> usize {
self.served.load(Ordering::SeqCst)
}
}

impl Drop for LoopbackServer {
fn drop(&mut self) {
self.stopping.store(true, Ordering::SeqCst);
// Wake the accept loop so it sees the flag. If even that connection
// fails, leave the thread detached rather than block on it.
let woken = TcpStream::connect(("127.0.0.1", self.port)).is_ok();
if let Some(handle) = self.handle.take()
&& woken
{
let _ = handle.join();
}
}
}

/// Read the request line and headers, so the response is not written into a
/// request still being sent.
fn read_request_head(stream: &mut TcpStream) {
let mut head = Vec::new();
let mut chunk = [0_u8; 1024];
while !head.windows(4).any(|window| window == b"\r\n\r\n") && head.len() < 64 * 1024 {
match stream.read(&mut chunk) {
Ok(0) | Err(_) => break,
Ok(read) => head.extend_from_slice(&chunk[..read]),
}
}
}
54 changes: 54 additions & 0 deletions crates/rocm-core/tests/common/proptest_cases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
//
// SPDX-License-Identifier: MIT

//! The case-count rule for property tests that set their own count. This file
//! is its one implementation: `rocm`'s and `rocm-core`'s crate-internal
//! `test_support`, the `rocm-core` and `rocm-engine-protocol` integration
//! tests, and the Lemonade engine's unit tests all include it by `#[path]`.
//! `archive_props::run_property` applies the same rule inline, since that file
//! is itself included by `#[path]` into three crates.

/// Case count for a property test: `PROPTEST_CASES` (proptest's own knob,
/// which an explicit count would otherwise override) when it is set and above
/// `default`, otherwise `default`.
///
/// Never below `default`: each count is chosen so its property reaches the
/// cases it targets, and some of those are hit by a small share of cases, so a
/// lower count would pass with the defect present. `PROPTEST_CASES=0` would
/// otherwise pass every property without running one.
pub fn proptest_cases(default: u32) -> u32 {
Comment thread
rominf marked this conversation as resolved.
Resolved
case_count_from_env("PROPTEST_CASES", default)
}

/// Case count read from `var`, raising `default` but never lowering it, for
/// the same reason as [`proptest_cases`]. `rocm`'s uninstall properties use it
/// with their own knob, `ROCM_DELETION_PROP_CASES`. A value that is ignored,
/// because it is lower or does not parse, is reported on stderr.
pub fn case_count_from_env(var: &str, default: u32) -> u32 {
let requested = std::env::var(var).ok();
let cases = raised_case_count(requested.as_deref(), default);
if let Some(requested) = requested
&& requested.parse::<u32>().ok() != Some(cases)
{
eprintln!("{var}={requested} ignored: this property runs at least {default} cases");
}
cases
}

/// The pure half of [`case_count_from_env`]: `requested` raises `default` and
/// never lowers it.
pub fn raised_case_count(requested: Option<&str>, default: u32) -> u32 {
requested
.and_then(|value| value.parse().ok())
.map_or(default, |requested: u32| requested.max(default))
}

#[test]
fn proptest_cases_can_raise_the_default_but_never_lower_it() {
assert_eq!(raised_case_count(None, 24), 24);
assert_eq!(raised_case_count(Some("1000"), 24), 1000);
assert_eq!(raised_case_count(Some("0"), 24), 24);
assert_eq!(raised_case_count(Some("8"), 24), 24);
assert_eq!(raised_case_count(Some("lots"), 24), 24);
}
Loading
Loading