Skip to content

Commit

Permalink
add test for ip_allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
davepacheco committed Jan 16, 2024
1 parent 58f7e39 commit ce76d0f
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions nexus/deployment/src/ip_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,66 @@ impl IpAllocator {
Some(next)
}
}

#[cfg(test)]
mod test {
use super::IpAllocator;
use std::collections::BTreeSet;
use std::net::Ipv6Addr;

#[test]
fn test_basic() {
let range_start: Ipv6Addr = "fd00::d0".parse().unwrap();
let range_end: Ipv6Addr = "fd00::e8".parse().unwrap();
let reserved: BTreeSet<Ipv6Addr> = [
// These first two are deliberately out of range.
"fd00::ff".parse().unwrap(),
"fd00::c0".parse().unwrap(),
"fd00::d3".parse().unwrap(),
"fd00::d7".parse().unwrap(),
]
.iter()
.copied()
.collect();

let mut allocator = IpAllocator::new(range_start, range_end);
for r in &reserved {
allocator.reserve(*r);
}

let mut allocated = BTreeSet::new();
while let Some(addr) = allocator.alloc() {
println!("allocated: {}", addr);
assert!(!reserved.contains(&addr));
assert!(!allocated.contains(&addr));
allocated.insert(addr);
}

assert_eq!(
allocated,
[
// Because d7 was reserved, everything up to it is also skipped.
// It doesn't have to work that way, but it currently does.
"fd00::d8".parse().unwrap(),
"fd00::d9".parse().unwrap(),
"fd00::da".parse().unwrap(),
"fd00::db".parse().unwrap(),
"fd00::dc".parse().unwrap(),
"fd00::dd".parse().unwrap(),
"fd00::de".parse().unwrap(),
"fd00::df".parse().unwrap(),
"fd00::e0".parse().unwrap(),
"fd00::e1".parse().unwrap(),
"fd00::e2".parse().unwrap(),
"fd00::e3".parse().unwrap(),
"fd00::e4".parse().unwrap(),
"fd00::e5".parse().unwrap(),
"fd00::e6".parse().unwrap(),
"fd00::e7".parse().unwrap(),
]
.iter()
.copied()
.collect()
);
}
}

0 comments on commit ce76d0f

Please sign in to comment.