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

Commit

Permalink
Add basic test for coverage example and library (#3035)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Porges authored Apr 19, 2023
1 parent f11ae85 commit 6f06b8f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ env:
CARGO_TERM_COLOR: always
SCCACHE_DIR: ${{github.workspace}}/sccache/
SCCACHE_CACHE_SIZE: 1G
ACTIONS_CACHE_KEY_DATE: 2022-11-21-02
ACTIONS_CACHE_KEY_DATE: 2023-04-19
CI: true

jobs:
Expand Down
11 changes: 0 additions & 11 deletions src/agent/Cargo.lock

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

25 changes: 25 additions & 0 deletions src/agent/coverage/examples/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,28 @@ fn dump_cobertura(binary: &BinaryCoverage) -> Result<()> {

Ok(())
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn can_run_coverage() {
#[cfg(target_os = "linux")]
let cmd = command(&["ls"].map(str::to_string), None);

#[cfg(target_os = "windows")]
let cmd = command(&["cmd.exe", "/c", "dir"].map(str::to_string), None);

let recorded = CoverageRecorder::new(cmd)
.timeout(Duration::from_secs(5))
.record()
.unwrap();

assert_ne!("", recorded.output.stdout);

// only non-debuggable modules are found on Windows
#[cfg(target_os = "linux")]
assert!(recorded.coverage.modules.len() > 0);
}
}
7 changes: 5 additions & 2 deletions src/agent/onefuzz-agent/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

#![allow(clippy::too_many_arguments)]
use std::time::Duration;

use anyhow::{Error, Result};
use tokio::time;

Expand Down Expand Up @@ -29,6 +31,7 @@ pub struct Agent {
last_poll_command: Result<Option<NodeCommand>, PollCommandError>,
managed: bool,
machine_id: uuid::Uuid,
sleep_duration: Duration,
}

impl Agent {
Expand Down Expand Up @@ -59,6 +62,7 @@ impl Agent {
last_poll_command,
managed,
machine_id,
sleep_duration: Duration::from_secs(30),
}
}

Expand Down Expand Up @@ -365,8 +369,7 @@ impl Agent {
}

async fn sleep(&self) {
let delay = time::Duration::from_secs(30);
time::sleep(delay).await;
time::sleep(self.sleep_duration).await;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/agent/onefuzz-agent/src/agent/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ impl Fixture {

#[tokio::test]
async fn test_update_free_no_work() {
let agent = Fixture.agent();
let mut agent = Fixture.agent();
agent.sleep_duration = Duration::from_secs(5);

let (agent, done) = agent.update().await.unwrap();
assert!(!done);
Expand Down
2 changes: 1 addition & 1 deletion src/ci/agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export RUST_BACKTRACE=full

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

# TODO: re-enable integration tests.
# cargo test --release --manifest-path ./onefuzz-task/Cargo.toml --features integration_test -- --nocapture
Expand Down
2 changes: 1 addition & 1 deletion src/ci/rust-prereqs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set -ex

cargo install sccache [email protected] cargo-llvm-cov cargo-deny cargo-insta
cargo install sccache [email protected] cargo-llvm-cov cargo-deny cargo-insta cargo-nextest

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

0 comments on commit 6f06b8f

Please sign in to comment.