ALPHA: wire is a tool to deploy nixos systems wire.althaea.zone/

add test for Target

+47 -1
+47 -1
wire/lib/src/hive/node.rs
··· 453 453 hive::{Hive, get_hive_location}, 454 454 location, 455 455 }; 456 - use std::path::PathBuf; 456 + use std::{assert_matches::assert_matches, path::PathBuf}; 457 457 use std::{collections::HashMap, env}; 458 458 459 459 fn get_steps(goal_executor: GoalExecutor) -> std::vec::Vec<Step> { ··· 628 628 CleanUp.into() 629 629 ] 630 630 ); 631 + } 632 + 633 + #[test] 634 + fn target_fails_increments() { 635 + let mut target = Target::from_host("localhost"); 636 + 637 + assert_eq!(target.current_host, 0); 638 + 639 + for i in 0..100 { 640 + target.host_failed(); 641 + assert_eq!(target.current_host, i + 1); 642 + } 643 + } 644 + 645 + #[test] 646 + fn get_preferred_host_fails() { 647 + let mut target = Target { 648 + hosts: vec![ 649 + "un.reachable.1".into(), 650 + "un.reachable.2".into(), 651 + "un.reachable.3".into(), 652 + "un.reachable.4".into(), 653 + "un.reachable.5".into(), 654 + ], 655 + ..Default::default() 656 + }; 657 + 658 + assert_ne!( 659 + target.get_preferred_host().unwrap().to_string(), 660 + "un.reachable.5" 661 + ); 662 + 663 + for i in 1..=5 { 664 + assert_eq!( 665 + target.get_preferred_host().unwrap().to_string(), 666 + format!("un.reachable.{i}") 667 + ); 668 + target.host_failed(); 669 + } 670 + 671 + for _ in 0..5 { 672 + assert_matches!( 673 + target.get_preferred_host(), 674 + Err(HiveLibError::NetworkError(NetworkError::HostsExhausted)) 675 + ); 676 + } 631 677 } 632 678 }