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

netdog: Make net_config unit tests reusable across versions #2385

Merged
merged 2 commits into from
Sep 1, 2022
Merged
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
1 change: 1 addition & 0 deletions sources/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sources/api/netdog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ toml = { version = "0.5", features = ["preserve_order"] }

[dev-dependencies]
tempfile = "3.2.0"
handlebars = "4.3"

[build-dependencies]
generate-readme = { version = "0.1", path = "../../generate-readme" }
133 changes: 14 additions & 119 deletions sources/api/netdog/src/net_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ where
Ok(Some(Box::new(net_config)))
}

#[cfg(test)]
mod test_macros;
#[cfg(test)]
mod tests {
use std::path::PathBuf;
Expand All @@ -165,139 +167,32 @@ mod tests {
}

#[test]
fn ok_cmdline() {
let cmdline = cmdline().join("ok");
assert!(from_command_line(cmdline).unwrap().is_some());
}

#[test]
fn multiple_interface_from_cmdline() {
let cmdline = cmdline().join("multiple_interface");
assert!(from_command_line(cmdline).is_err())
}

#[test]
fn no_interfaces_cmdline() {
let cmdline = cmdline().join("no_interfaces");
assert!(from_command_line(cmdline).unwrap().is_none())
}

#[test]
fn invalid_version() {
let bad = net_config().join("bad_version.toml");
assert!(from_path(bad).is_err())
}

#[test]
fn ok_config() {
fn ok_net_config() {
let ok = net_config().join("net_config.toml");
assert!(from_path(ok).is_ok())
}

#[test]
fn invalid_interface_config() {
let bad = net_config().join("invalid_interface_config.toml");
assert!(from_path(bad).is_err())
}

#[test]
fn invalid_dhcp4_config() {
let bad = net_config().join("invalid_dhcp4_config.toml");
assert!(from_path(bad).is_err())
}

#[test]
fn invalid_dhcp6_config() {
let bad = net_config().join("invalid_dhcp6_config.toml");
assert!(from_path(bad).is_err())
}

#[test]
fn invalid_dhcp_config() {
let ok = net_config().join("invalid_dhcp_config.toml");
assert!(from_path(ok).is_err())
}

#[test]
fn dhcp4_missing_enable() {
let bad = net_config().join("dhcp4_missing_enabled.toml");
assert!(from_path(bad).is_err())
assert!(from_path(ok).unwrap().is_some())
}

#[test]
fn dhcp6_missing_enable() {
let bad = net_config().join("dhcp6_missing_enabled.toml");
assert!(from_path(bad).is_err())
}

#[test]
fn no_interfaces() {
fn no_interfaces_net_config() {
let bad = net_config().join("no_interfaces.toml");
assert!(from_path(bad).unwrap().is_none())
}

#[test]
fn defined_primary_interface() {
let ok_path = net_config().join("net_config.toml");
let cfg = from_path(ok_path).unwrap().unwrap();

let expected = "eno2";
let actual = cfg.primary_interface().unwrap();
assert_eq!(expected, actual)
}

#[test]
fn undefined_primary_interface() {
let ok_path = net_config().join("no_primary.toml");
let cfg = from_path(ok_path).unwrap().unwrap();

let expected = "eno3";
let actual = cfg.primary_interface().unwrap();
println!("{}", &actual);
assert_eq!(expected, actual)
}

#[test]
fn multiple_primary_interfaces() {
let multiple = net_config().join("multiple_primary.toml");
assert!(from_path(multiple).is_err())
fn ok_cmdline() {
let cmdline = cmdline().join("ok");
assert!(from_command_line(cmdline).unwrap().is_some());
}

#[test]
fn ok_interface_from_str() {
let ok = &[
"eno1:dhcp4,dhcp6",
"eno1:dhcp4,dhcp6?",
"eno1:dhcp4?,dhcp6",
"eno1:dhcp4?,dhcp6?",
"eno1:dhcp6?,dhcp4?",
"eno1:dhcp4",
"eno1:dhcp4?",
"eno1:dhcp6",
"eno1:dhcp6?",
];
for ok_str in ok {
assert!(NetConfigV1::from_str(ok_str).is_ok())
}
fn multiple_interface_from_cmdline() {
let cmdline = cmdline().join("multiple_interface");
assert!(from_command_line(cmdline).is_err())
}

#[test]
fn invalid_interface_from_str() {
let bad = &[
"",
":",
"eno1:",
":dhcp4,dhcp6",
"dhcp4",
"eno1:dhc4",
"eno1:dhcp",
"eno1:dhcp4+",
"eno1:dhcp?",
"eno1:dhcp4?,dhcp4",
"ENO1:DHCP4?,DhCp6",
];
for bad_str in bad {
assert!(NetConfigV1::from_str(bad_str).is_err())
}
fn no_interfaces_cmdline() {
let cmdline = cmdline().join("no_interfaces");
assert!(from_command_line(cmdline).unwrap().is_none())
}
}
73 changes: 73 additions & 0 deletions sources/api/netdog/src/net_config/test_macros/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! The basic_tests macro contains tests that are applicable to all versions of network
//! configuration. These tests ensure some basic parsing/validating works properly. The tests
//! also ensure general network config rules are followed, such as a single primary interface is
//! defined, etc.
//!
//! The macro's only argument is the version of net config currently being tested.
macro_rules! basic_tests {
($version:expr) => {
mod basic {
use $crate::net_config::deserialize_config;
use $crate::net_config::test_macros::gen_boilerplate;

gen_boilerplate!($version, "basic");

#[test]
fn invalid_version() {
let bad = net_config().join("bad_version.toml");
let bad_str = fs::read_to_string(bad).unwrap();
assert!(deserialize_config(&bad_str).is_err())
}

#[test]
fn ok_config() {
let ok = net_config().join("net_config.toml");
let rendered = render_config_template(ok);
assert!(deserialize_config(&rendered).is_ok())
}

#[test]
fn invalid_interface_config() {
let bad = net_config().join("invalid_interface_config.toml");
let rendered = render_config_template(bad);
assert!(deserialize_config(&rendered).is_err())
}

#[test]
fn no_interfaces() {
let bad = net_config().join("no_interfaces.toml");
let rendered = render_config_template(bad);
assert!(deserialize_config(&rendered).is_ok())
}

#[test]
fn defined_primary_interface() {
let ok_path = net_config().join("net_config.toml");
let cfg = deserialize_config(&render_config_template(ok_path)).unwrap();

let expected = "eno2";
let actual = cfg.primary_interface().unwrap();
assert_eq!(expected, actual)
}

#[test]
fn undefined_primary_interface() {
let ok_path = net_config().join("no_primary.toml");
let cfg = deserialize_config(&render_config_template(ok_path)).unwrap();

let expected = "eno3";
let actual = cfg.primary_interface().unwrap();
assert_eq!(expected, actual)
}

#[test]
fn multiple_primary_interfaces() {
let multiple = net_config().join("multiple_primary.toml");
let rendered = render_config_template(multiple);
assert!(deserialize_config(&rendered).is_err())
}
}
};
}

pub(crate) use basic_tests;
51 changes: 51 additions & 0 deletions sources/api/netdog/src/net_config/test_macros/dhcp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! The dhcp_tests macro contains tests pertaining to DHCP. These tests are applicable to all
//! versions of network configuration.
//!
//! The macro's only argument is the version of net config currently being tested.
macro_rules! dhcp_tests {
($version:expr) => {
mod dhcp {
use $crate::net_config::deserialize_config;
use $crate::net_config::test_macros::gen_boilerplate;

gen_boilerplate!($version, "dhcp");

#[test]
fn invalid_dhcp4_options() {
let bad = net_config().join("invalid_dhcp4_options.toml");
let rendered = render_config_template(bad);
assert!(deserialize_config(&rendered).is_err())
}

#[test]
fn invalid_dhcp6_options() {
let bad = net_config().join("invalid_dhcp6_options.toml");
let rendered = render_config_template(bad);
assert!(deserialize_config(&rendered).is_err())
}

#[test]
fn invalid_dhcp_config() {
let bad = net_config().join("invalid_dhcp_config.toml");
let rendered = render_config_template(bad);
assert!(deserialize_config(&rendered).is_err())
}

#[test]
fn dhcp4_missing_enable() {
let bad = net_config().join("dhcp4_missing_enabled.toml");
let rendered = render_config_template(bad);
assert!(deserialize_config(&rendered).is_err())
}

#[test]
fn dhcp6_missing_enable() {
let bad = net_config().join("dhcp6_missing_enabled.toml");
let rendered = render_config_template(bad);
assert!(deserialize_config(&rendered).is_err())
}
}
};
}

pub(crate) use dhcp_tests;
67 changes: 67 additions & 0 deletions sources/api/netdog/src/net_config/test_macros/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! The test_macros module contains macros meant to simplify testing across net config versions.
//!
//! Net configuration versions are typically additive, meaning new versions add support for new
//! features, while continuing support for previous features. We want to ensure that we test all
//! applicable features for a version, without duplicating test code between version files and/or
//! test blocks. The macros themselves are declarative and define a new module that contains all
//! the tests. This allows us to use the same tests across all versions of net config, and get
//! nicely formatted `cargo test` output showing the net config version, module, and test. This is
//! much nicer than looping over versions within a single test, and provides much better error
//! output for the user.
//!
//! Each macro typically has an associated directory inside the `test_data` folder that provides
//! templated net config files for parsing.
#[cfg(test)]
pub(super) mod basic;
#[cfg(test)]
pub(super) mod dhcp;

pub(super) use basic::basic_tests;
pub(super) use dhcp::dhcp_tests;

/// gen_boilerplate!() is a convenience macro meant to be used inside of test macros to generate
/// some generally useful boilerplate code. It creates a `VERSION` constant in case the test
/// macros need it, and provides some convenience functions for gathering test data directories,
/// and rendering config templates in said directories.
///
/// The macro receives arguments for the net config version, as well as the directory where the
/// associated test files can be found.
macro_rules! gen_boilerplate {
($version:expr, $test_dir:expr) => {
use handlebars::Handlebars;
use serde::Serialize;
use std::fs;
use std::path::{Path, PathBuf};

static VERSION: usize = $version;

fn test_data() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_data")
}

fn net_config() -> PathBuf {
test_data().join("net_config").join($test_dir)
}

#[derive(Serialize)]
struct Context {
version: usize,
}

fn render_config_template<P>(path: P) -> String
where
P: AsRef<Path>,
{
let path = path.as_ref();
let path_str = fs::read_to_string(path).unwrap();

let mut hb = Handlebars::new();
hb.register_template_string("template", &path_str).unwrap();

let context = Context { version: VERSION };
hb.render("template", &context).unwrap()
}
};
}

pub(super) use gen_boilerplate;
Loading