|
| 1 | +# Advanced E2E Testing Techniques |
| 2 | + |
| 3 | +This guide covers advanced testing techniques and workflows for experienced users. |
| 4 | + |
| 5 | +## 🧪 Manual E2E Testing with Cross-Environment Registration |
| 6 | + |
| 7 | +When manually testing the `register` command or the deployment pipeline, you can use a cross-environment technique that avoids manually provisioning VMs. |
| 8 | + |
| 9 | +### The Technique |
| 10 | + |
| 11 | +Use the deployer to provision one environment, then register that VM with a second environment: |
| 12 | + |
| 13 | +```bash |
| 14 | +# 1. Create and provision the first environment (owns the VM) |
| 15 | +torrust-tracker-deployer --working-dir envs create environment --env-file envs/env-01.json |
| 16 | +torrust-tracker-deployer --working-dir envs provision env-01 |
| 17 | + |
| 18 | +# 2. Get the instance IP from env-01 |
| 19 | +cat envs/data/env-01/environment.json | grep instance_ip |
| 20 | +# Example output: "instance_ip": "10.140.190.186" |
| 21 | + |
| 22 | +# 3. Create the second environment and register it with env-01's VM |
| 23 | +torrust-tracker-deployer --working-dir envs create environment --env-file envs/env-02.json |
| 24 | +torrust-tracker-deployer --working-dir envs register env-02 --instance-ip 10.140.190.186 |
| 25 | + |
| 26 | +# 4. Test the register workflow (configure, test, destroy) |
| 27 | +torrust-tracker-deployer --working-dir envs configure env-02 |
| 28 | +torrust-tracker-deployer --working-dir envs test env-02 |
| 29 | +torrust-tracker-deployer --working-dir envs destroy env-02 # VM preserved! |
| 30 | + |
| 31 | +# 5. Clean up the actual VM |
| 32 | +torrust-tracker-deployer --working-dir envs destroy env-01 # VM destroyed |
| 33 | +``` |
| 34 | + |
| 35 | +### Why This Works |
| 36 | + |
| 37 | +- **env-01** has `provision_method: null` (or `Provisioned`) → destroy removes the VM |
| 38 | +- **env-02** has `provision_method: Registered` → destroy preserves the VM |
| 39 | + |
| 40 | +### Use Cases |
| 41 | + |
| 42 | +This technique is useful for: |
| 43 | + |
| 44 | +- **Testing register command**: Without needing external infrastructure |
| 45 | +- **Verifying destroy behavior**: Confirming registered infrastructure is preserved |
| 46 | +- **Testing deployment pipeline**: On registered environments |
| 47 | +- **Rapid iteration**: Reuse same VM across multiple test cycles |
| 48 | +- **Resource efficiency**: Avoid repeated VM provisioning during development |
| 49 | + |
| 50 | +### Advanced Patterns |
| 51 | + |
| 52 | +#### Multiple Registered Environments |
| 53 | + |
| 54 | +You can register multiple environments to the same VM: |
| 55 | + |
| 56 | +```bash |
| 57 | +# Provision one VM |
| 58 | +torrust-tracker-deployer provision env-01 |
| 59 | + |
| 60 | +# Register multiple test environments to it |
| 61 | +torrust-tracker-deployer register env-test-a --instance-ip 10.140.190.186 |
| 62 | +torrust-tracker-deployer register env-test-b --instance-ip 10.140.190.186 |
| 63 | +torrust-tracker-deployer register env-test-c --instance-ip 10.140.190.186 |
| 64 | + |
| 65 | +# Test different configurations on same VM |
| 66 | +torrust-tracker-deployer configure env-test-a |
| 67 | +torrust-tracker-deployer configure env-test-b # Different config |
| 68 | +torrust-tracker-deployer configure env-test-c # Another config |
| 69 | + |
| 70 | +# Clean up all test environments (VM preserved) |
| 71 | +torrust-tracker-deployer destroy env-test-a |
| 72 | +torrust-tracker-deployer destroy env-test-b |
| 73 | +torrust-tracker-deployer destroy env-test-c |
| 74 | + |
| 75 | +# Finally destroy the VM |
| 76 | +torrust-tracker-deployer destroy env-01 |
| 77 | +``` |
| 78 | + |
| 79 | +#### Non-Standard SSH Ports |
| 80 | + |
| 81 | +Test with custom SSH ports: |
| 82 | + |
| 83 | +```bash |
| 84 | +# Register with custom SSH port |
| 85 | +torrust-tracker-deployer register env-test \ |
| 86 | + --instance-ip 10.140.190.186 \ |
| 87 | + --ssh-port 2222 |
| 88 | + |
| 89 | +# All subsequent commands use the custom port automatically |
| 90 | +torrust-tracker-deployer configure env-test |
| 91 | +torrust-tracker-deployer test env-test |
| 92 | +``` |
| 93 | + |
| 94 | +## 🔧 Custom Template Testing |
| 95 | + |
| 96 | +Test custom templates without modifying the main template directory: |
| 97 | + |
| 98 | +```bash |
| 99 | +# Copy templates to a custom location |
| 100 | +cp -r templates/ /tmp/my-custom-templates/ |
| 101 | + |
| 102 | +# Modify templates as needed |
| 103 | +vim /tmp/my-custom-templates/ansible/playbooks/install-docker.yml |
| 104 | + |
| 105 | +# Run tests with custom templates |
| 106 | +cargo run --bin e2e-deployment-workflow-tests -- \ |
| 107 | + --templates-dir /tmp/my-custom-templates |
| 108 | +``` |
| 109 | + |
| 110 | +## 🐛 Advanced Debugging Techniques |
| 111 | + |
| 112 | +### Inspect Container State During Execution |
| 113 | + |
| 114 | +Use `--keep` flag and connect while tests are paused: |
| 115 | + |
| 116 | +```bash |
| 117 | +# Terminal 1: Run test with keep flag |
| 118 | +cargo run --bin e2e-deployment-workflow-tests -- --keep |
| 119 | + |
| 120 | +# Terminal 2: While test is running, find container |
| 121 | +docker ps |
| 122 | + |
| 123 | +# Terminal 3: Connect and inspect |
| 124 | +docker exec -it <container-id> /bin/bash |
| 125 | + |
| 126 | +# Inside container: check logs, validate state, etc. |
| 127 | +journalctl -u docker |
| 128 | +cat /var/log/cloud-init-output.log |
| 129 | +``` |
| 130 | + |
| 131 | +### LXD VM Snapshots for Debugging |
| 132 | + |
| 133 | +Create snapshots at specific test stages: |
| 134 | + |
| 135 | +```bash |
| 136 | +# During test execution, create snapshot |
| 137 | +lxc snapshot torrust-tracker-vm pre-configure |
| 138 | + |
| 139 | +# If test fails, restore to snapshot |
| 140 | +lxc restore torrust-tracker-vm pre-configure |
| 141 | + |
| 142 | +# Manually test the failing step |
| 143 | +lxc exec torrust-tracker-vm -- /bin/bash |
| 144 | +``` |
| 145 | + |
| 146 | +### Ansible Verbose Output |
| 147 | + |
| 148 | +Enable verbose Ansible output for debugging: |
| 149 | + |
| 150 | +```bash |
| 151 | +# Set environment variable before running tests |
| 152 | +export ANSIBLE_VERBOSITY=3 |
| 153 | +cargo run --bin e2e-deployment-workflow-tests |
| 154 | +``` |
| 155 | + |
| 156 | +## 📊 Performance Analysis |
| 157 | + |
| 158 | +### Measure Test Execution Time |
| 159 | + |
| 160 | +```bash |
| 161 | +# Time complete test run |
| 162 | +time cargo run --bin e2e-complete-workflow-tests |
| 163 | + |
| 164 | +# Time individual phases |
| 165 | +time cargo run --bin e2e-infrastructure-lifecycle-tests |
| 166 | +time cargo run --bin e2e-deployment-workflow-tests |
| 167 | +``` |
| 168 | + |
| 169 | +### Profile Resource Usage |
| 170 | + |
| 171 | +```bash |
| 172 | +# Monitor system resources during test |
| 173 | +docker stats # For deployment workflow tests |
| 174 | +lxc info torrust-tracker-vm # For infrastructure tests |
| 175 | +``` |
| 176 | + |
| 177 | +## 🔄 Continuous Integration Testing |
| 178 | + |
| 179 | +### Local CI Simulation |
| 180 | + |
| 181 | +Simulate GitHub Actions environment locally: |
| 182 | + |
| 183 | +```bash |
| 184 | +# Use act to run GitHub Actions locally |
| 185 | +act -j test-e2e-infrastructure |
| 186 | +act -j test-e2e-deployment |
| 187 | +``` |
| 188 | + |
| 189 | +### Parallel Test Execution |
| 190 | + |
| 191 | +Run independent test suites in parallel: |
| 192 | + |
| 193 | +```bash |
| 194 | +# Terminal 1 |
| 195 | +cargo run --bin e2e-infrastructure-lifecycle-tests |
| 196 | + |
| 197 | +# Terminal 2 (can run simultaneously) |
| 198 | +cargo run --bin e2e-deployment-workflow-tests |
| 199 | +``` |
| 200 | + |
| 201 | +## 🎯 Best Practices |
| 202 | + |
| 203 | +1. **Use split tests for CI**: Always use infrastructure and deployment tests separately in CI |
| 204 | +2. **Complete tests locally**: Run complete workflow tests before submitting PRs |
| 205 | +3. **Debug with --keep**: Always use `--keep` flag when debugging failed tests |
| 206 | +4. **Custom templates**: Test template changes with `--templates-dir` before committing |
| 207 | +5. **Cross-environment**: Use cross-environment registration for rapid iteration |
| 208 | +6. **Snapshots**: Leverage LXD snapshots for complex debugging scenarios |
| 209 | +7. **Cleanup**: Always clean up resources after manual testing |
| 210 | + |
| 211 | +## 🔗 Related Documentation |
| 212 | + |
| 213 | +- [Running Tests](running-tests.md) - Basic test execution |
| 214 | +- [Troubleshooting](troubleshooting.md) - Common issues and fixes |
| 215 | +- [Architecture](architecture.md) - Understanding the test architecture |
| 216 | +- [Contributing](contributing.md) - Extending E2E tests |
0 commit comments