Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(validation): validation fails if component has source_config #337

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/wadm-types/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::sync::OnceLock;
use anyhow::{Context as _, Result};
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{LinkProperty, Manifest, TraitProperty, LATEST_VERSION};

Expand Down Expand Up @@ -327,6 +328,7 @@ pub async fn validate_manifest(manifest: &Manifest) -> Result<Vec<ValidationFail
);
failures.extend(check_misnamed_interfaces(manifest));
failures.extend(check_dangling_links(manifest));
failures.extend(check_source_config_on_components(manifest));
Ok(failures)
}

Expand Down Expand Up @@ -403,6 +405,33 @@ fn check_dangling_links(manifest: &Manifest) -> Vec<ValidationFailure> {
failures
}

fn check_source_config_on_components(manifest: &Manifest) -> Vec<ValidationFailure> {
let forbidden_config_key = "source_config";
let mut failures = Vec::new();
let components = manifest.component_lookup();
let component_traits = components
.into_iter()
.filter_map(|(name, component)| match &component.traits {
Some(traits) => Some((name, traits)),
None => None,
});
for (name, traits) in component_traits {
for trait_ in traits {
if let TraitProperty::Custom(custom) = trait_.properties.clone() {
if let Some(_) = custom.get(forbidden_config_key) {
markkovari marked this conversation as resolved.
Show resolved Hide resolved
failures.push(ValidationFailure::new(
ValidationFailureLevel::Error,
format!(
"component [{name}] has source_config in one of its traits properties",
),
))
}
}
}
}
failures
}

#[cfg(test)]
mod tests {
use super::is_valid_manifest_name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: sample_application_with_source_config_on_component
annotations:
version: v0.0.1
description: Sample manifest that does not pass since it has a source_config on a component
spec:
components:
- name: http-component
type: component
properties:
image: ghcr.io/wasmcloud/component-http-hello-world:0.1.0
traits:
- type: spreadscaler
properties:
instances: 1
# This is not allowed since 1.0.0 should error during validation
source_config:
- name: any_value
properties:
SOME_OTHER_KEY: any_other_value
Comment on lines +15 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trait should be a link trait, just because that's where the source_config value lives

26 changes: 25 additions & 1 deletion tests/validation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{Context as _, Result};

use wadm_types::validation::{validate_manifest_file, ValidationFailureLevel, ValidationOutput};
use wadm_types::validation::{validate_manifest_file, ValidationFailure, ValidationFailureLevel, ValidationOutput};

/// Ensure that valid YAML manifests are valid
#[tokio::test]
Expand Down Expand Up @@ -120,3 +120,27 @@ async fn validate_policy() -> Result<()> {
assert!(failures.valid(), "manifest is valid");
Ok(())
}

#[tokio::test]
async fn validate_source_config_on_component_errors() -> Result<()> {
let (_manifest, failures) = validate_manifest_file(
"./tests/fixtures/manifests/with-source-config-on-component.wadm.yaml",
)
.await
.context("failed to validate manifest")?;
assert!(!failures.is_empty(), "failures present, all errors");
let failures = failures.iter().collect::<Vec<_>>();
assert_eq!(failures.len(), 1, "expected one failure");
let ValidationFailure { level, msg, .. } = failures.get(0).expect("expected one failure");
assert_eq!(
*level,
ValidationFailureLevel::Error,
"expected error level"
);
assert_eq!(
msg,
"component [http-component] has source_config in one of its traits properties",
"expected error message, but was incorrect",
);
Ok(())
}