Skip to content

Commit 7750eb2

Browse files
committed
test: improve e2e test cleanup to handle leftover resources
- Add pre-test cleanup to check for and remove leftover libvirt resources - Improve post-test cleanup with fallback to manual resource removal - Handle existing domains, volumes, and OpenTofu state from previous runs - Add domstate to project word list for spell checking - Fixes e2e test failures caused by resource conflicts from previous runs
1 parent 472a4f2 commit 7750eb2

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

project-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cpanm
1212
cpanminus
1313
dialout
1414
dmacvicar
15+
domstate
1516
dpkg
1617
getcwd
1718
hashref

t/e2e/provision.t

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,65 @@ BEGIN {
2222
}
2323
}
2424

25-
plan tests => 4;
25+
plan tests => 5;
2626

2727
# Change to project root for the test
2828
my $original_cwd = path('.');
2929
my $project_root = path(__FILE__)->parent->parent->parent;
3030
chdir $project_root or die "Cannot change to project root: $!";
3131

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+
3284
# Ensure we have required templates
3385
ok(-f 'templates/provision/tofu/providers/libvirt/main.tf', 'Required OpenTofu template exists');
3486
ok(-f 'templates/provision/cloud-init.yml', 'Required cloud-init template exists');
@@ -63,13 +115,39 @@ subtest 'provision creates expected infrastructure' => sub {
63115

64116
# Cleanup: destroy infrastructure after test
65117
END {
118+
# Ensure we're in the right directory for cleanup
119+
if ($project_root && -d $project_root) {
120+
chdir $project_root;
121+
}
122+
66123
if (-f 'build/tofu/terraform.tfstate') {
67124
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)
69127
if (-d 'build/tofu') {
70128
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');
72130
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';
73151
}
74152
}
75153

0 commit comments

Comments
 (0)