Skip to content

Commit 0ce9fec

Browse files
committed
add test_vm_network_configuration & test_duplicate_vm_creation
Verifies that the VM has at least one network interface Tests basic network connectivity by pinging the gateway Checks that network interfaces are properly configured Tests two VM creation attempts with the same configuration Causes the second creation to fail Verifies that the error message contains “already exists”
1 parent ecd5dd2 commit 0ce9fec

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

tests/vm_tests.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,65 @@ async fn test_resource_allocation() -> Result<()> {
212212
vm.destroy()?;
213213
vm.undefine()?;
214214

215+
Ok(())
216+
}
217+
218+
// Network Configuration Test: Validate network interfaces and connectivity
219+
#[tokio::test]
220+
async fn test_vm_network_configuration() -> Result<()> {
221+
let libvirt = LibvirtManagerWrapper::new()?;
222+
let config = test_vm_config();
223+
224+
// Create VM with network configuration
225+
let vm = libvirt.0.create_vm(&config)
226+
.await
227+
.context("Failed to create VM for network test")?;
228+
229+
vm.create()?;
230+
231+
// Validate network interfaces
232+
let interfaces = vm.get_interfaces()?;
233+
assert!(!interfaces.is_empty(), "VM should have at least one network interface");
234+
235+
// Basic connectivity check (ping gateway)
236+
let active_iface = interfaces.first().unwrap();
237+
let ping_result = vm.execute_command(&format!("ping -c 3 {}", active_iface.gateway)).await;
238+
assert!(ping_result.is_ok(), "VM should have network connectivity");
239+
240+
// Cleanup
241+
vm.destroy()?;
242+
vm.undefine()?;
243+
Ok(())
244+
}
245+
246+
// Negative Test: Duplicate VM creation and error handling
247+
#[tokio::test]
248+
async fn test_duplicate_vm_creation() -> Result<()> {
249+
let libvirt = LibvirtManagerWrapper::new()?;
250+
let config = test_vm_config();
251+
252+
// First creation should succeed
253+
let vm1 = libvirt.0.create_vm(&config)
254+
.await
255+
.context("First VM creation should succeed")?;
256+
257+
// Second creation with same config should fail
258+
let result = libvirt.0.create_vm(&config).await;
259+
assert!(
260+
result.is_err(),
261+
"Should return error when creating duplicate VM"
262+
);
263+
264+
// Verify error type
265+
if let Err(e) = result {
266+
assert!(
267+
e.to_string().contains("already exists"),
268+
"Error should indicate duplicate VM"
269+
);
270+
}
271+
272+
// Cleanup
273+
vm1.destroy()?;
274+
vm1.undefine()?;
215275
Ok(())
216276
}

0 commit comments

Comments
 (0)