Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add .env to test local profile #75

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ASYNC_SSH2_TEST_HOST_IP=10.10.10.2
ASYNC_SSH2_TEST_HOST_PW=root
ASYNC_SSH2_TEST_HOST_USER=root
ASYNC_SSH2_TEST_KNOWN_HOSTS=./tests/async-ssh2-tokio/known_hosts
ASYNC_SSH2_TEST_CLIENT_PRIV=./tests/client.ed25519
ASYNC_SSH2_TEST_CLIENT_PUB=./tests/client.ed25519.pub
ASYNC_SSH2_TEST_CLIENT_PROT_PRIV=./tests/client.prot.ed25519
ASYNC_SSH2_TEST_CLIENT_PROT_PUB=./tests/client.prot.ed25519.pub
ASYNC_SSH2_TEST_CLIENT_PROT_PASS=test
ASYNC_SSH2_TEST_HOST_PORT=22
ASYNC_SSH2_TEST_SERVER_PUB=./tests/sshd-test/ssh_host_ed25519_key.pub
ASYNC_SSH2_TEST_UPLOAD_FILE=./tests/async-ssh2-tokio/test-upload-file
ASYNC_SSH2_TEST_HTTP_SERVER_IP=10.10.10.4
ASYNC_SSH2_TEST_HTTP_SERVER_PORT=8000
ASYNC_SSH2_TEST_HOST_NAME=localhost
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ authors = ["Miyoshi-Ryota <m1yosh1.ry0t4@gmail.com>"]
openssl = ["russh/openssl"]

[dependencies]
russh = "0.45.0"
russh-keys = "0.45.0"
russh-sftp = "2.0.1"
thiserror = "1.0"
russh = "0.46.0"
russh-keys = "0.46.0"
russh-sftp = "2.0.6"
thiserror = "2.0"
async-trait = "0.1"
tokio = { version = "1", features = ["fs"] }
dotenv = "0.15.0"

[dev-dependencies]
53 changes: 39 additions & 14 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,25 +612,46 @@ impl Handler for ClientHandler {

#[cfg(test)]
mod tests {
use crate::client::*;
use core::time;

use dotenv::dotenv;
use std::path::Path;
use std::sync::Once;
use tokio::io::AsyncReadExt;
static INIT: Once = Once::new();

fn initialize() {
// Perform your initialization tasks here
println!("Running initialization code before tests...");
// Example: load .env file if we are using non-docker environment
if is_running_in_docker() {
println!("Running inside Docker.");
} else {
println!("Not running inside Docker. Load env from file");
dotenv().ok();
}
}
fn is_running_in_docker() -> bool {
Path::new("/.dockerenv").exists() || check_cgroup()
}

use crate::client::*;
fn check_cgroup() -> bool {
match std::fs::read_to_string("/proc/1/cgroup") {
Ok(contents) => contents.contains("docker"),
Err(_) => false,
}
}

fn env(name: &str) -> String {
INIT.call_once(|| {
initialize();
});
std::env::var(name).expect(
"Failed to get env var needed for test, make sure to set the following env vars:
ASYNC_SSH2_TEST_HOST_USER
ASYNC_SSH2_TEST_HOST_PW
ASYNC_SSH2_TEST_HOST_IP
ASYNC_SSH2_TEST_HOST_PORT
ASYNC_SSH2_TEST_CLIENT_PROT_PRIV
ASYNC_SSH2_TEST_CLIENT_PRIV
ASYNC_SSH2_TEST_CLIENT_PROT_PASS
ASYNC_SSH2_TEST_SERVER_PUB
ASYNC_SSH2_TEST_UPLOAD_FILE
",
format!(
"Failed to get env var needed for test, make sure to set the following env var: {}",
name
)
.as_str(),
)
}

Expand Down Expand Up @@ -1036,7 +1057,11 @@ ASYNC_SSH2_TEST_UPLOAD_FILE
ServerCheckMethod::with_known_hosts_file(&env("ASYNC_SSH2_TEST_KNOWN_HOSTS")),
)
.await;
assert!(client.is_ok());
if is_running_in_docker() {
assert!(client.is_ok());
} else {
assert!(client.is_err());// DNS can't find the docker hostname if the rust running without docker container
}
}

#[tokio::test]
Expand Down
2 changes: 1 addition & 1 deletion tests/sshd-test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Warsaw

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

Expand Down