Skip to content

Commit

Permalink
chore: fail fast for unsupported report formats
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Aug 26, 2024
1 parent 9387493 commit 8dd5306
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
13 changes: 2 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ use std::hash::{Hash, Hasher};
use std::sync::{atomic::AtomicUsize, Arc, RwLock};
use std::time::{self, Duration};
use std::{fmt, io};
use tokio::fs::File;

use crate::config::{GooseConfiguration, GooseDefaults};
use crate::controller::{ControllerProtocol, ControllerRequest};
Expand Down Expand Up @@ -1723,16 +1722,8 @@ impl GooseAttack {
goose_attack_run_state.throttle_threads_tx = throttle_threads_tx;
goose_attack_run_state.parent_to_throttle_tx = parent_to_throttle_tx;

// If enabled, try to create the report file to confirm access.
for file in &self.configuration.report_file {
let _ = File::create(&file)
.await
.map_err(|err| GooseError::InvalidOption {
option: "--report-file".to_string(),
value: file.clone(),
detail: format!("Failed to create report file: {err}"),
})?;
}
// Try to create the requested report files, to confirm access.
self.create_reports().await?;

// Record when the GooseAttack officially started.
self.started = Some(time::Instant::now());
Expand Down
32 changes: 27 additions & 5 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3003,8 +3003,11 @@ impl GooseAttack {
};
}

/// Write all requested reports.
pub(crate) async fn write_reports(&self) -> Result<(), GooseError> {
/// Process all requested reports.
///
/// If `write` is true, then also write the data. Otherwise just create the files to ensure
/// we have access.
async fn process_reports(&self, write: bool) -> Result<(), GooseError> {
let create = |path: PathBuf| async move {
File::create(&path)
.await
Expand All @@ -3019,13 +3022,22 @@ impl GooseAttack {
let path = PathBuf::from(report);
match path.extension().map(OsStr::to_string_lossy).as_deref() {
Some("html" | "htm") => {
self.write_html_report(create(path).await?, report).await?;
let file = create(path).await?;
if write {
self.write_html_report(file, report).await?;
}
}
Some("json") => {
self.write_json_report(create(path).await?).await?;
let file = create(path).await?;
if write {
self.write_json_report(file).await?;
}
}
Some("md") => {
self.write_markdown_report(create(path).await?).await?;
let file = create(path).await?;
if write {
self.write_markdown_report(file).await?;
}
}
None => {
return Err(GooseError::InvalidOption {
Expand All @@ -3047,6 +3059,16 @@ impl GooseAttack {
Ok(())
}

/// Create all requested reports, to ensure we have access.
pub(crate) async fn create_reports(&self) -> Result<(), GooseError> {
self.process_reports(false).await
}

/// Write all requested reports.
pub(crate) async fn write_reports(&self) -> Result<(), GooseError> {
self.process_reports(true).await
}

/// Write a JSON report.
pub(crate) async fn write_json_report(&self, report_file: File) -> Result<(), GooseError> {
let data = common::prepare_data(
Expand Down

0 comments on commit 8dd5306

Please sign in to comment.