Skip to content

Commit 7b947f7

Browse files
committed
refactor: replace println macros with tracing function calls
Replace all println statements with appropriate tracing macros: - Use info for general operational information - Use warn for warnings (cleanup failures, test unavailability) - Use error for fatal errors (deployment failures, test failures) This improves logging structure by providing proper log levels, filtering capability, and better integration with the tracing crate. Files modified: - src/command_wrappers/ansible.rs: 1 println to tracing::warn - src/command_wrappers/opentofu/client.rs: 2 println to tracing::warn - src/bin/e2e_tests.rs: ~25 println to info/warn/error
1 parent bc5593a commit 7b947f7

File tree

3 files changed

+50
-51
lines changed

3 files changed

+50
-51
lines changed

src/bin/e2e_tests.rs

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::net::IpAddr;
44
use std::sync::Arc;
55
use std::time::Instant;
66
use tempfile::TempDir;
7+
use tracing::{error, info, warn};
78
use tracing_subscriber::fmt;
89

910
// Import command execution system
@@ -70,8 +71,8 @@ impl TestEnvironment {
7071
// Clean and prepare templates directory
7172
Self::clean_and_prepare_templates(&services)?;
7273

73-
println!("📁 Temporary directory: {}", temp_dir.path().display());
74-
println!(
74+
info!("📁 Temporary directory: {}", temp_dir.path().display());
75+
info!(
7576
"📄 Templates directory: {}",
7677
services.template_manager.templates_dir().display()
7778
);
@@ -105,7 +106,7 @@ impl TestEnvironment {
105106
std::fs::set_permissions(&temp_ssh_key, perms)?;
106107
}
107108

108-
println!(
109+
info!(
109110
"🔑 SSH key copied to temporary location: {}",
110111
ssh_key_path.display()
111112
);
@@ -116,7 +117,7 @@ impl TestEnvironment {
116117
/// Clean and prepare templates directory to ensure fresh embedded templates
117118
fn clean_and_prepare_templates(services: &Services) -> Result<()> {
118119
// Clean templates directory to ensure we use fresh templates from embedded resources
119-
println!("🧹 Cleaning templates directory to ensure fresh embedded templates...");
120+
info!("🧹 Cleaning templates directory to ensure fresh embedded templates...");
120121
services
121122
.template_manager
122123
.reset_templates_dir()
@@ -163,30 +164,30 @@ impl TestEnvironment {
163164
}
164165

165166
fn run_ansible_playbook(&self, playbook: &str) -> Result<()> {
166-
println!("🎭 Stage 4: Running Ansible playbook: {playbook}");
167+
info!("🎭 Stage 4: Running Ansible playbook: {playbook}");
167168

168169
self.services
169170
.ansible_client
170171
.run_playbook(playbook)
171172
.context(format!("Failed to run Ansible playbook: {playbook}"))?;
172173

173-
println!("✅ Stage 4: Ansible playbook executed successfully");
174+
info!("✅ Stage 4: Ansible playbook executed successfully");
174175
Ok(())
175176
}
176177

177178
fn provision_infrastructure(&self) -> Result<IpAddr> {
178-
println!("🚀 Stage 2: Provisioning test infrastructure...");
179+
info!("🚀 Stage 2: Provisioning test infrastructure...");
179180

180181
// Initialize OpenTofu
181-
println!(" Initializing OpenTofu...");
182+
info!(" Initializing OpenTofu...");
182183
self.services
183184
.opentofu_client
184185
.init()
185186
.map_err(anyhow::Error::from)
186187
.context("Failed to initialize OpenTofu")?;
187188

188189
// Apply infrastructure
189-
println!(" Applying infrastructure...");
190+
info!(" Applying infrastructure...");
190191
self.services
191192
.opentofu_client
192193
.apply(true) // auto_approve = true
@@ -214,9 +215,9 @@ impl TestEnvironment {
214215
.get_instance_ip()
215216
.context("Failed to get instance IP from LXD client")?;
216217

217-
println!("✅ Stage 2 complete: Infrastructure provisioned");
218-
println!(" Instance IP from OpenTofu: {opentofu_instance_ip}");
219-
println!(" Instance IP from LXD client: {lxd_instance_ip}");
218+
info!("✅ Stage 2 complete: Infrastructure provisioned");
219+
info!(" Instance IP from OpenTofu: {opentofu_instance_ip}");
220+
info!(" Instance IP from LXD client: {lxd_instance_ip}");
220221

221222
// Return the IP from OpenTofu as it's our preferred source
222223
Ok(opentofu_instance_ip)
@@ -244,13 +245,13 @@ impl TestEnvironment {
244245

245246
fn cleanup(&self) {
246247
if self.config.keep_env {
247-
println!("🔒 Keeping test environment as requested");
248-
println!(" Instance: torrust-vm");
249-
println!(" Connect with: lxc exec torrust-vm -- /bin/bash");
248+
info!("🔒 Keeping test environment as requested");
249+
info!(" Instance: torrust-vm");
250+
info!(" Connect with: lxc exec torrust-vm -- /bin/bash");
250251
return;
251252
}
252253

253-
println!("🧹 Cleaning up test environment...");
254+
info!("🧹 Cleaning up test environment...");
254255

255256
// Destroy infrastructure using OpenTofuClient
256257
let result = self
@@ -260,8 +261,8 @@ impl TestEnvironment {
260261
.map_err(anyhow::Error::from);
261262

262263
match result {
263-
Ok(_) => println!("✅ Test environment cleaned up successfully"),
264-
Err(e) => println!("⚠️ Warning: Cleanup failed: {e}"),
264+
Ok(_) => info!("✅ Test environment cleaned up successfully"),
265+
Err(e) => warn!("⚠️ Warning: Cleanup failed: {e}"),
265266
}
266267
}
267268
}
@@ -279,10 +280,10 @@ impl Drop for TestEnvironment {
279280
}
280281

281282
async fn validate_deployment(env: &TestEnvironment, instance_ip: &IpAddr) -> Result<()> {
282-
println!("🔍 Starting deployment validation...");
283+
info!("🔍 Starting deployment validation...");
283284

284285
// Validate cloud-init completion
285-
println!(" Validating cloud-init completion...");
286+
info!(" Validating cloud-init completion...");
286287
let cloud_init_validator = CloudInitValidator::new(
287288
&env.config.ssh_config.ssh_key_path,
288289
&env.config.ssh_config.ssh_username,
@@ -294,7 +295,7 @@ async fn validate_deployment(env: &TestEnvironment, instance_ip: &IpAddr) -> Res
294295
.map_err(|e| anyhow::anyhow!(e))?;
295296

296297
// Validate Docker installation
297-
println!(" Validating Docker installation...");
298+
info!(" Validating Docker installation...");
298299
let docker_validator = DockerValidator::new(
299300
&env.config.ssh_config.ssh_key_path,
300301
&env.config.ssh_config.ssh_username,
@@ -306,7 +307,7 @@ async fn validate_deployment(env: &TestEnvironment, instance_ip: &IpAddr) -> Res
306307
.map_err(|e| anyhow::anyhow!(e))?;
307308

308309
// Validate Docker Compose installation
309-
println!(" Validating Docker Compose installation...");
310+
info!(" Validating Docker Compose installation...");
310311
let docker_compose_validator = DockerComposeValidator::new(
311312
&env.config.ssh_config.ssh_key_path,
312313
&env.config.ssh_config.ssh_username,
@@ -317,18 +318,18 @@ async fn validate_deployment(env: &TestEnvironment, instance_ip: &IpAddr) -> Res
317318
.await
318319
.map_err(|e| anyhow::anyhow!(e))?;
319320

320-
println!("✅ All deployment validations passed!");
321+
info!("✅ All deployment validations passed!");
321322
Ok(())
322323
}
323324

324325
async fn run_full_deployment_test(env: &TestEnvironment) -> Result<IpAddr> {
325-
println!("🧪 Starting full deployment E2E test with template-based workflow");
326-
println!(" This will test the complete 4-stage template system:");
327-
println!(" Stage 1: Render provision templates to build/");
328-
println!(" Stage 2: Provision VM with OpenTofu from build/");
329-
println!(" Stage 3: Render configuration templates with variables");
330-
println!(" Stage 4: Run Ansible playbooks from build/");
331-
println!();
326+
info!("🧪 Starting full deployment E2E test with template-based workflow");
327+
info!(" This will test the complete 4-stage template system:");
328+
info!(" Stage 1: Render provision templates to build/");
329+
info!(" Stage 2: Provision VM with OpenTofu from build/");
330+
info!(" Stage 3: Render configuration templates with variables");
331+
info!(" Stage 4: Run Ansible playbooks from build/");
332+
info!("");
332333

333334
// Stage 1: Render provision templates to build/tofu/
334335
env.render_provision_templates().await?;
@@ -351,28 +352,26 @@ async fn run_full_deployment_test(env: &TestEnvironment) -> Result<IpAddr> {
351352
env.render_configuration_templates(&instance_ip).await?;
352353

353354
// Stage 4: Run Ansible playbooks from build directory
354-
println!("📋 Step 1: Waiting for cloud-init completion...");
355+
info!("📋 Step 1: Waiting for cloud-init completion...");
355356
env.run_ansible_playbook("wait-cloud-init")?;
356357

357358
// Run the install-docker playbook
358359
// NOTE: We skip the update-apt-cache playbook in E2E tests to avoid CI network issues
359360
// The install-docker playbook now assumes the cache is already updated or will handle stale cache gracefully
360-
println!("📋 Step 2: Installing Docker...");
361+
info!("📋 Step 2: Installing Docker...");
361362
env.run_ansible_playbook("install-docker")?;
362363

363364
// Run the install-docker-compose playbook
364-
println!("📋 Step 3: Installing Docker Compose...");
365+
info!("📋 Step 3: Installing Docker Compose...");
365366
env.run_ansible_playbook("install-docker-compose")?;
366367

367-
println!("✅ Deployment stages completed successfully!");
368-
println!(" ✅ Infrastructure provisioned with OpenTofu");
369-
println!(" ✅ Configuration rendered with Ansible templates");
370-
println!(" ✅ Ansible playbooks executed successfully");
368+
info!("✅ Deployment stages completed successfully!");
369+
info!(" ✅ Infrastructure provisioned with OpenTofu");
370+
info!(" ✅ Configuration rendered with Ansible templates");
371+
info!(" ✅ Ansible playbooks executed successfully");
371372

372-
println!("🎉 Full deployment E2E test completed successfully!");
373-
println!(
374-
" ℹ️ Docker/Docker Compose installation status varies based on network connectivity"
375-
);
373+
info!("🎉 Full deployment E2E test completed successfully!");
374+
info!(" ℹ️ Docker/Docker Compose installation status varies based on network connectivity");
376375

377376
// Return the instance IP for validation in main
378377
Ok(instance_ip)
@@ -385,8 +384,8 @@ async fn main() -> Result<()> {
385384

386385
let cli = Cli::parse();
387386

388-
println!("🚀 Torrust Tracker Deploy E2E Tests");
389-
println!("===========================================");
387+
info!("🚀 Torrust Tracker Deploy E2E Tests");
388+
info!("===========================================");
390389

391390
let env = TestEnvironment::new(cli.keep, cli.templates_dir)?;
392391

@@ -397,28 +396,28 @@ async fn main() -> Result<()> {
397396
// Handle deployment results and run validation if deployment succeeded
398397
let validation_result = match result {
399398
Ok(instance_ip) => {
400-
println!();
399+
info!("");
401400
validate_deployment(&env, &instance_ip).await
402401
}
403402
Err(deployment_err) => {
404-
println!("❌ Deployment failed: {deployment_err}");
403+
error!("❌ Deployment failed: {deployment_err}");
405404
Err(deployment_err)
406405
}
407406
};
408407

409408
env.cleanup();
410409

411410
let test_duration = test_start.elapsed();
412-
println!("\n📊 Test execution time: {test_duration:?}");
411+
info!("📊 Test execution time: {test_duration:?}");
413412

414413
// Handle final results
415414
match validation_result {
416415
Ok(()) => {
417-
println!("✅ All tests passed and cleanup completed successfully");
416+
info!("✅ All tests passed and cleanup completed successfully");
418417
Ok(())
419418
}
420419
Err(test_err) => {
421-
println!("❌ Test failed: {test_err}");
420+
error!("❌ Test failed: {test_err}");
422421
Err(test_err)
423422
}
424423
}

src/command_wrappers/ansible.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ mod tests {
161161
}
162162
Err(_) => {
163163
// Expected if Ansible is not installed
164-
println!("Ansible not available for testing");
164+
tracing::warn!("Ansible not available for testing");
165165
}
166166
}
167167
}

src/command_wrappers/opentofu/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ resource "null_resource" "test" {
277277
}
278278
Err(_) => {
279279
// Expected if OpenTofu is not installed
280-
println!("OpenTofu not available for testing");
280+
tracing::warn!("OpenTofu not available for testing");
281281
}
282282
}
283283
}
@@ -298,7 +298,7 @@ resource "null_resource" "test" {
298298
}
299299
Err(_) => {
300300
// Expected if OpenTofu is not installed
301-
println!("OpenTofu not available for testing");
301+
tracing::warn!("OpenTofu not available for testing");
302302
}
303303
}
304304
}

0 commit comments

Comments
 (0)