Skip to content

Commit fe90722

Browse files
committed
refactor: reorganize test structure into unit/integration/e2e directories
- Move unit tests to t/unit/ directory (app.t, command/provision.t, provision/opentofu.t) - Move integration tests to t/integration/ directory (app.t) - Keep e2e tests in t/e2e/ directory (provision.t, renamed from 001-provision.t) - Create dedicated test scripts: test-unit, test-integration, test-e2e, test-all - Update GitHub Actions workflow to run unit and integration tests separately - Exclude e2e tests from CI as they require local virtualization - Add Test::Exception dependency to cpanfile for e2e tests - Fix cleanup in e2e tests to use OpenTofu destroy directly All tests pass: unit (3 files, 11 tests), integration (1 file, 1 test), e2e (1 file, 4 tests)
1 parent fa9176a commit fe90722

File tree

13 files changed

+148
-11
lines changed

13 files changed

+148
-11
lines changed

.github/workflows/testing.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
name: Testing
23

34
on:
@@ -58,7 +59,12 @@ jobs:
5859
eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
5960
carmel install
6061
61-
- name: Run tests
62+
- name: Run unit tests
6263
run: |
6364
eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
64-
carmel exec -- prove -lr t/
65+
./script/test-unit
66+
67+
- name: Run integration tests
68+
run: |
69+
eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
70+
./script/test-integration

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,39 @@ cd build/tofu && tofu output
111111

112112
## Testing
113113

114-
Run the test suite:
114+
The project includes two types of tests:
115+
116+
### Unit Tests
117+
118+
Run unit tests that don't require virtualization:
119+
120+
```bash
121+
./script/test-unit
122+
```
123+
124+
These tests can run in CI environments and test individual components in isolation.
125+
126+
### E2E Tests
127+
128+
Run end-to-end tests that require local virtualization support:
115129

116130
```bash
117-
carmel exec -- prove -l t/
131+
./script/test-e2e
118132
```
133+
134+
**Requirements for E2E tests:**
135+
136+
- Local machine with KVM/libvirt support
137+
- OpenTofu installed
138+
- Required system tools: `qemu-system-x86_64`, `sshpass`
139+
- Cannot run in CI environments
140+
141+
### All Tests
142+
143+
Run both unit and E2E tests:
144+
145+
```bash
146+
./script/test-all
147+
```
148+
149+
E2E tests will be automatically skipped in CI environments or when virtualization tools are not available.

cpanfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ requires 'Path::Tiny';
77
on 'test' => sub {
88
requires 'Test2::Suite';
99
requires 'Test::MockModule';
10+
requires 'Test::Exception';
1011
};
1112

1213
on 'develop' => sub {

project-words.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
autoport
22
buildx
33
cdrom
4+
chdir
45
cloudinit
56
commoninit
67
containerd
@@ -10,6 +11,7 @@ cpanminus
1011
dialout
1112
dmacvicar
1213
dpkg
14+
Ilib
1315
installdeps
1416
keyrings
1517
libvirtd
@@ -30,6 +32,7 @@ shogo
3032
somaxconn
3133
sshpass
3234
subtest
35+
tfstate
3336
usermod
3437
vcpu
3538
virsh

script/test-all

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Run all tests (unit, integration, and e2e)
4+
exec carmel exec -- prove -l t/unit/ t/integration/ t/e2e/ --ext .t --recurse

script/test-e2e

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Run E2E tests only
4+
exec carmel exec -- prove -l t/e2e/ --ext .t --recurse

script/test-integration

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Run integration tests only
4+
exec carmel exec -- prove -l t/integration/ --ext .t --recurse

script/test-unit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Run unit tests only
4+
exec carmel exec -- prove -l t/unit/ --ext .t --recurse

t/e2e/provision.t

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use v5.40;
2+
use Test::More;
3+
use Test::Exception;
4+
use Capture::Tiny qw(capture);
5+
use File::Temp qw(tempdir);
6+
use Path::Tiny qw(path);
7+
8+
# Skip this test if we're in CI or if virtualization is not available
9+
BEGIN {
10+
if ($ENV{CI} || $ENV{SKIP_E2E}) {
11+
plan skip_all => 'E2E tests require local virtualization support';
12+
}
13+
14+
# Check if required tools are available
15+
my $missing_tools = [];
16+
for my $tool (qw(tofu libvirtd qemu-system-x86_64 sshpass)) {
17+
system("which $tool >/dev/null 2>&1") != 0 and push @$missing_tools, $tool;
18+
}
19+
20+
if (@$missing_tools) {
21+
plan skip_all => "Missing required tools: " . join(', ', @$missing_tools);
22+
}
23+
}
24+
25+
plan tests => 4;
26+
27+
# Change to project root for the test
28+
my $original_cwd = path('.');
29+
my $project_root = path(__FILE__)->parent->parent->parent;
30+
chdir $project_root or die "Cannot change to project root: $!";
31+
32+
# Ensure we have required templates
33+
ok(-f 'templates/provision/tofu/providers/libvirt/main.tf', 'Required OpenTofu template exists');
34+
ok(-f 'templates/provision/cloud-init.yml', 'Required cloud-init template exists');
35+
36+
subtest 'provision command executes successfully' => sub {
37+
plan tests => 3;
38+
39+
# Capture command output
40+
my ($stdout, $stderr, $exit_code) = capture {
41+
system($^X, '-Ilib', 'bin/torrust-deploy', 'provision');
42+
};
43+
44+
# Command should complete successfully
45+
is($exit_code, 0, 'provision command exits with status 0');
46+
47+
# Output should contain success message
48+
like($stdout, qr/Provisioning completed successfully!/, 'output contains success message');
49+
50+
# Should not have critical errors in stderr
51+
unlike($stderr, qr/(?:fatal|error|died)/i, 'no critical errors in stderr');
52+
};
53+
54+
subtest 'provision creates expected infrastructure' => sub {
55+
plan tests => 2;
56+
57+
# Check if build directory was created
58+
ok(-d 'build/tofu', 'build/tofu directory was created');
59+
60+
# Check if OpenTofu state file exists
61+
ok(-f 'build/tofu/terraform.tfstate', 'OpenTofu state file was created');
62+
};
63+
64+
# Cleanup: destroy infrastructure after test
65+
END {
66+
if (-f 'build/tofu/terraform.tfstate') {
67+
note "Cleaning up test infrastructure...";
68+
# Manual cleanup using OpenTofu since destroy command doesn't exist yet
69+
if (-d 'build/tofu') {
70+
chdir 'build/tofu';
71+
system('tofu', 'destroy', '-auto-approve') if -f 'main.tf';
72+
chdir '../..';
73+
}
74+
}
75+
76+
# Restore original working directory
77+
chdir $original_cwd if $original_cwd;
78+
}
79+
80+
done_testing();

t/integration.t renamed to t/integration/app.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use Test2::V0;
22
use FindBin qw($Bin);
3-
use lib "$Bin/../lib";
3+
use lib "$Bin/../../lib";
44

55
use TorrustDeploy::App;
66

0 commit comments

Comments
 (0)