From f4ec21ec2a03464e8ea58075dae950106e0556c7 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Sat, 15 Mar 2025 18:45:57 -0400 Subject: [PATCH] integration-test: kernel_assert anti-condition Rather than emitting a warning, assert the inverse of the condition when the current kernel version is lower than required. This strengthens the assertions made by our tests (provided we run them over kernel versions before and after the listed version, which is not yet the case). --- test/integration-test/src/utils.rs | 58 ++++++++---------------------- 1 file changed, 15 insertions(+), 43 deletions(-) diff --git a/test/integration-test/src/utils.rs b/test/integration-test/src/utils.rs index ac20f62b..924ab9a6 100644 --- a/test/integration-test/src/utils.rs +++ b/test/integration-test/src/utils.rs @@ -71,59 +71,31 @@ impl Drop for NetNsGuard { } } -/// Performs `assert!` macro. If the assertion fails and host kernel version -/// is above feature version, then fail test. +/// If the `KernelVersion::current >= $version`, `assert!($cond)`, else `assert!(!$cond)`. macro_rules! kernel_assert { ($cond:expr, $version:expr $(,)?) => { - let pass: bool = $cond; - if !pass { - let feat_version: aya::util::KernelVersion = $version; - let current = aya::util::KernelVersion::current().unwrap(); - let cond_literal = stringify!($cond); - if current >= feat_version { - // Host kernel is expected to have the feat but does not - panic!( - r#" assertion `{cond_literal}` failed: expected host kernel v{current} to have v{feat_version} feature"#, - ); - } else { - // Continue with tests since host is not expected to have feat - eprintln!( - r#"ignoring assertion at {}:{} - assertion `{cond_literal}` failed: continuing since host kernel v{current} is not expected to have v{feat_version} feature"#, - file!(), line!(), - ); - } + let current = aya::util::KernelVersion::current().unwrap(); + let required: aya::util::KernelVersion = $version; + if current >= required { + assert!($cond, "{current} >= {required}"); + } else { + assert!(!$cond, "{current} < {required}"); } }; } pub(crate) use kernel_assert; -/// Performs `assert_eq!` macro. If the assertion fails and host kernel version -/// is above feature version, then fail test. +/// If the `KernelVersion::current >= $version`, `assert_eq!($left, $right)`, else +/// `assert_ne!($left, $right)`. macro_rules! kernel_assert_eq { ($left:expr, $right:expr, $version:expr $(,)?) => { - if $left != $right { - let feat_version: aya::util::KernelVersion = $version; - let current = aya::util::KernelVersion::current().unwrap(); - if current >= feat_version { - // Host kernel is expected to have the feat but does not - panic!( - r#" assertion `left == right` failed: expected host kernel v{current} to have v{feat_version} feature - left: {:?} - right: {:?}"#, - $left, $right, - ); - } else { - // Continue with tests since host is not expected to have feat - eprintln!( - r#"ignoring assertion at {}:{} - assertion `left == right` failed: continuing since host kernel v{current} is not expected to have v{feat_version} feature - left: {:?} - right: {:?}"#, - file!(), line!(), $left, $right, - ); - } + let current = aya::util::KernelVersion::current().unwrap(); + let required: aya::util::KernelVersion = $version; + if current >= required { + assert_eq!($left, $right, "{current} >= {required}"); + } else { + assert_ne!($left, $right, "{current} < {required}"); } }; }