-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemplate.rs
More file actions
162 lines (135 loc) · 5.4 KB
/
Copy pathtemplate.rs
File metadata and controls
162 lines (135 loc) · 5.4 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
//! Template Generation Tests
//!
//! This module tests the template generation workflow for creating
//! configuration file templates with placeholder values.
use crate::bootstrap::Container;
use crate::domain::provider::Provider;
use crate::presentation::controllers::create;
use crate::presentation::controllers::tests::TestContext;
use crate::presentation::dispatch::ExecutionContext;
use crate::presentation::input::cli::CreateAction;
use crate::presentation::views::VerbosityLevel;
#[tokio::test]
async fn it_should_generate_template_with_default_path() {
let test_context = TestContext::new();
// Change to temp directory so template is created there
let original_dir = std::env::current_dir().unwrap();
std::env::set_current_dir(test_context.working_dir()).unwrap();
let action = CreateAction::Template {
output_path: None,
provider: Provider::Lxd,
};
let container = Container::new(VerbosityLevel::Silent, test_context.working_dir());
let context = ExecutionContext::new(std::sync::Arc::new(container));
let result = create::route_command(action, test_context.working_dir(), &context).await;
// Restore original directory
std::env::set_current_dir(original_dir).unwrap();
assert!(
result.is_ok(),
"Template generation should succeed: {:?}",
result.err()
);
// Verify file exists at default path
let template_path = test_context.working_dir().join("environment-template.json");
assert!(
template_path.exists(),
"Template file should be created at: {}",
template_path.display()
);
// Verify file content is valid JSON
let file_content = std::fs::read_to_string(&template_path).unwrap();
let parsed: serde_json::Value =
serde_json::from_str(&file_content).expect("Template should be valid JSON");
// Verify structure
assert!(parsed["environment"]["name"].is_string());
assert!(parsed["ssh_credentials"]["private_key_path"].is_string());
assert_eq!(parsed["ssh_credentials"]["username"], "torrust");
assert_eq!(parsed["ssh_credentials"]["port"], 22);
}
#[tokio::test]
async fn it_should_generate_template_with_custom_path() {
let test_context = TestContext::new();
let custom_path = test_context
.working_dir()
.join("config")
.join("my-env.json");
let action = CreateAction::Template {
output_path: Some(custom_path.clone()),
provider: Provider::Lxd,
};
let container = Container::new(VerbosityLevel::Silent, test_context.working_dir());
let context = ExecutionContext::new(std::sync::Arc::new(container));
let result = create::route_command(action, test_context.working_dir(), &context).await;
assert!(result.is_ok(), "Template generation should succeed");
// Verify file exists at custom path
assert!(
custom_path.exists(),
"Template file should be created at custom path: {}",
custom_path.display()
);
// Verify parent directory was created
assert!(custom_path.parent().unwrap().exists());
}
#[tokio::test]
async fn it_should_generate_valid_json_template() {
let test_context = TestContext::new();
let template_path = test_context.working_dir().join("test.json");
let action = CreateAction::Template {
output_path: Some(template_path.clone()),
provider: Provider::Lxd,
};
let container = Container::new(VerbosityLevel::Silent, test_context.working_dir());
let context = ExecutionContext::new(std::sync::Arc::new(container));
create::route_command(action, test_context.working_dir(), &context)
.await
.unwrap();
// Read and parse the generated template
let file_content = std::fs::read_to_string(&template_path).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&file_content).unwrap();
// Verify structure matches expectations
assert!(parsed.is_object());
assert!(parsed["environment"].is_object());
assert!(parsed["ssh_credentials"].is_object());
// Verify placeholder values
assert_eq!(
parsed["environment"]["name"],
"REPLACE_WITH_ENVIRONMENT_NAME"
);
assert_eq!(
parsed["ssh_credentials"]["private_key_path"],
"REPLACE_WITH_SSH_PRIVATE_KEY_ABSOLUTE_PATH"
);
assert_eq!(
parsed["ssh_credentials"]["public_key_path"],
"REPLACE_WITH_SSH_PUBLIC_KEY_ABSOLUTE_PATH"
);
// Verify default values
assert_eq!(parsed["ssh_credentials"]["username"], "torrust");
assert_eq!(parsed["ssh_credentials"]["port"], 22);
}
#[tokio::test]
async fn it_should_create_parent_directories() {
let test_context = TestContext::new();
let deep_path = test_context
.working_dir()
.join("a")
.join("b")
.join("c")
.join("template.json");
let action = CreateAction::Template {
output_path: Some(deep_path.clone()),
provider: Provider::Lxd,
};
let container = Container::new(VerbosityLevel::Silent, test_context.working_dir());
let context = ExecutionContext::new(std::sync::Arc::new(container));
let result = create::route_command(action, test_context.working_dir(), &context).await;
assert!(result.is_ok(), "Should create parent directories");
assert!(
deep_path.exists(),
"Template should be created at deep path"
);
assert!(
deep_path.parent().unwrap().exists(),
"Parent directories should exist"
);
}