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
//! End-to-end tests for SSH deployment
//! These tests use Docker to create a temporary environment for testing SSH deployment
#[cfg(test)]
mod tests {
use {
crate::utils::ssh_deploy::{
client::SshClient,
deploy::deploy_svm_node,
types::{AuthMethod, DeploymentConfig, NetworkType, ServerConfig},
},
std::collections::HashMap,
std::process::Command,
std::time::Duration,
tokio::time,
};
/// Setup a Docker container for testing
///
/// # Returns
/// * `Result<String, Box<dyn std::error::Error>>` - Container ID
async fn setup_docker_container() -> Result<String, Box<dyn std::error::Error>> {
// Pull Ubuntu image
let status = Command::new("docker")
.args(["pull", "ubuntu:20.04"])
.status()?;
if !status.success() {
return Err("Failed to pull Docker image".into());
}
// Start container with SSH
let output = Command::new("docker")
.args([
"run",
"-d",
"--privileged",
"-p", "2222:22",
"ubuntu:20.04",
"/bin/bash",
"-c",
"apt-get update && apt-get install -y openssh-server && \
mkdir -p /run/sshd && \
echo 'root:password' | chpasswd && \
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config && \
service ssh start && \
tail -f /dev/null"
])
.output()?;
if !output.status.success() {
return Err(format!(
"Failed to start Docker container: {}",
String::from_utf8_lossy(&output.stderr)
)
.into());
}
let container_id = String::from_utf8_lossy(&output.stdout).trim().to_string();
// Wait for SSH to be ready
time::sleep(Duration::from_secs(5)).await;
Ok(container_id)
}
/// Cleanup Docker container
///
/// # Arguments
/// * `container_id` - Container ID
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success/failure
async fn cleanup_docker_container(
container_id: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let status = Command::new("docker")
.args(["stop", container_id])
.status()?;
if !status.success() {
return Err("Failed to stop Docker container".into());
}
let status = Command::new("docker").args(["rm", container_id]).status()?;
if !status.success() {
return Err("Failed to remove Docker container".into());
}
Ok(())
}
/// Test SSH connection to Docker container
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success/failure
#[tokio::test]
#[ignore] // Ignore by default as it requires Docker
async fn test_ssh_connection() -> Result<(), Box<dyn std::error::Error>> {
// Setup Docker container
let container_id = setup_docker_container().await?;
// Create server config
let server_config = ServerConfig {
host: "localhost".to_string(),
port: 2222,
auth: AuthMethod::Password {
username: "root".to_string(),
password: "password".to_string(),
},
install_dir: "/opt/osvm".to_string(),
};
// Create SSH client
let mut client = SshClient::new(server_config.clone())?;
// Connect to server
client.connect()?;
// Execute command
let output = client.execute_command("echo 'Hello, world!'")?;
assert_eq!(output.trim(), "Hello, world!");
// Close connection
client.close();
// Cleanup Docker container
cleanup_docker_container(&container_id).await?;
Ok(())
}
/// Test system information retrieval
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success/failure
#[tokio::test]
#[ignore] // Ignore by default as it requires Docker
async fn test_system_info() -> Result<(), Box<dyn std::error::Error>> {
// Setup Docker container
let container_id = setup_docker_container().await?;
// Create server config
let server_config = ServerConfig {
host: "localhost".to_string(),
port: 2222,
auth: AuthMethod::Password {
username: "root".to_string(),
password: "password".to_string(),
},
install_dir: "/opt/osvm".to_string(),
};
// Create SSH client
let mut client = SshClient::new(server_config.clone())?;
// Connect to server
client.connect()?;
// Get system information
let system_info = client.get_system_info()?;
// Verify system information
assert!(system_info.contains_key("cpu_cores"));
assert!(system_info.contains_key("memory_gb"));
assert!(system_info.contains_key("disk_total"));
assert!(system_info.contains_key("disk_available"));
assert!(system_info.contains_key("kernel"));
// Close connection
client.close();
// Cleanup Docker container
cleanup_docker_container(&container_id).await?;
Ok(())
}
/// Test file operations
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success/failure
#[tokio::test]
#[ignore] // Ignore by default as it requires Docker
async fn test_file_operations() -> Result<(), Box<dyn std::error::Error>> {
// Setup Docker container
let container_id = setup_docker_container().await?;
// Create server config
let server_config = ServerConfig {
host: "localhost".to_string(),
port: 2222,
auth: AuthMethod::Password {
username: "root".to_string(),
password: "password".to_string(),
},
install_dir: "/opt/osvm".to_string(),
};
// Create SSH client
let mut client = SshClient::new(server_config.clone())?;
// Connect to server
client.connect()?;
// Create directory
client.create_directory("/tmp/test")?;
assert!(client.directory_exists("/tmp/test")?);
// Create temporary file
let temp_path = std::env::temp_dir().join("test.txt");
std::fs::write(&temp_path, "Hello, world!")?;
// Upload file
client.upload_file(&temp_path, "/tmp/test/test.txt")?;
assert!(client.file_exists("/tmp/test/test.txt")?);
// Verify file content
let output = client.execute_command("cat /tmp/test/test.txt")?;
assert_eq!(output.trim(), "Hello, world!");
// Delete temporary file
std::fs::remove_file(temp_path)?;
// Close connection
client.close();
// Cleanup Docker container
cleanup_docker_container(&container_id).await?;
Ok(())
}
/// Test Sonic RPC deployment
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success/failure
#[tokio::test]
#[ignore] // Ignore by default as it requires Docker and takes a long time
async fn test_sonic_rpc_deployment() -> Result<(), Box<dyn std::error::Error>> {
// Setup Docker container
let container_id = setup_docker_container().await?;
// Create server config
let server_config = ServerConfig {
host: "localhost".to_string(),
port: 2222,
auth: AuthMethod::Password {
username: "root".to_string(),
password: "password".to_string(),
},
install_dir: "/opt/osvm".to_string(),
};
// Create deployment config
let deployment_config = DeploymentConfig {
svm_type: "sonic".to_string(),
node_type: "rpc".to_string(),
network: NetworkType::Devnet,
node_name: "sonic-rpc".to_string(),
rpc_url: None,
additional_params: HashMap::new(),
version: None,
client_type: None,
hot_swap_enabled: false,
metrics_config: None,
disk_config: None,
};
// Deploy Sonic RPC node
let result = deploy_svm_node(
server_config.clone(),
deployment_config,
Some(Box::new(|progress, message| {
println!("Progress: {}% - {}", progress, message);
})),
)
.await;
// Verify deployment result
assert!(result.is_ok());
// Create SSH client
let mut client = SshClient::new(server_config.clone())?;
// Connect to server
client.connect()?;
// Verify service is running
let status = client.execute_command("systemctl is-active sonic-rpc-devnet")?;
assert_eq!(status.trim(), "active");
// Close connection
client.close();
// Cleanup Docker container
cleanup_docker_container(&container_id).await?;
Ok(())
}
}