@@ -22,13 +22,65 @@ BEGIN {
22
22
}
23
23
}
24
24
25
- plan tests => 4 ;
25
+ plan tests => 5 ;
26
26
27
27
# Change to project root for the test
28
28
my $original_cwd = path(' .' );
29
29
my $project_root = path(__FILE__ )-> parent-> parent-> parent;
30
30
chdir $project_root or die " Cannot change to project root: $! " ;
31
31
32
+ # Clean up any leftover resources from previous test runs
33
+ subtest ' cleanup leftover resources from previous runs' => sub {
34
+ plan tests => 1;
35
+
36
+ my $cleanup_needed = 0;
37
+
38
+ # Check for existing libvirt domain
39
+ my $domain_exists = system (' sudo virsh domstate torrust-tracker >/dev/null 2>&1' ) == 0;
40
+ if ($domain_exists ) {
41
+ note " Found existing torrust-tracker domain, cleaning up..." ;
42
+ system (' sudo virsh destroy torrust-tracker >/dev/null 2>&1' );
43
+ system (' sudo virsh undefine torrust-tracker >/dev/null 2>&1' );
44
+ $cleanup_needed = 1;
45
+ }
46
+
47
+ # Check for existing cloudinit volume
48
+ my $cloudinit_exists = system (' sudo virsh vol-info torrust-cloudinit.iso --pool default >/dev/null 2>&1' ) == 0;
49
+ if ($cloudinit_exists ) {
50
+ note " Found existing torrust-cloudinit.iso volume, cleaning up..." ;
51
+ system (' sudo virsh vol-delete torrust-cloudinit.iso --pool default >/dev/null 2>&1' );
52
+ $cleanup_needed = 1;
53
+ }
54
+
55
+ # Check for other volumes that might be left over
56
+ for my $vol_name (qw( torrust-tracker-vm.qcow2 ubuntu-22.04-base.qcow2) ) {
57
+ my $vol_exists = system (" sudo virsh vol-info '$vol_name ' --pool default >/dev/null 2>&1" ) == 0;
58
+ if ($vol_exists ) {
59
+ note " Found existing $vol_name volume, cleaning up..." ;
60
+ system (" sudo virsh vol-delete '$vol_name ' --pool default >/dev/null 2>&1" );
61
+ $cleanup_needed = 1;
62
+ }
63
+ }
64
+
65
+ # Clean up any existing build/tofu state
66
+ if (-f ' build/tofu/terraform.tfstate' ) {
67
+ note " Found existing OpenTofu state, cleaning up..." ;
68
+ if (-d ' build/tofu' ) {
69
+ chdir ' build/tofu' ;
70
+ system (' tofu destroy -auto-approve >/dev/null 2>&1' );
71
+ chdir ' ../..' ;
72
+ }
73
+ $cleanup_needed = 1;
74
+ }
75
+
76
+ if ($cleanup_needed ) {
77
+ note " Cleanup completed, waiting a moment for resources to be fully removed..." ;
78
+ sleep 2;
79
+ }
80
+
81
+ pass(' Pre-test cleanup completed' );
82
+ };
83
+
32
84
# Ensure we have required templates
33
85
ok(-f ' templates/provision/tofu/providers/libvirt/main.tf' , ' Required OpenTofu template exists' );
34
86
ok(-f ' templates/provision/cloud-init.yml' , ' Required cloud-init template exists' );
@@ -63,13 +115,39 @@ subtest 'provision creates expected infrastructure' => sub {
63
115
64
116
# Cleanup: destroy infrastructure after test
65
117
END {
118
+ # Ensure we're in the right directory for cleanup
119
+ if ($project_root && -d $project_root ) {
120
+ chdir $project_root ;
121
+ }
122
+
66
123
if (-f ' build/tofu/terraform.tfstate' ) {
67
124
note " Cleaning up test infrastructure..." ;
68
- # Manual cleanup using OpenTofu since destroy command doesn't exist yet
125
+
126
+ # Try OpenTofu destroy first (proper way)
69
127
if (-d ' build/tofu' ) {
70
128
chdir ' build/tofu' ;
71
- system (' tofu' , ' destroy' , ' -auto-approve' ) if -f ' main.tf ' ;
129
+ my $destroy_result = system (' tofu destroy -auto-approve >/dev/null 2>&1 ' ) ;
72
130
chdir ' ../..' ;
131
+
132
+ # If OpenTofu destroy failed, manually clean up libvirt resources
133
+ if ($destroy_result != 0) {
134
+ note " OpenTofu destroy failed, manually cleaning up libvirt resources..." ;
135
+
136
+ # Clean up domain
137
+ system (' sudo virsh destroy torrust-tracker >/dev/null 2>&1' );
138
+ system (' sudo virsh undefine torrust-tracker >/dev/null 2>&1' );
139
+
140
+ # Clean up volumes
141
+ for my $vol_name (qw( torrust-cloudinit.iso torrust-tracker-vm.qcow2 ubuntu-22.04-base.qcow2) ) {
142
+ system (" sudo virsh vol-delete '$vol_name ' --pool default >/dev/null 2>&1" );
143
+ }
144
+ }
145
+ }
146
+
147
+ # Clean up build directory if everything was destroyed successfully
148
+ if (system (' sudo virsh domstate torrust-tracker >/dev/null 2>&1' ) != 0) {
149
+ # Domain doesn't exist, safe to remove build state
150
+ system (' rm -rf build/tofu' ) if -d ' build/tofu' ;
73
151
}
74
152
}
75
153
0 commit comments