|
| 1 | +# Build Directory Not Cleaned Between E2E Test Runs |
| 2 | + |
| 3 | +## Issue Summary |
| 4 | + |
| 5 | +The E2E test suite does not properly clean the `build/` directory between test runs, causing stale cached template files to persist. This results in outdated configurations being used instead of the latest templates. |
| 6 | + |
| 7 | +## Problem Description |
| 8 | + |
| 9 | +When running E2E tests, the following sequence occurs: |
| 10 | + |
| 11 | +1. **Template Rendering**: `RenderOpenTofuTemplatesStep` calls `TofuTemplateRenderer.render()` |
| 12 | +2. **Directory Creation**: `create_build_directory()` uses `tokio::fs::create_dir_all()` |
| 13 | +3. **File Copying**: Static templates are copied to `build/tofu/lxd/` |
| 14 | +4. **Issue**: Existing files are NOT removed before copying new ones |
| 15 | + |
| 16 | +## Evidence |
| 17 | + |
| 18 | +- **Template Source**: `templates/tofu/lxd/main.tf` correctly uses `variable "instance_name"` |
| 19 | +- **Build Output**: `build/tofu/lxd/main.tf` incorrectly shows `variable "container_name"` |
| 20 | +- **Root Cause**: `tokio::fs::create_dir_all()` creates directories but does not clean existing content |
| 21 | + |
| 22 | +## Impact |
| 23 | + |
| 24 | +- Template changes are not reflected in E2E test runs |
| 25 | +- Inconsistent behavior between fresh environments and cached environments |
| 26 | +- Failed refactoring validation (instance name parameterization not working) |
| 27 | +- Potential for stale configuration bugs in deployment workflows |
| 28 | + |
| 29 | +## Technical Details |
| 30 | + |
| 31 | +### Affected Components |
| 32 | + |
| 33 | +- `src/tofu/template/renderer/mod.rs` - `TofuTemplateRenderer::create_build_directory()` |
| 34 | +- `src/steps/rendering/opentofu_templates.rs` - `RenderOpenTofuTemplatesStep::execute()` |
| 35 | +- E2E test workflow in `src/bin/e2e_tests.rs` |
| 36 | + |
| 37 | +### Current Behavior |
| 38 | + |
| 39 | +```rust |
| 40 | +// In TofuTemplateRenderer::create_build_directory() |
| 41 | +async fn create_build_directory(&self) -> Result<PathBuf, ProvisionTemplateError> { |
| 42 | + let build_tofu_dir = self.build_opentofu_directory(); |
| 43 | + tokio::fs::create_dir_all(&build_tofu_dir) // ❌ Does not clean existing content |
| 44 | + .await |
| 45 | + .map_err(|source| ProvisionTemplateError::DirectoryCreationFailed { |
| 46 | + directory: build_tofu_dir.display().to_string(), |
| 47 | + source, |
| 48 | + })?; |
| 49 | + Ok(build_tofu_dir) |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Expected Behavior |
| 54 | + |
| 55 | +The build directory should be cleaned before template rendering to ensure fresh state: |
| 56 | + |
| 57 | +```rust |
| 58 | +async fn create_build_directory(&self) -> Result<PathBuf, ProvisionTemplateError> { |
| 59 | + let build_tofu_dir = self.build_opentofu_directory(); |
| 60 | + |
| 61 | + // Clean existing content if directory exists |
| 62 | + if build_tofu_dir.exists() { |
| 63 | + tokio::fs::remove_dir_all(&build_tofu_dir).await?; |
| 64 | + } |
| 65 | + |
| 66 | + // Create fresh directory structure |
| 67 | + tokio::fs::create_dir_all(&build_tofu_dir).await?; |
| 68 | + Ok(build_tofu_dir) |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Proposed Solutions |
| 73 | + |
| 74 | +### Option 1: Clean in Template Renderer (Recommended) |
| 75 | + |
| 76 | +- Modify `create_build_directory()` to remove existing content |
| 77 | +- Ensures fresh state for every template rendering operation |
| 78 | +- Minimal code changes, focused responsibility |
| 79 | + |
| 80 | +### Option 2: Clean in E2E Preflight Cleanup |
| 81 | + |
| 82 | +- Add build directory cleanup to `preflight_cleanup::cleanup_lingering_resources()` |
| 83 | +- Consistent with existing cleanup patterns |
| 84 | +- Requires coordination between cleanup and rendering phases |
| 85 | + |
| 86 | +### Option 3: Clean in RenderOpenTofuTemplatesStep |
| 87 | + |
| 88 | +- Add cleanup logic directly in the rendering step |
| 89 | +- More explicit control over cleanup timing |
| 90 | +- Slightly more complex but very clear intent |
| 91 | + |
| 92 | +## Testing Strategy |
| 93 | + |
| 94 | +1. **Verify Issue**: Confirm stale files persist between E2E runs |
| 95 | +2. **Implement Fix**: Apply chosen solution |
| 96 | +3. **Validate Cleanup**: Ensure build directory is properly cleaned |
| 97 | +4. **E2E Validation**: Run full E2E test suite to confirm template refresh |
| 98 | +5. **Regression Testing**: Multiple consecutive E2E runs to verify consistency |
| 99 | + |
| 100 | +## Related Issues |
| 101 | + |
| 102 | +- Instance name parameterization refactor blocked by stale templates |
| 103 | +- Potential for similar issues in Ansible template rendering |
| 104 | +- Build directory management needs comprehensive review |
| 105 | + |
| 106 | +## Priority |
| 107 | + |
| 108 | +**High** - Blocks active refactoring work and could cause deployment inconsistencies. |
| 109 | + |
| 110 | +## Labels |
| 111 | + |
| 112 | +- `bug` |
| 113 | +- `e2e-tests` |
| 114 | +- `template-rendering` |
| 115 | +- `build-system` |
| 116 | +- `infrastructure` |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +**Created**: September 17, 2025 |
| 121 | +**Status**: Open |
| 122 | +**Assignee**: TBD |
0 commit comments