@@ -14,29 +14,25 @@ use crate::{get_bck_command, get_test_image, run_bcvk};
1414pub fn test_base_disk_creation_and_reuse ( ) {
1515 let test_image = get_test_image ( ) ;
1616
17- // Generate unique names for test VMs
18- let timestamp = std:: time:: SystemTime :: now ( )
19- . duration_since ( std:: time:: UNIX_EPOCH )
20- . unwrap ( )
21- . as_secs ( ) ;
22- let vm1_name = format ! ( "test-base-disk-vm1-{}" , timestamp) ;
23- let vm2_name = format ! ( "test-base-disk-vm2-{}" , timestamp) ;
17+ // Generate unique names for test VMs using shortuuid pattern
18+ let vm1_name_template = "test-base-disk-vm1-{shortuuid}" ;
19+ let vm2_name_template = "test-base-disk-vm2-{shortuuid}" ;
2420
2521 println ! ( "Testing base disk creation and reuse" ) ;
26- println ! ( "VM1: {}" , vm1_name) ;
27- println ! ( "VM2: {}" , vm2_name) ;
2822
29- // Cleanup any existing test domains
30- cleanup_domain ( & vm1_name ) ;
31- cleanup_domain ( & vm2_name ) ;
23+ // Create temp files for domain names
24+ let vm1_id_file = tempfile :: NamedTempFile :: new ( ) . expect ( "Failed to create temp file" ) ;
25+ let vm1_id_path = vm1_id_file . path ( ) . to_str ( ) . expect ( "Invalid temp file path" ) ;
3226
3327 // Create first VM - this should create a new base disk
3428 println ! ( "Creating first VM (should create base disk)..." ) ;
3529 let vm1_output = run_bcvk ( & [
3630 "libvirt" ,
3731 "run" ,
3832 "--name" ,
39- & vm1_name,
33+ vm1_name_template,
34+ "--write-id-to" ,
35+ vm1_id_path,
4036 "--filesystem" ,
4137 "ext4" ,
4238 & test_image,
@@ -47,12 +43,17 @@ pub fn test_base_disk_creation_and_reuse() {
4743 println ! ( "VM1 stderr: {}" , vm1_output. stderr) ;
4844
4945 if !vm1_output. success ( ) {
50- cleanup_domain ( & vm1_name) ;
51- cleanup_domain ( & vm2_name) ;
52-
5346 panic ! ( "Failed to create first VM: {}" , vm1_output. stderr) ;
5447 }
5548
49+ // Read the domain name from the file
50+ let vm1_name = std:: fs:: read_to_string ( vm1_id_path)
51+ . expect ( "Failed to read VM1 domain name from file" )
52+ . trim ( )
53+ . to_string ( ) ;
54+
55+ println ! ( "Created VM1: {}" , vm1_name) ;
56+
5657 // Verify base disk was created
5758 assert ! (
5859 vm1_output. stdout. contains( "Using base disk" ) || vm1_output. stdout. contains( "base disk" ) ,
@@ -61,11 +62,16 @@ pub fn test_base_disk_creation_and_reuse() {
6162
6263 // Create second VM - this should reuse the base disk
6364 println ! ( "Creating second VM (should reuse base disk)..." ) ;
65+ let vm2_id_file = tempfile:: NamedTempFile :: new ( ) . expect ( "Failed to create temp file" ) ;
66+ let vm2_id_path = vm2_id_file. path ( ) . to_str ( ) . expect ( "Invalid temp file path" ) ;
67+
6468 let vm2_output = run_bcvk ( & [
6569 "libvirt" ,
6670 "run" ,
6771 "--name" ,
68- & vm2_name,
72+ vm2_name_template,
73+ "--write-id-to" ,
74+ vm2_id_path,
6975 "--filesystem" ,
7076 "ext4" ,
7177 & test_image,
@@ -75,14 +81,24 @@ pub fn test_base_disk_creation_and_reuse() {
7581 println ! ( "VM2 stdout: {}" , vm2_output. stdout) ;
7682 println ! ( "VM2 stderr: {}" , vm2_output. stderr) ;
7783
78- // Cleanup before assertions
79- cleanup_domain ( & vm1_name) ;
80- cleanup_domain ( & vm2_name) ;
81-
8284 if !vm2_output. success ( ) {
85+ // Cleanup VM1 before panicking
86+ cleanup_domain ( & vm1_name) ;
8387 panic ! ( "Failed to create second VM: {}" , vm2_output. stderr) ;
8488 }
8589
90+ // Read the domain name from the file
91+ let vm2_name = std:: fs:: read_to_string ( vm2_id_path)
92+ . expect ( "Failed to read VM2 domain name from file" )
93+ . trim ( )
94+ . to_string ( ) ;
95+
96+ println ! ( "Created VM2: {}" , vm2_name) ;
97+
98+ // Cleanup before assertions
99+ cleanup_domain ( & vm1_name) ;
100+ cleanup_domain ( & vm2_name) ;
101+
86102 // Verify base disk was reused (should be faster and mention using existing)
87103 assert ! (
88104 vm2_output. stdout. contains( "Using base disk" ) || vm2_output. stdout. contains( "base disk" ) ,
@@ -170,34 +186,40 @@ pub fn test_base_disks_prune_dry_run() {
170186pub fn test_vm_disk_references_base ( ) {
171187 let test_image = get_test_image ( ) ;
172188
173- let timestamp = std:: time:: SystemTime :: now ( )
174- . duration_since ( std:: time:: UNIX_EPOCH )
175- . unwrap ( )
176- . as_secs ( ) ;
177- let vm_name = format ! ( "test-disk-ref-{}" , timestamp) ;
189+ let vm_name_template = "test-disk-ref-{shortuuid}" ;
178190
179191 println ! ( "Testing VM disk references base disk" ) ;
180192
181- cleanup_domain ( & vm_name) ;
193+ // Create temp file for domain name
194+ let id_file = tempfile:: NamedTempFile :: new ( ) . expect ( "Failed to create temp file" ) ;
195+ let id_path = id_file. path ( ) . to_str ( ) . expect ( "Invalid temp file path" ) ;
182196
183197 // Create VM
184198 let output = run_bcvk ( & [
185199 "libvirt" ,
186200 "run" ,
187201 "--name" ,
188- & vm_name,
202+ vm_name_template,
203+ "--write-id-to" ,
204+ id_path,
189205 "--filesystem" ,
190206 "ext4" ,
191207 & test_image,
192208 ] )
193209 . expect ( "Failed to create VM" ) ;
194210
195211 if !output. success ( ) {
196- cleanup_domain ( & vm_name) ;
197-
198212 panic ! ( "Failed to create VM: {}" , output. stderr) ;
199213 }
200214
215+ // Read the domain name from the file
216+ let vm_name = std:: fs:: read_to_string ( id_path)
217+ . expect ( "Failed to read domain name from file" )
218+ . trim ( )
219+ . to_string ( ) ;
220+
221+ println ! ( "Created VM: {}" , vm_name) ;
222+
201223 // Get VM disk path from domain XML
202224 let dumpxml_output = Command :: new ( "virsh" )
203225 . args ( & [ "dumpxml" , & vm_name] )
@@ -238,7 +260,6 @@ pub fn test_vm_disk_references_base() {
238260 println ! ( "✓ VM disk reference test passed" ) ;
239261}
240262
241- /// Helper function to cleanup domain and its disk
242263fn cleanup_domain ( domain_name : & str ) {
243264 println ! ( "Cleaning up domain: {}" , domain_name) ;
244265
0 commit comments