From 93f112949c4765aadcc9a7b5bcaf762c826d2fcb Mon Sep 17 00:00:00 2001 From: mmynk Date: Tue, 31 Oct 2023 20:02:38 +0000 Subject: [PATCH] Add UT --- below/tc/src/test.rs | 108 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 below/tc/src/test.rs diff --git a/below/tc/src/test.rs b/below/tc/src/test.rs new file mode 100644 index 00000000..2dcfb774 --- /dev/null +++ b/below/tc/src/test.rs @@ -0,0 +1,108 @@ +use super::*; + +struct FakeNetlink; + +impl tc::NetlinkConnection for FakeNetlink { + fn new() -> std::result::Result + where + Self: Sized { + Ok(Self {}) + } + + fn qdiscs(&self) -> std::result::Result, tc::errors::NetlinkError> { + Ok( + vec![ + tc::TcMsg { + header: tc::TcHeader { + index: 2, + handle: 0, + parent: 2, + }, + attrs: vec![ + tc::TcAttr::Kind("fq_codel".to_string()), + tc::TcAttr::Options(vec![ + tc::TcOption { + kind: 1, + bytes: vec![135, 19, 0, 0], + }, + tc::TcOption { + kind: 2, + bytes: vec![0, 40, 0, 0], + }, + tc::TcOption { + kind: 3, + bytes: vec![159, 134, 1, 0], + }, + tc::TcOption { + kind: 4, + bytes: vec![1, 0, 0, 0], + }, + tc::TcOption { + kind: 6, + bytes: vec![234, 5, 0, 0], + }, + tc::TcOption { + kind: 8, + bytes: vec![64, 0, 0, 0], + }, + tc::TcOption { + kind: 9, + bytes: vec![0, 0, 0, 2], + }, + tc::TcOption { + kind: 5, + bytes: vec![0, 4, 0, 0], + }, + ]), + tc::TcAttr::Stats(vec![ + 76, 222, 96, 2, 0, 0, 0, 0, 55, 135, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]), + tc::TcAttr::Stats2(vec![ + tc::TcStats2::StatsBasic(vec![76, 222, 96, 2, 0, 0, 0, 0, 55, 135, 2, 0, 0, 0, 0, 0]), + tc::TcStats2::StatsQueue(vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, + ]), + tc::TcStats2::StatsApp(vec![ + 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]), + ]), + tc::TcAttr::Xstats(vec![ + 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]), + tc::TcAttr::HwOffload(0), + ], + } + ]) + } + + fn classes(&self, _index: i32) -> std::result::Result, tc::errors::NetlinkError> { + Ok(Vec::new()) + } + + fn links(&self) -> std::result::Result, tc::errors::NetlinkError> { + Ok(Vec::new()) + } +} + +#[test] +fn test_tc_stats() { + let tc_map = read_tc_stats::().unwrap(); + + let tc = tc_map.get(&2).unwrap().get(0).unwrap(); + assert_eq!(tc.index, 2); + assert_eq!(tc.handle, 0); + assert_eq!(tc.parent, 2); + + assert_eq!(tc.kind, "fq_codel"); + assert_eq!(tc.stats.bytes, Some(39902796)); + assert_eq!(tc.stats.packets, Some(165687)); + assert_eq!(tc.stats.qlen, Some(0)); + assert_eq!(tc.stats.bps, Some(0)); + assert_eq!(tc.stats.pps, Some(0)); + + assert!(tc.qdisc.is_some()); + assert!(tc.stats.xstats.is_some()); +}