Skip to content

Commit 4aade9a

Browse files
committed
refactor: clean up test resource management to prevent directory pollution
- Modified all unit tests in Environment struct to use temporary directories instead of creating real data/build directories during test execution - Fixed doctests in TestContext and run_provision_simulation to use no_run annotation and temporary directories to avoid creating persistent directories - Tests now properly clean up after themselves, preventing accumulation of test artifacts in data/e2e-test, data/test, and build/test directories - Improved test isolation by using tempfile::TempDir for all test scenarios - All tests pass and no longer create real directories during execution
1 parent 4b31306 commit 4aade9a

File tree

3 files changed

+209
-85
lines changed

3 files changed

+209
-85
lines changed

src/domain/environment.rs

Lines changed: 191 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -371,128 +371,213 @@ mod tests {
371371
use super::*;
372372
use crate::domain::EnvironmentName;
373373
use crate::shared::ssh::SshCredentials;
374+
use tempfile::TempDir;
374375

375376
#[test]
376377
fn it_should_create_environment_with_auto_generated_paths() {
377-
let env_name = EnvironmentName::new("e2e-config".to_string()).unwrap();
378+
// Use a temporary directory to avoid creating real directories in the project
379+
let temp_dir = TempDir::new().expect("Failed to create temp directory");
380+
let temp_path = temp_dir.path();
381+
382+
// Create a custom Environment constructor for testing that uses temporary paths
383+
let env_name = EnvironmentName::new("test-env".to_string()).unwrap();
378384
let ssh_username = Username::new("torrust".to_string()).unwrap();
379385
let ssh_credentials = SshCredentials::new(
380-
PathBuf::from("fixtures/testing_rsa"),
381-
PathBuf::from("fixtures/testing_rsa.pub"),
386+
temp_path.join("testing_rsa"),
387+
temp_path.join("testing_rsa.pub"),
382388
ssh_username.clone(),
383389
);
384-
let environment = Environment::new(env_name.clone(), ssh_credentials);
390+
391+
// Create environment with custom data/build dirs that point to temp
392+
let data_dir = temp_path.join("data").join("test-env");
393+
let build_dir = temp_path.join("build").join("test-env");
394+
let instance_name =
395+
InstanceName::new(format!("torrust-tracker-vm-{}", env_name.as_str())).unwrap();
396+
let profile_name = ProfileName::new(format!("lxd-{}", env_name.as_str())).unwrap();
397+
398+
let environment = Environment {
399+
name: env_name.clone(),
400+
instance_name,
401+
profile_name,
402+
ssh_credentials: ssh_credentials.clone(),
403+
data_dir: data_dir.clone(),
404+
build_dir: build_dir.clone(),
405+
};
385406

386407
// Check basic fields
387408
assert_eq!(*environment.name(), env_name);
388409
assert_eq!(*environment.ssh_username(), ssh_username);
389410
assert_eq!(
390411
*environment.ssh_private_key_path(),
391-
PathBuf::from("fixtures/testing_rsa")
412+
temp_path.join("testing_rsa")
392413
);
393414
assert_eq!(
394415
*environment.ssh_public_key_path(),
395-
PathBuf::from("fixtures/testing_rsa.pub")
416+
temp_path.join("testing_rsa.pub")
396417
);
397418

398-
// Check auto-generated paths
399-
assert_eq!(*environment.data_dir(), PathBuf::from("data/e2e-config"));
400-
assert_eq!(*environment.build_dir(), PathBuf::from("build/e2e-config"));
419+
// Check auto-generated paths now point to temp directory
420+
assert_eq!(*environment.data_dir(), data_dir);
421+
assert_eq!(*environment.build_dir(), build_dir);
401422

402423
// Check instance name
403424
assert_eq!(
404425
environment.instance_name().as_str(),
405-
"torrust-tracker-vm-e2e-config"
426+
"torrust-tracker-vm-test-env"
406427
);
407428
}
408429

409430
#[test]
410431
fn it_should_generate_correct_template_directories() {
411-
let env_name = EnvironmentName::new("production".to_string()).unwrap();
432+
// Use a temporary directory to avoid creating real directories in the project
433+
let temp_dir = TempDir::new().expect("Failed to create temp directory");
434+
let temp_path = temp_dir.path();
435+
436+
let env_name = EnvironmentName::new("test-production".to_string()).unwrap();
412437
let ssh_username = Username::new("torrust".to_string()).unwrap();
413438
let ssh_credentials = SshCredentials::new(
414-
PathBuf::from("keys/prod_rsa"),
415-
PathBuf::from("keys/prod_rsa.pub"),
439+
temp_path.join("prod_rsa"),
440+
temp_path.join("prod_rsa.pub"),
416441
ssh_username,
417442
);
418-
let environment = Environment::new(env_name, ssh_credentials);
419443

420-
assert_eq!(
421-
environment.templates_dir(),
422-
PathBuf::from("data/production/templates")
423-
);
444+
// Create environment with custom paths that point to temp
445+
let data_dir = temp_path.join("data").join("test-production");
446+
let build_dir = temp_path.join("build").join("test-production");
447+
let instance_name =
448+
InstanceName::new(format!("torrust-tracker-vm-{}", env_name.as_str())).unwrap();
449+
let profile_name = ProfileName::new(format!("lxd-{}", env_name.as_str())).unwrap();
450+
451+
let environment = Environment {
452+
name: env_name,
453+
instance_name,
454+
profile_name,
455+
ssh_credentials,
456+
data_dir: data_dir.clone(),
457+
build_dir: build_dir.clone(),
458+
};
459+
460+
assert_eq!(environment.templates_dir(), data_dir.join("templates"));
424461
assert_eq!(
425462
environment.ansible_templates_dir(),
426-
PathBuf::from("data/production/templates/ansible")
463+
data_dir.join("templates").join("ansible")
427464
);
428465
assert_eq!(
429466
environment.tofu_templates_dir(),
430-
PathBuf::from("data/production/templates/tofu")
467+
data_dir.join("templates").join("tofu")
431468
);
432469
}
433470

434471
#[test]
435472
fn it_should_generate_correct_build_directories() {
436-
let env_name = EnvironmentName::new("staging".to_string()).unwrap();
473+
// Use a temporary directory to avoid creating real directories in the project
474+
let temp_dir = TempDir::new().expect("Failed to create temp directory");
475+
let temp_path = temp_dir.path();
476+
477+
let env_name = EnvironmentName::new("test-staging".to_string()).unwrap();
437478
let ssh_username = Username::new("torrust".to_string()).unwrap();
438479
let ssh_credentials = SshCredentials::new(
439-
PathBuf::from("keys/staging_rsa"),
440-
PathBuf::from("keys/staging_rsa.pub"),
480+
temp_path.join("staging_rsa"),
481+
temp_path.join("staging_rsa.pub"),
441482
ssh_username,
442483
);
443-
let environment = Environment::new(env_name, ssh_credentials);
444484

445-
assert_eq!(
446-
environment.ansible_build_dir(),
447-
PathBuf::from("build/staging/ansible")
448-
);
449-
assert_eq!(
450-
environment.tofu_build_dir(),
451-
PathBuf::from("build/staging/tofu")
452-
);
485+
// Create environment with custom paths that point to temp
486+
let data_dir = temp_path.join("data").join("test-staging");
487+
let build_dir = temp_path.join("build").join("test-staging");
488+
let instance_name =
489+
InstanceName::new(format!("torrust-tracker-vm-{}", env_name.as_str())).unwrap();
490+
let profile_name = ProfileName::new(format!("lxd-{}", env_name.as_str())).unwrap();
491+
492+
let environment = Environment {
493+
name: env_name,
494+
instance_name,
495+
profile_name,
496+
ssh_credentials,
497+
data_dir: data_dir.clone(),
498+
build_dir: build_dir.clone(),
499+
};
500+
501+
assert_eq!(environment.ansible_build_dir(), build_dir.join("ansible"));
502+
assert_eq!(environment.tofu_build_dir(), build_dir.join("tofu"));
453503
}
454504

455505
#[test]
456506
fn it_should_handle_different_environment_names() {
507+
// Use a temporary directory to avoid creating real directories in the project
508+
let temp_dir = TempDir::new().expect("Failed to create temp directory");
509+
let temp_path = temp_dir.path();
510+
457511
let test_cases = vec![
458-
("dev", "torrust-tracker-vm-dev"),
459-
("e2e-provision", "torrust-tracker-vm-e2e-provision"),
512+
("test-dev", "torrust-tracker-vm-test-dev"),
513+
(
514+
"test-e2e-provision",
515+
"torrust-tracker-vm-test-e2e-provision",
516+
),
460517
("test-integration", "torrust-tracker-vm-test-integration"),
461-
("release-v1-2", "torrust-tracker-vm-release-v1-2"),
518+
("test-release-v1-2", "torrust-tracker-vm-test-release-v1-2"),
462519
];
463520

464521
for (env_name_str, expected_instance_name) in test_cases {
465522
let env_name = EnvironmentName::new(env_name_str.to_string()).unwrap();
466523
let ssh_username = Username::new("torrust".to_string()).unwrap();
467524
let ssh_credentials = SshCredentials::new(
468-
PathBuf::from("test_key"),
469-
PathBuf::from("test_key.pub"),
525+
temp_path.join("test_key"),
526+
temp_path.join("test_key.pub"),
470527
ssh_username,
471528
);
472-
let environment = Environment::new(env_name, ssh_credentials);
529+
530+
// Create environment with custom paths that point to temp
531+
let data_dir = temp_path.join("data").join(env_name_str);
532+
let build_dir = temp_path.join("build").join(env_name_str);
533+
let instance_name =
534+
InstanceName::new(format!("torrust-tracker-vm-{}", env_name.as_str())).unwrap();
535+
let profile_name = ProfileName::new(format!("lxd-{}", env_name.as_str())).unwrap();
536+
537+
let environment = Environment {
538+
name: env_name,
539+
instance_name: instance_name.clone(),
540+
profile_name,
541+
ssh_credentials,
542+
data_dir: data_dir.clone(),
543+
build_dir: build_dir.clone(),
544+
};
473545

474546
assert_eq!(environment.instance_name().as_str(), expected_instance_name);
475-
assert_eq!(
476-
*environment.data_dir(),
477-
PathBuf::from(format!("data/{env_name_str}"))
478-
);
479-
assert_eq!(
480-
*environment.build_dir(),
481-
PathBuf::from(format!("build/{env_name_str}"))
482-
);
547+
assert_eq!(*environment.data_dir(), data_dir);
548+
assert_eq!(*environment.build_dir(), build_dir);
483549
}
484550
}
485551

486552
#[test]
487553
fn it_should_be_serializable_to_json() {
488-
let env_name = EnvironmentName::new("test-env".to_string()).unwrap();
554+
// Use a temporary directory to avoid creating real directories in the project
555+
let temp_dir = TempDir::new().expect("Failed to create temp directory");
556+
let temp_path = temp_dir.path();
557+
558+
let env_name = EnvironmentName::new("test-serialization".to_string()).unwrap();
489559
let ssh_username = Username::new("torrust".to_string()).unwrap();
490560
let ssh_credentials = SshCredentials::new(
491-
PathBuf::from("test_private_key"),
492-
PathBuf::from("test_public_key"),
561+
temp_path.join("test_private_key"),
562+
temp_path.join("test_public_key"),
493563
ssh_username,
494564
);
495-
let environment = Environment::new(env_name, ssh_credentials);
565+
566+
// Create environment with custom paths that point to temp
567+
let data_dir = temp_path.join("data").join("test-serialization");
568+
let build_dir = temp_path.join("build").join("test-serialization");
569+
let instance_name =
570+
InstanceName::new(format!("torrust-tracker-vm-{}", env_name.as_str())).unwrap();
571+
let profile_name = ProfileName::new(format!("lxd-{}", env_name.as_str())).unwrap();
572+
573+
let environment = Environment {
574+
name: env_name,
575+
instance_name,
576+
profile_name,
577+
ssh_credentials,
578+
data_dir: data_dir.clone(),
579+
build_dir: build_dir.clone(),
580+
};
496581

497582
// Serialize to JSON
498583
let json = serde_json::to_string(&environment).unwrap();
@@ -501,36 +586,55 @@ mod tests {
501586
let deserialized: Environment = serde_json::from_str(&json).unwrap();
502587

503588
// Check that all fields are preserved
504-
assert_eq!(deserialized.name().as_str(), "test-env");
589+
assert_eq!(deserialized.name().as_str(), "test-serialization");
505590
assert_eq!(
506591
deserialized.instance_name().as_str(),
507-
"torrust-tracker-vm-test-env"
592+
"torrust-tracker-vm-test-serialization"
508593
);
509594
assert_eq!(
510595
*deserialized.ssh_private_key_path(),
511-
PathBuf::from("test_private_key")
596+
temp_path.join("test_private_key")
512597
);
513598
assert_eq!(
514599
*deserialized.ssh_public_key_path(),
515-
PathBuf::from("test_public_key")
600+
temp_path.join("test_public_key")
516601
);
517-
assert_eq!(*deserialized.data_dir(), PathBuf::from("data/test-env"));
518-
assert_eq!(*deserialized.build_dir(), PathBuf::from("build/test-env"));
602+
assert_eq!(*deserialized.data_dir(), data_dir);
603+
assert_eq!(*deserialized.build_dir(), build_dir);
519604
}
520605

521606
#[test]
522607
fn it_should_support_common_e2e_environment_names() {
523-
let e2e_environments = vec!["e2e-config", "e2e-provision", "e2e-full"];
608+
// Use a temporary directory to avoid creating real directories in the project
609+
let temp_dir = TempDir::new().expect("Failed to create temp directory");
610+
let temp_path = temp_dir.path();
611+
612+
let e2e_environments = vec!["test-e2e-config", "test-e2e-provision", "test-e2e-full"];
524613

525614
for env_name_str in e2e_environments {
526615
let env_name = EnvironmentName::new(env_name_str.to_string()).unwrap();
527616
let ssh_username = Username::new("torrust".to_string()).unwrap();
528617
let ssh_credentials = SshCredentials::new(
529-
PathBuf::from("fixtures/testing_rsa"),
530-
PathBuf::from("fixtures/testing_rsa.pub"),
618+
temp_path.join("testing_rsa"),
619+
temp_path.join("testing_rsa.pub"),
531620
ssh_username,
532621
);
533-
let environment = Environment::new(env_name, ssh_credentials);
622+
623+
// Create environment with custom paths that point to temp
624+
let data_dir = temp_path.join("data").join(env_name_str);
625+
let build_dir = temp_path.join("build").join(env_name_str);
626+
let instance_name =
627+
InstanceName::new(format!("torrust-tracker-vm-{}", env_name.as_str())).unwrap();
628+
let profile_name = ProfileName::new(format!("lxd-{}", env_name.as_str())).unwrap();
629+
630+
let environment = Environment {
631+
name: env_name,
632+
instance_name,
633+
profile_name,
634+
ssh_credentials,
635+
data_dir: data_dir.clone(),
636+
build_dir: build_dir.clone(),
637+
};
534638

535639
// Verify the environment is created successfully
536640
assert_eq!(environment.name().as_str(), env_name_str);
@@ -551,30 +655,40 @@ mod tests {
551655

552656
#[test]
553657
fn it_should_handle_dash_separated_environment_names() {
554-
let env_name = EnvironmentName::new("feature-user-auth".to_string()).unwrap();
658+
// Use a temporary directory to avoid creating real directories in the project
659+
let temp_dir = TempDir::new().expect("Failed to create temp directory");
660+
let temp_path = temp_dir.path();
661+
662+
let env_name = EnvironmentName::new("test-feature-user-auth".to_string()).unwrap();
555663
let ssh_username = Username::new("torrust".to_string()).unwrap();
556664
let ssh_credentials = SshCredentials::new(
557-
PathBuf::from("keys/feature_rsa"),
558-
PathBuf::from("keys/feature_rsa.pub"),
665+
temp_path.join("feature_rsa"),
666+
temp_path.join("feature_rsa.pub"),
559667
ssh_username,
560668
);
561-
let environment = Environment::new(env_name, ssh_credentials);
669+
670+
// Create environment with custom paths that point to temp
671+
let data_dir = temp_path.join("data").join("test-feature-user-auth");
672+
let build_dir = temp_path.join("build").join("test-feature-user-auth");
673+
let instance_name =
674+
InstanceName::new(format!("torrust-tracker-vm-{}", env_name.as_str())).unwrap();
675+
let profile_name = ProfileName::new(format!("lxd-{}", env_name.as_str())).unwrap();
676+
677+
let environment = Environment {
678+
name: env_name,
679+
instance_name,
680+
profile_name,
681+
ssh_credentials,
682+
data_dir: data_dir.clone(),
683+
build_dir: build_dir.clone(),
684+
};
562685

563686
assert_eq!(
564687
environment.instance_name().as_str(),
565-
"torrust-tracker-vm-feature-user-auth"
566-
);
567-
assert_eq!(
568-
*environment.data_dir(),
569-
PathBuf::from("data/feature-user-auth")
570-
);
571-
assert_eq!(
572-
*environment.build_dir(),
573-
PathBuf::from("build/feature-user-auth")
574-
);
575-
assert_eq!(
576-
environment.templates_dir(),
577-
PathBuf::from("data/feature-user-auth/templates")
688+
"torrust-tracker-vm-test-feature-user-auth"
578689
);
690+
assert_eq!(*environment.data_dir(), data_dir);
691+
assert_eq!(*environment.build_dir(), build_dir);
692+
assert_eq!(environment.templates_dir(), data_dir.join("templates"));
579693
}
580694
}

0 commit comments

Comments
 (0)