diff --git a/src/client/get_deployment.rs b/src/client/get_deployment.rs index 99d73a2..593c661 100644 --- a/src/client/get_deployment.rs +++ b/src/client/get_deployment.rs @@ -122,7 +122,7 @@ mod tests { mongodb_load_sample_data: None, mongot_log_file: None, runner_log_file: None, - do_not_track: None, + do_not_track: false, telemetry_base_url: None, } ); diff --git a/src/models/deployment.rs b/src/models/deployment.rs index c1e097e..c55d503 100644 --- a/src/models/deployment.rs +++ b/src/models/deployment.rs @@ -33,14 +33,14 @@ pub struct Deployment { pub mongodb_initdb_root_password: Option, pub mongodb_initdb_root_username_file: Option, pub mongodb_initdb_root_username: Option, - pub mongodb_load_sample_data: Option, + pub mongodb_load_sample_data: Option, // Logging pub mongot_log_file: Option, pub runner_log_file: Option, // Telemetry - pub do_not_track: Option, + pub do_not_track: bool, pub telemetry_base_url: Option, } @@ -125,14 +125,15 @@ impl TryFrom for Deployment { mongodb_initdb_root_password, mongodb_initdb_root_username_file, mongodb_initdb_root_username, - mongodb_load_sample_data, + mongodb_load_sample_data: mongodb_load_sample_data.map(is_seeding_true), // Logging mongot_log_file, runner_log_file, // Telemetry - do_not_track, + // If the DO_NOT_TRACK environment variable is set, do not track is enabled + do_not_track: do_not_track.is_some(), telemetry_base_url, }) } @@ -152,6 +153,22 @@ fn extract_local_seed_location( mount.source.clone() } +/// Determine if the value is a boolean or an integer that is larger than 0, and return true if it is +fn is_seeding_true(value: impl AsRef) -> bool { + let value = value.as_ref(); + // Try to parse the value as a boolean, if it succeeds, return the value + if let Ok(value) = value.to_ascii_lowercase().parse::() { + return value; + } + + // Try to parse the value as an integer, if it succeeds, return true if the value is larger than 0 + if let Ok(value) = value.parse::() { + return value > 0; + } + + false +} + #[cfg(test)] mod tests { use super::*; @@ -179,7 +196,6 @@ mod tests { "MONGODB_INITDB_DATABASE=testdb".to_string(), "RUNNER_LOG_FILE=/tmp/runner.log".to_string(), "MONGOT_LOG_FILE=/tmp/mongot.log".to_string(), - "DO_NOT_TRACK=false".to_string(), "TELEMETRY_BASE_URL=https://telemetry.example.com".to_string(), "MONGODB_LOAD_SAMPLE_DATA=true".to_string(), ]; @@ -271,15 +287,12 @@ mod tests { deployment.mongot_log_file, Some("/tmp/mongot.log".to_string()) ); - assert_eq!(deployment.do_not_track, Some("false".to_string())); + assert_eq!(deployment.do_not_track, false); assert_eq!( deployment.telemetry_base_url, Some("https://telemetry.example.com".to_string()) ); - assert_eq!( - deployment.mongodb_load_sample_data, - Some("true".to_string()) - ); + assert_eq!(deployment.mongodb_load_sample_data, Some(true)); } #[test] @@ -342,4 +355,19 @@ mod tests { let result = extract_local_seed_location(&container_inspect_response); assert_eq!(result, Some("/host/seed-data".to_string())); } + + #[test] + fn test_is_seeding_true() { + // True values + assert!(is_seeding_true("true")); + assert!(is_seeding_true("True")); + assert!(is_seeding_true("TRUE")); + assert!(is_seeding_true("1")); + assert!(is_seeding_true("2")); + + // False values + assert!(!is_seeding_true("false")); + assert!(!is_seeding_true("0")); + assert!(!is_seeding_true("something else")); + } }