From f5137683052810c964a817c1a9680de21da8ef87 Mon Sep 17 00:00:00 2001 From: David Pitoniak <84917393+pitoniak32@users.noreply.github.com> Date: Tue, 10 Dec 2024 15:16:48 -0500 Subject: [PATCH] test: use rstest for table testing sdk resource (#2407) Co-authored-by: Cijo Thomas --- opentelemetry-sdk/Cargo.toml | 1 + opentelemetry-sdk/src/resource/mod.rs | 90 +++++++++++++++------------ 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/opentelemetry-sdk/Cargo.toml b/opentelemetry-sdk/Cargo.toml index f140f9702b..eb5a4862c2 100644 --- a/opentelemetry-sdk/Cargo.toml +++ b/opentelemetry-sdk/Cargo.toml @@ -35,6 +35,7 @@ rustdoc-args = ["--cfg", "docsrs"] [dev-dependencies] criterion = { workspace = true, features = ["html_reports"] } +rstest = "0.23.0" temp-env = { workspace = true } [target.'cfg(not(target_os = "windows"))'.dev-dependencies] diff --git a/opentelemetry-sdk/src/resource/mod.rs b/opentelemetry-sdk/src/resource/mod.rs index 1b46279085..3663786535 100644 --- a/opentelemetry-sdk/src/resource/mod.rs +++ b/opentelemetry-sdk/src/resource/mod.rs @@ -263,6 +263,8 @@ pub trait ResourceDetector { #[cfg(test)] mod tests { + use rstest::rstest; + use super::*; #[test] @@ -308,47 +310,55 @@ mod tests { assert_eq!(resource_a.merge(&resource_b), expected_resource); } - #[test] - fn merge_resource_schema_url() { - // if both resources contains key value pairs - let test_cases = vec![ - (Some("http://schema/a"), None, Some("http://schema/a")), - (Some("http://schema/a"), Some("http://schema/b"), None), - (None, Some("http://schema/b"), Some("http://schema/b")), - ( - Some("http://schema/a"), - Some("http://schema/a"), - Some("http://schema/a"), - ), - (None, None, None), - ]; - - for (schema_url_a, schema_url_b, expected_schema_url) in test_cases.into_iter() { - let resource_a = Resource::from_schema_url( - vec![KeyValue::new("key", "")], - schema_url_a.unwrap_or(""), - ); - let resource_b = Resource::from_schema_url( - vec![KeyValue::new("key", "")], - schema_url_b.unwrap_or(""), - ); - - let merged_resource = resource_a.merge(&resource_b); - let result_schema_url = merged_resource.schema_url(); - - assert_eq!( - result_schema_url.map(|s| s as &str), - expected_schema_url, - "Merging schema_url_a {:?} with schema_url_b {:?} did not yield expected result {:?}", - schema_url_a, schema_url_b, expected_schema_url - ); - } - - // if only one resource contains key value pairs - let resource = Resource::from_schema_url(vec![], "http://schema/a"); - let other_resource = Resource::new(vec![KeyValue::new("key", "")]); + #[rstest] + #[case(Some("http://schema/a"), None, Some("http://schema/a"))] + #[case(Some("http://schema/a"), Some("http://schema/b"), None)] + #[case(None, Some("http://schema/b"), Some("http://schema/b"))] + #[case( + Some("http://schema/a"), + Some("http://schema/a"), + Some("http://schema/a") + )] + #[case(None, None, None)] + fn merge_resource_schema_url( + #[case] schema_url_a: Option<&'static str>, + #[case] schema_url_b: Option<&'static str>, + #[case] expected_schema_url: Option<&'static str>, + ) { + let resource_a = + Resource::from_schema_url(vec![KeyValue::new("key", "")], schema_url_a.unwrap_or("")); + let resource_b = + Resource::from_schema_url(vec![KeyValue::new("key", "")], schema_url_b.unwrap_or("")); + + let merged_resource = resource_a.merge(&resource_b); + let result_schema_url = merged_resource.schema_url(); + + assert_eq!( + result_schema_url.map(|s| s as &str), + expected_schema_url, + "Merging schema_url_a {:?} with schema_url_b {:?} did not yield expected result {:?}", + schema_url_a, + schema_url_b, + expected_schema_url + ); + } - assert_eq!(resource.merge(&other_resource).schema_url(), None); + #[rstest] + #[case(vec![], vec![KeyValue::new("key", "b")], "http://schema/a", None)] + #[case(vec![KeyValue::new("key", "a")], vec![KeyValue::new("key", "b")], "http://schema/a", Some("http://schema/a"))] + fn merge_resource_with_missing_attribtes( + #[case] key_values_a: Vec, + #[case] key_values_b: Vec, + #[case] schema_url: &'static str, + #[case] expected_schema_url: Option<&'static str>, + ) { + let resource = Resource::from_schema_url(key_values_a, schema_url); + let other_resource = Resource::new(key_values_b); + + assert_eq!( + resource.merge(&other_resource).schema_url(), + expected_schema_url + ); } #[test]