-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtests.rs
More file actions
301 lines (270 loc) · 9.39 KB
/
Copy pathtests.rs
File metadata and controls
301 lines (270 loc) · 9.39 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
//! Tests for Environment Creation Handler
//!
//! This module contains comprehensive tests for the environment creation
//! functionality, including integration tests and unit tests for helper functions.
use std::fs;
use std::path::Path;
use std::sync::Arc;
use tempfile::TempDir;
use super::errors::CreateEnvironmentCommandError;
use crate::bootstrap::Container;
use crate::presentation::dispatch::ExecutionContext;
use crate::presentation::views::VerbosityLevel;
fn create_test_context(working_dir: &Path) -> ExecutionContext {
let container = Container::new(VerbosityLevel::Silent, working_dir);
ExecutionContext::new(Arc::new(container))
}
#[tokio::test]
async fn it_should_create_environment_from_valid_config() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("config.json");
// Use absolute paths to SSH keys to ensure they work regardless of current directory
let project_root = env!("CARGO_MANIFEST_DIR");
let private_key_path = format!("{project_root}/fixtures/testing_rsa");
let public_key_path = format!("{project_root}/fixtures/testing_rsa.pub");
// Write a valid configuration file
let config_json = format!(
r#"{{
"environment": {{
"name": "test-create-env"
}},
"ssh_credentials": {{
"private_key_path": "{private_key_path}",
"public_key_path": "{public_key_path}"
}},
"provider": {{
"provider": "lxd",
"profile_name": "lxd-test-create-env"
}},
"tracker": {{
"core": {{
"database": {{
"driver": "sqlite3",
"database_name": "tracker.db"
}},
"private": false
}},
"udp_trackers": [
{{
"bind_address": "0.0.0.0:6969"
}}
],
"http_trackers": [
{{
"bind_address": "0.0.0.0:7070"
}}
],
"http_api": {{
"bind_address": "0.0.0.0:1212",
"admin_token": "MyAccessToken"
}},
"health_check_api": {{
"bind_address": "127.0.0.1:1313"
}}
}}
}}"#
);
fs::write(&config_path, config_json).unwrap();
let working_dir = temp_dir.path();
let context = create_test_context(working_dir);
let result = context
.container()
.create_environment_controller()
.execute(&config_path, working_dir)
.await;
assert!(
result.is_ok(),
"Should successfully create environment: {:?}",
result.err()
);
// Verify environment state file was created by repository
// Repository creates: <base_dir>/data/{env-name}/environment.json
let env_state_file = working_dir.join("data/test-create-env/environment.json");
assert!(
env_state_file.exists(),
"Environment state file should be created at: {}",
env_state_file.display()
);
}
#[tokio::test]
async fn it_should_return_error_for_missing_config_file() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("nonexistent.json");
let working_dir = temp_dir.path();
let context = create_test_context(working_dir);
let result = context
.container()
.create_environment_controller()
.execute(&config_path, working_dir)
.await;
assert!(result.is_err());
match result.unwrap_err() {
CreateEnvironmentCommandError::ConfigFileNotFound { path } => {
assert_eq!(path, config_path);
}
other => panic!("Expected ConfigFileNotFound, got: {other:?}"),
}
}
#[tokio::test]
async fn it_should_return_error_for_invalid_json() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("invalid.json");
// Write invalid JSON
fs::write(&config_path, r#"{"invalid json"#).unwrap();
let working_dir = temp_dir.path();
let context = create_test_context(working_dir);
let result = context
.container()
.create_environment_controller()
.execute(&config_path, working_dir)
.await;
assert!(result.is_err());
match result.unwrap_err() {
CreateEnvironmentCommandError::ConfigParsingFailed { .. } => {
// Expected
}
other => panic!("Expected ConfigParsingFailed, got: {other:?}"),
}
}
#[tokio::test]
async fn it_should_return_error_for_duplicate_environment() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("config.json");
// Use absolute paths to SSH keys to ensure they work regardless of current directory
let project_root = env!("CARGO_MANIFEST_DIR");
let private_key_path = format!("{project_root}/fixtures/testing_rsa");
let public_key_path = format!("{project_root}/fixtures/testing_rsa.pub");
let config_json = format!(
r#"{{
"environment": {{
"name": "duplicate-env"
}},
"ssh_credentials": {{
"private_key_path": "{private_key_path}",
"public_key_path": "{public_key_path}"
}},
"provider": {{
"provider": "lxd",
"profile_name": "lxd-duplicate-env"
}},
"tracker": {{
"core": {{
"database": {{
"driver": "sqlite3",
"database_name": "tracker.db"
}},
"private": false
}},
"udp_trackers": [
{{
"bind_address": "0.0.0.0:6969"
}}
],
"http_trackers": [
{{
"bind_address": "0.0.0.0:7070"
}}
],
"http_api": {{
"bind_address": "0.0.0.0:1212",
"admin_token": "MyAccessToken"
}},
"health_check_api": {{
"bind_address": "127.0.0.1:1313"
}}
}}
}}"#
);
fs::write(&config_path, config_json).unwrap();
let working_dir = temp_dir.path();
let context = create_test_context(working_dir);
// Create environment first time
let result1 = context
.container()
.create_environment_controller()
.execute(&config_path, working_dir)
.await;
assert!(result1.is_ok(), "First create should succeed");
// Try to create same environment again (use new context to avoid any state issues)
let context2 = create_test_context(working_dir);
let result2 = context2
.container()
.create_environment_controller()
.execute(&config_path, working_dir)
.await;
assert!(result2.is_err(), "Second create should fail");
match result2.unwrap_err() {
CreateEnvironmentCommandError::CommandFailed { .. } => {
// Expected - environment already exists
}
other => panic!("Expected CommandFailed, got: {other:?}"),
}
}
#[tokio::test]
async fn it_should_create_environment_in_custom_working_dir() {
let temp_dir = TempDir::new().unwrap();
let custom_working_dir = temp_dir.path().join("custom");
fs::create_dir(&custom_working_dir).unwrap();
let config_path = temp_dir.path().join("config.json");
// Use absolute paths to SSH keys to ensure they work regardless of current directory
let project_root = env!("CARGO_MANIFEST_DIR");
let private_key_path = format!("{project_root}/fixtures/testing_rsa");
let public_key_path = format!("{project_root}/fixtures/testing_rsa.pub");
let config_json = format!(
r#"{{
"environment": {{
"name": "custom-location-env"
}},
"ssh_credentials": {{
"private_key_path": "{private_key_path}",
"public_key_path": "{public_key_path}"
}},
"provider": {{
"provider": "lxd",
"profile_name": "lxd-custom-location-env"
}},
"tracker": {{
"core": {{
"database": {{
"driver": "sqlite3",
"database_name": "tracker.db"
}},
"private": false
}},
"udp_trackers": [
{{
"bind_address": "0.0.0.0:6969"
}}
],
"http_trackers": [
{{
"bind_address": "0.0.0.0:7070"
}}
],
"http_api": {{
"bind_address": "0.0.0.0:1212",
"admin_token": "MyAccessToken"
}},
"health_check_api": {{
"bind_address": "127.0.0.1:1313"
}}
}}
}}"#
);
fs::write(&config_path, config_json).unwrap();
let context = create_test_context(&custom_working_dir);
let result = context
.container()
.create_environment_controller()
.execute(&config_path, &custom_working_dir)
.await;
assert!(result.is_ok(), "Should create in custom working dir");
// Verify environment was created in custom location
// Repository creates: <base_dir>/data/{env-name}/environment.json
let env_state_file = custom_working_dir.join("data/custom-location-env/environment.json");
assert!(
env_state_file.exists(),
"Environment state should be in custom working directory at: {}",
env_state_file.display()
);
}