|
| 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(); |
0 commit comments