-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage_builder.rs
More file actions
583 lines (525 loc) · 18.5 KB
/
Copy pathimage_builder.rs
File metadata and controls
583 lines (525 loc) · 18.5 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
//! Container Image Builder for E2E Testing Containers
//!
//! This module provides a builder pattern for constructing and building container images
//! used in E2E testing scenarios. It separates the container image building logic from
//! the container lifecycle management, following the Single Responsibility Principle.
//!
//! ## Design
//!
//! The builder follows the standard Rust builder pattern with method chaining:
//!
//! ```rust,no_run
//! use torrust_tracker_deployer_lib::testing::e2e::containers::ContainerImageBuilder;
//! use std::path::PathBuf;
//! use std::time::Duration;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let builder = ContainerImageBuilder::new()
//! .with_name("torrust-provisioned-instance")
//! .with_tag("latest")
//! .with_dockerfile(PathBuf::from("docker/provisioned-instance/Dockerfile"))
//! .with_context(PathBuf::from("."))
//! .with_build_timeout(Duration::from_secs(300));
//!
//! builder.build()?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Error Handling
//!
//! The module provides specific error types for container build operations:
//! - `ContainerBuildExecution` - Command execution failures
//! - `ContainerBuildFailed` - Build process failures with stderr output
//! - `ImageNameRequired` - Image name not provided (use `with_name()`)
//! - `DockerfilePathRequired` - Dockerfile path not provided (use `with_dockerfile()`)
//!
//! ## Required Configuration
//!
//! The following must be provided before calling `build()`:
//! - **Image name**: Must be set with `with_name()`
//! - **Dockerfile path**: Must be set with `with_dockerfile()`
//!
//! ## Default Configuration
//!
//! The builder provides sensible defaults for optional parameters:
//! - **Tag**: "latest"
//! - **Build context**: "." (current directory)
//! - **Build timeout**: 300 seconds
use std::path::PathBuf;
use std::process::Command;
use std::time::Duration;
use tracing::info;
/// Specific error types for container image building operations
#[derive(Debug, thiserror::Error)]
pub enum ContainerBuildError {
/// Container build command execution failed
#[error("Failed to execute docker build command for image '{image_name}:{tag}' using dockerfile '{dockerfile_path}' in context '{context_path}': {source}")]
ContainerBuildExecution {
image_name: String,
tag: String,
dockerfile_path: String,
context_path: String,
#[source]
source: std::io::Error,
},
/// Container build process failed with non-zero exit code
#[error("Docker build failed for image '{image_name}:{tag}' using dockerfile '{dockerfile_path}' in context '{context_path}' after {build_duration_secs}s with stderr: {stderr}")]
ContainerBuildFailed {
image_name: String,
tag: String,
dockerfile_path: String,
context_path: String,
build_duration_secs: u64,
stderr: String,
},
/// Container build process timed out
#[error("Docker build timed out after {timeout_secs}s for image '{image_name}:{tag}' using dockerfile '{dockerfile_path}' in context '{context_path}'")]
ContainerBuildTimeout {
image_name: String,
tag: String,
dockerfile_path: String,
context_path: String,
timeout_secs: u64,
},
/// Required image name was not provided
#[error("Image name is required but was not provided")]
ImageNameRequired,
/// Required dockerfile path was not provided
#[error("Dockerfile path is required but was not provided")]
DockerfilePathRequired,
/// Dockerfile does not exist at the specified path
#[error("Dockerfile not found at path '{dockerfile_path}' (resolved to '{absolute_path}')")]
DockerfileNotFound {
dockerfile_path: String,
absolute_path: String,
},
/// Context path does not exist
#[error("Context path not found at '{context_path}' (resolved to '{absolute_path}')")]
ContextPathNotFound {
context_path: String,
absolute_path: String,
},
}
/// Result type alias for container build operations
pub type Result<T> = std::result::Result<T, Box<ContainerBuildError>>;
/// Builder for constructing and building container images
///
/// This builder follows the standard Rust builder pattern, allowing
/// method chaining to configure container image build parameters.
///
/// # Required Values
///
/// The following values must be provided before calling `build()`:
/// - **Image name**: Must be set with `with_name()`
/// - **Dockerfile path**: Must be set with `with_dockerfile()`
///
/// # Default Values
///
/// - **Tag**: "latest"
/// - **Build context**: "." (current directory)
/// - **Build timeout**: 300 seconds
#[derive(Debug, Clone)]
pub struct ContainerImageBuilder {
image_name: Option<String>,
tag: String,
dockerfile_path: Option<PathBuf>,
context_path: PathBuf,
build_timeout: Duration,
}
impl ContainerImageBuilder {
/// Create a new container image builder with default configuration
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::e2e::containers::ContainerImageBuilder;
///
/// let builder = ContainerImageBuilder::new();
/// ```
#[must_use]
pub fn new() -> Self {
Self {
image_name: None,
tag: "latest".to_string(),
dockerfile_path: None,
context_path: PathBuf::from("."),
build_timeout: Duration::from_secs(300),
}
}
/// Set the container image name
///
/// # Arguments
///
/// * `name` - The container image name
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::e2e::containers::ContainerImageBuilder;
///
/// let builder = ContainerImageBuilder::new()
/// .with_name("my-custom-image");
/// ```
#[must_use]
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.image_name = Some(name.into());
self
}
/// Set the container image tag
///
/// # Arguments
///
/// * `tag` - The container image tag
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::e2e::containers::ContainerImageBuilder;
///
/// let builder = ContainerImageBuilder::new()
/// .with_tag("v1.0.0");
/// ```
#[must_use]
pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
self.tag = tag.into();
self
}
/// Set the path to the Dockerfile
///
/// # Arguments
///
/// * `path` - Path to the Dockerfile
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::e2e::containers::ContainerImageBuilder;
/// use std::path::PathBuf;
///
/// let builder = ContainerImageBuilder::new()
/// .with_dockerfile(PathBuf::from("custom/Dockerfile"));
/// ```
#[must_use]
pub fn with_dockerfile(mut self, path: PathBuf) -> Self {
self.dockerfile_path = Some(path);
self
}
/// Set the Docker build context path
///
/// # Arguments
///
/// * `path` - Path to the build context directory
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::e2e::containers::ContainerImageBuilder;
/// use std::path::PathBuf;
///
/// let builder = ContainerImageBuilder::new()
/// .with_context(PathBuf::from("./app"));
/// ```
#[must_use]
pub fn with_context(mut self, path: PathBuf) -> Self {
self.context_path = path;
self
}
/// Set the build timeout duration
///
/// # Arguments
///
/// * `timeout` - Maximum time to wait for build completion
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::e2e::containers::ContainerImageBuilder;
/// use std::time::Duration;
///
/// let builder = ContainerImageBuilder::new()
/// .with_build_timeout(Duration::from_secs(600));
/// ```
#[must_use]
pub fn with_build_timeout(mut self, timeout: Duration) -> Self {
self.build_timeout = timeout;
self
}
/// Build the container image using the configured parameters
///
/// This method executes the `docker build` command with the configured
/// parameters. It provides detailed error information if the build fails.
///
/// # Errors
///
/// Returns an error if:
/// - Image name was not provided (use `with_name()`)
/// - Dockerfile path was not provided (use `with_dockerfile()`)
/// - Docker command cannot be executed (e.g., Docker not installed)
/// - Docker build process fails (non-zero exit code)
///
/// # Examples
///
/// ```rust,no_run
/// use torrust_tracker_deployer_lib::testing::e2e::containers::ContainerImageBuilder;
/// use std::path::PathBuf;
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let builder = ContainerImageBuilder::new()
/// .with_name("my-image")
/// .with_dockerfile(PathBuf::from("Dockerfile"));
/// builder.build()?;
/// # Ok(())
/// # }
/// ```
pub fn build(&self) -> Result<()> {
let image_name = self
.image_name
.as_ref()
.ok_or_else(|| Box::new(ContainerBuildError::ImageNameRequired))?;
let dockerfile_path = self
.dockerfile_path
.as_ref()
.ok_or_else(|| Box::new(ContainerBuildError::DockerfilePathRequired))?;
let image_tag = format!("{}:{}", image_name, self.tag);
let dockerfile_path_str = dockerfile_path.display().to_string();
let context_path_str = self.context_path.display().to_string();
info!(
image_name = %image_name,
tag = %self.tag,
dockerfile = %dockerfile_path.display(),
context = %self.context_path.display(),
timeout_secs = self.build_timeout.as_secs(),
"Building Docker image"
);
let start_time = std::time::Instant::now();
let output = Command::new("docker")
.args([
"build",
"-t",
&image_tag,
"-f",
&dockerfile_path_str,
&context_path_str,
])
.output()
.map_err(|source| {
Box::new(ContainerBuildError::ContainerBuildExecution {
image_name: image_name.clone(),
tag: self.tag.clone(),
dockerfile_path: dockerfile_path_str.clone(),
context_path: context_path_str.clone(),
source,
})
})?;
let build_duration = start_time.elapsed();
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(Box::new(ContainerBuildError::ContainerBuildFailed {
image_name: image_name.clone(),
tag: self.tag.clone(),
dockerfile_path: dockerfile_path_str,
context_path: context_path_str,
build_duration_secs: build_duration.as_secs(),
stderr: stderr.to_string(),
}));
}
info!(
image_name = %image_name,
tag = %self.tag,
build_duration_ms = build_duration.as_millis(),
"Docker image built successfully"
);
Ok(())
}
/// Get the full image tag (name:tag) that will be used for the build
///
/// # Panics
///
/// Panics if image name has not been set. Use `with_name()` first.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::e2e::containers::ContainerImageBuilder;
///
/// let builder = ContainerImageBuilder::new()
/// .with_name("my-app")
/// .with_tag("v1.0");
///
/// assert_eq!(builder.image_tag(), "my-app:v1.0");
/// ```
#[must_use]
pub fn image_tag(&self) -> String {
let image_name = self
.image_name
.as_ref()
.expect("Image name must be set before calling image_tag()");
format!("{}:{}", image_name, self.tag)
}
/// Get the image name if it has been set
#[must_use]
pub fn image_name(&self) -> Option<&str> {
self.image_name.as_deref()
}
/// Get the image tag
#[must_use]
pub fn tag(&self) -> &str {
&self.tag
}
/// Get the dockerfile path if it has been set
#[must_use]
pub fn dockerfile_path(&self) -> Option<&PathBuf> {
self.dockerfile_path.as_ref()
}
/// Get the build context path
#[must_use]
pub fn context_path(&self) -> &PathBuf {
&self.context_path
}
/// Get the build timeout
#[must_use]
pub fn build_timeout(&self) -> Duration {
self.build_timeout
}
}
impl Default for ContainerImageBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
#[test]
fn it_should_create_builder_with_default_values() {
let builder = ContainerImageBuilder::new();
assert_eq!(builder.image_name(), None);
assert_eq!(builder.tag(), "latest");
assert_eq!(builder.dockerfile_path(), None);
assert_eq!(builder.context_path(), &PathBuf::from("."));
assert_eq!(builder.build_timeout(), Duration::from_secs(300));
}
#[test]
fn it_should_create_default_builder() {
let builder = ContainerImageBuilder::default();
assert_eq!(builder.image_name(), None);
assert_eq!(builder.tag(), "latest");
}
#[test]
fn it_should_configure_image_name() {
let builder = ContainerImageBuilder::new().with_name("custom-image");
assert_eq!(builder.image_name(), Some("custom-image"));
assert_eq!(builder.image_tag(), "custom-image:latest");
}
#[test]
fn it_should_configure_image_tag() {
let builder = ContainerImageBuilder::new()
.with_name("test-image")
.with_tag("v1.2.3");
assert_eq!(builder.tag(), "v1.2.3");
assert_eq!(builder.image_tag(), "test-image:v1.2.3");
}
#[test]
fn it_should_configure_dockerfile_path() {
let dockerfile_path = PathBuf::from("custom/Dockerfile");
let builder = ContainerImageBuilder::new().with_dockerfile(dockerfile_path.clone());
assert_eq!(builder.dockerfile_path(), Some(&dockerfile_path));
}
#[test]
fn it_should_configure_context_path() {
let context_path = PathBuf::from("./app");
let builder = ContainerImageBuilder::new().with_context(context_path.clone());
assert_eq!(builder.context_path(), &context_path);
}
#[test]
fn it_should_configure_build_timeout() {
let timeout = Duration::from_secs(600);
let builder = ContainerImageBuilder::new().with_build_timeout(timeout);
assert_eq!(builder.build_timeout(), timeout);
}
#[test]
fn it_should_chain_configuration_methods() {
let builder = ContainerImageBuilder::new()
.with_name("my-app")
.with_tag("v2.0")
.with_dockerfile(PathBuf::from("custom/Dockerfile"))
.with_context(PathBuf::from("./src"))
.with_build_timeout(Duration::from_secs(900));
assert_eq!(builder.image_name(), Some("my-app"));
assert_eq!(builder.tag(), "v2.0");
assert_eq!(builder.image_tag(), "my-app:v2.0");
assert_eq!(
builder.dockerfile_path(),
Some(&PathBuf::from("custom/Dockerfile"))
);
assert_eq!(builder.context_path(), &PathBuf::from("./src"));
assert_eq!(builder.build_timeout(), Duration::from_secs(900));
}
#[test]
fn it_should_have_proper_error_display_messages() {
let error = ContainerBuildError::ContainerBuildFailed {
image_name: "test-image".to_string(),
tag: "v1.0".to_string(),
dockerfile_path: "/path/to/Dockerfile".to_string(),
context_path: "/build/context".to_string(),
build_duration_secs: 120,
stderr: "build error message".to_string(),
};
let message = error.to_string();
assert!(message.contains("Docker build failed"));
assert!(message.contains("test-image:v1.0"));
assert!(message.contains("build error message"));
assert!(message.contains("/path/to/Dockerfile"));
assert!(message.contains("/build/context"));
assert!(message.contains("120s"));
}
#[test]
fn it_should_preserve_error_chain_for_docker_build_execution() {
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "docker not found");
let error = ContainerBuildError::ContainerBuildExecution {
image_name: "test-image".to_string(),
tag: "v1.0".to_string(),
dockerfile_path: "/path/to/Dockerfile".to_string(),
context_path: "/build/context".to_string(),
source: io_error,
};
assert!(error
.to_string()
.contains("Failed to execute docker build command"));
assert!(error.to_string().contains("test-image:v1.0"));
assert!(error.to_string().contains("/path/to/Dockerfile"));
assert!(error.to_string().contains("/build/context"));
assert!(error.source().is_some());
}
#[test]
fn it_should_fail_build_when_image_name_not_provided() {
let builder = ContainerImageBuilder::new().with_dockerfile(PathBuf::from("Dockerfile"));
let result = builder.build();
assert!(result.is_err());
let error = result.unwrap_err();
assert!(matches!(*error, ContainerBuildError::ImageNameRequired));
assert!(error.to_string().contains("Image name is required"));
}
#[test]
fn it_should_fail_build_when_dockerfile_path_not_provided() {
let builder = ContainerImageBuilder::new().with_name("test-image");
let result = builder.build();
assert!(result.is_err());
let error = result.unwrap_err();
assert!(matches!(
*error,
ContainerBuildError::DockerfilePathRequired
));
assert!(error.to_string().contains("Dockerfile path is required"));
}
#[test]
#[should_panic(expected = "Image name must be set before calling image_tag()")]
fn it_should_panic_when_calling_image_tag_without_image_name() {
let builder = ContainerImageBuilder::new();
drop(builder.image_tag());
}
// Note: Actual docker build integration tests would require Docker
// and are better suited for the e2e test binaries
}