-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathembedded.rs
More file actions
564 lines (466 loc) · 20.2 KB
/
Copy pathembedded.rs
File metadata and controls
564 lines (466 loc) · 20.2 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
//! Embedded template management system
//!
//! This module provides the `TemplateManager` which handles embedded template
//! resources and their extraction to the filesystem for use by the deployment
//! system. It uses `rust-embed` to bundle templates within the binary.
//!
//! ## Key Features
//!
//! - Embedded template resource management using `rust-embed`
//! - Template extraction to filesystem for processing
//! - Directory structure creation and management
//! - Template cleanup and reset functionality for testing
//! - Comprehensive error handling with detailed context
//!
//! ## Template Organization
//!
//! Templates are organized in the source tree under `templates/` and embedded
//! into the binary at compile time. The manager can extract these templates
//! to a working directory for use by template engines and deployment tools.
//!
//! ## Usage Scenarios
//!
//! - Initial template setup for deployment operations
//! - Template refresh for testing environments
//! - Development and debugging support with accessible template files
use rust_embed::RustEmbed;
use std::fs;
use std::path::{Path, PathBuf};
use thiserror::Error;
/// Errors that can occur during template manager operations
#[derive(Debug, Error)]
pub enum TemplateManagerError {
#[error("Failed to create templates directory: {path}")]
DirectoryCreation {
path: String,
#[source]
source: std::io::Error,
},
#[error("Template file not found in embedded resources: {relative_path}")]
TemplateNotFound { relative_path: String },
#[error("Invalid UTF-8 in embedded template: {relative_path}")]
InvalidUtf8 {
relative_path: String,
#[source]
source: std::str::Utf8Error,
},
#[error("Failed to create parent directory for template: {path}")]
ParentDirectoryCreation {
path: String,
#[source]
source: std::io::Error,
},
#[error("Failed to write template file: {path}")]
TemplateWrite {
path: String,
#[source]
source: std::io::Error,
},
#[error("Template file already exists: {path}")]
TemplateAlreadyExists { path: String },
}
/// Embedded template files from the ./templates directory
#[derive(RustEmbed)]
#[folder = "templates/"]
pub struct EmbeddedTemplates;
/// Template manager that handles on-demand creation of templates from embedded resources
pub struct TemplateManager {
templates_dir: PathBuf,
}
impl TemplateManager {
/// Create a new template manager with a custom templates directory
pub fn new<P: Into<PathBuf>>(templates_dir: P) -> Self {
Self {
templates_dir: templates_dir.into(),
}
}
/// Create the templates directory if it doesn't exist
///
/// # Errors
///
/// Returns an error if the directory creation fails due to permissions or filesystem issues.
pub fn ensure_templates_dir(&self) -> Result<(), TemplateManagerError> {
if !self.templates_dir.exists() {
fs::create_dir_all(&self.templates_dir).map_err(|source| {
TemplateManagerError::DirectoryCreation {
path: self.templates_dir.display().to_string(),
source,
}
})?;
}
Ok(())
}
/// Clean and prepare the templates directory to ensure fresh embedded templates
///
/// This method combines `clean_templates_dir()` and `ensure_templates_dir()` to provide
/// a clean state for template operations. It's particularly useful in testing and
/// development environments where you want to ensure fresh templates from embedded resources.
///
/// # Errors
///
/// Returns an error if either directory cleaning or creation fails due to permissions
/// or filesystem issues.
pub fn reset_templates_dir(&self) -> Result<(), TemplateManagerError> {
self.clean_templates_dir()?;
self.ensure_templates_dir()?;
Ok(())
}
/// Get the path to a template file, creating it from embedded resources if it doesn't exist
///
/// # Errors
///
/// Returns an error if:
/// - The template is not found in embedded resources
/// - The embedded template contains invalid UTF-8
/// - File system operations fail (directory creation or file writing)
pub fn get_template_path(&self, relative_path: &str) -> Result<PathBuf, TemplateManagerError> {
let template_path = self.templates_dir.join(relative_path);
// If the template file already exists, return its path
if template_path.exists() {
return Ok(template_path);
}
// Create the template from embedded resources
self.create_template_from_embedded(relative_path)?;
Ok(template_path)
}
/// Create a template file from embedded resources
fn create_template_from_embedded(
&self,
relative_path: &str,
) -> Result<(), TemplateManagerError> {
let template_path = self.templates_dir.join(relative_path);
// Check if template already exists - don't overwrite
if template_path.exists() {
return Err(TemplateManagerError::TemplateAlreadyExists {
path: template_path.display().to_string(),
});
}
// Get the embedded file content
let embedded_file = EmbeddedTemplates::get(relative_path).ok_or_else(|| {
TemplateManagerError::TemplateNotFound {
relative_path: relative_path.to_string(),
}
})?;
let content = std::str::from_utf8(&embedded_file.data).map_err(|source| {
TemplateManagerError::InvalidUtf8 {
relative_path: relative_path.to_string(),
source,
}
})?;
// Ensure parent directory exists
if let Some(parent) = template_path.parent() {
fs::create_dir_all(parent).map_err(|source| {
TemplateManagerError::ParentDirectoryCreation {
path: template_path.display().to_string(),
source,
}
})?;
}
// Write the content to the file
fs::write(&template_path, content).map_err(|source| {
TemplateManagerError::TemplateWrite {
path: template_path.display().to_string(),
source,
}
})?;
tracing::debug!("Created template from embedded resources: {relative_path}");
Ok(())
}
/// Get the templates directory path
#[must_use]
pub fn templates_dir(&self) -> &Path {
&self.templates_dir
}
/// Clean the templates directory by removing all files and subdirectories
///
/// This is useful for development/testing to ensure fresh templates are used
/// from embedded resources.
///
/// # Errors
///
/// Returns an error if the directory removal fails due to permissions or filesystem issues.
pub fn clean_templates_dir(&self) -> Result<(), TemplateManagerError> {
if self.templates_dir.exists() {
fs::remove_dir_all(&self.templates_dir).map_err(|source| {
TemplateManagerError::DirectoryCreation {
path: self.templates_dir.display().to_string(),
source,
}
})?;
tracing::debug!(
"Cleaned templates directory: {}",
self.templates_dir.display()
);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use tempfile::TempDir;
#[test]
fn it_should_create_templates_directory() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("test_templates");
let manager = TemplateManager::new(&templates_path);
// Directory should not exist initially
assert!(!templates_path.exists());
// After ensuring directory, it should exist
manager.ensure_templates_dir().unwrap();
assert!(templates_path.exists());
assert!(templates_path.is_dir());
}
#[test]
fn it_should_create_template_from_embedded_resources() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("test_templates");
let manager = TemplateManager::new(&templates_path);
manager.ensure_templates_dir().unwrap();
// Try to get a template path - this should create the file from embedded resources
let template_path = manager.get_template_path("ansible/ansible.cfg").unwrap();
// The file should now exist
assert!(template_path.exists());
assert!(template_path.is_file());
// The content should match what we expect (basic verification)
let content = fs::read_to_string(&template_path).unwrap();
assert!(!content.is_empty());
}
#[test]
fn it_should_fail_when_template_not_found_in_embedded_resources() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("test_templates");
let manager = TemplateManager::new(&templates_path);
manager.ensure_templates_dir().unwrap();
// Try to get a non-existent template path
let result = manager.get_template_path("non-existent/template.txt");
// This should fail with TemplateNotFound error
assert!(result.is_err());
let error = result.unwrap_err();
match error {
TemplateManagerError::TemplateNotFound { relative_path } => {
assert_eq!(relative_path, "non-existent/template.txt");
}
_ => panic!("Expected TemplateNotFound error, got: {error:?}"),
}
}
#[cfg(unix)]
#[test]
fn it_should_fail_when_directory_creation_is_denied() {
let temp_dir = TempDir::new().unwrap();
let read_only_dir = temp_dir.path().join("read_only");
fs::create_dir(&read_only_dir).unwrap();
// Make directory read-only
let mut perms = fs::metadata(&read_only_dir).unwrap().permissions();
perms.set_mode(0o444); // Read-only
fs::set_permissions(&read_only_dir, perms).unwrap();
let templates_path = read_only_dir.join("should_fail");
let manager = TemplateManager::new(&templates_path);
// This should fail with DirectoryCreation error
let result = manager.ensure_templates_dir();
assert!(result.is_err());
let error = result.unwrap_err();
match error {
TemplateManagerError::DirectoryCreation { path, source } => {
assert!(path.contains("should_fail"));
assert_eq!(source.kind(), std::io::ErrorKind::PermissionDenied);
}
_ => panic!("Expected DirectoryCreation error, got: {error:?}"),
}
// Restore permissions for cleanup
let mut perms = fs::metadata(&read_only_dir).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&read_only_dir, perms).unwrap();
}
#[cfg(unix)]
#[test]
fn it_should_fail_when_parent_directory_creation_is_denied() {
let temp_dir = TempDir::new().unwrap();
let read_only_dir = temp_dir.path().join("read_only");
fs::create_dir(&read_only_dir).unwrap();
// Make directory read-only
let mut perms = fs::metadata(&read_only_dir).unwrap().permissions();
perms.set_mode(0o444); // Read-only
fs::set_permissions(&read_only_dir, perms).unwrap();
let templates_path = temp_dir.path().join("templates");
let manager = TemplateManager::new(&templates_path);
manager.ensure_templates_dir().unwrap();
// Create a template path that requires parent directory creation inside read-only dir
// We'll need to temporarily modify the manager to use a path that will fail
let failing_manager = TemplateManager::new(&read_only_dir);
let result = failing_manager.get_template_path("some/nested/template.txt");
assert!(result.is_err());
let error = result.unwrap_err();
match error {
TemplateManagerError::TemplateNotFound { .. } => {
// This is expected since the template doesn't exist in embedded resources
// But let's test with a real template that exists
}
_ => panic!("Unexpected error: {error:?}"),
}
// Test with a real template that exists
let result = failing_manager.get_template_path("ansible/ansible.cfg");
assert!(result.is_err());
let error = result.unwrap_err();
match error {
TemplateManagerError::ParentDirectoryCreation { path, source } => {
assert!(path.contains("ansible.cfg"));
assert_eq!(source.kind(), std::io::ErrorKind::PermissionDenied);
}
_ => panic!("Expected ParentDirectoryCreation error, got: {error:?}"),
}
// Restore permissions for cleanup
let mut perms = fs::metadata(&read_only_dir).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&read_only_dir, perms).unwrap();
}
#[cfg(unix)]
#[test]
fn it_should_fail_when_template_write_is_denied() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("templates");
let manager = TemplateManager::new(&templates_path);
manager.ensure_templates_dir().unwrap();
// Create the ansible subdirectory
let ansible_dir = templates_path.join("ansible");
fs::create_dir(&ansible_dir).unwrap();
// Make the ansible directory read-only so file creation will fail
let mut perms = fs::metadata(&ansible_dir).unwrap().permissions();
perms.set_mode(0o444); // Read-only
fs::set_permissions(&ansible_dir, perms).unwrap();
// Try to get a template path - this should fail when trying to write the file
let result = manager.get_template_path("ansible/ansible.cfg");
assert!(result.is_err());
let error = result.unwrap_err();
match error {
TemplateManagerError::TemplateWrite { path, source } => {
assert!(path.contains("ansible.cfg"));
assert_eq!(source.kind(), std::io::ErrorKind::PermissionDenied);
}
_ => panic!("Expected TemplateWrite error, got: {error:?}"),
}
// Restore permissions for cleanup
let mut perms = fs::metadata(&ansible_dir).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&ansible_dir, perms).unwrap();
}
#[test]
fn it_should_return_existing_template_path_without_recreating() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("test_templates");
let manager = TemplateManager::new(&templates_path);
manager.ensure_templates_dir().unwrap();
// First call should create the template
let template_path1 = manager.get_template_path("ansible/ansible.cfg").unwrap();
assert!(template_path1.exists());
// Modify the file content to verify it's not recreated
let original_content = fs::read_to_string(&template_path1).unwrap();
fs::write(&template_path1, "modified content").unwrap();
// Second call should return the same path without recreating
let template_path2 = manager.get_template_path("ansible/ansible.cfg").unwrap();
assert_eq!(template_path1, template_path2);
// Content should still be modified, proving it wasn't recreated
let current_content = fs::read_to_string(&template_path2).unwrap();
assert_eq!(current_content, "modified content");
assert_ne!(current_content, original_content);
}
#[test]
fn it_should_provide_correct_templates_dir_path() {
let test_path = PathBuf::from("/test/path");
let manager = TemplateManager::new(&test_path);
assert_eq!(manager.templates_dir(), Path::new("/test/path"));
}
#[test]
fn it_should_handle_nested_template_paths() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("test_templates");
let manager = TemplateManager::new(&templates_path);
manager.ensure_templates_dir().unwrap();
// Test deeply nested template path
let template_path = manager.get_template_path("tofu/lxd/main.tf").unwrap();
assert!(template_path.exists());
assert!(template_path.is_file());
// Verify parent directories were created
assert!(template_path.parent().unwrap().exists());
assert!(template_path.parent().unwrap().parent().unwrap().exists());
}
#[test]
fn it_should_fail_when_trying_to_create_existing_template() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("test_templates");
let manager = TemplateManager::new(&templates_path);
manager.ensure_templates_dir().unwrap();
// Create a template first time
let first_path = manager.get_template_path("ansible/ansible.cfg").unwrap();
// Try to create the same template again through get_template_path - should succeed since it returns existing
let second_path = manager.get_template_path("ansible/ansible.cfg").unwrap();
// Both paths should be the same and the file should exist
assert_eq!(first_path, second_path);
assert!(second_path.exists());
}
#[test]
fn it_should_clean_templates_directory() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("test_templates");
let manager = TemplateManager::new(&templates_path);
manager.ensure_templates_dir().unwrap();
// Create some templates
let template_path1 = manager.get_template_path("ansible/ansible.cfg").unwrap();
let template_path2 = manager.get_template_path("tofu/lxd/main.tf").unwrap();
// Verify templates exist
assert!(template_path1.exists());
assert!(template_path2.exists());
assert!(templates_path.exists());
// Clean the directory
manager.clean_templates_dir().unwrap();
// Templates directory should be gone
assert!(!templates_path.exists());
assert!(!template_path1.exists());
assert!(!template_path2.exists());
}
#[test]
fn it_should_handle_clean_on_nonexistent_directory() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("nonexistent_templates");
let manager = TemplateManager::new(&templates_path);
// Clean should not fail on non-existent directory
let result = manager.clean_templates_dir();
assert!(result.is_ok());
}
#[test]
fn it_should_reset_templates_directory() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("test_templates");
let manager = TemplateManager::new(&templates_path);
// Initially the directory should not exist
assert!(!templates_path.exists());
// First, create the directory and some templates
manager.ensure_templates_dir().unwrap();
let template_path = manager.get_template_path("ansible/ansible.cfg").unwrap();
assert!(template_path.exists());
assert!(templates_path.exists());
// Now use the combined method
manager.reset_templates_dir().unwrap();
// Directory should exist but templates should be gone
assert!(templates_path.exists());
assert!(templates_path.is_dir());
// Old template file should be gone (directory was cleaned)
assert!(!template_path.exists());
}
#[test]
fn it_should_reset_templates_directory_on_nonexistent_directory() {
let temp_dir = TempDir::new().unwrap();
let templates_path = temp_dir.path().join("nonexistent_templates");
let manager = TemplateManager::new(&templates_path);
// Directory should not exist initially
assert!(!templates_path.exists());
// Combined method should work on non-existent directory
let result = manager.reset_templates_dir();
assert!(result.is_ok());
// Directory should now exist
assert!(templates_path.exists());
assert!(templates_path.is_dir());
}
}