Skip to content

Commit 3f42f48

Browse files
committed
fix: resolve all clippy warnings and errors
- Fix uninlined_format_args warnings by using direct variable interpolation in format strings - Fix needless_borrows_for_generic_args warnings by removing unnecessary borrows from array arguments - Remove async keywords from functions that don't use await statements - Remove all unnecessary .await calls after making functions synchronous - Fix unnecessary_wraps warning by making cleanup() return void instead of Result<()> - All clippy checks now pass with strict linting rules enabled
1 parent 28efdd3 commit 3f42f48

File tree

1 file changed

+32
-52
lines changed

1 file changed

+32
-52
lines changed

src/bin/e2e_tests.rs

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,7 @@ impl TestEnvironment {
8383
})
8484
}
8585

86-
async fn run_command(
87-
&self,
88-
cmd: &str,
89-
args: &[&str],
90-
working_dir: Option<&Path>,
91-
) -> Result<String> {
86+
fn run_command(&self, cmd: &str, args: &[&str], working_dir: Option<&Path>) -> Result<String> {
9287
let mut command = Command::new(cmd);
9388
command.args(args);
9489

@@ -124,7 +119,7 @@ impl TestEnvironment {
124119
Ok(String::from_utf8_lossy(&output.stdout).to_string())
125120
}
126121

127-
async fn provision_infrastructure(&self) -> Result<String> {
122+
fn provision_infrastructure(&self) -> Result<String> {
128123
println!("🚀 Provisioning test infrastructure...");
129124

130125
// First, we need to update the container name in the OpenTofu config
@@ -136,32 +131,28 @@ impl TestEnvironment {
136131
// Initialize OpenTofu
137132
println!(" Initializing OpenTofu...");
138133
self.run_command("tofu", &["init"], Some(&tofu_dir))
139-
.await
140134
.context("Failed to initialize OpenTofu")?;
141135

142136
// Apply infrastructure
143137
println!(" Applying infrastructure...");
144138
self.run_command("tofu", &["apply", "-auto-approve"], Some(&tofu_dir))
145-
.await
146139
.context("Failed to apply OpenTofu configuration")?;
147140

148141
// Get the container IP
149142
let container_ip = self
150143
.get_container_ip()
151-
.await
152144
.context("Failed to get container IP after provisioning")?;
153145

154146
println!("✅ Infrastructure provisioned successfully");
155-
println!(" Container IP: {}", container_ip);
147+
println!(" Container IP: {container_ip}");
156148

157149
Ok(container_ip)
158150
}
159151

160-
async fn get_container_ip(&self) -> Result<String> {
152+
fn get_container_ip(&self) -> Result<String> {
161153
// Get container information
162154
let output = self
163155
.run_command("lxc", &["list", "torrust-vm", "--format=json"], None)
164-
.await
165156
.context("Failed to list LXC containers")?;
166157

167158
let containers: Value =
@@ -193,7 +184,7 @@ impl TestEnvironment {
193184

194185
while attempt < max_attempts {
195186
let result = Command::new("ssh")
196-
.args(&[
187+
.args([
197188
"-i",
198189
self.ssh_key_path.to_str().unwrap(),
199190
"-o",
@@ -202,7 +193,7 @@ impl TestEnvironment {
202193
"UserKnownHostsFile=/dev/null",
203194
"-o",
204195
"ConnectTimeout=5",
205-
&format!("torrust@{}", ip),
196+
&format!("torrust@{ip}"),
206197
"echo 'SSH connected'",
207198
])
208199
.stdout(Stdio::null())
@@ -246,10 +237,8 @@ impl TestEnvironment {
246237
let ip_regex =
247238
Regex::new(r"ansible_host: \d+\.\d+\.\d+\.\d+").context("Failed to create IP regex")?;
248239

249-
let updated_content = ip_regex.replace(
250-
&inventory_content,
251-
&format!("ansible_host: {}", container_ip),
252-
);
240+
let updated_content =
241+
ip_regex.replace(&inventory_content, &format!("ansible_host: {container_ip}"));
253242

254243
// Replace the SSH key path to use our temporary key
255244
let ssh_key_regex = Regex::new(r"ansible_ssh_private_key_file: [^\n]+")
@@ -267,46 +256,45 @@ impl TestEnvironment {
267256
.await
268257
.context("Failed to write updated inventory file")?;
269258

270-
println!("✅ Ansible inventory updated with IP: {}", container_ip);
259+
println!("✅ Ansible inventory updated with IP: {container_ip}");
271260
println!(
272261
"✅ Ansible inventory updated with SSH key: {}",
273262
self.ssh_key_path.display()
274263
);
275264
Ok(())
276265
}
277266

278-
async fn run_ansible_playbook(&self, playbook: &str) -> Result<()> {
279-
println!("🎭 Running Ansible playbook: {}", playbook);
267+
fn run_ansible_playbook(&self, playbook: &str) -> Result<()> {
268+
println!("🎭 Running Ansible playbook: {playbook}");
280269

281270
let ansible_dir = self.project_root.join("config/ansible");
282-
let playbook_path = format!("{}.yml", playbook);
271+
let playbook_path = format!("{playbook}.yml");
283272

284273
let mut args = vec!["ansible-playbook", &playbook_path];
285274
if self.verbose {
286275
args.push("-vvv");
287276
}
288277

289278
self.run_command("ansible-playbook", &[&playbook_path], Some(&ansible_dir))
290-
.await
291-
.context(format!("Failed to run Ansible playbook: {}", playbook))?;
279+
.context(format!("Failed to run Ansible playbook: {playbook}"))?;
292280

293281
println!("✅ Ansible playbook executed successfully");
294282
Ok(())
295283
}
296284

297-
async fn validate_cloud_init_completion(&self, container_ip: &str) -> Result<()> {
285+
fn validate_cloud_init_completion(&self, container_ip: &str) -> Result<()> {
298286
println!("🔍 Validating cloud-init completion...");
299287

300288
// Check cloud-init status
301289
let output = Command::new("ssh")
302-
.args(&[
290+
.args([
303291
"-i",
304292
self.ssh_key_path.to_str().unwrap(),
305293
"-o",
306294
"StrictHostKeyChecking=no",
307295
"-o",
308296
"UserKnownHostsFile=/dev/null",
309-
&format!("torrust@{}", container_ip),
297+
&format!("torrust@{container_ip}"),
310298
"cloud-init status",
311299
])
312300
.output()
@@ -326,14 +314,14 @@ impl TestEnvironment {
326314

327315
// Check for completion marker file
328316
let marker_check = Command::new("ssh")
329-
.args(&[
317+
.args([
330318
"-i",
331319
self.ssh_key_path.to_str().unwrap(),
332320
"-o",
333321
"StrictHostKeyChecking=no",
334322
"-o",
335323
"UserKnownHostsFile=/dev/null",
336-
&format!("torrust@{}", container_ip),
324+
&format!("torrust@{container_ip}"),
337325
"test -f /var/lib/cloud/instance/boot-finished",
338326
])
339327
.status()
@@ -349,29 +337,25 @@ impl TestEnvironment {
349337
Ok(())
350338
}
351339

352-
async fn cleanup(&self) -> Result<()> {
340+
fn cleanup(&self) {
353341
if self.keep_env {
354342
println!("🔒 Keeping test environment as requested");
355343
println!(" Container: torrust-vm");
356344
println!(" Connect with: lxc exec torrust-vm -- /bin/bash");
357-
return Ok(());
345+
return;
358346
}
359347

360348
println!("🧹 Cleaning up test environment...");
361349

362350
let tofu_dir = self.project_root.join("config/tofu/lxd");
363351

364352
// Destroy infrastructure
365-
let result = self
366-
.run_command("tofu", &["destroy", "-auto-approve"], Some(&tofu_dir))
367-
.await;
353+
let result = self.run_command("tofu", &["destroy", "-auto-approve"], Some(&tofu_dir));
368354

369355
match result {
370356
Ok(_) => println!("✅ Test environment cleaned up successfully"),
371-
Err(e) => println!("⚠️ Warning: Cleanup failed: {}", e),
357+
Err(e) => println!("⚠️ Warning: Cleanup failed: {e}"),
372358
}
373-
374-
Ok(())
375359
}
376360
}
377361

@@ -381,7 +365,7 @@ impl Drop for TestEnvironment {
381365
// Try basic cleanup in case async cleanup failed
382366
let tofu_dir = self.project_root.join("config/tofu/lxd");
383367
let _ = Command::new("tofu")
384-
.args(&["destroy", "-auto-approve"])
368+
.args(["destroy", "-auto-approve"])
385369
.current_dir(&tofu_dir)
386370
.output();
387371
}
@@ -392,7 +376,7 @@ async fn test_wait_cloud_init(env: &TestEnvironment) -> Result<()> {
392376
println!("🧪 Starting wait-cloud-init E2E test");
393377

394378
// 1. Provision infrastructure
395-
let container_ip = env.provision_infrastructure().await?;
379+
let container_ip = env.provision_infrastructure()?;
396380

397381
// 2. Wait for SSH connectivity
398382
env.wait_for_ssh_connectivity(&container_ip).await?;
@@ -401,10 +385,10 @@ async fn test_wait_cloud_init(env: &TestEnvironment) -> Result<()> {
401385
env.update_ansible_inventory(&container_ip).await?;
402386

403387
// 4. Run the wait-cloud-init playbook
404-
env.run_ansible_playbook("wait-cloud-init").await?;
388+
env.run_ansible_playbook("wait-cloud-init")?;
405389

406390
// 5. Validate cloud-init completion
407-
env.validate_cloud_init_completion(&container_ip).await?;
391+
env.validate_cloud_init_completion(&container_ip)?;
408392

409393
println!("🎉 wait-cloud-init E2E test completed successfully!");
410394
Ok(())
@@ -425,23 +409,19 @@ async fn main() -> Result<()> {
425409
TestType::WaitCloudInit => test_wait_cloud_init(&env).await,
426410
};
427411

428-
let cleanup_result = env.cleanup().await;
412+
env.cleanup();
429413

430414
let test_duration = test_start.elapsed();
431-
println!("\n📊 Test execution time: {:?}", test_duration);
415+
println!("\n📊 Test execution time: {test_duration:?}");
432416

433417
// Handle results
434-
match (result, cleanup_result) {
435-
(Ok(()), Ok(())) => {
418+
match result {
419+
Ok(()) => {
436420
println!("✅ All tests passed and cleanup completed successfully");
437421
Ok(())
438422
}
439-
(Ok(()), Err(cleanup_err)) => {
440-
println!("✅ Tests passed but cleanup failed: {}", cleanup_err);
441-
Ok(()) // Don't fail the test due to cleanup issues
442-
}
443-
(Err(test_err), _) => {
444-
println!("❌ Test failed: {}", test_err);
423+
Err(test_err) => {
424+
println!("❌ Test failed: {test_err}");
445425
Err(test_err)
446426
}
447427
}

0 commit comments

Comments
 (0)