-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror.rs
More file actions
640 lines (543 loc) · 21.3 KB
/
error.rs
File metadata and controls
640 lines (543 loc) · 21.3 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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Error types for SSH server container operations
use std::path::PathBuf;
use thiserror::Error;
use crate::adapters::docker::DockerError;
/// Errors that can occur when working with SSH server containers
#[derive(Debug, Error)]
pub enum SshServerError {
/// SSH server Dockerfile not found at expected location
#[error(
"SSH server Dockerfile not found at '{expected_path}'
Tip: Ensure 'docker/ssh-server/Dockerfile' exists in the project root"
)]
DockerfileNotFound { expected_path: PathBuf },
/// Docker build command failed
#[error("Docker build command failed for image '{image_name}:{image_tag}'
Tip: Run 'docker build -t {image_name}:{image_tag} {dockerfile_dir}' manually to see detailed errors")]
DockerBuildFailed {
image_name: String,
image_tag: String,
dockerfile_dir: String,
stdout: String,
stderr: String,
},
/// Failed to start SSH server container
#[error(
"Failed to start SSH server container
Tip: Check if Docker daemon is running with 'docker ps'"
)]
ContainerStartFailed {
#[source]
source: testcontainers::core::error::TestcontainersError,
},
/// Failed to get mapped port for SSH container
#[error(
"Failed to get mapped port for SSH container
Tip: Verify container is running with 'docker ps'"
)]
PortMappingFailed {
#[source]
source: testcontainers::core::error::TestcontainersError,
},
/// Docker command execution failed
#[error(
"Docker command execution failed: {command}
Tip: Verify Docker is installed and accessible: 'docker --version'"
)]
DockerCommandFailed {
command: String,
#[source]
source: std::io::Error,
},
/// Invalid UTF-8 in Dockerfile path
#[error(
"Invalid UTF-8 in Dockerfile path: '{path}'
Tip: Avoid non-ASCII characters in file paths"
)]
InvalidUtf8InPath { path: String },
/// Docker client operation failed
#[error(
"Docker client operation failed
Tip: Check Docker daemon status with 'docker ps'"
)]
DockerClientError {
#[source]
source: Box<DockerError>,
},
}
impl SshServerError {
/// Get detailed troubleshooting guidance for this error
///
/// This method provides comprehensive troubleshooting steps that can be
/// displayed to users when they need more help resolving the error.
///
/// # Example
///
/// ```rust,no_run
/// use torrust_tracker_deployer_lib::testing::integration::ssh_server::RealSshServerContainer;
///
/// # async fn example() {
/// if let Err(e) = RealSshServerContainer::start().await {
/// eprintln!("Error: {e}");
/// eprintln!("\nTroubleshooting:\n{}", e.help());
/// }
/// # }
/// ```
#[must_use]
pub fn help(&self) -> &'static str {
match self {
Self::DockerfileNotFound { .. } => {
"Dockerfile Not Found - Detailed Troubleshooting:
1. Verify the Dockerfile exists:
ls -la docker/ssh-server/Dockerfile
2. Check you're running from project root:
pwd # Should show the torrust-tracker-deployer directory
3. If using a custom Dockerfile location:
- Update the DOCKERFILE_DIR constant in constants.rs
- Ensure the path is relative to the project root
For more information, see the SSH server documentation."
}
Self::DockerBuildFailed { .. } => {
"Docker Build Failed - Detailed Troubleshooting:
1. Run the build command manually to see full output:
docker build -t torrust-ssh-server:latest docker/ssh-server
2. Common issues:
- Check Dockerfile syntax
- Verify base image is accessible: docker pull ubuntu:22.04
- Check network connectivity for package downloads
- Review build logs for specific error messages
3. Check Docker daemon status:
systemctl status docker # Linux systemd
docker info # General information
4. Try cleaning Docker build cache:
docker builder prune
For more information, see Docker documentation."
}
Self::ContainerStartFailed { .. } => {
"Container Start Failed - Detailed Troubleshooting:
1. Check if Docker daemon is running:
docker ps
2. Verify sufficient resources:
docker system df # Check disk space
docker info # Check memory/CPU limits
3. Check for port conflicts:
netstat -tlnp | grep :22 # Linux
ss -tlnp | grep :22 # Alternative
lsof -i :22 # macOS
4. Review Docker logs:
docker logs <container_id>
For more information, see testcontainers documentation."
}
Self::PortMappingFailed { .. } => {
"Port Mapping Failed - Detailed Troubleshooting:
1. Verify container is running:
docker ps
2. Check container port configuration:
docker port <container_id>
3. Check if the required port is already in use:
netstat -tlnp # Linux
ss -tlnp # Alternative
lsof -i :22 # macOS
For more information, see Docker networking documentation."
}
Self::DockerCommandFailed { .. } => {
"Docker Command Execution Failed - Detailed Troubleshooting:
1. Verify Docker is installed:
docker --version
2. Check Docker daemon is running:
systemctl status docker # Linux systemd
docker ps # Quick check
3. Verify user permissions:
groups # Check if user is in 'docker' group
sudo usermod -aG docker $USER # Add user to docker group
# Log out and log back in for group changes to take effect
4. Try running Docker with sudo (temporary workaround):
sudo docker ps
For more information, see Docker installation documentation."
}
Self::InvalidUtf8InPath { .. } => {
"Invalid UTF-8 in Path - Detailed Troubleshooting:
1. Check the Dockerfile path contains only valid UTF-8 characters
2. Avoid special characters, emoji, or non-ASCII characters in paths
3. Use ASCII characters for file and directory names
This is typically a configuration or system encoding issue."
}
Self::DockerClientError { .. } => {
"Docker Client Operation Failed - Detailed Troubleshooting:
1. Verify Docker daemon is running:
docker ps
2. Check Docker system status:
docker info
3. Review Docker logs:
journalctl -u docker # Linux systemd
docker system events # Real-time events
4. Verify user permissions:
groups # Check if user is in 'docker' group
sudo usermod -aG docker $USER # Add user to docker group
# Log out and log back in for group changes to take effect
For more information, see the source error details and Docker documentation."
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io;
/// Helper to create a test `TestcontainersError`
/// Note: We use `PortNotExposed` variant as it doesn't require many fields
fn create_testcontainers_error() -> testcontainers::core::error::TestcontainersError {
testcontainers::core::error::TestcontainersError::PortNotExposed {
id: "test-container".to_string(),
port: testcontainers::core::ContainerPort::Tcp(22),
}
}
mod error_construction {
use super::*;
#[test]
fn it_should_create_dockerfile_not_found_error() {
let path = PathBuf::from("/path/to/dockerfile");
let error = SshServerError::DockerfileNotFound {
expected_path: path.clone(),
};
let message = error.to_string();
assert!(message.contains("/path/to/dockerfile"));
assert!(message.contains("Tip:"));
}
#[test]
fn it_should_create_docker_build_failed_error() {
let error = SshServerError::DockerBuildFailed {
image_name: "test-image".to_string(),
image_tag: "latest".to_string(),
dockerfile_dir: "/path/to/dockerfile".to_string(),
stdout: "Building...".to_string(),
stderr: "Error: base image not found".to_string(),
};
let message = error.to_string();
assert!(message.contains("test-image:latest"));
assert!(message.contains("Tip:"));
}
#[test]
fn it_should_create_container_start_failed_error() {
let source = create_testcontainers_error();
let error = SshServerError::ContainerStartFailed { source };
let message = error.to_string();
assert!(message.contains("Failed to start SSH server container"));
assert!(message.contains("Tip:"));
}
#[test]
fn it_should_create_port_mapping_failed_error() {
let source = create_testcontainers_error();
let error = SshServerError::PortMappingFailed { source };
let message = error.to_string();
assert!(message.contains("Failed to get mapped port"));
assert!(message.contains("Tip:"));
}
#[test]
fn it_should_create_docker_command_failed_error() {
let source = io::Error::new(io::ErrorKind::NotFound, "docker not found");
let error = SshServerError::DockerCommandFailed {
command: "docker build".to_string(),
source,
};
let message = error.to_string();
assert!(message.contains("docker build"));
assert!(message.contains("Tip:"));
}
#[test]
fn it_should_create_invalid_utf8_in_path_error() {
let error = SshServerError::InvalidUtf8InPath {
path: "/invalid/path/��".to_string(),
};
let message = error.to_string();
assert!(message.contains("Invalid UTF-8"));
assert!(message.contains("Tip:"));
}
#[test]
fn it_should_create_docker_client_error() {
let docker_error = DockerError::BuildFailed {
image: "test:latest".to_string(),
source: crate::shared::command::CommandError::ExecutionFailed {
command: "docker".to_string(),
exit_code: "1".to_string(),
stdout: String::new(),
stderr: "Build failed".to_string(),
},
};
let error = SshServerError::DockerClientError {
source: Box::new(docker_error),
};
let message = error.to_string();
assert!(message.contains("Docker client operation failed"));
assert!(message.contains("Tip:"));
}
}
mod error_messages {
use super::*;
#[test]
fn it_should_include_tip_in_all_error_messages() {
let errors = vec![
SshServerError::DockerfileNotFound {
expected_path: PathBuf::from("/test"),
},
SshServerError::DockerBuildFailed {
image_name: "test".to_string(),
image_tag: "latest".to_string(),
dockerfile_dir: "/test".to_string(),
stdout: String::new(),
stderr: String::new(),
},
SshServerError::ContainerStartFailed {
source: create_testcontainers_error(),
},
SshServerError::PortMappingFailed {
source: create_testcontainers_error(),
},
SshServerError::DockerCommandFailed {
command: "test".to_string(),
source: io::Error::other("test"),
},
SshServerError::InvalidUtf8InPath {
path: "test".to_string(),
},
SshServerError::DockerClientError {
source: Box::new(DockerError::BuildFailed {
image: "test:latest".to_string(),
source: crate::shared::command::CommandError::ExecutionFailed {
command: "docker".to_string(),
exit_code: "1".to_string(),
stdout: String::new(),
stderr: "error".to_string(),
},
}),
},
];
for error in errors {
let message = error.to_string();
assert!(
message.contains("Tip:"),
"Error message should contain tip: {message}"
);
}
}
#[test]
fn it_should_provide_clear_context_in_error_messages() {
// DockerfileNotFound should include path
let error = SshServerError::DockerfileNotFound {
expected_path: PathBuf::from("/custom/path/Dockerfile"),
};
assert!(error.to_string().contains("/custom/path/Dockerfile"));
// DockerBuildFailed should include image name and tag
let error = SshServerError::DockerBuildFailed {
image_name: "my-image".to_string(),
image_tag: "v1.0".to_string(),
dockerfile_dir: "/path".to_string(),
stdout: String::new(),
stderr: String::new(),
};
let message = error.to_string();
assert!(message.contains("my-image:v1.0"));
// DockerCommandFailed should include command
let error = SshServerError::DockerCommandFailed {
command: "docker ps -a".to_string(),
source: io::Error::other("test"),
};
assert!(error.to_string().contains("docker ps -a"));
}
}
mod help_methods {
use super::*;
#[test]
fn it_should_provide_help_for_all_error_variants() {
let errors = vec![
SshServerError::DockerfileNotFound {
expected_path: PathBuf::from("/test"),
},
SshServerError::DockerBuildFailed {
image_name: "test".to_string(),
image_tag: "latest".to_string(),
dockerfile_dir: "/test".to_string(),
stdout: String::new(),
stderr: String::new(),
},
SshServerError::ContainerStartFailed {
source: create_testcontainers_error(),
},
SshServerError::PortMappingFailed {
source: create_testcontainers_error(),
},
SshServerError::DockerCommandFailed {
command: "test".to_string(),
source: io::Error::other("test"),
},
SshServerError::InvalidUtf8InPath {
path: "test".to_string(),
},
SshServerError::DockerClientError {
source: Box::new(DockerError::BuildFailed {
image: "test:latest".to_string(),
source: crate::shared::command::CommandError::ExecutionFailed {
command: "docker".to_string(),
exit_code: "1".to_string(),
stdout: String::new(),
stderr: "error".to_string(),
},
}),
},
];
for error in errors {
let help = error.help();
assert!(
!help.is_empty(),
"Help should not be empty for error: {error}"
);
assert!(
help.contains("Troubleshooting"),
"Help should contain troubleshooting section for error: {error}"
);
}
}
#[test]
fn it_should_include_platform_specific_commands_in_help() {
// Check that help includes platform-specific commands
let error = SshServerError::ContainerStartFailed {
source: create_testcontainers_error(),
};
let help = error.help();
// Should include both netstat and ss (Linux alternatives)
assert!(help.contains("netstat") || help.contains("ss"));
}
#[test]
fn it_should_include_actionable_steps_in_help() {
let error = SshServerError::DockerBuildFailed {
image_name: "test".to_string(),
image_tag: "latest".to_string(),
dockerfile_dir: "/test".to_string(),
stdout: String::new(),
stderr: String::new(),
};
let help = error.help();
// Should include numbered steps
assert!(help.contains("1."));
assert!(help.contains("2."));
// Should include specific commands
assert!(help.contains("docker"));
}
}
mod error_source_chaining {
use super::*;
#[test]
fn it_should_preserve_source_error_for_container_start_failed() {
let source = create_testcontainers_error();
let error = SshServerError::ContainerStartFailed { source };
// Should be able to access source through Error trait
assert!(
std::error::Error::source(&error).is_some(),
"ContainerStartFailed should preserve source error"
);
}
#[test]
fn it_should_preserve_source_error_for_port_mapping_failed() {
let source = create_testcontainers_error();
let error = SshServerError::PortMappingFailed { source };
assert!(
std::error::Error::source(&error).is_some(),
"PortMappingFailed should preserve source error"
);
}
#[test]
fn it_should_preserve_source_error_for_docker_command_failed() {
let source = io::Error::other("test error");
let error = SshServerError::DockerCommandFailed {
command: "test".to_string(),
source,
};
assert!(
std::error::Error::source(&error).is_some(),
"DockerCommandFailed should preserve source error"
);
}
#[test]
fn it_should_preserve_source_error_for_docker_client_error() {
let docker_error = DockerError::BuildFailed {
image: "test:latest".to_string(),
source: crate::shared::command::CommandError::ExecutionFailed {
command: "docker".to_string(),
exit_code: "1".to_string(),
stdout: String::new(),
stderr: "error".to_string(),
},
};
let error = SshServerError::DockerClientError {
source: Box::new(docker_error),
};
assert!(
std::error::Error::source(&error).is_some(),
"DockerClientError should preserve source error"
);
}
}
mod pattern_matching {
use super::*;
#[test]
fn it_should_enable_pattern_matching_on_error_variants() {
let error = SshServerError::DockerfileNotFound {
expected_path: PathBuf::from("/test"),
};
// Should be able to match on specific error variant
match error {
SshServerError::DockerfileNotFound { expected_path } => {
assert_eq!(expected_path, PathBuf::from("/test"));
}
_ => panic!("Should match DockerfileNotFound variant"),
}
}
#[test]
fn it_should_extract_error_context_through_pattern_matching() {
let error = SshServerError::DockerBuildFailed {
image_name: "my-image".to_string(),
image_tag: "v2.0".to_string(),
dockerfile_dir: "/custom/path".to_string(),
stdout: "build output".to_string(),
stderr: "build errors".to_string(),
};
match error {
SshServerError::DockerBuildFailed {
image_name,
image_tag,
dockerfile_dir,
stdout,
stderr,
} => {
assert_eq!(image_name, "my-image");
assert_eq!(image_tag, "v2.0");
assert_eq!(dockerfile_dir, "/custom/path");
assert_eq!(stdout, "build output");
assert_eq!(stderr, "build errors");
}
_ => panic!("Should match DockerBuildFailed variant"),
}
}
}
mod error_display {
use super::*;
#[test]
fn it_should_implement_debug_trait() {
let error = SshServerError::DockerfileNotFound {
expected_path: PathBuf::from("/test"),
};
let debug_output = format!("{error:?}");
assert!(!debug_output.is_empty());
assert!(debug_output.contains("DockerfileNotFound"));
}
#[test]
fn it_should_implement_display_trait() {
let error = SshServerError::InvalidUtf8InPath {
path: "/test".to_string(),
};
let display_output = format!("{error}");
assert!(!display_output.is_empty());
assert!(display_output.contains("Invalid UTF-8"));
}
}
}