Skip to content

Commit 5014c11

Browse files
committed
refactor: extract ConfigurationTemplateRenderer collaborator from e2e_tests.rs
- Create src/stages/configuration/template_renderer.rs with structured error handling - Add support for both static file copying and dynamic Tera template rendering - Implement comprehensive unit tests following it_should_ naming convention - Use tracing crate for logging instead of println! statements - Simplify render_configuration_templates method in e2e_tests.rs - Follow same architectural pattern as existing ProvisionTemplateRenderer - All tests pass (197 passed, 0 failed, 3 ignored) - All linters pass (clippy, rustfmt, shellcheck, markdown, yaml, toml)
1 parent a8044d0 commit 5014c11

File tree

4 files changed

+568
-70
lines changed

4 files changed

+568
-70
lines changed

src/bin/e2e_tests.rs

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ use torrust_tracker_deploy::ansible::AnsibleClient;
1111
use torrust_tracker_deploy::lxd::LxdClient;
1212
use torrust_tracker_deploy::opentofu::OpenTofuClient;
1313
use torrust_tracker_deploy::ssh::SshClient;
14-
use torrust_tracker_deploy::stages::ProvisionTemplateRenderer;
14+
use torrust_tracker_deploy::stages::{ConfigurationTemplateRenderer, ProvisionTemplateRenderer};
1515
// Import template system
16-
use torrust_tracker_deploy::template::file::File;
1716
use torrust_tracker_deploy::template::wrappers::ansible::inventory::{
18-
AnsibleHost, InventoryContext, InventoryTemplate, SshPrivateKeyFile,
17+
AnsibleHost, InventoryContext, SshPrivateKeyFile,
1918
};
2019
use torrust_tracker_deploy::template::TemplateManager;
2120
// Import remote actions
@@ -51,6 +50,7 @@ struct TestEnvironment {
5150
template_manager: TemplateManager,
5251
opentofu_client: OpenTofuClient,
5352
provision_renderer: ProvisionTemplateRenderer,
53+
configuration_renderer: ConfigurationTemplateRenderer,
5454
ssh_client: SshClient,
5555
lxd_client: LxdClient,
5656
ansible_client: AnsibleClient,
@@ -111,6 +111,10 @@ impl TestEnvironment {
111111
let provision_renderer =
112112
ProvisionTemplateRenderer::new(project_root.join("build"), verbose);
113113

114+
// Create configuration template renderer
115+
let configuration_renderer =
116+
ConfigurationTemplateRenderer::new(project_root.join("build"), verbose);
117+
114118
if verbose {
115119
println!(
116120
"🔑 SSH key copied to temporary location: {}",
@@ -132,6 +136,7 @@ impl TestEnvironment {
132136
template_manager,
133137
opentofu_client,
134138
provision_renderer,
139+
configuration_renderer,
135140
ssh_client,
136141
lxd_client,
137142
ansible_client,
@@ -150,26 +155,7 @@ impl TestEnvironment {
150155

151156
/// Stage 3: Render configuration templates (`Ansible`) with runtime variables to build/ansible/
152157
async fn render_configuration_templates(&self, instance_ip: &str) -> Result<()> {
153-
println!("🎭 Stage 3: Rendering configuration templates with variables...");
154-
155-
// Create build directory structure
156-
let build_ansible_dir = self.build_dir.join("ansible");
157-
tokio::fs::create_dir_all(&build_ansible_dir)
158-
.await
159-
.context("Failed to create build/ansible directory")?;
160-
161-
// Render inventory.yml.tera with runtime variables
162-
let inventory_template_path = self
163-
.template_manager
164-
.get_template_path("ansible/inventory.yml.tera")?;
165-
let inventory_output_path = build_ansible_dir.join("inventory.yml");
166-
167-
let inventory_template_content = std::fs::read_to_string(&inventory_template_path)
168-
.context("Failed to read inventory template file")?;
169-
170-
let inventory_template_file = File::new("inventory.yml.tera", inventory_template_content)
171-
.context("Failed to create inventory template file")?;
172-
158+
// Create inventory context with runtime variables
173159
let inventory_context = {
174160
let host = AnsibleHost::from_str(instance_ip).context("Failed to parse instance IP")?;
175161
let ssh_key = SshPrivateKeyFile::new(self.ssh_key_path.to_string_lossy().as_ref())
@@ -181,54 +167,12 @@ impl TestEnvironment {
181167
.build()
182168
.context("Failed to create InventoryContext")?
183169
};
184-
let inventory_template =
185-
InventoryTemplate::new(&inventory_template_file, inventory_context)
186-
.context("Failed to create InventoryTemplate")?;
187-
188-
inventory_template
189-
.render(&inventory_output_path)
190-
.context("Failed to render inventory template")?;
191-
192-
// Copy static ansible files
193-
// Copy ansible.cfg
194-
let source_cfg = self
195-
.template_manager
196-
.get_template_path("ansible/ansible.cfg")?;
197-
let dest_cfg = build_ansible_dir.join("ansible.cfg");
198-
tokio::fs::copy(&source_cfg, &dest_cfg)
199-
.await
200-
.context("Failed to copy ansible.cfg to build directory")?;
201-
202-
// Copy playbooks
203-
for playbook in &[
204-
"update-apt-cache.yml",
205-
"install-docker.yml",
206-
"install-docker-compose.yml",
207-
"wait-cloud-init.yml",
208-
] {
209-
let source_playbook = self
210-
.template_manager
211-
.get_template_path(&format!("ansible/{playbook}"))?;
212-
let dest_playbook = build_ansible_dir.join(playbook);
213-
tokio::fs::copy(&source_playbook, &dest_playbook)
214-
.await
215-
.with_context(|| format!("Failed to copy {playbook} to build directory"))?;
216-
}
217170

218-
if self.verbose {
219-
println!(
220-
" ✅ Configuration templates rendered to: {}",
221-
build_ansible_dir.display()
222-
);
223-
println!(" ✅ Inventory rendered with IP: {instance_ip}");
224-
println!(
225-
" ✅ Inventory rendered with SSH key: {}",
226-
self.ssh_key_path.display()
227-
);
228-
}
229-
230-
println!("✅ Stage 3 complete: Configuration templates ready");
231-
Ok(())
171+
// Use the configuration renderer to handle all template rendering
172+
self.configuration_renderer
173+
.render(&self.template_manager, &inventory_context)
174+
.await
175+
.map_err(|e| anyhow::anyhow!(e))
232176
}
233177

234178
fn run_ansible_playbook(&self, playbook: &str) -> Result<()> {

src/stages/configuration/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod template_renderer;
2+
3+
pub use template_renderer::ConfigurationTemplateRenderer;

0 commit comments

Comments
 (0)