diff --git a/Cargo.lock b/Cargo.lock index e42eba76..01f2f676 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3629,9 +3629,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.18" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9ff14f98b1a4b289c6248a023c1c2fa1491062964e9fed67ab29c4e4da4a052" +checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4" dependencies = [ "lazy_static", ] diff --git a/Cargo.toml b/Cargo.toml index 765acbd1..c2a1250b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,6 @@ tar = ">=0.4.36" # logger tracing = "0.1.26" tracing-subscriber = "0.2.18" -tracing-test = "0.1.0" # Docker deps # shiplift = "0.6.0" @@ -74,6 +73,7 @@ test-utilities = { path = "test_utilities" } tempdir = "0.3" tempfile = "3.2.0" maplit = "1.0.2" +tracing-test = "0.1.0" [features] default = [] diff --git a/src/logger.rs b/src/logger.rs index 6e240657..cd7990bb 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -47,6 +47,7 @@ impl Logger for StdIoLogger { organization_id = event_details.organisation_id().short(), cluster_id = event_details.cluster_id().short(), execution_id = event_details.execution_id().short(), + provider = event_details.provider_kind().to_string().as_str(), stage = stage.to_string().as_str(), step = stage.sub_step_name().as_str(), transmitter = event_details.transmitter().to_string().as_str(), @@ -65,3 +66,173 @@ impl Logger for StdIoLogger { Box::new(self.clone()) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::cloud_provider::scaleway::application::Region; + use crate::cloud_provider::Kind; + use crate::errors::EngineError; + use crate::events::{EnvironmentStep, EventDetails, EventMessage, InfrastructureStep, Stage, Transmitter}; + use crate::models::QoveryIdentifier; + use tracing_test::traced_test; + use url::Url; + use uuid::Uuid; + + struct TestCase<'a> { + log_level: LogLevel, + event: EngineEvent, + description: &'a str, + } + + #[traced_test] + #[test] + fn test_log() { + // setup: + let orga_id = QoveryIdentifier::new(Uuid::new_v4().to_string()); + let cluster_id = QoveryIdentifier::new(Uuid::new_v4().to_string()); + let cluster_name = format!("qovery-{}", cluster_id); + let execution_id = QoveryIdentifier::new(Uuid::new_v4().to_string()); + let app_id = QoveryIdentifier::new(Uuid::new_v4().to_string()); + let app_name = format!("simple-app-{}", app_id); + let qovery_message = "Qovery message"; + let user_message = "User message"; + let raw_message = "Raw message"; + let raw_message_safe = "Raw message safe"; + let link = Url::parse("https://qovery.com").expect("cannot parse Url"); + let hint = "An hint !"; + + let test_cases = vec![ + TestCase { + log_level: LogLevel::Error, + event: EngineEvent::Error(EngineError::new_unknown( + EventDetails::new( + Kind::Scw, + orga_id.clone(), + cluster_id.clone(), + execution_id.clone(), + Region::Paris.as_str().to_string(), + Stage::Infrastructure(InfrastructureStep::Create), + Transmitter::Kubernetes(cluster_id.to_string(), cluster_name.to_string()), + ), + qovery_message.to_string(), + user_message.to_string(), + Some(raw_message.to_string()), + Some(raw_message_safe.to_string()), + Some(link.clone()), + Some(hint.to_string()), + )), + description: "Error event", + }, + TestCase { + log_level: LogLevel::Info, + event: EngineEvent::Deploying( + EventDetails::new( + Kind::Scw, + orga_id.clone(), + cluster_id.clone(), + execution_id.clone(), + Region::Paris.as_str().to_string(), + Stage::Infrastructure(InfrastructureStep::Create), + Transmitter::Kubernetes(cluster_id.to_string(), cluster_name.to_string()), + ), + EventMessage::new(raw_message.to_string(), Some(raw_message_safe.to_string())), + ), + description: "Deploying info event", + }, + TestCase { + log_level: LogLevel::Debug, + event: EngineEvent::Pausing( + EventDetails::new( + Kind::Scw, + orga_id.clone(), + cluster_id.clone(), + execution_id.clone(), + Region::Paris.as_str().to_string(), + Stage::Environment(EnvironmentStep::Pause), + Transmitter::Application(app_id.to_string(), app_name.to_string()), + ), + EventMessage::new(raw_message.to_string(), Some(raw_message_safe.to_string())), + ), + description: "Pausing application debug event", + }, + TestCase { + log_level: LogLevel::Warning, + event: EngineEvent::Pausing( + EventDetails::new( + Kind::Scw, + orga_id.clone(), + cluster_id.clone(), + execution_id.clone(), + Region::Paris.as_str().to_string(), + Stage::Environment(EnvironmentStep::Delete), + Transmitter::Application(app_id.to_string(), app_name.to_string()), + ), + EventMessage::new(raw_message.to_string(), Some(raw_message_safe.to_string())), + ), + description: "Deleting application warning event", + }, + ]; + + let logger = StdIoLogger::new(); + + for tc in test_cases { + // execute: + logger.log(tc.log_level.clone(), tc.event.clone()); + + // validate: + assert!( + logs_contain(match tc.log_level { + LogLevel::Debug => "DEBUG", + LogLevel::Info => "INFO", + LogLevel::Warning => "WARN", + LogLevel::Error => "ERROR", + }), + "{}", + tc.description + ); + + assert!( + logs_contain(format!("organization_id=\"{}\"", orga_id.short()).as_str()), + "{}", + tc.description + ); + assert!( + logs_contain(format!("cluster_id=\"{}\"", cluster_id.short()).as_str()), + "{}", + tc.description + ); + assert!( + logs_contain(format!("execution_id=\"{}\"", execution_id.short()).as_str()), + "{}", + tc.description + ); + + let details = tc.event.get_details(); + assert!( + logs_contain(format!("provider=\"{}\"", details.provider_kind().to_string()).as_str()), + "{}", + tc.description + ); + + assert!( + logs_contain(format!("stage=\"{}\"", details.stage().to_string()).as_str()), + "{}", + tc.description + ); + assert!( + logs_contain(format!("step=\"{}\"", details.stage().sub_step_name().to_string()).as_str()), + "{}", + tc.description + ); + assert!( + logs_contain(format!("transmitter=\"{}\"", details.transmitter().to_string()).as_str()), + "{}", + tc.description + ); + + let message = tc.event.get_message().to_string(); + assert!(logs_contain(&message), "{}", tc.description); + } + } +} diff --git a/test_utilities/Cargo.lock b/test_utilities/Cargo.lock index 4a49fd3e..b9144781 100644 --- a/test_utilities/Cargo.lock +++ b/test_utilities/Cargo.lock @@ -2138,7 +2138,6 @@ dependencies = [ "tokio 1.10.0", "tracing", "tracing-subscriber", - "tracing-test", "trust-dns-resolver", "url 2.2.2", "uuid 0.8.2", @@ -3717,29 +3716,6 @@ dependencies = [ "tracing-serde", ] -[[package]] -name = "tracing-test" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3b48778c2d401c6a7fcf38a0e3c55dc8e8e753cbd381044a8cdb6fd69a29f53" -dependencies = [ - "lazy_static", - "tracing-core", - "tracing-subscriber", - "tracing-test-macro", -] - -[[package]] -name = "tracing-test-macro" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c49adbab879d2e0dd7f75edace5f0ac2156939ecb7e6a1e8fa14e53728328c48" -dependencies = [ - "lazy_static", - "quote 1.0.8", - "syn 1.0.73", -] - [[package]] name = "trust-dns-proto" version = "0.20.3"