Skip to content

feat: Rename E2E provision tests to include destroy workflow with DestroyCommand integration - #15

Merged
josecelano merged 5 commits into
mainfrom
copilot/rename-e2e-provision-tests
Oct 21, 2025
Merged

josecelano merged 5 commits into
mainfrom
copilot/rename-e2e-provision-tests

Conversation

Copilot AI commented Oct 21, 2025 •

Copy link
Copy Markdown
Contributor
  • Rename src/bin/e2e_provision_tests.rs to src/bin/e2e_provision_and_destroy_tests.rs
  • Update binary name in Cargo.toml from e2e-provision-tests to e2e-provision-and-destroy-tests
  • Update binary to use run_destroy_command instead of cleanup_test_infrastructure
  • Add fallback to manual cleanup (cleanup_test_infrastructure) on destroy command failure
  • Update binary documentation and comments to reflect provision+destroy workflow
  • Update GitHub Actions workflow (.github/workflows/test-e2e-provision.yml) with new binary name
  • Update E2E testing documentation (docs/e2e-testing.md) with new binary name and functionality
  • Update all documentation references that mention the old binary name
  • Run linter and build to verify changes
  • Run CodeQL security check
  • Fix GitHub Actions workflow directory conflict when DestroyCommand succeeds

Summary

Successfully renamed and updated the E2E provision tests binary to include destroy functionality:

Key Changes

  1. Binary Renamed: e2e-provision-tests → e2e-provision-and-destroy-tests

    • Updated Cargo.toml with new binary name
    • Renamed source file from e2e_provision_tests.rs to e2e_provision_and_destroy_tests.rs
  2. DestroyCommand Integration:

    • Replaced manual cleanup with run_destroy_command from application layer
    • Added fallback to cleanup_test_infrastructure on destroy command failures
    • Ensures proper cleanup in all scenarios (success or failure)
  3. Documentation Updates:

    • Updated docs/e2e-testing.md with new binary name and destroy workflow
    • Updated docs/codebase-architecture.md binary list
    • Updated docs/decisions/docker-testing-evolution.md
    • Updated docs/research/ansible-testing-strategy.md examples
    • Updated docs/features/environment-state-management/ documentation
  4. CI/CD Updates:

    • Updated .github/workflows/test-e2e-provision.yml with new binary name
    • Updated workflow descriptions and log messages
    • Fixed workflow cleanup step to only run on failure (prevents directory conflict)
    • Added directory existence checks before accessing build directories

GitHub Actions Workflow Fix

Fixed directory conflict where the cleanup step was failing because DestroyCommand successfully removes the build/ directory:

  • Changed cleanup from if: always() to if: failure() - only runs when tests fail
  • Added directory existence checks before attempting to use working directories
  • Maintains safety net for provision failures and DestroyCommand failures
  • Prevents conflicts when DestroyCommand succeeds and properly cleans up

Testing

  • ✅ All linters passing (markdown, yaml, toml, cspell, clippy, rustfmt, shellcheck)
  • ✅ Build successful for all targets
  • ✅ Binary can be executed and shows correct help text
  • ✅ CodeQL security check passing (0 alerts)
  • ✅ GitHub Actions workflow YAML validation passing

Verification

The binary now provides complete lifecycle testing:

  1. Preflight cleanup
  2. Infrastructure provisioning
  3. Cloud-init validation
  4. Infrastructure destruction via DestroyCommand (with fallback)

This ensures comprehensive E2E testing of the full provision+destroy lifecycle while maintaining reliable cleanup in CI environments.

Original prompt

This section details on the original issue you should resolve

<issue_title>Rename and Update E2E Provision Tests to Include Destroy</issue_title>
<issue_description># Rename and Update E2E Provision Tests to Include Destroy

Issue Type: Sub-issue (9.2)
Parent Epic: #9 (epic-app-layer-destroy-command.md)
Related Roadmap: Section 1.2
Dependencies: Issue #11 (Add DestroyCommand in Application Layer) must be completed first
Priority: High
Estimated Effort: 3-4 hours


📋 Issue Overview

Modify and rename src/bin/e2e_provision_tests.rs to src/bin/e2e_provision_and_destroy_tests.rs, updating it to use the new destroy command instead of manual cleanup, ensuring CI can run complete provision+destroy cycles.

This ensures we have automated testing of the destroy functionality in CI, preventing regressions. The provision tests are already running real OpenTofu infrastructure, so adding destroy testing here gives us confidence the feature works end-to-end.

🎯 Goals

  1. Replace manual cleanup with DestroyCommand in E2E provision tests
  2. Maintain fallback to manual cleanup for destroy command failures
  3. Ensure compatibility with GitHub Actions CI environment
  4. Provide comprehensive E2E testing of the full provision+destroy lifecycle

📦 Scope

Core Changes

  • Rename e2e_provision_tests.rs to e2e_provision_and_destroy_tests.rs
  • Replace manual cleanup with DestroyCommand
  • Keep manual cleanup as fallback for destroy command failures
  • Ensure the binary works in GitHub Actions environment

Integration Points

  1. DestroyCommand Integration: Use the new application layer command
  2. Error Handling: Graceful fallback to manual cleanup on destroy failures
  3. CI Compatibility: Ensure tests work in GitHub Actions
  4. Logging: Proper error handling and logging for destroy operations

🏗️ Technical Design

Test Flow Integration

Current flow:

Provision → Manual Cleanup

New flow:

Provision → DestroyCommand → (Fallback: Manual Cleanup on failure)

Error Handling Strategy

// Pseudo-code for destroy integration
match destroy_command.execute(&environment).await {
    Ok(_) => {
        info!("Environment destroyed successfully using DestroyCommand");
    }
    Err(e) => {
        warn!("DestroyCommand failed: {e}, falling back to manual cleanup");
        manual_cleanup(&environment).await?;
    }
}

Binary Renaming

Rename e2e_provision_tests.rs to e2e_provision_and_destroy_tests.rs to reflect the expanded functionality:

  • More descriptive of actual test coverage
  • Clearly indicates full lifecycle testing
  • Aligns with the enhanced scope
  • Better represents the complete provision+destroy workflow

📋 Acceptance Criteria

  • Binary renamed from e2e_provision_tests.rs to e2e_provision_and_destroy_tests.rs
  • Binary uses DestroyCommand for cleanup instead of manual cleanup
  • Manual cleanup preserved as fallback for destroy failures
  • Binary works correctly in GitHub Actions
  • Proper error handling and logging for destroy operations
  • CI configuration updated with new binary name
  • E2E test documentation updated with new binary name and functionality

🧪 Testing Strategy

Local Testing

  • Run the updated binary locally to verify destroy command integration
  • Test both success and failure scenarios
  • Validate fallback to manual cleanup works correctly

CI Testing

  • Ensure the updated binary works in GitHub Actions
  • Verify no regressions in existing provision test functionality
  • Confirm destroy command is properly tested in CI environment

Error Scenarios

  • Test destroy command failures and fallback behavior
  • Validate proper logging and error reporting
  • Ensure manual cleanup still works as expected

🔗 Dependencies

  • Requires: Issue Add DestroyCommand in Application Layer #11 (Add DestroyCommand in Application Layer) - must be completed first
  • Requires: Existing E2E provision test infrastructure
  • Blocks: Issue 9.3 (Developer Documentation)

📝 Implementation Notes

Current E2E Provision Test Structure

The current src/bin/e2e_provision_tests.rs binary:

  1. Provisions infrastructure using OpenTofu
  2. Validates deployment
  3. Performs manual cleanup using existing cleanup functions

After this issue, the renamed src/bin/e2e_provision_and_destroy_tests.rs binary will:

  1. Provision infrastructure using OpenTofu
  2. Validate deployment
  3. Destroy infrastructure using DestroyCommand (with manual cleanup fallback)

Integration Approach

  1. Rename Binary: Move e2e_provision_tests.rs to `e2e_provision_and_...

Fixes #12


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI self-assigned this Oct 21, 2025
Copilot AI and others added 3 commits October 21, 2025 17:07
- Rename src/bin/e2e_provision_tests.rs to src/bin/e2e_provision_and_destroy_tests.rs
- Update binary to use DestroyCommand with fallback to manual cleanup
- Update Cargo.toml with new binary name
- Update GitHub Actions workflow with new binary name and descriptions

Co-authored-by: josecelano <[email protected]>
- Update docs/e2e-testing.md with new binary name and destroy workflow
- Update docs/codebase-architecture.md binary list
- Update docs/decisions/docker-testing-evolution.md with new binary name
- Update docs/research/ansible-testing-strategy.md examples
- Update docs/features/environment-state-management/ documentation

Co-authored-by: josecelano <[email protected]>
- Add backticks to DestroyCommand in doc comment
- Remove unnecessary Result return type from run_infrastructure_destroy
- Run cargo fmt to format code

Co-authored-by: josecelano <[email protected]>
Copilot AI changed the title [WIP] Rename and update E2E provision tests to include destroy feat: Rename E2E provision tests to include destroy workflow with DestroyCommand integration Oct 21, 2025
Copilot AI requested a review from josecelano October 21, 2025 17:23
@josecelano

Copy link
Copy Markdown
Member

🛠️ Fix: GitHub Actions Workflow Directory Conflict

@copilot, the E2E Provision workflow is failing due to a directory conflict after the DestroyCommand integration. I've identified and fixed the issue.

Problem Analysis

The workflow step "Cleanup infrastructure (always run)" was failing with:

Error: An error occurred trying to start process '/usr/bin/bash' with working directory '/home/runner/work/torrust-tracker-deployer/torrust-tracker-deployer/build/e2e-provision/tofu/lxd'. No such file or directory

Root Cause: The new e2e-provision-and-destroy-tests binary uses DestroyCommand which successfully destroys infrastructure AND cleans up the build/ directory. When the workflow tries to run cleanup with working-directory: build/e2e-provision/tofu/lxd, that directory has already been deleted.

Solution: Failure-Only Cleanup

Changed the cleanup step to only run on test failure (if: failure() instead of if: always()). This provides:

✅ Safety net for provision failures: If the test fails during provisioning (before DestroyCommand runs), we still attempt cleanup
✅ Safety net for DestroyCommand failures: If DestroyCommand fails, manual cleanup attempts to clean up
✅ No conflict on success: When DestroyCommand succeeds, no redundant cleanup attempts
✅ Robust directory handling: Checks if build directory exists before trying to use it

Updated Workflow Logic

- name: Cleanup infrastructure (on failure only)
  if: failure()  # Only run when test fails
  run: |
    echo "Test failed - attempting emergency cleanup..."
    
    # Try OpenTofu cleanup only if build directory still exists
    if [ -d "build/e2e-provision/tofu/lxd" ]; then
      echo "Found OpenTofu state directory, attempting tofu destroy..."
      cd build/e2e-provision/tofu/lxd
      sudo -E tofu destroy -auto-approve || echo "Tofu destroy failed or nothing to destroy"
    else
      echo "No OpenTofu state directory found (likely cleaned up by DestroyCommand)"
    fi
    
    # Always attempt LXD cleanup (no working directory dependency)  
    echo "Attempting LXD resource cleanup..."
    sudo lxc delete torrust-tracker-vm-e2e-provision --force || echo "Container deletion failed or container doesn't exist"
    sudo lxc profile delete torrust-profile-e2e-provision || echo "Profile deletion failed or profile doesn't exist"

This maintains the "better to double-fail than leave resources" philosophy while avoiding the directory conflict when tests succeed and DestroyCommand properly cleans up.

…ry existence

- Change cleanup step from 'if: always()' to 'if: failure()' to avoid conflicts when DestroyCommand succeeds
- Add directory existence checks before attempting to cd into build directories
- Update 'Get test outputs' step to handle missing build directory gracefully
- Update 'Debug information' step to check if directory exists before accessing it
- Maintain emergency cleanup capability for provision and DestroyCommand failures
- Remove working-directory dependency from cleanup step to prevent errors

Co-authored-by: josecelano <[email protected]>

Copilot AI commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

🛠️ Fix: GitHub Actions Workflow Directory Conflict

@copilot, the E2E Provision workflow is failing due to a directory conflict after the DestroyCommand integration. I've identified and fi...

Fixed in commit 31e8c61. The workflow cleanup step now:

  • Runs only on failure (if: failure()) instead of always
  • Checks if build/e2e-provision/tofu/lxd directory exists before attempting to use it
  • Removes the working-directory constraint that was causing the error
  • Maintains emergency cleanup for both provision and DestroyCommand failures

The "Get test outputs" and "Debug information" steps also now check for directory existence before attempting to access the build directory.

@josecelano
josecelano marked this pull request as ready for review October 21, 2025 20:02
@josecelano

Copy link
Copy Markdown
Member

ACK bd76a84

@josecelano
josecelano merged commit 0743e0b into main Oct 21, 2025
josecelano added a commit that referenced this pull request Dec 12, 2025
- Add rule #15 to Essential Rules section in AGENTS.md
- Emphasizes CRITICAL importance of using it_should_* pattern
- Explicitly forbids test_* prefix for test function names
- References docs/contributing/testing/unit-testing.md for details
- Includes examples of correct vs incorrect naming
- Preventive measure to avoid future test naming violations
@josecelano
josecelano deleted the copilot/rename-e2e-provision-tests branch April 15, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rename and Update E2E Provision Tests to Include Destroy

2 participants