-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging_integration.rs
More file actions
558 lines (475 loc) · 16.9 KB
/
logging_integration.rs
File metadata and controls
558 lines (475 loc) · 16.9 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
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Integration tests for logging configuration
//!
//! These tests verify that the logging system works correctly with different
//! configurations by running the `test_logging` binary and examining its output.
//!
//! ## Test Coverage
//!
//! - File-only output mode (no stderr)
//! - File-and-stderr output mode (both outputs)
//! - Pretty format logging
//! - JSON format logging
//! - Compact format logging
//! - Log file append mode
//! - All log levels (trace, debug, info, warn, error)
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;
use torrust_tracker_deployer_lib::bootstrap::logging::LOG_FILE_NAME;
/// Helper struct to manage test execution and cleanup
struct LoggingTest {
/// Temporary directory that's automatically cleaned up when dropped.
/// Must be kept to prevent premature cleanup.
temp_dir: TempDir,
test_dir: PathBuf,
log_file_path: PathBuf,
}
impl LoggingTest {
/// Create a new test environment with isolated data directory
fn new() -> Self {
let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
let test_dir = temp_dir.path().to_path_buf();
let log_file_path = test_dir.join("data/logs").join(LOG_FILE_NAME);
let instance = Self {
temp_dir,
test_dir,
log_file_path,
};
tracing::info!(
temp_dir = %instance.temp_dir.path().display(),
"Created isolated test environment"
);
instance
}
/// Run the `test_logging` binary with specified options
fn run_test_logging(&self, format: &str, output: &str) -> TestOutput {
// Use the cargo binary path to find the compiled test_logging binary
let binary_path = std::env::current_exe()
.expect("Failed to get current test executable path")
.parent()
.expect("Failed to get parent directory")
.parent()
.expect("Failed to get deps parent directory")
.join("test_logging");
// Use isolated temp directory for logs (follows testing conventions)
let log_dir = self.test_dir.join("data/logs");
let output = Command::new(&binary_path)
.args([
"--format",
format,
"--output",
output,
"--log-dir",
&log_dir.to_string_lossy(),
])
.current_dir(&self.test_dir)
.output()
.expect("Failed to execute test_logging binary");
TestOutput {
stdout: String::from_utf8_lossy(&output.stdout).to_string(),
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
success: output.status.success(),
}
}
/// Read the log file contents, waiting for it to be written by the non-blocking logger
///
/// The logging system uses `tracing_appender::non_blocking` which writes logs
/// via a background thread. This method polls the file until it has content,
/// avoiding unnecessary delays when logs are already written while handling
/// the race condition when the test reads before the background thread writes.
fn read_log_file(&self) -> String {
use std::time::Duration;
const MAX_RETRIES: u32 = 20;
const RETRY_DELAY_MS: u64 = 50;
const INITIAL_WAIT_MS: u64 = 10;
// Small initial wait to allow the non-blocking writer's background thread to start
std::thread::sleep(Duration::from_millis(INITIAL_WAIT_MS));
for attempt in 0..MAX_RETRIES {
if let Ok(content) = fs::read_to_string(&self.log_file_path) {
if !content.is_empty() {
return content;
}
}
// If file is empty or doesn't exist yet, wait and retry
if attempt < MAX_RETRIES - 1 {
std::thread::sleep(Duration::from_millis(RETRY_DELAY_MS));
}
}
panic!(
"Log file at {} never received content after {} retries ({}ms total wait)",
self.log_file_path.display(),
MAX_RETRIES,
INITIAL_WAIT_MS + (u64::from(MAX_RETRIES) * RETRY_DELAY_MS)
);
}
/// Check if log file exists
fn log_file_exists(&self) -> bool {
self.log_file_path.exists()
}
/// Get line count in log file
fn log_file_line_count(&self) -> usize {
if !self.log_file_exists() {
return 0;
}
self.read_log_file().lines().count()
}
}
struct TestOutput {
stdout: String,
stderr: String,
success: bool,
}
#[test]
fn it_should_write_logs_to_file_only_in_file_only_mode() {
let test = LoggingTest::new();
let output = test.run_test_logging("pretty", "file-only");
// Should complete successfully
assert!(
output.success,
"test_logging binary should execute successfully"
);
// Should print completion marker to stdout
assert!(
output.stdout.contains("LOGGING_TEST_COMPLETE"),
"stdout should contain completion marker"
);
// Stderr should be empty (no logging output)
// Note: May contain compilation messages, but no log messages
assert!(
!output.stderr.contains("INFO"),
"stderr should not contain INFO log messages in file-only mode"
);
assert!(
!output.stderr.contains("WARN"),
"stderr should not contain WARN log messages in file-only mode"
);
assert!(
!output.stderr.contains("ERROR"),
"stderr should not contain ERROR log messages in file-only mode"
);
// Log file should exist and contain logs
assert!(
test.log_file_exists(),
"log file should be created automatically"
);
let log_content = test.read_log_file();
assert!(
log_content.contains("INFO"),
"log file should contain INFO level logs"
);
assert!(
log_content.contains("WARN"),
"log file should contain WARN level logs"
);
assert!(
log_content.contains("ERROR"),
"log file should contain ERROR level logs"
);
}
#[test]
fn it_should_write_logs_to_both_file_and_stderr_in_file_and_stderr_mode() {
let test = LoggingTest::new();
let output = test.run_test_logging("pretty", "file-and-stderr");
// Should complete successfully
assert!(
output.success,
"test_logging binary should execute successfully"
);
// Should print completion marker to stdout
assert!(
output.stdout.contains("LOGGING_TEST_COMPLETE"),
"stdout should contain completion marker"
);
// Stderr should contain log messages
assert!(
output.stderr.contains("INFO"),
"stderr should contain INFO log messages in file-and-stderr mode"
);
assert!(
output.stderr.contains("WARN"),
"stderr should contain WARN log messages in file-and-stderr mode"
);
assert!(
output.stderr.contains("ERROR"),
"stderr should contain ERROR log messages in file-and-stderr mode"
);
// Log file should also exist and contain logs
assert!(
test.log_file_exists(),
"log file should be created automatically"
);
let log_content = test.read_log_file();
assert!(
log_content.contains("INFO"),
"log file should contain INFO level logs"
);
assert!(
log_content.contains("WARN"),
"log file should contain WARN level logs"
);
assert!(
log_content.contains("ERROR"),
"log file should contain ERROR level logs"
);
}
#[test]
fn it_should_format_logs_in_json_format() {
let test = LoggingTest::new();
let output = test.run_test_logging("json", "file-and-stderr");
assert!(
output.success,
"test_logging binary should execute successfully"
);
// JSON format should have specific structure
let log_content = test.read_log_file();
// JSON logs should contain timestamp and level fields
assert!(
log_content.contains(r#""timestamp":"#),
"JSON logs should contain timestamp field"
);
assert!(
log_content.contains(r#""level":"INFO"#) || log_content.contains(r#""level":"info"#),
"JSON logs should contain INFO level"
);
assert!(
log_content.contains(r#""level":"WARN"#) || log_content.contains(r#""level":"warn"#),
"JSON logs should contain WARN level"
);
assert!(
log_content.contains(r#""level":"ERROR"#) || log_content.contains(r#""level":"error"#),
"JSON logs should contain ERROR level"
);
}
#[test]
fn it_should_format_logs_in_compact_format() {
let test = LoggingTest::new();
let output = test.run_test_logging("compact", "file-and-stderr");
assert!(
output.success,
"test_logging binary should execute successfully"
);
let log_content = test.read_log_file();
// Compact format should still contain log levels and messages
assert!(
log_content.contains("INFO"),
"compact logs should contain INFO level"
);
assert!(
log_content.contains("WARN"),
"compact logs should contain WARN level"
);
assert!(
log_content.contains("ERROR"),
"compact logs should contain ERROR level"
);
// Compact format should be more concise than pretty format
// (This is a heuristic - compact format typically has fewer lines)
let line_count = log_content.lines().count();
assert!(
line_count <= 20,
"compact format should produce relatively few lines, got: {line_count}"
);
}
#[test]
fn it_should_append_to_existing_log_file() {
let test = LoggingTest::new();
// Run first time
let output1 = test.run_test_logging("compact", "file-only");
assert!(output1.success, "first test_logging run should succeed");
let line_count_after_first = test.log_file_line_count();
assert!(
line_count_after_first > 0,
"log file should have content after first run"
);
// Run second time
let output2 = test.run_test_logging("compact", "file-only");
assert!(output2.success, "second test_logging run should succeed");
let line_count_after_second = test.log_file_line_count();
// Line count should increase (append mode)
assert!(
line_count_after_second > line_count_after_first,
"log file should grow after second run (append mode). Before: {line_count_after_first}, After: {line_count_after_second}"
);
// Verify that logs were appended (line count increased)
// Note: We're being lenient here because other processes might also be writing
// to the log file during test execution
assert!(
line_count_after_second >= line_count_after_first + 3,
"log file should have at least 3 more lines after second run (INFO, WARN, ERROR). Before: {line_count_after_first}, After: {line_count_after_second}"
);
}
#[test]
fn it_should_emit_all_log_levels_when_trace_enabled() {
let test = LoggingTest::new();
// Run with RUST_LOG=trace to enable all levels
let binary_path = std::env::current_exe()
.expect("Failed to get current test executable path")
.parent()
.expect("Failed to get parent directory")
.parent()
.expect("Failed to get deps parent directory")
.join("test_logging");
let output = Command::new(&binary_path)
.args(["--format", "pretty", "--output", "file-and-stderr"])
.env("RUST_LOG", "trace")
.current_dir(&test.test_dir)
.output()
.expect("Failed to execute test_logging binary");
let success = output.status.success();
assert!(success, "test_logging binary should execute successfully");
let log_content = test.read_log_file();
// With trace enabled, all levels should appear
assert!(
log_content.contains("TRACE"),
"log file should contain TRACE level logs when RUST_LOG=trace"
);
assert!(
log_content.contains("DEBUG"),
"log file should contain DEBUG level logs when RUST_LOG=trace"
);
assert!(
log_content.contains("INFO"),
"log file should contain INFO level logs"
);
assert!(
log_content.contains("WARN"),
"log file should contain WARN level logs"
);
assert!(
log_content.contains("ERROR"),
"log file should contain ERROR level logs"
);
}
#[test]
fn it_should_create_log_directory_automatically() {
let test = LoggingTest::new();
// Verify data/logs directory doesn't exist initially
let logs_dir = test.test_dir.join("data/logs");
assert!(
!logs_dir.exists(),
"logs directory should not exist before running test"
);
// Run test_logging
let output = test.run_test_logging("pretty", "file-only");
assert!(
output.success,
"test_logging binary should execute successfully"
);
// Verify directory was created
assert!(
logs_dir.exists(),
"logs directory should be created automatically"
);
assert!(logs_dir.is_dir(), "data/logs should be a directory");
// Verify log file was created inside
assert!(
test.log_file_exists(),
"log file should be created inside the logs directory"
);
}
#[test]
fn it_should_not_include_ansi_codes_in_file_output_compact_format() {
let test = LoggingTest::new();
let output = test.run_test_logging("compact", "file-only");
assert!(
output.success,
"test_logging binary should execute successfully"
);
// Read raw bytes from log file
let log_bytes = fs::read(&test.log_file_path).expect("Failed to read log file");
// Check for ANSI escape sequence marker (0x1B)
assert!(
!log_bytes.contains(&0x1B),
"log file should not contain ANSI escape sequences (0x1B) in compact format"
);
// Verify log content is still present
let log_content = String::from_utf8_lossy(&log_bytes);
assert!(
log_content.contains("INFO"),
"log file should still contain INFO level logs"
);
assert!(
log_content.contains("WARN"),
"log file should still contain WARN level logs"
);
assert!(
log_content.contains("ERROR"),
"log file should still contain ERROR level logs"
);
}
#[test]
fn it_should_not_include_ansi_codes_in_file_output_pretty_format() {
let test = LoggingTest::new();
let output = test.run_test_logging("pretty", "file-only");
assert!(
output.success,
"test_logging binary should execute successfully"
);
// Read raw bytes from log file
let log_bytes = fs::read(&test.log_file_path).expect("Failed to read log file");
// Check for ANSI escape sequence marker (0x1B)
assert!(
!log_bytes.contains(&0x1B),
"log file should not contain ANSI escape sequences (0x1B) in pretty format"
);
// Verify log content is still present
let log_content = String::from_utf8_lossy(&log_bytes);
assert!(
log_content.contains("INFO"),
"log file should still contain INFO level logs"
);
assert!(
log_content.contains("WARN"),
"log file should still contain WARN level logs"
);
assert!(
log_content.contains("ERROR"),
"log file should still contain ERROR level logs"
);
}
#[test]
fn it_should_not_include_ansi_codes_in_file_output_json_format() {
let test = LoggingTest::new();
let output = test.run_test_logging("json", "file-only");
assert!(
output.success,
"test_logging binary should execute successfully"
);
// Read raw bytes from log file
let log_bytes = fs::read(&test.log_file_path).expect("Failed to read log file");
// Check for ANSI escape sequence marker (0x1B)
assert!(
!log_bytes.contains(&0x1B),
"log file should not contain ANSI escape sequences (0x1B) in JSON format"
);
// Verify JSON structure is valid
let log_content = String::from_utf8_lossy(&log_bytes);
assert!(
log_content.contains(r#""level":"INFO"#) || log_content.contains(r#""level":"info"#),
"JSON logs should contain INFO level"
);
}
#[test]
fn it_should_not_include_ansi_codes_in_file_output_dual_mode() {
let test = LoggingTest::new();
// Run with file-and-stderr mode to test dual output
let output = test.run_test_logging("compact", "file-and-stderr");
assert!(
output.success,
"test_logging binary should execute successfully"
);
// Read raw bytes from log file
let log_bytes = fs::read(&test.log_file_path).expect("Failed to read log file");
// File output should NOT contain ANSI codes
assert!(
!log_bytes.contains(&0x1B),
"log file should not contain ANSI escape sequences (0x1B) in file-and-stderr mode"
);
// Verify log content is still present in file
let log_content = String::from_utf8_lossy(&log_bytes);
assert!(
log_content.contains("INFO"),
"log file should still contain INFO level logs"
);
}