nazrin/nzr-virt/src/xml/test.rs
snow flurry 6da77159b1 Complete rewrite time
Main changes:

* Use diesel instead of sled
* Split libvirt components into new crate, nzr-virt
* Start moving toward network-based cloud-init

To facilitate the latter, nzrdhcp is an added unicast-only DHCP server,
intended to be used behind a DHCP relay.
2024-08-10 00:58:20 -07:00

125 lines
3.6 KiB
Rust

use uuid::uuid;
use super::build::DomainBuilder;
use super::*;
use crate::datasize;
trait Unprettify {
fn unprettify(&self) -> String;
}
impl Unprettify for &str {
fn unprettify(&self) -> String {
self.split('\n')
.map(|s| s.trim())
.collect::<Vec<&str>>()
.concat()
}
}
#[test]
fn domain_serde() {
let dom_str = r#"<domain type="kvm">
<name>test-vm</name>
<uuid>9a8f2611-a976-4d06-ac91-2750ac3462b3</uuid>
<description>This is a test</description>
<vcpu placement="static">2</vcpu>
<memory unit="MiB">512</memory>
<features>
<apic/>
<acpi/>
</features>
<cpu mode="host-passthrough">
<topology sockets="1" dies="1" cores="2" threads="1"/>
</cpu>
<devices>
<disk type="volume" device="disk">
<source pool="tank" volume="test-vm-root"/>
<target dev="sda" bus="virtio"/>
</disk>
<interface type="bridge">
<mac address="02:0b:ee:ca:fe:42"/>
<source bridge="virbr0"/>
<model type="virtio"/>
</interface>
</devices>
<os>
<boot dev="hd"/>
<type arch="x86_64" machine="pc-i440fx-5.2">hvm</type>
<bios useserial="yes" rebootTimeout="0"/>
</os>
</domain>"#
.unprettify();
println!("Serializing domain...");
let mac = MacAddr::new(0x02, 0x0b, 0xee, 0xca, 0xfe, 0x42);
let uuid = uuid!("9a8f2611-a976-4d06-ac91-2750ac3462b3");
let domain = DomainBuilder::default()
.name("test-vm")
.uuid(uuid)
.description("This is a test")
.disk_device(|dsk| {
dsk.volume_source("tank", "test-vm-root")
.target("sda", "virtio")
})
.net_device(|net| net.with_bridge("virbr0").mac_addr(mac))
.build();
let dom_xml = quick_xml::se::to_string(&domain).unwrap();
println!("{}", dom_xml);
assert_eq!(&dom_xml, &dom_str);
println!("Deserializing domain...");
let _new_dom: Domain = quick_xml::de::from_str(&dom_str).unwrap();
}
#[test]
fn pool_serde() {
let pool_str = r#"<pool type="dir">
<name>default</name>
<uuid>4ad34b59-9418-483d-9533-2e8b51e7317e</uuid>
<capacity unit="GiB">200</capacity>
<allocation unit="GiB">120</allocation>
<available unit="GiB">80</available>
<target>
<path>/var/lib/libvirt/images</path>
</target>
</pool>"#
.unprettify();
println!("Serializing pool...");
let pool: Pool = Pool {
r#type: "dir".to_owned(),
name: "default".to_owned(),
uuid: Some(uuid!("4ad34b59-9418-483d-9533-2e8b51e7317e")),
capacity: Some(datasize!(200 GiB)),
allocation: Some(datasize!(120 GiB)),
available: Some(datasize!(80 GiB)),
target: Some(VolTarget {
path: Some("/var/lib/libvirt/images".to_owned()),
format: None,
}),
};
let pool_xml = quick_xml::se::to_string(&pool).unwrap();
assert_eq!(&pool_xml, &pool_str);
println!("Deserializing pool...");
let pool: Pool = quick_xml::de::from_str(&pool_str).unwrap();
assert_eq!(pool.name, "default".to_owned());
assert_eq!(pool.r#type, "dir".to_owned());
}
#[test]
fn vol_serde() {
let vol_str = r#"<volume type="file">
<name>test</name>
<allocation unit="GiB">20</allocation>
<capacity unit="GiB">20</capacity>
<target>
<format type="qcow2"/>
</target>
</volume>"#
.unprettify();
println!("Serializing volume...");
let vol: Volume = Volume::new("test", VolType::Qcow2, datasize!(20 GiB));
let vol_xml = quick_xml::se::to_string(&vol).unwrap();
assert_eq!(&vol_xml, &vol_str);
println!("Deserializing volume...");
let vol_obj: Volume = quick_xml::de::from_str(&vol_str).unwrap();
assert_eq!(vol_obj, vol);
}