Skip to content

Commit 4df9fbd

Browse files
committed
add .env to test local profile
1 parent bbf283e commit 4df9fbd

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

.env

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ASYNC_SSH2_TEST_HOST_IP=10.10.10.2
2+
ASYNC_SSH2_TEST_HOST_PW=root
3+
ASYNC_SSH2_TEST_HOST_USER=root
4+
ASYNC_SSH2_TEST_KNOWN_HOSTS=./tests/async-ssh2-tokio/known_hosts
5+
ASYNC_SSH2_TEST_CLIENT_PRIV=./tests/client.ed25519
6+
ASYNC_SSH2_TEST_CLIENT_PUB=./tests/client.ed25519.pub
7+
ASYNC_SSH2_TEST_CLIENT_PROT_PRIV=./tests/client.prot.ed25519
8+
ASYNC_SSH2_TEST_CLIENT_PROT_PUB=./tests/client.prot.ed25519.pub
9+
ASYNC_SSH2_TEST_CLIENT_PROT_PASS=test
10+
ASYNC_SSH2_TEST_HOST_PORT=22
11+
ASYNC_SSH2_TEST_SERVER_PUB=./tests/sshd-test/ssh_host_ed25519_key.pub
12+
ASYNC_SSH2_TEST_UPLOAD_FILE=./tests/async-ssh2-tokio/test-upload-file
13+
ASYNC_SSH2_TEST_HTTP_SERVER_IP=10.10.10.4
14+
ASYNC_SSH2_TEST_HTTP_SERVER_PORT=8000
15+
ASYNC_SSH2_TEST_HOST_NAME=localhost

Cargo.toml

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ authors = ["Miyoshi-Ryota <m1yosh1.ry0t4@gmail.com>"]
1616
openssl = ["russh/openssl"]
1717

1818
[dependencies]
19-
russh = "0.45.0"
20-
russh-keys = "0.45.0"
21-
russh-sftp = "2.0.1"
22-
thiserror = "1.0"
19+
russh = "0.46.0"
20+
russh-keys = "0.46.0"
21+
russh-sftp = "2.0.6"
22+
thiserror = "2.0"
2323
async-trait = "0.1"
2424
tokio = { version = "1", features = ["fs"] }
25+
dotenv = "0.15.0"
2526

2627
[dev-dependencies]

src/client.rs

+39-14
Original file line numberDiff line numberDiff line change
@@ -612,25 +612,46 @@ impl Handler for ClientHandler {
612612

613613
#[cfg(test)]
614614
mod tests {
615+
use crate::client::*;
615616
use core::time;
616-
617+
use dotenv::dotenv;
618+
use std::path::Path;
619+
use std::sync::Once;
617620
use tokio::io::AsyncReadExt;
621+
static INIT: Once = Once::new();
622+
623+
fn initialize() {
624+
// Perform your initialization tasks here
625+
println!("Running initialization code before tests...");
626+
// Example: load .env file if we are using non-docker environment
627+
if is_running_in_docker() {
628+
println!("Running inside Docker.");
629+
} else {
630+
println!("Not running inside Docker. Load env from file");
631+
dotenv().ok();
632+
}
633+
}
634+
fn is_running_in_docker() -> bool {
635+
Path::new("/.dockerenv").exists() || check_cgroup()
636+
}
618637

619-
use crate::client::*;
638+
fn check_cgroup() -> bool {
639+
match std::fs::read_to_string("/proc/1/cgroup") {
640+
Ok(contents) => contents.contains("docker"),
641+
Err(_) => false,
642+
}
643+
}
620644

621645
fn env(name: &str) -> String {
646+
INIT.call_once(|| {
647+
initialize();
648+
});
622649
std::env::var(name).expect(
623-
"Failed to get env var needed for test, make sure to set the following env vars:
624-
ASYNC_SSH2_TEST_HOST_USER
625-
ASYNC_SSH2_TEST_HOST_PW
626-
ASYNC_SSH2_TEST_HOST_IP
627-
ASYNC_SSH2_TEST_HOST_PORT
628-
ASYNC_SSH2_TEST_CLIENT_PROT_PRIV
629-
ASYNC_SSH2_TEST_CLIENT_PRIV
630-
ASYNC_SSH2_TEST_CLIENT_PROT_PASS
631-
ASYNC_SSH2_TEST_SERVER_PUB
632-
ASYNC_SSH2_TEST_UPLOAD_FILE
633-
",
650+
format!(
651+
"Failed to get env var needed for test, make sure to set the following env var: {}",
652+
name
653+
)
654+
.as_str(),
634655
)
635656
}
636657

@@ -1036,7 +1057,11 @@ ASYNC_SSH2_TEST_UPLOAD_FILE
10361057
ServerCheckMethod::with_known_hosts_file(&env("ASYNC_SSH2_TEST_KNOWN_HOSTS")),
10371058
)
10381059
.await;
1039-
assert!(client.is_ok());
1060+
if is_running_in_docker() {
1061+
assert!(client.is_ok());
1062+
} else {
1063+
assert!(client.is_err());// DNS can't find the docker hostname if the rust running without docker container
1064+
}
10401065
}
10411066

10421067
#[tokio::test]

tests/sshd-test/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ENV DEBIAN_FRONTEND=noninteractive
66
ENV TZ=Europe/Warsaw
77

88
RUN apt-get update && \
9-
apt-get install -y openssh-server='1:9.6p1-3ubuntu13.4' openssh-sftp-server='1:9.6p1-3ubuntu13.4' && \
9+
apt-get install -y openssh-server openssh-sftp-server && \
1010
apt-get clean && \
1111
rm -rf /var/lib/apt/lists/*
1212

0 commit comments

Comments
 (0)