Skip to content

Commit

Permalink
fixup! feat: add integrations with mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Dec 7, 2023
1 parent fa8d238 commit 5d46635
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 16 deletions.
7 changes: 6 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ pub struct AppArgs {
#[arg(long)]
pub working_directory: Option<String>,

/// The name of the environment variable that contains the MongoDB URI to patch,
/// if not provided it will be read from the CODSPEED_MONGO_INSTR_URI_ENV_NAME environment variable
#[arg(long)]
pub mongo_uri_env_name: Option<String>,

/// Only for debugging purposes, skips the upload of the results
#[arg(long, default_value = "false", hide = true)]
pub skip_upload: bool,
Expand All @@ -52,7 +57,7 @@ pub async fn run() -> Result<()> {
let args = AppArgs::parse();
let config = Config::try_from(args)?;
let provider = ci_provider::get_provider(&config)?;
let integrations = Integrations::from_env();
let integrations = Integrations::from(&config);

let log_level = env::var("CODSPEED_LOG")
.ok()
Expand Down
99 changes: 99 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Config {
pub token: Option<String>,
pub working_directory: Option<String>,
pub command: String,
pub mongo_uri_env_name: Option<String>,

pub skip_upload: bool,
pub skip_setup: bool,
Expand All @@ -25,6 +26,7 @@ impl Config {
token: None,
working_directory: None,
command: "".into(),
mongo_uri_env_name: None,
skip_upload: false,
skip_setup: false,
}
Expand All @@ -41,13 +43,110 @@ impl TryFrom<AppArgs> for Config {
.map_err(|e| anyhow!("Invalid upload URL: {}, {}", raw_upload_url, e))?;
let skip_upload = args.skip_upload || env::var("CODSPEED_SKIP_UPLOAD") == Ok("true".into());
let token = args.token.or_else(|| env::var("CODSPEED_TOKEN").ok());
let mongo_uri_env_name = args
.mongo_uri_env_name
.or_else(|| env::var("CODSPEED_MONGO_INSTR_URI_ENV_NAME").ok());
Ok(Self {
upload_url,
token,
working_directory: args.working_directory,
mongo_uri_env_name,
command: args.command.join(" "),
skip_upload,
skip_setup: args.skip_setup,
})
}
}

#[cfg(test)]
mod tests {
use temp_env::{with_var, with_vars};

use super::*;

#[test]
fn test_try_from_env_empty() {
// TODO: this test fails if we remove the `with_var` call, open an issue on https://github.com/vmx/temp-env with a reproduction
with_var("FOO", Some("bar"), || {
let config = Config::try_from(AppArgs {
upload_url: None,
token: None,
working_directory: None,
mongo_uri_env_name: None,
skip_upload: false,
skip_setup: false,
command: vec!["cargo".into(), "codspeed".into(), "bench".into()],
})
.unwrap();
assert_eq!(config.upload_url, Url::parse(DEFAULT_UPLOAD_URL).unwrap());
assert_eq!(config.token, None);
assert_eq!(config.working_directory, None);
assert_eq!(config.mongo_uri_env_name, None);
assert!(!config.skip_upload);
assert!(!config.skip_setup);
assert_eq!(config.command, "cargo codspeed bench");
});
}

#[test]
fn test_try_from_args() {
let config = Config::try_from(AppArgs {
upload_url: Some("https://example.com/upload".into()),
token: Some("token".into()),
working_directory: Some("/tmp".into()),
mongo_uri_env_name: Some("MONGODB_URI".into()),
skip_upload: true,
skip_setup: true,
command: vec!["cargo".into(), "codspeed".into(), "bench".into()],
})
.unwrap();

assert_eq!(
config.upload_url,
Url::parse("https://example.com/upload").unwrap()
);
assert_eq!(config.token, Some("token".into()));
assert_eq!(config.working_directory, Some("/tmp".into()));
assert_eq!(config.mongo_uri_env_name, Some("MONGODB_URI".into()));
assert!(config.skip_upload);
assert!(config.skip_setup);
assert_eq!(config.command, "cargo codspeed bench");
}

#[test]
fn test_try_from_full() {
with_vars(
vec![
("CODSPEED_TOKEN", Some("token_from_env")),
(
"CODSPEED_MONGO_INSTR_URI_ENV_NAME",
Some("MONGODB_URI_FROM_ENV"),
),
("CODSPEED_SKIP_UPLOAD", Some("true")),
],
|| {
let config = Config::try_from(AppArgs {
upload_url: None,
token: None,
working_directory: None,
mongo_uri_env_name: None,
skip_upload: false,
skip_setup: false,
command: vec!["cargo".into(), "codspeed".into(), "bench".into()],
})
.unwrap();

assert_eq!(config.upload_url, Url::parse(DEFAULT_UPLOAD_URL).unwrap());
assert_eq!(config.token, Some("token_from_env".into()));
assert_eq!(config.working_directory, None);
assert_eq!(
config.mongo_uri_env_name,
Some("MONGODB_URI_FROM_ENV".into())
);
assert!(config.skip_upload);
assert!(!config.skip_setup);
assert_eq!(config.command, "cargo codspeed bench");
},
);
}
}
35 changes: 21 additions & 14 deletions src/integrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use std::env;

use crate::config::Config;

pub mod mongo_tracer;

Expand All @@ -20,17 +21,6 @@ pub enum IntegrationNames {
}

impl Integrations {
pub fn from_env() -> Self {
let mongodb_uri_env_name = env::var("CODSPEED_MONGO_INSTR_URI_ENV_NAME");

let mongodb = match mongodb_uri_env_name {
Ok(uri_env_name) => Some(MongoDBConfig { uri_env_name }),
Err(_) => None,
};

Self { mongodb }
}

pub fn is_mongodb_enabled(&self) -> bool {
self.mongodb.is_some()
}
Expand All @@ -46,6 +36,19 @@ impl Integrations {
}
}

impl From<&Config> for Integrations {
fn from(config: &Config) -> Self {
let mongodb = config
.mongo_uri_env_name
.as_ref()
.map(|uri_env_name| MongoDBConfig {
uri_env_name: uri_env_name.clone(),
});

Self { mongodb }
}
}

#[cfg(test)]
impl Integrations {
/// Constructs a new `Integrations` with default values for testing purposes
Expand All @@ -66,7 +69,7 @@ mod tests {

#[test]
fn test_from_env_empty() {
let integrations = Integrations::from_env();
let integrations = Integrations::from(&Config::test());
assert!(integrations.mongodb.is_none());
}

Expand All @@ -76,7 +79,11 @@ mod tests {
"CODSPEED_MONGO_INSTR_URI_ENV_NAME",
Some("MONGODB_URI"),
|| {
let integrations = Integrations::from_env();
let config = Config {
mongo_uri_env_name: Some("MONGODB_URI".into()),
..Config::test()
};
let integrations = Integrations::from(&config);
assert_eq!(
integrations.mongodb,
Some(MongoDBConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/runner/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const MONGODB_TRACER_VERSION: &str = "0.1.2";

async fn install_mongodb_tracer() -> Result<()> {
debug!("Installing mongodb-tracer");
let installer_url = format!("https://codspeed-public-assets.s3.eu-west-1.amazonaws.com/mongo-tracer/v${MONGODB_TRACER_VERSION}/cs-mongo-tracer-installer.sh");
let installer_url = format!("https://codspeed-public-assets.s3.eu-west-1.amazonaws.com/mongo-tracer/v{MONGODB_TRACER_VERSION}/cs-mongo-tracer-installer.sh");
let installer_path = env::temp_dir().join("cs-mongo-tracer-installer.sh");
download_file(
&Url::parse(installer_url.as_str()).unwrap(),
Expand Down

0 comments on commit 5d46635

Please sign in to comment.