A: share tracing capture and peer config fixtures in tests (gate round 1)

This commit is contained in:
ari committed 2026-10-19 22:31:29 -04:00
1 parent 6bff60c210
commit d3bef6bd94
5 files changed
+106 -136

No files matched your search

+56 -68
View File
@@ -86,32 +86,56 @@ fn slinuxm(config: &Path, args: &[&str]) -> std::process::Output {
.expect("run slinuxm")
}
/// One `beta` peer with a private token file, plus the config that names it.
///
/// `scalars` goes into the `[peers]` table ahead of the entry (timeouts and
/// the like); pass `""` for the defaults.
struct PeerFixture {
dir: TempDir,
token: PathBuf,
config: PathBuf,
}
impl PeerFixture {
fn new(scalars: &str) -> Self {
let dir = TempDir::new();
let token = dir.path().join("peer.token");
write_private_token(&token, SECRET);
let config = dir.path().join("config.toml");
std::fs::write(
&config,
format!(
"[peers]\nenabled = true\n{scalars}\n\
[[peers.peer]]\n\
name = \"beta\"\n\
url = \"http://127.0.0.1:1984\"\n\
token_file = \"{}\"\n",
token.display()
),
)
.unwrap();
Self { dir, token, config }
}
fn dir(&self) -> &Path {
self.dir.path()
}
fn token(&self) -> &Path {
&self.token
}
fn config(&self) -> &Path {
&self.config
}
}
#[test]
fn print_config_shows_peer_paths_and_not_token_contents() {
let dir = TempDir::new();
let token = dir.path().join("peer.token");
write_private_token(&token, SECRET);
let cfg = dir.path().join("config.toml");
std::fs::write(
&cfg,
format!(
"\
[peers]\n\
enabled = true\n\
tcp_interval_secs = 20\n\
tcp_timeout_secs = 4\n\
\n\
[[peers.peer]]\n\
name = \"beta\"\n\
url = \"http://127.0.0.1:1984\"\n\
token_file = \"{}\"\n\
",
token.display()
),
)
.unwrap();
let fixture = PeerFixture::new("tcp_interval_secs = 20\ntcp_timeout_secs = 4");
let (dir, token, cfg) = (fixture.dir(), fixture.token(), fixture.config());
let out = slinuxm(&cfg, &["--print-config"]);
let out = slinuxm(cfg, &["--print-config"]);
let stdout = String::from_utf8(out.stdout).unwrap();
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(
@@ -124,7 +148,7 @@ fn print_config_shows_peer_paths_and_not_token_contents() {
assert!(!stdout.contains("SHOULD_NOT_LEAK"), "{stdout}");
assert!(!stderr.contains("SHOULD_NOT_LEAK"), "{stderr}");
let bare = dir.path().join("empty.toml");
let bare = dir.join("empty.toml");
std::fs::write(&bare, "").unwrap();
let out = slinuxm(&bare, &["--print-config"]);
let stdout = String::from_utf8(out.stdout).unwrap();
@@ -142,28 +166,10 @@ fn print_config_shows_peer_paths_and_not_token_contents() {
#[test]
fn peer_list_prints_config_and_never_the_token() {
let dir = TempDir::new();
let token = dir.path().join("peer.token");
write_private_token(&token, SECRET);
let cfg = dir.path().join("config.toml");
std::fs::write(
&cfg,
format!(
"\
[peers]\n\
enabled = true\n\
\n\
[[peers.peer]]\n\
name = \"beta\"\n\
url = \"http://127.0.0.1:1984\"\n\
token_file = \"{}\"\n\
",
token.display()
),
)
.unwrap();
let fixture = PeerFixture::new("");
let (dir, token, cfg) = (fixture.dir(), fixture.token(), fixture.config());
let out = slinuxm(&cfg, &["peer", "list"]);
let out = slinuxm(cfg, &["peer", "list"]);
let stdout = String::from_utf8(out.stdout).unwrap();
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(out.status.success(), "peer list failed: {stderr}");
@@ -173,7 +179,7 @@ fn peer_list_prints_config_and_never_the_token() {
assert!(!stdout.contains(SECRET), "{stdout}");
assert!(!stderr.contains(SECRET), "{stderr}");
let empty = dir.path().join("empty.toml");
let empty = dir.join("empty.toml");
std::fs::write(&empty, "").unwrap();
let out = slinuxm(&empty, &["peer", "list"]);
let stdout = String::from_utf8(out.stdout).unwrap();
@@ -204,28 +210,10 @@ async fn peer_check_reports_a_probe_through_the_fake_transport() {
#[test]
fn peer_check_binary_reports_the_missing_transport_without_the_token() {
let dir = TempDir::new();
let token = dir.path().join("peer.token");
write_private_token(&token, SECRET);
let cfg = dir.path().join("config.toml");
std::fs::write(
&cfg,
format!(
"\
[peers]\n\
enabled = true\n\
\n\
[[peers.peer]]\n\
name = \"beta\"\n\
url = \"http://127.0.0.1:1984\"\n\
token_file = \"{}\"\n\
",
token.display()
),
)
.unwrap();
let fixture = PeerFixture::new("");
let cfg = fixture.config();
let out = slinuxm(&cfg, &["peer", "check", "beta"]);
let out = slinuxm(cfg, &["peer", "check", "beta"]);
let stdout = String::from_utf8(out.stdout).unwrap();
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(!out.status.success(), "no transport yet: {stdout}");
@@ -233,7 +221,7 @@ fn peer_check_binary_reports_the_missing_transport_without_the_token() {
assert!(!stdout.contains(SECRET), "{stdout}");
assert!(!stderr.contains(SECRET), "{stderr}");
let out = slinuxm(&cfg, &["peer", "check", "nope"]);
let out = slinuxm(cfg, &["peer", "check", "nope"]);
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(!out.status.success());
assert!(stderr.contains("unknown peer"), "{stderr}");