Skip to content

Commit 27b252d

Browse files
Add tests for distributed cargo building
1 parent 8919a3d commit 27b252d

2 files changed

Lines changed: 131 additions & 3 deletions

File tree

tests/dist.rs

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ extern crate sccache;
77
extern crate serde_json;
88

99
use crate::harness::{
10-
get_stats, sccache_command, start_local_daemon, stop_local_daemon, write_json_cfg, write_source,
10+
cargo_command, get_stats, init_cargo, sccache_command, start_local_daemon, stop_local_daemon,
11+
write_json_cfg, write_source,
1112
};
1213
use assert_cmd::prelude::*;
1314
use sccache::config::HTTPUrl;
@@ -17,6 +18,7 @@ use sccache::dist::{
1718
};
1819
use std::ffi::OsStr;
1920
use std::path::Path;
21+
use std::process::Output;
2022

2123
use sccache::errors::*;
2224

@@ -48,6 +50,48 @@ fn basic_compile(tmpdir: &Path, sccache_cfg_path: &Path, sccache_cached_cfg_path
4850
.success();
4951
}
5052

53+
fn rust_compile(tmpdir: &Path, sccache_cfg_path: &Path, sccache_cached_cfg_path: &Path) -> Output {
54+
let sccache_path = assert_cmd::cargo::cargo_bin("sccache").into_os_string();
55+
let envs: Vec<(_, &OsStr)> = vec![
56+
("RUSTC_WRAPPER", sccache_path.as_ref()),
57+
("CARGO_TARGET_DIR", "target".as_ref()),
58+
("RUST_BACKTRACE", "1".as_ref()),
59+
("SCCACHE_LOG", "debug".as_ref()),
60+
("SCCACHE_CONF", sccache_cfg_path.as_ref()),
61+
("SCCACHE_CACHED_CONF", sccache_cached_cfg_path.as_ref()),
62+
];
63+
let cargo_name = "sccache-dist-test";
64+
let cargo_path = init_cargo(tmpdir, cargo_name);
65+
66+
let manifest_file = "Cargo.toml";
67+
let source_file = "src/main.rs";
68+
69+
write_source(
70+
&cargo_path,
71+
manifest_file,
72+
r#"[package]
73+
name = "sccache-dist-test"
74+
version = "0.1.0"
75+
edition = "2021"
76+
[dependencies]
77+
libc = "0.2.169""#,
78+
);
79+
write_source(
80+
&cargo_path,
81+
source_file,
82+
r#"fn main() {
83+
println!("Hello, world!");
84+
}"#,
85+
);
86+
87+
cargo_command()
88+
.current_dir(cargo_path)
89+
.args(["build", "--release"])
90+
.envs(envs)
91+
.output()
92+
.unwrap()
93+
}
94+
5195
pub fn dist_test_sccache_client_cfg(
5296
tmpdir: &Path,
5397
scheduler_url: HTTPUrl,
@@ -224,3 +268,73 @@ fn test_dist_failingserver() {
224268
assert_eq!(1, info.stats.cache_misses.all());
225269
});
226270
}
271+
272+
#[test]
273+
#[cfg_attr(not(feature = "dist-tests"), ignore)]
274+
fn test_dist_cargo_build() {
275+
let tmpdir = tempfile::Builder::new()
276+
.prefix("sccache_dist_test")
277+
.tempdir()
278+
.unwrap();
279+
let tmpdir = tmpdir.path();
280+
let sccache_dist = harness::sccache_dist_path();
281+
282+
let mut system = harness::DistSystem::new(&sccache_dist, tmpdir);
283+
system.add_scheduler();
284+
let _server_handle = system.add_server();
285+
286+
let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url());
287+
let sccache_cfg_path = tmpdir.join("sccache-cfg.json");
288+
write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg);
289+
let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg");
290+
291+
stop_local_daemon();
292+
start_local_daemon(&sccache_cfg_path, &sccache_cached_cfg_path);
293+
rust_compile(tmpdir, &sccache_cfg_path, &sccache_cached_cfg_path)
294+
.assert()
295+
.success();
296+
get_stats(|info| {
297+
assert_eq!(1, info.stats.dist_compiles.values().sum::<usize>());
298+
assert_eq!(0, info.stats.dist_errors);
299+
assert_eq!(8, info.stats.compile_requests);
300+
assert_eq!(1, info.stats.requests_executed);
301+
assert_eq!(0, info.stats.cache_hits.all());
302+
assert_eq!(1, info.stats.cache_misses.all());
303+
});
304+
}
305+
306+
#[test]
307+
#[cfg_attr(not(feature = "dist-tests"), ignore)]
308+
fn test_dist_cargo_makeflags() {
309+
let tmpdir = tempfile::Builder::new()
310+
.prefix("sccache_dist_test")
311+
.tempdir()
312+
.unwrap();
313+
let tmpdir = tmpdir.path();
314+
let sccache_dist = harness::sccache_dist_path();
315+
316+
let mut system = harness::DistSystem::new(&sccache_dist, tmpdir);
317+
system.add_scheduler();
318+
let _server_handle = system.add_server();
319+
320+
let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url());
321+
let sccache_cfg_path = tmpdir.join("sccache-cfg.json");
322+
write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg);
323+
let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg");
324+
325+
stop_local_daemon();
326+
start_local_daemon(&sccache_cfg_path, &sccache_cached_cfg_path);
327+
let compile_output = rust_compile(tmpdir, &sccache_cfg_path, &sccache_cached_cfg_path);
328+
329+
assert!(!String::from_utf8_lossy(&compile_output.stderr)
330+
.contains("warning: failed to connect to jobserver from environment variable"));
331+
332+
get_stats(|info| {
333+
assert_eq!(1, info.stats.dist_compiles.values().sum::<usize>());
334+
assert_eq!(0, info.stats.dist_errors);
335+
assert_eq!(8, info.stats.compile_requests);
336+
assert_eq!(1, info.stats.requests_executed);
337+
assert_eq!(0, info.stats.cache_hits.all());
338+
assert_eq!(1, info.stats.cache_misses.all());
339+
});
340+
}

tests/harness/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,17 @@ pub fn write_source(path: &Path, filename: &str, contents: &str) {
110110
f.write_all(contents.as_bytes()).unwrap();
111111
}
112112

113+
pub fn init_cargo(path: &Path, cargo_name: &str) -> PathBuf {
114+
let cargo_path = path.join(cargo_name);
115+
let source_path = "src";
116+
fs::create_dir_all(cargo_path.join(source_path)).unwrap();
117+
cargo_path
118+
}
119+
113120
// Prune any environment variables that could adversely affect test execution.
114-
pub fn sccache_command() -> Command {
121+
pub fn prune_command(mut cmd: Command) -> Command {
115122
use sccache::util::OsStrExt;
116123

117-
let mut cmd = Command::new(assert_cmd::cargo::cargo_bin("sccache"));
118124
for (var, _) in env::vars_os() {
119125
if var.starts_with("SCCACHE_") {
120126
cmd.env_remove(var);
@@ -123,6 +129,14 @@ pub fn sccache_command() -> Command {
123129
cmd
124130
}
125131

132+
pub fn sccache_command() -> Command {
133+
prune_command(Command::new(assert_cmd::cargo::cargo_bin("sccache")))
134+
}
135+
136+
pub fn cargo_command() -> Command {
137+
prune_command(Command::new("cargo"))
138+
}
139+
126140
#[cfg(feature = "dist-server")]
127141
pub fn sccache_dist_path() -> PathBuf {
128142
assert_cmd::cargo::cargo_bin("sccache-dist")

0 commit comments

Comments
 (0)