test_install_lifecycle.rs
1 //! Integration tests for installation lifecycle. 2 //! 3 //! Tests: install → start → health → stop → uninstall 4 5 mod common; 6 7 use common::assertions::*; 8 use common::fixtures::*; 9 use common::mocks::*; 10 use common::TestEnv; 11 12 #[test] 13 fn test_install_binary_fresh() { 14 // Setup: Fresh environment 15 let env = TestEnv::new().expect("Failed to create test environment"); 16 let systemd = MockSystemd::new(); 17 let release_server = MockReleaseServer::new("https://test.ac-dc.network".to_string()); 18 19 // Prepare mock release 20 release_server.add_file("/releases/adnet-v1.0.0.tar.gz", sample_binary_tarball()); 21 release_server.add_checksum( 22 "/releases/adnet-v1.0.0.tar.gz", 23 "abc123def456", 24 ); 25 26 // Install binary 27 let binary_path = env 28 .create_binary("adnet", b"#!/bin/bash\necho 'adnet v1.0.0'") 29 .expect("Failed to create binary"); 30 31 // Verify installation 32 assert_binary_exists(&binary_path); 33 34 // Verify systemd service created 35 systemd.enable_service("adnet").expect("Failed to enable"); 36 assert_service_enabled(&systemd, "adnet"); 37 } 38 39 #[test] 40 fn test_install_with_config() { 41 // Setup 42 let env = TestEnv::new().expect("Failed to create test environment"); 43 44 // Create config 45 let config_content = sample_config_toml(); 46 let config_path = env 47 .create_config("adnet.toml", &config_content) 48 .expect("Failed to create config"); 49 50 // Verify config 51 assert_config_contains(&config_path, "registry_url"); 52 assert_config_contains(&config_path, "verify_checksums = true"); 53 54 // Parse and validate config 55 let parsed: toml::Value = toml::from_str(&config_content).expect("Invalid TOML"); 56 assert!(parsed.get("install").is_some()); 57 assert!(parsed.get("verification").is_some()); 58 } 59 60 #[test] 61 fn test_start_after_install() { 62 // Setup 63 let env = TestEnv::new().expect("Failed to create test environment"); 64 let systemd = MockSystemd::new(); 65 66 // Install 67 let _binary = env 68 .create_binary("adnet", b"#!/bin/bash\necho 'adnet running'") 69 .expect("Failed to create binary"); 70 71 systemd.enable_service("adnet").expect("Failed to enable"); 72 73 // Start service 74 systemd.start_service("adnet").expect("Failed to start"); 75 76 // Verify running 77 assert_service_running(&systemd, "adnet"); 78 assert_service_enabled(&systemd, "adnet"); 79 } 80 81 #[test] 82 fn test_stop_service() { 83 // Setup 84 let env = TestEnv::new().expect("Failed to create test environment"); 85 let systemd = MockSystemd::new(); 86 87 // Install and start 88 let _binary = env 89 .create_binary("adnet", b"#!/bin/bash\necho running") 90 .expect("Failed to create binary"); 91 92 systemd.enable_service("adnet").expect("Failed to enable"); 93 systemd.start_service("adnet").expect("Failed to start"); 94 95 assert_service_running(&systemd, "adnet"); 96 97 // Stop service 98 systemd.stop_service("adnet").expect("Failed to stop"); 99 100 // Verify stopped 101 assert_service_stopped(&systemd, "adnet"); 102 103 // Service should still be enabled 104 assert_service_enabled(&systemd, "adnet"); 105 } 106 107 #[test] 108 fn test_uninstall_cleanup() { 109 // Setup 110 let env = TestEnv::new().expect("Failed to create test environment"); 111 let systemd = MockSystemd::new(); 112 113 // Install 114 let binary_path = env 115 .create_binary("adnet", b"#!/bin/bash\necho test") 116 .expect("Failed to create binary"); 117 118 let config_path = env 119 .create_config("adnet.toml", &sample_config_toml()) 120 .expect("Failed to create config"); 121 122 let data_dir = env 123 .create_data_subdir("adnet") 124 .expect("Failed to create data dir"); 125 126 systemd.enable_service("adnet").expect("Failed to enable"); 127 systemd.start_service("adnet").expect("Failed to start"); 128 129 // Verify installation 130 assert_binary_exists(&binary_path); 131 assert!(config_path.exists()); 132 assert!(data_dir.exists()); 133 assert_service_running(&systemd, "adnet"); 134 135 // Uninstall 136 systemd.stop_service("adnet").expect("Failed to stop"); 137 systemd.disable_service("adnet").expect("Failed to disable"); 138 139 std::fs::remove_file(&binary_path).expect("Failed to remove binary"); 140 std::fs::remove_file(&config_path).expect("Failed to remove config"); 141 std::fs::remove_dir_all(&data_dir).expect("Failed to remove data dir"); 142 143 // Verify cleanup 144 assert!(!binary_path.exists(), "Binary still exists after uninstall"); 145 assert!(!config_path.exists(), "Config still exists after uninstall"); 146 assert!(!data_dir.exists(), "Data dir still exists after uninstall"); 147 assert_service_stopped(&systemd, "adnet"); 148 } 149 150 #[test] 151 fn test_restart_service() { 152 // Setup 153 let env = TestEnv::new().expect("Failed to create test environment"); 154 let systemd = MockSystemd::new(); 155 156 // Install and start 157 let _binary = env 158 .create_binary("adnet", b"#!/bin/bash\necho running") 159 .expect("Failed to create binary"); 160 161 systemd.enable_service("adnet").expect("Failed to enable"); 162 systemd.start_service("adnet").expect("Failed to start"); 163 164 // Restart 165 systemd.restart_service("adnet").expect("Failed to restart"); 166 167 // Verify still running 168 assert_service_running(&systemd, "adnet"); 169 } 170 171 #[test] 172 fn test_multiple_services() { 173 // Setup 174 let env = TestEnv::new().expect("Failed to create test environment"); 175 let systemd = MockSystemd::new(); 176 177 // Install multiple components 178 let _adnet = env 179 .create_binary("adnet", b"#!/bin/bash\necho adnet") 180 .expect("Failed to create adnet"); 181 182 let _monitor = env 183 .create_binary("adnet-monitor", b"#!/bin/bash\necho monitor") 184 .expect("Failed to create monitor"); 185 186 // Enable and start both 187 systemd.enable_service("adnet").expect("Failed to enable adnet"); 188 systemd 189 .enable_service("adnet-monitor") 190 .expect("Failed to enable monitor"); 191 192 systemd.start_service("adnet").expect("Failed to start adnet"); 193 systemd 194 .start_service("adnet-monitor") 195 .expect("Failed to start monitor"); 196 197 // Verify both running 198 assert_service_running(&systemd, "adnet"); 199 assert_service_running(&systemd, "adnet-monitor"); 200 }