Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 6f06b8f

Browse files
authored
Add basic test for coverage example and library (#3035)
The coverage code is not currently exercised by any test. * Add a test to the `coverage` example so that it can run in PR builds. * Specify `--all-targets` so that example tests are run. * Install and use [`nextest`](https://nexte.st/) instead of the standard test runner. * This will parallelize test runs across binaries so the overall test run is faster. * Make sleep duration of agent configurable and reduce it in the test run so that it doesn't wait for 30 seconds.
1 parent f11ae85 commit 6f06b8f

File tree

7 files changed

+35
-17
lines changed

7 files changed

+35
-17
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ env:
1818
CARGO_TERM_COLOR: always
1919
SCCACHE_DIR: ${{github.workspace}}/sccache/
2020
SCCACHE_CACHE_SIZE: 1G
21-
ACTIONS_CACHE_KEY_DATE: 2022-11-21-02
21+
ACTIONS_CACHE_KEY_DATE: 2023-04-19
2222
CI: true
2323

2424
jobs:

src/agent/Cargo.lock

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/agent/coverage/examples/coverage.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,28 @@ fn dump_cobertura(binary: &BinaryCoverage) -> Result<()> {
167167

168168
Ok(())
169169
}
170+
171+
#[cfg(test)]
172+
mod test {
173+
use super::*;
174+
175+
#[test]
176+
fn can_run_coverage() {
177+
#[cfg(target_os = "linux")]
178+
let cmd = command(&["ls"].map(str::to_string), None);
179+
180+
#[cfg(target_os = "windows")]
181+
let cmd = command(&["cmd.exe", "/c", "dir"].map(str::to_string), None);
182+
183+
let recorded = CoverageRecorder::new(cmd)
184+
.timeout(Duration::from_secs(5))
185+
.record()
186+
.unwrap();
187+
188+
assert_ne!("", recorded.output.stdout);
189+
190+
// only non-debuggable modules are found on Windows
191+
#[cfg(target_os = "linux")]
192+
assert!(recorded.coverage.modules.len() > 0);
193+
}
194+
}

src/agent/onefuzz-agent/src/agent.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the MIT License.
33

44
#![allow(clippy::too_many_arguments)]
5+
use std::time::Duration;
6+
57
use anyhow::{Error, Result};
68
use tokio::time;
79

@@ -29,6 +31,7 @@ pub struct Agent {
2931
last_poll_command: Result<Option<NodeCommand>, PollCommandError>,
3032
managed: bool,
3133
machine_id: uuid::Uuid,
34+
sleep_duration: Duration,
3235
}
3336

3437
impl Agent {
@@ -59,6 +62,7 @@ impl Agent {
5962
last_poll_command,
6063
managed,
6164
machine_id,
65+
sleep_duration: Duration::from_secs(30),
6266
}
6367
}
6468

@@ -365,8 +369,7 @@ impl Agent {
365369
}
366370

367371
async fn sleep(&self) {
368-
let delay = time::Duration::from_secs(30);
369-
time::sleep(delay).await;
372+
time::sleep(self.sleep_duration).await;
370373
}
371374
}
372375

src/agent/onefuzz-agent/src/agent/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ impl Fixture {
8484

8585
#[tokio::test]
8686
async fn test_update_free_no_work() {
87-
let agent = Fixture.agent();
87+
let mut agent = Fixture.agent();
88+
agent.sleep_duration = Duration::from_secs(5);
8889

8990
let (agent, done) = agent.update().await.unwrap();
9091
assert!(!done);

src/ci/agent.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export RUST_BACKTRACE=full
6161

6262
# Run tests and collect coverage
6363
# https://github.com/taiki-e/cargo-llvm-cov
64-
cargo llvm-cov --locked --workspace --lcov --output-path "$output_dir/lcov.info"
64+
cargo llvm-cov nextest --all-targets --locked --workspace --lcov --output-path "$output_dir/lcov.info"
6565

6666
# TODO: re-enable integration tests.
6767
# cargo test --release --manifest-path ./onefuzz-task/Cargo.toml --features integration_test -- --nocapture

src/ci/rust-prereqs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
set -ex
77

8-
cargo install sccache cargo-license@0.4.2 cargo-llvm-cov cargo-deny cargo-insta
8+
cargo install sccache cargo-license@0.4.2 cargo-llvm-cov cargo-deny cargo-insta cargo-nextest
99

1010
# sccache --start-server
1111
# export RUSTC_WRAPPER=$(which sccache)

0 commit comments

Comments
 (0)