-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrepository_factory.rs
More file actions
182 lines (157 loc) · 6.54 KB
/
Copy pathrepository_factory.rs
File metadata and controls
182 lines (157 loc) · 6.54 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
//! Repository factory for creating environment-specific repositories
//!
//! This module provides a factory pattern for creating `FileEnvironmentRepository`
//! instances with consistent configuration (like lock timeouts) but environment-specific
//! data directories.
//!
//! # Design Rationale
//!
//! The repository needs environment-specific data directories that are only known at runtime
//! (e.g., `data/production/`, `data/staging/`). However, configuration like lock timeout
//! is known at application startup. The factory pattern allows us to:
//!
//! 1. Configure the factory once at startup with compile-time known settings
//! 2. Create environment-specific repositories at runtime with the correct data directory
//!
//! # Usage
//!
//! ```rust,no_run
//! use std::time::Duration;
//! use std::path::PathBuf;
//! use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
//!
//! // Create factory at application startup
//! let factory = RepositoryFactory::new(Duration::from_secs(30));
//!
//! // Create environment-specific repository at runtime
//! let data_dir = PathBuf::from("data/production");
//! let repo = factory.create(data_dir);
//! ```
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use crate::domain::environment::repository::EnvironmentRepository;
use crate::infrastructure::persistence::filesystem::file_environment_repository::FileEnvironmentRepository;
/// Factory for creating `FileEnvironmentRepository` instances
///
/// The factory is configured once at application startup with settings like lock timeout,
/// then used to create environment-specific repositories at runtime.
#[derive(Clone)]
pub struct RepositoryFactory {
/// Lock acquisition timeout for all repositories created by this factory
lock_timeout: Duration,
}
impl RepositoryFactory {
/// Create a new repository factory with the specified lock timeout
///
/// # Arguments
///
/// * `lock_timeout` - Maximum time to wait for lock acquisition in created repositories
///
/// # Examples
///
/// ```rust
/// use std::time::Duration;
/// use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
///
/// let factory = RepositoryFactory::new(Duration::from_secs(30));
/// ```
#[must_use]
pub fn new(lock_timeout: Duration) -> Self {
Self { lock_timeout }
}
/// Create a new `FileEnvironmentRepository` for a specific data directory
///
/// # Arguments
///
/// * `data_dir` - Base directory for environment state files (e.g., `data/production`)
///
/// # Returns
///
/// An `Arc`-wrapped trait object that can be used as `Arc<dyn EnvironmentRepository>`
///
/// # Examples
///
/// ```rust
/// use std::time::Duration;
/// use std::path::PathBuf;
/// use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
///
/// let factory = RepositoryFactory::new(Duration::from_secs(30));
/// let repo = factory.create(PathBuf::from("data/production"));
/// ```
#[must_use]
pub fn create(&self, data_dir: PathBuf) -> Arc<dyn EnvironmentRepository> {
let repository =
FileEnvironmentRepository::new(data_dir).with_lock_timeout(self.lock_timeout);
Arc::new(repository)
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn it_should_create_repository_factory_with_timeout() {
let timeout = Duration::from_secs(30);
let factory = RepositoryFactory::new(timeout);
// Verify factory was created (we can't directly inspect timeout, but creation succeeds)
assert_eq!(factory.lock_timeout, timeout);
}
#[test]
fn it_should_create_repository_with_specific_data_dir() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let factory = RepositoryFactory::new(Duration::from_secs(10));
let data_dir = temp_dir.path().join("production");
let _repo = factory.create(data_dir);
// Repository creation should succeed (we just verify it doesn't panic)
}
#[test]
fn it_should_create_multiple_repositories_from_same_factory() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let factory = RepositoryFactory::new(Duration::from_secs(10));
let prod_dir = temp_dir.path().join("production");
let staging_dir = temp_dir.path().join("staging");
let _repo1 = factory.create(prod_dir);
let _repo2 = factory.create(staging_dir);
// Both repositories should be created successfully
}
#[test]
fn it_should_be_clonable() {
let factory = RepositoryFactory::new(Duration::from_secs(30));
let factory_clone = factory.clone();
assert_eq!(factory.lock_timeout, factory_clone.lock_timeout);
}
#[test]
fn it_should_create_repository_that_can_save_and_load_environment() {
use crate::adapters::ssh::SshCredentials;
#[allow(unused_imports)] // Needed for trait methods on Arc<dyn EnvironmentRepository>
use crate::domain::environment::repository::EnvironmentRepository;
use crate::domain::environment::{Environment, EnvironmentName};
use crate::shared::Username;
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let factory = RepositoryFactory::new(Duration::from_secs(10));
// Create an environment
let env_name =
EnvironmentName::new("test-env".to_string()).expect("Valid environment name");
let ssh_credentials = SshCredentials::new(
temp_dir.path().join("test_key"),
temp_dir.path().join("test_key.pub"),
Username::new("test_user").expect("Valid username"),
);
let environment = Environment::new(env_name.clone(), ssh_credentials, 22);
// Create repository with temp directory as base (not environment-specific directory)
// The repository will automatically create the environment subdirectory
let repo = factory.create(temp_dir.path().to_path_buf());
// Convert to AnyEnvironmentState for repository operations
let env_state = environment.into_any();
// Save the environment
repo.save(&env_state).expect("Should save successfully");
// Load it back
let loaded = repo
.load(&env_name)
.expect("Should load successfully")
.expect("Environment should exist");
assert_eq!(loaded.name(), &env_name);
}
}