Skip to content

Commit

Permalink
Add expect_enum content additional validation
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Feb 29, 2024
1 parent 9cb38b4 commit 8ca2512
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/dsl/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ pub struct Expectation {
pub name: String,
pub expect: Option<String>,
pub expect_same: Option<String>,
pub expect_enum: Option<String>,
pub failure_message: Option<String>,
pub warning_message: Option<String>,
}

#[derive(Debug)]
Expand Down
77 changes: 77 additions & 0 deletions src/dsl/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ pub fn validate(
));
};

if is_expect_enum {
results.append(&mut validate_expect_enum_content(
expectation_expression,
check_id,
index,
));
};

results
})
.partition(Result::is_ok);
Expand Down Expand Up @@ -220,6 +228,32 @@ fn validate_string_expression(
}
}

fn validate_expect_enum_content(
expression: &str,
check_id: &str,
index: usize,
) -> Vec<Result<(), ValidationError>> {
let mut results = vec![];

if !expression.contains("\"passing\"") {
results.push(Err(ValidationError {
check_id: check_id.to_string(),
error: "passing return value not found".to_string(),
instance_path: format!("/expectations/{:?}", index).to_string(),
}));
}

if !expression.contains("\"warning\"") {
results.push(Err(ValidationError {
check_id: check_id.to_string(),
error: "warning return value not found. Consider using `expect` expression if a warning return is not needed".to_string(),
instance_path: format!("/expectations/{:?}", index).to_string(),
}));
}

results
}

pub fn get_json_schema() -> JSONSchema {
let value = serde_json::from_str(SCHEMA).unwrap();

Expand Down Expand Up @@ -883,4 +917,47 @@ mod tests {
);
assert_eq!(validation_errors[1].instance_path, "/expectations/1");
}

#[test]
fn validate_invalid_expect_enum_without_returns() {
let input = r#"
id: 156F64
name: Corosync configuration file
group: Corosync
description: |
Corosync `token` timeout is set to expected value
remediation: |
## Abstract
The value of the Corosync `token` timeout is not set as recommended.
## Remediation
...
facts:
- name: corosync_token_timeout
gatherer: corosync.conf
values:
- name: expected_passing_value
default: 5000
expectations:
- name: timeout
expect_enum: facts.corosync_token_timeout == values.expected_passing_value
"#;

let engine = Engine::new();

let json_value: serde_json::Value =
serde_yaml::from_str(input).expect("Unable to parse yaml");
let json_schema = get_json_schema();
let validation_errors = validate(&json_value, "156F64", &json_schema, &engine).unwrap_err();
assert_eq!(validation_errors[0].check_id, "156F64");
assert_eq!(
validation_errors[0].error,
"passing return value not found"
);
assert_eq!(validation_errors[0].instance_path, "/expectations/0");
assert_eq!(
validation_errors[1].error,
"warning return value not found. Consider using `expect` expression if a warning return is not needed"
);
assert_eq!(validation_errors[1].instance_path, "/expectations/0");
}
}

0 comments on commit 8ca2512

Please sign in to comment.