use super::hash_desired_state;
use crate::core::types::{Resource, ResourceType};
use std::collections::HashMap;
fn overlay(hosts: Option<HashMap<String, String>>) -> Resource {
Resource {
resource_type: ResourceType::OverlayInterface,
overlay_ip: Some("10.42.0.11/24".into()),
overlay_hosts: hosts,
..Default::default()
}
}
fn hosts(pairs: &[(&str, &str)]) -> HashMap<String, String> {
pairs
.iter()
.map(|(k, v)| ((*k).to_string(), (*v).to_string()))
.collect()
}
#[test]
fn overlay_hosts_hash_is_insertion_order_independent() {
for (a, b) in [("h1", "h2"), ("alpha", "beta"), ("z9", "a0"), ("m", "mm")] {
let fwd = hosts(&[(a, "10.0.0.1"), (b, "10.0.0.2")]);
let rev = hosts(&[(b, "10.0.0.2"), (a, "10.0.0.1")]);
assert_eq!(
hash_desired_state(&overlay(Some(fwd))),
hash_desired_state(&overlay(Some(rev))),
"overlay_hosts hash must be insertion-order independent ({a}, {b})"
);
}
}
#[test]
fn overlay_hosts_presence_changes_the_hash() {
let with = overlay(Some(hosts(&[("h1", "10.0.0.1")])));
let without = overlay(None);
assert_ne!(
hash_desired_state(&with),
hash_desired_state(&without),
"overlay_hosts presence must change the desired-state hash"
);
}
#[test]
fn overlay_hosts_content_changes_the_hash() {
let a = overlay(Some(hosts(&[("h1", "10.0.0.1")])));
let b = overlay(Some(hosts(&[("h1", "10.0.0.99")])));
assert_ne!(
hash_desired_state(&a),
hash_desired_state(&b),
"changing a host's address must change the desired-state hash"
);
}