Skip to content

Commit

Permalink
netdog: Update net_config to use test macros
Browse files Browse the repository at this point in the history
This change updates `net_config` to take advantage of the previously
added test macros.  It removes the majority of the tests from `mod.rs`,
since the test themselves were moved to macros.  It updates the location
of the test data files, and templates the version.  The last and final
change is to add the test macros to `v1`.
  • Loading branch information
zmrow committed Sep 1, 2022
1 parent 21931d6 commit cced97b
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 129 deletions.
131 changes: 12 additions & 119 deletions sources/api/netdog/src/net_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,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())
assert!(from_path(ok).unwrap().is_some())
}

#[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())
}

#[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())
}
}
48 changes: 48 additions & 0 deletions sources/api/netdog/src/net_config/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,51 @@ impl FromStr for Sigils {
Ok(sigils)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::net_config::test_macros::basic_tests;
use crate::net_config::test_macros::dhcp_tests;

basic_tests!(1);
dhcp_tests!(1);

#[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())
}
}

#[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())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 1
version = {{version}}

[eno1]
dhcp4 = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 1
version = {{version}}

[eno1]
dhcp4 = true
Expand Down
50 changes: 50 additions & 0 deletions sources/api/netdog/test_data/net_config/basic/net_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version = {{version}}

[eno1]
dhcp4 = true

[eno2]
dhcp6 = true
primary = true

[eno3]
dhcp4 = true
dhcp6 = false

[eno4]
dhcp4 = false
dhcp6 = true

[eno5]
dhcp4 = true
dhcp6 = true

[eno6.dhcp4]
enabled = true
route-metric = 100

[eno6]
dhcp6 = false

[eno7]
dhcp4 = true

[eno7.dhcp6]
enabled = true
optional = true

[eno8.dhcp4]
enabled = true
optional = true

[eno8.dhcp6]
enabled = true
optional = true

[eno9.dhcp4]
enabled = true
optional = true

[eno10.dhcp6]
enabled = true
optional = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = {{version}}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 1
version = {{version}}

[eno3]
dhcp4 = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 1
version = {{version}}

[eno1.dhcp4]
optional = true
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 1
version = {{version}}

[eno1.dhcp6]
optional = true
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 1
version = {{version}}

[eno1]
dhcp4 = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 1
version = {{version}}

[eno1]
dhcp4 = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = {{version}}

[eno1]

This file was deleted.

0 comments on commit cced97b

Please sign in to comment.