From eeae8404fe7bc540d8b36b39ade4b5ab7e6c2e18 Mon Sep 17 00:00:00 2001 From: Laurent Querel Date: Sun, 21 Apr 2024 20:12:43 -0700 Subject: [PATCH] test(coverage): improve coverage of weaver_semconv --- crates/weaver_semconv/src/lib.rs | 63 +++++++++------------------ crates/weaver_semconv/src/registry.rs | 4 +- 2 files changed, 23 insertions(+), 44 deletions(-) diff --git a/crates/weaver_semconv/src/lib.rs b/crates/weaver_semconv/src/lib.rs index 950583d0..0a9a5df6 100644 --- a/crates/weaver_semconv/src/lib.rs +++ b/crates/weaver_semconv/src/lib.rs @@ -56,37 +56,6 @@ pub enum Error { error: String, }, - /// The semantic convention asset contains a duplicate attribute id. - #[error("Duplicate attribute id `{id}` detected while loading {path_or_url:?}, already defined in {origin_path_or_url:?}")] - DuplicateAttributeId { - /// The path or URL where the attribute id was defined for the first time. - origin_path_or_url: String, - /// The path or URL of the semantic convention asset. - path_or_url: String, - /// The duplicated attribute id. - id: String, - }, - - /// The semantic convention asset contains a duplicate group id. - #[error("Duplicate group id `{id}` detected while loading {path_or_url:?} and already defined in {origin}")] - DuplicateGroupId { - /// The path or URL of the semantic convention asset. - path_or_url: String, - /// The duplicated group id. - id: String, - /// The asset where the group id was already defined. - origin: String, - }, - - /// The semantic convention asset contains a duplicate metric name. - #[error("Duplicate metric name `{name}` detected while loading {path_or_url:?}")] - DuplicateMetricName { - /// The path or URL of the semantic convention asset. - path_or_url: String, - /// The duplicated metric name. - name: String, - }, - /// The semantic convention spec contains an invalid group definition. #[error("Invalid group '{group_id}' detected while resolving '{path_or_url:?}'. {error}")] InvalidGroup { @@ -111,13 +80,6 @@ pub enum Error { error: String, }, - /// The attribute reference is not found. - #[error("Attribute reference `{r#ref}` not found.")] - AttributeNotFound { - /// The attribute reference. - r#ref: String, - }, - /// The semantic convention asset contains an invalid metric definition. #[error("Invalid metric definition in {path_or_url:?}.\ngroup_id=`{group_id}`.\n{error}")] InvalidMetric { @@ -231,13 +193,14 @@ pub struct Stats { #[cfg(test)] mod tests { use crate::registry::SemConvRegistry; + use crate::Error; use std::vec; + use weaver_common::error::WeaverError; /// Load multiple semantic convention files in the semantic convention registry. /// No error should be emitted. - /// Spot check one or two pieces of loaded data. #[test] - fn test_load_catalog() { + fn test_valid_semconv_registry() { let yaml_files = vec![ "data/client.yaml", "data/cloud.yaml", @@ -266,10 +229,26 @@ mod tests { "data/tls.yaml", ]; - let mut catalog = SemConvRegistry::default(); + let mut registry = SemConvRegistry::default(); for yaml in yaml_files { - let result = catalog.add_semconv_spec_from_file(yaml); + let result = registry.add_semconv_spec_from_file(yaml); assert!(result.is_ok(), "{:#?}", result.err().unwrap()); } } + + #[test] + fn test_invalid_semconv_registry() { + let yaml_files = vec!["data/invalid.yaml"]; + + let mut registry = SemConvRegistry::default(); + for yaml in yaml_files { + let result = registry.add_semconv_spec_from_file(yaml); + assert!(result.is_err(), "{:#?}", result.ok().unwrap()); + if let Err(err) = result { + assert_eq!(err.errors().len(), 1); + let output = Error::format_errors(&[err]); + assert!(!output.is_empty()); + } + } + } } diff --git a/crates/weaver_semconv/src/registry.rs b/crates/weaver_semconv/src/registry.rs index ed0ecc3e..1ce9f341 100644 --- a/crates/weaver_semconv/src/registry.rs +++ b/crates/weaver_semconv/src/registry.rs @@ -200,7 +200,7 @@ mod tests { use crate::Error; #[test] - fn try_from_path_pattern() { + fn test_try_from_path_pattern() { // Test with a valid path pattern let registry = SemConvRegistry::try_from_path_pattern("test", "data/c*.yaml").unwrap(); assert_eq!(registry.id(), "test"); @@ -216,7 +216,7 @@ mod tests { } #[test] - fn test() { + fn test_semconv_spec_from_url() { let semconv_url = "https://raw.githubusercontent.com/open-telemetry/semantic-conventions/main/model/url.yaml"; let result = SemConvRegistry::semconv_spec_from_url(semconv_url); assert!(result.is_ok());