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

enrich testing for cidr helpers #179

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
33 changes: 19 additions & 14 deletions sources/api/schnauzer/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2429,18 +2429,22 @@ mod test_cidr_to_ipaddr {
}

#[cfg(test)]
mod test_combined_template_for_ip_cidr {
use crate::helpers::{cidr_to_ipaddr, is_ipv4, replace_ipv4_octet};
mod test_full_template_for_ip_cidrs {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This tests looks good

use crate::helpers::{cidr_to_ipaddr, is_ipv4, replace_ipv4_octet,IfNotNullHelper};
use handlebars::{Handlebars, RenderError};
use serde::Serialize;
use serde_json::json;

const TEMPLATE: &str = r#"
{{#if (is_ipv4 ipcidr)}}
{{ replace_ipv4_octet (cidr_to_ipaddr ipcidr) 3 "10" }}
{{else}}
{{cidr_to_ipaddr ipcidr}}a
{{/if}}
{{#if_not_null ipcidrs}}
{{#each ipcidrs}}
{{#if (is_ipv4 this)}}
bind {{ replace_ipv4_octet (cidr_to_ipaddr this) 3 "10" }}
{{else}}
bind {{cidr_to_ipaddr this}}a
{{/if}}
{{/each}}
{{/if_not_null}}
"#;

fn setup_and_render_template<T>(tmpl: &str, data: &T) -> Result<String, RenderError>
Expand All @@ -2451,24 +2455,25 @@ mod test_combined_template_for_ip_cidr {
registry.register_helper("is_ipv4", Box::new(is_ipv4));
registry.register_helper("cidr_to_ipaddr", Box::new(cidr_to_ipaddr));
registry.register_helper("replace_ipv4_octet", Box::new(replace_ipv4_octet));

registry.register_helper("if_not_null", Box::new(IfNotNullHelper));
registry.render_template(tmpl, data)
}

#[test]
fn test_combined_template_valid_cases() {
fn test_full_template_for_ip_cidrs_valid_cases() {
let test_cases = vec![
(json!({"ipcidr": "192.168.1.0/24"}), "192.168.1.10"), // IPv4 case with replacement
(json!({"ipcidr": "2001:db8::/32"}), "2001:db8::a"), // IPv6 case with 'a' suffix
(
json!({"ipcidrs": ["192.168.1.0/24", "10.100.0.0/16", "2001:db8::/32"]}),
"bind 192.168.1.10\nbind 10.100.0.10\nbind 2001:db8::a"
),
];

test_cases.iter().for_each(|test_case| {
let (config, expected) = test_case;
test_cases.iter().for_each(|(config, expected)| {
let rendered = setup_and_render_template(TEMPLATE, config)
.unwrap()
.trim()
.to_string();
assert_eq!(expected, &rendered);
});
}
}
}
13 changes: 13 additions & 0 deletions sources/api/schnauzer/tests/templates/succeeds/08_ipcidr.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[required-extensions]
std = { version = "v1", helpers = ["if_not_null", "cidr_to_ipaddr", "is_ipv4", "replace_ipv4_octet"] }
coredns = "v1"
+++
{{#if_not_null ipcidrs}}
{{#each ipcidrs}}
{{#if (is_ipv4 this)}}
bind {{ replace_ipv4_octet (cidr_to_ipaddr this) 3 "10" }}
{{else}}
bind {{cidr_to_ipaddr this}}a
{{/if}}
{{/each}}
{{/if_not_null}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
192.168.1.10
10.100.0.10
2001:db8::a
30 changes: 30 additions & 0 deletions sources/api/schnauzer/tests/v2_render_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,33 @@ async fn render_07_aws_config() {
expected_render
);
}

#[tokio::test]
async fn render_08_ipcidr() {
let template = include_str!("./templates/succeeds/08_ipcidr.template");

let expected_render = include_str!("./templates/succeeds_rendered/08_ipcidr.rendered");

let importer = FakeImporter::new(
json!({
"ipcidrs": [
"192.168.1.0/24",
"10.100.0.0/16",
"2001:db8::/32"
]
}),
vec![
("replace_ipv4_octet", schnauzer::helpers::replace_ipv4_octet),
("cidr_to_ipaddr", schnauzer::helpers::cidr_to_ipaddr),
("if_not_null", schnauzer::helpers::IfNotNullHelper),
("is_ipv4", schnauzer::helpers::is_ipv4),
Comment on lines +200 to +201
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Getting below error, need advise for correct way to import them

error[E0308]: mismatched types
   --> api/schnauzer/tests/v2_render_templates.rs:200:29
    |
200 |             ("if_not_null", schnauzer::helpers::IfNotNullHelper),
    |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found `IfNotNullHelper`
    |
    = note: expected fn pointer `for<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j> fn(&'a Helper<'b, 'c>, &'d Handlebars<'e>, &'f handlebars::Context, &'g mut RenderContext<'h, 'i>, &'j mut (dyn handlebars::Output + 'j)) -> Result<(), handlebars::RenderError>`
                   found struct `IfNotNullHelper`

error[E0308]: mismatched types
   --> api/schnauzer/tests/v2_render_templates.rs:201:25
    |
201 |             ("is_ipv4", schnauzer::helpers::is_ipv4),
    |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found `is_ipv4`
    |
    = note: expected fn pointer `for<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j> fn(&'a Helper<'b, 'c>, &'d Handlebars<'e>, &'f handlebars::Context, &'g mut RenderContext<'h, 'i>, &'j mut (dyn handlebars::Output + 'j)) -> Result<(), handlebars::RenderError>`
                   found struct `is_ipv4`

],
);

assert_eq!(
schnauzer::v2::render_template_str(&importer, template)
.await
.unwrap(),
expected_render
);
}