-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrender_command.rs
More file actions
545 lines (474 loc) · 19.8 KB
/
render_command.rs
File metadata and controls
545 lines (474 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! End-to-End Black Box Tests for Render Command
//!
//! This test suite provides true black-box testing of the render command
//! by running the production application as an external process. These tests
//! verify that the render command correctly generates deployment artifacts
//! without provisioning infrastructure.
//!
//! ## Test Approach
//!
//! - **Black Box**: Runs production binary as external process
//! - **Isolation**: Uses temporary directories for complete test isolation
//! - **Coverage**: Tests both input modes (env-name and env-file)
//! - **Verification**: Validates all artifacts are properly generated
//!
//! ## Test Scenarios
//!
//! 1. Render with env-name: Create environment → Render artifacts to custom directory
//! 2. Render with env-file: Generate artifacts directly from config file
//! 3. Render requires output directory: Verify --output-dir parameter is required
//! 4. Render fails on existing directory: Verify `OutputDirectoryExists` error
//! 5. Environment not found: Verify proper error handling
//! 6. Config file not found: Verify proper error handling
//! 7. Custom working directory: Verify render works from different locations
//!
//! ## Design Decisions
//!
//! - **Output directory required**: Tests verify --output-dir flag is mandatory
//! - **Output protection**: Tests verify existing directories are not overwritten
//! - **Created state only**: Tests render on environments in Created state
//! - **IP validation**: Tests verify IP address parameter is required and validated
//! - **Dual input modes**: Tests cover both --env-name and --env-file workflows
use super::super::support::{process_runner, EnvironmentStateAssertions, TempWorkspace};
use anyhow::Result;
use torrust_dependency_installer::{verify_dependencies, Dependency};
use torrust_tracker_deployer_lib::testing::e2e::tasks::black_box::create_test_environment_config;
/// Verify that all required dependencies are installed for render command E2E tests.
///
/// **Current State**: No system dependencies required.
///
/// These black-box tests run the production binary as an external process and verify
/// the render command workflow. Currently, they only test the command interface and
/// local artifact generation, without requiring infrastructure tools.
///
/// # Future Dependencies
///
/// If these tests evolve to verify actual infrastructure deployment or validation,
/// add required dependencies here:
/// ```ignore
/// let required_deps = &[Dependency::OpenTofu, Dependency::Ansible];
/// ```
///
/// # Errors
///
/// Returns an error if any required dependencies are missing or cannot be detected.
fn verify_required_dependencies() -> Result<()> {
// Currently no system dependencies required - empty array
let required_deps: &[Dependency] = &[];
verify_dependencies(required_deps)?;
Ok(())
}
#[test]
fn it_should_render_artifacts_using_env_name_successfully() {
// Verify dependencies before running tests
verify_required_dependencies().expect("Dependency verification failed");
// Arrange: Create temporary workspace
let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace");
// Create environment configuration file
let config = create_test_environment_config("test-render-env-name");
temp_workspace
.write_config_file("environment.json", &config)
.expect("Failed to write config file");
// Create environment in default location
let create_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_create_command("./environment.json")
.expect("Failed to run create command");
assert!(
create_result.success(),
"Create command failed: {}",
create_result.stderr()
);
// Verify environment is in Created state before render
let env_assertions = EnvironmentStateAssertions::new(temp_workspace.path());
env_assertions.assert_environment_exists("test-render-env-name");
env_assertions.assert_environment_state_is("test-render-env-name", "Created");
// Act: Render artifacts using env-name input mode
let output_dir = temp_workspace.path().join("render-output");
let render_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_render_command_with_env_name(
"test-render-env-name",
"192.168.1.100",
output_dir.to_str().unwrap(),
)
.expect("Failed to run render command");
// Assert: Verify command succeeded
assert!(
render_result.success(),
"Render command failed with exit code: {:?}\nstderr: {}",
render_result.exit_code(),
render_result.stderr()
);
// Assert: Verify output directory and artifacts were created
assert!(
output_dir.exists(),
"Output directory should exist at: {}",
output_dir.display()
);
// Verify key artifacts exist
let tofu_dir = output_dir.join("tofu");
assert!(
tofu_dir.exists(),
"Tofu directory should exist at: {}",
tofu_dir.display()
);
// Assert: Verify the JSON result is on stdout (not stderr)
let stdout = render_result.stdout();
let json: serde_json::Value =
serde_json::from_str(&stdout).expect("Render output must be valid JSON on stdout");
assert!(
json.get("output_dir").is_some(),
"Output should contain JSON output_dir field. Got stdout: {stdout}"
);
}
#[test]
fn it_should_render_artifacts_using_config_file_successfully() {
// Verify dependencies before running tests
verify_required_dependencies().expect("Dependency verification failed");
// Arrange: Create temporary workspace
let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace");
// Create environment configuration file (but don't create environment)
let config = create_test_environment_config("test-render-config-file");
temp_workspace
.write_config_file("environment.json", &config)
.expect("Failed to write config file");
// Get absolute path to config file for render command
let config_path = temp_workspace
.path()
.join("environment.json")
.to_str()
.expect("Failed to convert path to string")
.to_string();
// Act: Render artifacts directly from config file (no environment creation)
let output_dir = temp_workspace.path().join("render-config-output");
let render_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_render_command_with_config_file(
&config_path,
"192.168.1.101",
output_dir.to_str().unwrap(),
)
.expect("Failed to run render command");
// Assert: Verify command succeeded
assert!(
render_result.success(),
"Render command failed with exit code: {:?}\nstderr: {}",
render_result.exit_code(),
render_result.stderr()
);
// Assert: Environment should NOT be created in data/ (rendered from config only)
let env_assertions = EnvironmentStateAssertions::new(temp_workspace.path());
env_assertions.assert_environment_not_exists("test-render-config-file");
// Assert: Verify output directory and artifacts were created
assert!(
output_dir.exists(),
"Output directory should exist at: {}",
output_dir.display()
);
// Verify key artifacts exist
let tofu_dir = output_dir.join("tofu");
assert!(
tofu_dir.exists(),
"Tofu directory should exist at: {}",
tofu_dir.display()
);
// Assert: Verify the JSON result is on stdout (not stderr)
let stdout = render_result.stdout();
let json: serde_json::Value =
serde_json::from_str(&stdout).expect("Render output must be valid JSON on stdout");
assert!(
json.get("output_dir").is_some(),
"Output should contain JSON output_dir field. Got stdout: {stdout}"
);
}
#[test]
fn it_should_fail_when_output_directory_already_exists() {
// Verify dependencies before running tests
verify_required_dependencies().expect("Dependency verification failed");
// Arrange: Create temporary workspace and environment
let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace");
let config = create_test_environment_config("test-render-output-dir-exists");
temp_workspace
.write_config_file("environment.json", &config)
.expect("Failed to write config file");
let create_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_create_command("./environment.json")
.expect("Failed to run create command");
assert!(
create_result.success(),
"Create command failed: {}",
create_result.stderr()
);
// Act: Render artifacts first time
let output_dir = temp_workspace.path().join("render-idempotent-output");
let render1_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_render_command_with_env_name(
"test-render-output-dir-exists",
"192.168.1.102",
output_dir.to_str().unwrap(),
)
.expect("Failed to run first render command");
assert!(
render1_result.success(),
"First render failed: {}",
render1_result.stderr()
);
// Act: Render artifacts second time (should fail with OutputDirectoryExists error)
let render2_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_render_command_with_env_name(
"test-render-output-dir-exists",
"192.168.1.102",
output_dir.to_str().unwrap(),
)
.expect("Failed to run second render command");
// Assert: Second render should fail because output directory already exists
assert!(
!render2_result.success(),
"Second render should fail when output directory exists"
);
// Assert: Error message should mention output directory exists
let stderr = render2_result.stderr();
assert!(
stderr.contains("Output directory") || stderr.contains("already exists"),
"Error message should mention output directory exists. Stderr: {stderr}"
);
// Assert: Output directory should still exist with artifacts from first render
assert!(output_dir.exists(), "Output directory should still exist");
let tofu_dir = output_dir.join("tofu");
assert!(tofu_dir.exists(), "Tofu directory should still exist");
}
#[test]
fn it_should_fail_when_environment_not_found() {
// Verify dependencies before running tests
verify_required_dependencies().expect("Dependency verification failed");
// Arrange: Create temporary workspace (but don't create environment)
let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace");
// Act: Try to render non-existent environment
let output_dir = temp_workspace.path().join("nonexistent-output");
let render_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_render_command_with_env_name(
"nonexistent-env",
"192.168.1.103",
output_dir.to_str().unwrap(),
)
.expect("Failed to run render command");
// Assert: Verify command failed with proper error
assert!(
!render_result.success(),
"Render command should fail for non-existent environment"
);
// Assert: Verify error message mentions environment not found
let stderr = render_result.stderr();
assert!(
stderr.contains("not found") || stderr.contains("Environment not found"),
"Error message should mention environment not found. Stderr: {stderr}"
);
}
#[test]
fn it_should_fail_when_config_file_not_found() {
// Verify dependencies before running tests
verify_required_dependencies().expect("Dependency verification failed");
// Arrange: Create temporary workspace (but don't create config file)
let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace");
// Act: Try to render with non-existent config file
let output_dir = temp_workspace.path().join("missing-config-output");
let render_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_render_command_with_config_file(
"./nonexistent.json",
"192.168.1.104",
output_dir.to_str().unwrap(),
)
.expect("Failed to run render command");
// Assert: Verify command failed with proper error
assert!(
!render_result.success(),
"Render command should fail for non-existent config file"
);
// Assert: Verify error message mentions file not found
let stderr = render_result.stderr();
assert!(
stderr.contains("not found") || stderr.contains("No such file"),
"Error message should mention file not found. Stderr: {stderr}"
);
}
#[test]
fn it_should_work_with_custom_working_directory() {
// Verify dependencies before running tests
verify_required_dependencies().expect("Dependency verification failed");
// Arrange: Create temporary workspace
let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace");
// Create environment configuration file
let config = create_test_environment_config("test-render-custom-dir");
temp_workspace
.write_config_file("environment.json", &config)
.expect("Failed to write config file");
// Create environment in custom location
let create_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_create_command("./environment.json")
.expect("Failed to run create command");
assert!(
create_result.success(),
"Create command failed: {}",
create_result.stderr()
);
// Act: Render from custom working directory
let output_dir = temp_workspace.path().join("custom-dir-output");
let render_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_render_command_with_env_name(
"test-render-custom-dir",
"192.168.1.105",
output_dir.to_str().unwrap(),
)
.expect("Failed to run render command");
// Assert: Verify command succeeded
assert!(
render_result.success(),
"Render command failed with exit code: {:?}\nstderr: {}",
render_result.exit_code(),
render_result.stderr()
);
// Assert: Verify artifacts were created in output directory
assert!(
output_dir.exists(),
"Output directory should exist at: {}",
output_dir.display()
);
let tofu_dir = output_dir.join("tofu");
assert!(
tofu_dir.exists(),
"Tofu directory should exist at: {}",
tofu_dir.display()
);
}
#[test]
fn it_should_complete_full_lifecycle_from_create_to_render() {
// Verify dependencies before running tests
verify_required_dependencies().expect("Dependency verification failed");
// Arrange: Create temporary workspace
let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace");
// Create environment configuration file
let config = create_test_environment_config("test-full-lifecycle-render");
temp_workspace
.write_config_file("environment.json", &config)
.expect("Failed to write config file");
// Step 1: Create environment
let create_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_create_command("./environment.json")
.expect("Failed to run create command");
assert!(
create_result.success(),
"Create command failed: {}",
create_result.stderr()
);
// Verify environment was created in Created state
let env_assertions = EnvironmentStateAssertions::new(temp_workspace.path());
env_assertions.assert_environment_exists("test-full-lifecycle-render");
env_assertions.assert_environment_state_is("test-full-lifecycle-render", "Created");
// Step 2: Render artifacts
let output_dir = temp_workspace.path().join("lifecycle-output");
let render_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_render_command_with_env_name(
"test-full-lifecycle-render",
"192.168.1.106",
output_dir.to_str().unwrap(),
)
.expect("Failed to run render command");
assert!(
render_result.success(),
"Render command failed with exit code: {:?}\nstderr: {}",
render_result.exit_code(),
render_result.stderr()
);
// Step 3: Verify artifacts were generated
assert!(
output_dir.exists(),
"Output directory should exist at: {}",
output_dir.display()
);
let tofu_dir = output_dir.join("tofu");
assert!(
tofu_dir.exists(),
"Tofu directory should exist at: {}",
tofu_dir.display()
);
// Verify environment remains in Created state (render doesn't change state)
env_assertions.assert_environment_state_is("test-full-lifecycle-render", "Created");
// Verify the JSON result is on stdout (not stderr)
let stdout = render_result.stdout();
let json: serde_json::Value =
serde_json::from_str(&stdout).expect("Render output must be valid JSON on stdout");
assert!(
json.get("output_dir").is_some(),
"Output should contain JSON output_dir field. Got stdout: {stdout}"
);
}
#[test]
fn it_should_produce_json_by_default() {
// Verify dependencies before running tests
verify_required_dependencies().expect("Dependency verification failed");
// Arrange: Create environment first so render has something to work with
let temp_workspace = TempWorkspace::new().expect("Failed to create temp workspace");
let config = create_test_environment_config("test-render-json-default");
temp_workspace
.write_config_file("environment.json", &config)
.expect("Failed to write config file");
let config_path = temp_workspace.path().join("environment.json");
let create_result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_create_command(config_path.to_str().unwrap())
.expect("Failed to run create command");
assert!(
create_result.success(),
"Pre-condition: create must succeed, stderr: {}",
create_result.stderr()
);
// Act: Run render command without --output-format
let output_dir = temp_workspace.path().join("render-output");
let result = process_runner()
.working_dir(temp_workspace.path())
.log_dir(temp_workspace.path().join("logs"))
.run_render_command_with_env_name(
"test-render-json-default",
"192.168.1.100",
output_dir.to_str().unwrap(),
)
.expect("Failed to run render command");
// Assert: Command succeeds
assert!(
result.success(),
"Render command should succeed for an existing environment, stderr: {}",
result.stderr()
);
// Assert: stdout is valid JSON
let stdout = result.stdout();
let json: serde_json::Value =
serde_json::from_str(&stdout).expect("Render command default output must be valid JSON");
// Assert: Expected field is present
assert!(
json.get("output_dir").is_some(),
"Expected `output_dir` field in render JSON output, got: {stdout}"
);
}