|
| 1 | +//! End-to-End Black Box Tests for Destroy Command |
| 2 | +//! |
| 3 | +//! This test suite provides true black-box testing of the destroy command |
| 4 | +//! by running the production application as an external process. These tests |
| 5 | +//! verify that the destroy command correctly handles the working directory |
| 6 | +//! parameter, ensuring environments can be destroyed from custom locations. |
| 7 | +//! |
| 8 | +//! ## Test Approach |
| 9 | +//! |
| 10 | +//! - **Black Box**: Runs production binary as external process |
| 11 | +//! - **Isolation**: Uses temporary directories for complete test isolation |
| 12 | +//! - **Coverage**: Tests complete workflow from environment creation to destruction |
| 13 | +//! - **Verification**: Validates environment is properly removed |
| 14 | +//! |
| 15 | +//! ## Test Scenarios |
| 16 | +//! |
| 17 | +//! 1. Default working directory: Destroy environment from current directory |
| 18 | +//! 2. Custom working directory: Destroy environment from temporary directory |
| 19 | +//! 3. Full lifecycle: Create → Destroy with custom working directory |
| 20 | +
|
| 21 | +mod support; |
| 22 | + |
| 23 | +use support::{EnvironmentStateAssertions, ProcessRunner, TempWorkspace}; |
| 24 | + |
| 25 | +/// Helper function to create a test environment configuration |
| 26 | +fn create_test_environment_config(env_name: &str) -> String { |
| 27 | + // Use absolute paths to SSH keys to ensure they work regardless of current directory |
| 28 | + let project_root = env!("CARGO_MANIFEST_DIR"); |
| 29 | + let private_key_path = format!("{project_root}/fixtures/testing_rsa"); |
| 30 | + let public_key_path = format!("{project_root}/fixtures/testing_rsa.pub"); |
| 31 | + |
| 32 | + serde_json::json!({ |
| 33 | + "environment": { |
| 34 | + "name": env_name |
| 35 | + }, |
| 36 | + "ssh_credentials": { |
| 37 | + "private_key_path": private_key_path, |
| 38 | + "public_key_path": public_key_path, |
| 39 | + "username": "torrust", |
| 40 | + "port": 22 |
| 41 | + } |
| 42 | + }) |
| 43 | + .to_string() |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn it_should_destroy_environment_with_default_working_directory() { |
| 48 | + // Arrange: Create temporary workspace |
| 49 | + let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace"); |
| 50 | + |
| 51 | + // Create environment configuration file |
| 52 | + let config = create_test_environment_config("test-destroy-default"); |
| 53 | + temp_workspace |
| 54 | + .write_config_file("environment.json", &config) |
| 55 | + .expect("Failed to write config file"); |
| 56 | + |
| 57 | + // Create environment in default location |
| 58 | + let create_result = ProcessRunner::new() |
| 59 | + .working_dir(temp_workspace.path()) |
| 60 | + .run_create_command("./environment.json") |
| 61 | + .expect("Failed to run create command"); |
| 62 | + |
| 63 | + assert!( |
| 64 | + create_result.success(), |
| 65 | + "Create command failed: {}", |
| 66 | + create_result.stderr() |
| 67 | + ); |
| 68 | + |
| 69 | + // Verify environment was created |
| 70 | + let env_assertions = EnvironmentStateAssertions::new(temp_workspace.path()); |
| 71 | + env_assertions.assert_environment_exists("test-destroy-default"); |
| 72 | + |
| 73 | + // Act: Destroy environment using destroy command |
| 74 | + let destroy_result = ProcessRunner::new() |
| 75 | + .working_dir(temp_workspace.path()) |
| 76 | + .run_destroy_command("test-destroy-default") |
| 77 | + .expect("Failed to run destroy command"); |
| 78 | + |
| 79 | + // Assert: Verify command succeeded |
| 80 | + assert!( |
| 81 | + destroy_result.success(), |
| 82 | + "Destroy command failed with exit code: {:?}\nstderr: {}", |
| 83 | + destroy_result.exit_code(), |
| 84 | + destroy_result.stderr() |
| 85 | + ); |
| 86 | + |
| 87 | + // Assert: Verify environment was transitioned to Destroyed state |
| 88 | + let env_assertions = EnvironmentStateAssertions::new(temp_workspace.path()); |
| 89 | + env_assertions.assert_environment_exists("test-destroy-default"); |
| 90 | + env_assertions.assert_environment_state_is("test-destroy-default", "Destroyed"); |
| 91 | +} |
| 92 | + |
| 93 | +#[test] |
| 94 | +fn it_should_destroy_environment_with_custom_working_directory() { |
| 95 | + // Arrange: Create temporary workspace |
| 96 | + let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace"); |
| 97 | + |
| 98 | + // Create environment configuration file |
| 99 | + let config = create_test_environment_config("test-destroy-custom"); |
| 100 | + temp_workspace |
| 101 | + .write_config_file("environment.json", &config) |
| 102 | + .expect("Failed to write config file"); |
| 103 | + |
| 104 | + // Create environment in custom location |
| 105 | + let create_result = ProcessRunner::new() |
| 106 | + .working_dir(temp_workspace.path()) |
| 107 | + .run_create_command("./environment.json") |
| 108 | + .expect("Failed to run create command"); |
| 109 | + |
| 110 | + assert!( |
| 111 | + create_result.success(), |
| 112 | + "Create command failed: {}", |
| 113 | + create_result.stderr() |
| 114 | + ); |
| 115 | + |
| 116 | + // Verify environment was created in custom location |
| 117 | + let env_assertions = EnvironmentStateAssertions::new(temp_workspace.path()); |
| 118 | + env_assertions.assert_environment_exists("test-destroy-custom"); |
| 119 | + |
| 120 | + // Act: Destroy environment using same working directory |
| 121 | + let destroy_result = ProcessRunner::new() |
| 122 | + .working_dir(temp_workspace.path()) |
| 123 | + .run_destroy_command("test-destroy-custom") |
| 124 | + .expect("Failed to run destroy command"); |
| 125 | + |
| 126 | + // Assert: Verify command succeeded |
| 127 | + assert!( |
| 128 | + destroy_result.success(), |
| 129 | + "Destroy command failed with exit code: {:?}\nstderr: {}", |
| 130 | + destroy_result.exit_code(), |
| 131 | + destroy_result.stderr() |
| 132 | + ); |
| 133 | + |
| 134 | + // Assert: Verify environment was transitioned to Destroyed state in custom location |
| 135 | + let env_assertions = EnvironmentStateAssertions::new(temp_workspace.path()); |
| 136 | + env_assertions.assert_environment_exists("test-destroy-custom"); |
| 137 | + env_assertions.assert_environment_state_is("test-destroy-custom", "Destroyed"); |
| 138 | +} |
| 139 | + |
| 140 | +#[test] |
| 141 | +fn it_should_fail_when_environment_not_found_in_working_directory() { |
| 142 | + // Arrange: Create temporary workspace |
| 143 | + let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace"); |
| 144 | + |
| 145 | + // Act: Try to destroy non-existent environment |
| 146 | + let destroy_result = ProcessRunner::new() |
| 147 | + .working_dir(temp_workspace.path()) |
| 148 | + .run_destroy_command("nonexistent-environment") |
| 149 | + .expect("Failed to run destroy command"); |
| 150 | + |
| 151 | + // Assert: Command should fail |
| 152 | + assert!( |
| 153 | + !destroy_result.success(), |
| 154 | + "Command should have failed when environment doesn't exist" |
| 155 | + ); |
| 156 | + |
| 157 | + // Verify error message mentions environment not found |
| 158 | + let stderr = destroy_result.stderr(); |
| 159 | + assert!( |
| 160 | + stderr.contains("not found") || stderr.contains("does not exist"), |
| 161 | + "Error message should mention environment not found, got: {stderr}" |
| 162 | + ); |
| 163 | +} |
| 164 | + |
| 165 | +#[test] |
| 166 | +fn it_should_complete_full_lifecycle_with_custom_working_directory() { |
| 167 | + // Arrange: Create temporary workspace |
| 168 | + let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace"); |
| 169 | + |
| 170 | + // Create environment configuration file |
| 171 | + let config = create_test_environment_config("test-lifecycle"); |
| 172 | + temp_workspace |
| 173 | + .write_config_file("environment.json", &config) |
| 174 | + .expect("Failed to write config file"); |
| 175 | + |
| 176 | + // Act: Create environment in custom location |
| 177 | + let create_result = ProcessRunner::new() |
| 178 | + .working_dir(temp_workspace.path()) |
| 179 | + .run_create_command("./environment.json") |
| 180 | + .expect("Failed to run create command"); |
| 181 | + |
| 182 | + assert!( |
| 183 | + create_result.success(), |
| 184 | + "Create command failed: {}", |
| 185 | + create_result.stderr() |
| 186 | + ); |
| 187 | + |
| 188 | + // Verify environment exists |
| 189 | + let env_assertions = EnvironmentStateAssertions::new(temp_workspace.path()); |
| 190 | + env_assertions.assert_environment_exists("test-lifecycle"); |
| 191 | + env_assertions.assert_environment_state_is("test-lifecycle", "Created"); |
| 192 | + |
| 193 | + // Act: Destroy environment |
| 194 | + let destroy_result = ProcessRunner::new() |
| 195 | + .working_dir(temp_workspace.path()) |
| 196 | + .run_destroy_command("test-lifecycle") |
| 197 | + .expect("Failed to run destroy command"); |
| 198 | + |
| 199 | + // Assert: Both commands succeed |
| 200 | + assert!( |
| 201 | + destroy_result.success(), |
| 202 | + "Destroy command failed: {}", |
| 203 | + destroy_result.stderr() |
| 204 | + ); |
| 205 | + |
| 206 | + // Assert: Environment is transitioned to Destroyed state |
| 207 | + let env_assertions = EnvironmentStateAssertions::new(temp_workspace.path()); |
| 208 | + env_assertions.assert_environment_exists("test-lifecycle"); |
| 209 | + env_assertions.assert_environment_state_is("test-lifecycle", "Destroyed"); |
| 210 | +} |
0 commit comments