Skip to content

Commit

Permalink
refactor: amend ipaddr setting, local run init fn
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-mo-143 committed Jan 5, 2023
1 parent 47b619b commit f43e2e5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 96 deletions.
23 changes: 0 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cargo-shuttle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ headers = "0.3.8"
indicatif = "0.17.2"
ignore = "0.4.18"
indoc = "1.0.7"
local-ip-address = "0.5.0"
log = "0.4.17"
openssl = { version = '0.10', optional = true }
portpicker = "0.1.1"
Expand Down
10 changes: 7 additions & 3 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use futures::StreamExt;
use git2::{Repository, StatusOptions};
use ignore::overrides::OverrideBuilder;
use ignore::WalkBuilder;
use local_ip_address::local_ip;
use shuttle_common::models::{project, secret};
use shuttle_service::loader::{build_crate, Loader};
use shuttle_service::Logger;
Expand Down Expand Up @@ -406,8 +405,13 @@ impl Shuttle {
secrets,
working_directory.to_path_buf(),
)?;
let addr = SocketAddr::new(if run_args.router_ip
{local_ip().unwrap()} else {Ipv4Addr::LOCALHOST.into()}, run_args.port);
let addr = if run_args.router_ip {
std::net::IpAddr::V4(Ipv4Addr::new(0,0,0,0))
} else {
Ipv4Addr::LOCALHOST.into()
};

let addr = SocketAddr::new(addr, run_args.port);

trace!("loading project");
println!(
Expand Down
101 changes: 32 additions & 69 deletions cargo-shuttle/tests/integration/run.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
use cargo_shuttle::{Args, Command, ProjectArgs, RunArgs, Shuttle};
use local_ip_address::local_ip;
use portpicker::pick_unused_port;
use reqwest::StatusCode;
use std::{fs::canonicalize, process::exit, time::Duration};
use std::{fs::canonicalize, process::exit, time::Duration, net::Ipv4Addr};
use tokio::time::sleep;

/// creates a `cargo-shuttle` run instance with some reasonable defaults set.
async fn cargo_shuttle_run(working_directory: &str) -> u16 {
async fn cargo_shuttle_run(working_directory: &str, router_ip: bool) -> u16 {
let working_directory = canonicalize(working_directory).unwrap();

let url = if router_ip == false {
"localhost"
} else {
"0.0.0.0"
};

let port = pick_unused_port().unwrap();
let run_args = RunArgs { port, router_ip: false};

let run_args = if router_ip == false {
RunArgs { port, router_ip: false}
} else {
RunArgs { port, router_ip: true}
};

let runner = Shuttle::new().unwrap().run(Args {
api_url: Some("http://shuttle.invalid:80".to_string()),
Expand All @@ -36,7 +47,7 @@ async fn cargo_shuttle_run(working_directory: &str) -> u16 {

// Wait for service to be responsive
while (reqwest::Client::new()
.get(format!("http://localhost:{port}"))
.get(format!("http://{url}:{port}"))
.send()
.await)
.is_err()
Expand All @@ -53,7 +64,7 @@ async fn cargo_shuttle_run(working_directory: &str) -> u16 {

#[tokio::test(flavor = "multi_thread")]
async fn rocket_hello_world() {
let port = cargo_shuttle_run("../examples/rocket/hello-world").await;
let port = cargo_shuttle_run("../examples/rocket/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -69,7 +80,7 @@ async fn rocket_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn rocket_secrets() {
let port = cargo_shuttle_run("../examples/rocket/secrets").await;
let port = cargo_shuttle_run("../examples/rocket/secrets", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/secret"))
Expand All @@ -86,7 +97,7 @@ async fn rocket_secrets() {
// This example uses a shared Postgres. Thus local runs should create a docker container for it.
#[tokio::test(flavor = "multi_thread")]
async fn rocket_postgres() {
let port = cargo_shuttle_run("../examples/rocket/postgres").await;
let port = cargo_shuttle_run("../examples/rocket/postgres", false).await;
let client = reqwest::Client::new();

let post_text = client
Expand Down Expand Up @@ -115,7 +126,7 @@ async fn rocket_postgres() {

#[tokio::test(flavor = "multi_thread")]
async fn rocket_authentication() {
let port = cargo_shuttle_run("../examples/rocket/authentication").await;
let port = cargo_shuttle_run("../examples/rocket/authentication", false).await;
let client = reqwest::Client::new();

let public_text = client
Expand Down Expand Up @@ -171,7 +182,7 @@ async fn rocket_authentication() {

#[tokio::test(flavor = "multi_thread")]
async fn actix_web_hello_world() {
let port = cargo_shuttle_run("../examples/actix-web/hello-world").await;
let port = cargo_shuttle_run("../examples/actix-web/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -187,7 +198,7 @@ async fn actix_web_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn axum_hello_world() {
let port = cargo_shuttle_run("../examples/axum/hello-world").await;
let port = cargo_shuttle_run("../examples/axum/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -203,7 +214,7 @@ async fn axum_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn tide_hello_world() {
let port = cargo_shuttle_run("../examples/tide/hello-world").await;
let port = cargo_shuttle_run("../examples/tide/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -219,7 +230,7 @@ async fn tide_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn tower_hello_world() {
let port = cargo_shuttle_run("../examples/tower/hello-world").await;
let port = cargo_shuttle_run("../examples/tower/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -235,7 +246,7 @@ async fn tower_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn warp_hello_world() {
let port = cargo_shuttle_run("../examples/warp/hello-world").await;
let port = cargo_shuttle_run("../examples/warp/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -251,7 +262,7 @@ async fn warp_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn poem_hello_world() {
let port = cargo_shuttle_run("../examples/poem/hello-world").await;
let port = cargo_shuttle_run("../examples/poem/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -268,7 +279,7 @@ async fn poem_hello_world() {
// This example uses a shared Postgres. Thus local runs should create a docker container for it.
#[tokio::test(flavor = "multi_thread")]
async fn poem_postgres() {
let port = cargo_shuttle_run("../examples/poem/postgres").await;
let port = cargo_shuttle_run("../examples/poem/postgres", false).await;
let client = reqwest::Client::new();

let post_text = client
Expand Down Expand Up @@ -299,7 +310,7 @@ async fn poem_postgres() {
// This example uses a shared MongoDb. Thus local runs should create a docker container for it.
#[tokio::test(flavor = "multi_thread")]
async fn poem_mongodb() {
let port = cargo_shuttle_run("../examples/poem/mongodb").await;
let port = cargo_shuttle_run("../examples/poem/mongodb", false).await;
let client = reqwest::Client::new();

// Post a todo note and get the persisted todo objectId
Expand Down Expand Up @@ -331,7 +342,7 @@ async fn poem_mongodb() {

#[tokio::test(flavor = "multi_thread")]
async fn salvo_hello_world() {
let port = cargo_shuttle_run("../examples/salvo/hello-world").await;
let port = cargo_shuttle_run("../examples/salvo/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -347,7 +358,7 @@ async fn salvo_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn thruster_hello_world() {
let port = cargo_shuttle_run("../examples/thruster/hello-world").await;
let port = cargo_shuttle_run("../examples/thruster/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -361,61 +372,13 @@ async fn thruster_hello_world() {
assert_eq!(request_text, "Hello, World!");
}

/// creates a `cargo-shuttle` run instance with some reasonable defaults set, but using the router IP instead.
async fn cargo_shuttle_run_with_router_ip(working_directory: &str) -> u16 {
let working_directory = canonicalize(working_directory).unwrap();
let port = pick_unused_port().unwrap();
let ip = local_ip().unwrap();
let run_args = RunArgs { port, router_ip: true };

let runner = Shuttle::new().unwrap().run(Args {
api_url: Some("http://shuttle.invalid:80".to_string()),
project_args: ProjectArgs {
working_directory: working_directory.clone(),
name: None,
},
cmd: Command::Run(run_args),
});

let working_directory_clone = working_directory.clone();

tokio::spawn(async move {
sleep(Duration::from_secs(600)).await;

println!(
"run test for '{}' took too long. Did it fail to shutdown?",
working_directory_clone.display()
);
exit(1);
});

tokio::spawn(runner);

// Wait for service to be responsive
while (reqwest::Client::new()
.get(format!("http://{ip}:{port}"))
.send()
.await)
.is_err()
{
println!(
"waiting for '{}' to start up...",
working_directory.display()
);
sleep(Duration::from_millis(350)).await;
}

port
}

#[tokio::test(flavor = "multi_thread")]
async fn rocket_hello_world_with_router_ip() {

let port = cargo_shuttle_run_with_router_ip("../examples/rocket/hello-world").await;
let ip = local_ip().unwrap();
let port = cargo_shuttle_run("../examples/rocket/hello-world", true).await;

let request_text = reqwest::Client::new()
.get(format!("http://{ip:?}:{port}/hello"))
.get(format!("http://0.0.0.0:{port}/hello"))
.send()
.await
.unwrap()
Expand Down

0 comments on commit f43e2e5

Please sign in to comment.