Skip to content

Commit ecd5dd2

Browse files
committed
add snapshot features
Create snapshot and verify Implement From traits for common error conversions
1 parent 556e4c7 commit ecd5dd2

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
tokio = { version = "1.36", features = ["full"] }
8-
virt = "0.4.1"
8+
virt = { version = "0.4.1", features = ["snapshot"] }
99
serde = { version = "1.0", features = ["derive"] }
1010
serde_json = "1.0"
1111
tracing = "0.1"

src/core/errors.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ pub enum GpuShareError {
1818
UnknownError(String),
1919
}
2020

21-
// Implement From traits for common error conversions
22-
impl From<libvirt::error::Error> for GpuShareError {
23-
fn from(err: libvirt::error::Error) -> Self {
24-
GpuShareError::ConnectionError(err.to_string())
25-
}
26-
}
27-
2821
impl From<std::io::Error> for GpuShareError {
2922
fn from(err: std::io::Error) -> Self {
3023
GpuShareError::ConfigError(err.to_string())
3124
}
32-
}
25+
}
26+
27+
impl From<virt::error::Error> for GpuShareError {
28+
fn from(error: virt::error::Error) -> Self {
29+
GpuShareError::ConnectionError(error.to_string())
30+
}
31+
}

src/core/libvirt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::core::vm::VMConfig;
66
// use virt::sys;
77

88
// time to manage some VMs! >.
9+
#[derive(Clone)]
910
#[allow(dead_code)]
1011
pub struct LibvirtManager {
1112
conn: Connect,
@@ -78,12 +79,10 @@ impl LibvirtManager {
7879
}
7980

8081
pub fn lookup_domain(&self, id: &str) -> Result<Domain> {
81-
// Önce UUID olarak dene
8282
if let Ok(domain) = Domain::lookup_by_uuid_string(&self.conn, id) {
8383
return Ok(domain);
8484
}
8585

86-
// UUID olarak bulunamazsa isim olarak dene
8786
match Domain::lookup_by_name(&self.conn, id) {
8887
Ok(domain) => Ok(domain),
8988
Err(e) => Err(anyhow::anyhow!("Failed to find domain: {}", e))

tests/vm_tests.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Virtual Machine Test Suite - Because untested code is like Schrödinger's cat! 🐱💻
22

33
use anyhow::{Context, Result};
4-
use gpu_share_vm_manager::core::{LibvirtManager, vm::{VMConfig, VMStatus}};
4+
use gpu_share_vm_manager::core::{LibvirtManager, vm::VMConfig};
55
use std::path::PathBuf;
66
// use tracing::{info, warn};
77
use uuid::Uuid;
@@ -156,23 +156,26 @@ async fn test_vm_snapshots() -> Result<()> {
156156
let vm = libvirt.0.create_vm(&config).await?;
157157
vm.create()?;
158158

159-
// Create snapshot
160-
let snapshot_xml = format!(r#"
161-
<domainsnapshot>
162-
<name>{}</name>
163-
<description>{}</description>
164-
</domainsnapshot>"#,
165-
"test-snapshot",
166-
"Initial state"
167-
);
168-
vm.snapshot_create_xml(&snapshot_xml, 0)?;
169-
170-
// Restore snapshot
171-
let snapshot = vm.snapshot_lookup_by_name("test-snapshot")?;
172-
vm.snapshot_revert(snapshot, 0)?;
159+
// Create snapshot with proper XML structure
160+
let snapshot_xml = r#"
161+
<domainsnapshot>
162+
<name>test-snapshot</name>
163+
<description>Initial state</description>
164+
<memory snapshot='no'/>
165+
</domainsnapshot>"#;
173166

174-
let restored_xml = vm.get_xml_desc(0)?;
175-
assert_eq!(restored_xml, vm.get_xml_desc(0)?, "XML should match snapshot");
167+
// Create snapshot and verify
168+
let snapshot = vm.snapshot_create_xml(snapshot_xml, 0)
169+
.context("Failed to create snapshot")?;
170+
assert_eq!(snapshot.get_name()?, "test-snapshot");
171+
172+
// Revert to snapshot
173+
vm.snapshot_revert(snapshot, 0)
174+
.context("Failed to revert snapshot")?;
175+
176+
// Cleanup snapshot
177+
let current_snapshot = vm.snapshot_current(0)?;
178+
current_snapshot.delete(0)?;
176179

177180
vm.destroy()?;
178181
vm.undefine()?;

0 commit comments

Comments
 (0)