Skip to content

Commit f43e2e5

Browse files
committed
refactor: amend ipaddr setting, local run init fn
1 parent 47b619b commit f43e2e5

File tree

4 files changed

+39
-96
lines changed

4 files changed

+39
-96
lines changed

Cargo.lock

Lines changed: 0 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-shuttle/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ headers = "0.3.8"
2828
indicatif = "0.17.2"
2929
ignore = "0.4.18"
3030
indoc = "1.0.7"
31-
local-ip-address = "0.5.0"
3231
log = "0.4.17"
3332
openssl = { version = '0.10', optional = true }
3433
portpicker = "0.1.1"

cargo-shuttle/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use futures::StreamExt;
2727
use git2::{Repository, StatusOptions};
2828
use ignore::overrides::OverrideBuilder;
2929
use ignore::WalkBuilder;
30-
use local_ip_address::local_ip;
3130
use shuttle_common::models::{project, secret};
3231
use shuttle_service::loader::{build_crate, Loader};
3332
use shuttle_service::Logger;
@@ -406,8 +405,13 @@ impl Shuttle {
406405
secrets,
407406
working_directory.to_path_buf(),
408407
)?;
409-
let addr = SocketAddr::new(if run_args.router_ip
410-
{local_ip().unwrap()} else {Ipv4Addr::LOCALHOST.into()}, run_args.port);
408+
let addr = if run_args.router_ip {
409+
std::net::IpAddr::V4(Ipv4Addr::new(0,0,0,0))
410+
} else {
411+
Ipv4Addr::LOCALHOST.into()
412+
};
413+
414+
let addr = SocketAddr::new(addr, run_args.port);
411415

412416
trace!("loading project");
413417
println!(

cargo-shuttle/tests/integration/run.rs

Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
use cargo_shuttle::{Args, Command, ProjectArgs, RunArgs, Shuttle};
2-
use local_ip_address::local_ip;
32
use portpicker::pick_unused_port;
43
use reqwest::StatusCode;
5-
use std::{fs::canonicalize, process::exit, time::Duration};
4+
use std::{fs::canonicalize, process::exit, time::Duration, net::Ipv4Addr};
65
use tokio::time::sleep;
76

87
/// creates a `cargo-shuttle` run instance with some reasonable defaults set.
9-
async fn cargo_shuttle_run(working_directory: &str) -> u16 {
8+
async fn cargo_shuttle_run(working_directory: &str, router_ip: bool) -> u16 {
109
let working_directory = canonicalize(working_directory).unwrap();
10+
11+
let url = if router_ip == false {
12+
"localhost"
13+
} else {
14+
"0.0.0.0"
15+
};
16+
1117
let port = pick_unused_port().unwrap();
12-
let run_args = RunArgs { port, router_ip: false};
18+
19+
let run_args = if router_ip == false {
20+
RunArgs { port, router_ip: false}
21+
} else {
22+
RunArgs { port, router_ip: true}
23+
};
1324

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

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

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

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

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

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

92103
let post_text = client
@@ -115,7 +126,7 @@ async fn rocket_postgres() {
115126

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

121132
let public_text = client
@@ -171,7 +182,7 @@ async fn rocket_authentication() {
171182

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

364-
/// creates a `cargo-shuttle` run instance with some reasonable defaults set, but using the router IP instead.
365-
async fn cargo_shuttle_run_with_router_ip(working_directory: &str) -> u16 {
366-
let working_directory = canonicalize(working_directory).unwrap();
367-
let port = pick_unused_port().unwrap();
368-
let ip = local_ip().unwrap();
369-
let run_args = RunArgs { port, router_ip: true };
370-
371-
let runner = Shuttle::new().unwrap().run(Args {
372-
api_url: Some("http://shuttle.invalid:80".to_string()),
373-
project_args: ProjectArgs {
374-
working_directory: working_directory.clone(),
375-
name: None,
376-
},
377-
cmd: Command::Run(run_args),
378-
});
379-
380-
let working_directory_clone = working_directory.clone();
381-
382-
tokio::spawn(async move {
383-
sleep(Duration::from_secs(600)).await;
384-
385-
println!(
386-
"run test for '{}' took too long. Did it fail to shutdown?",
387-
working_directory_clone.display()
388-
);
389-
exit(1);
390-
});
391-
392-
tokio::spawn(runner);
393-
394-
// Wait for service to be responsive
395-
while (reqwest::Client::new()
396-
.get(format!("http://{ip}:{port}"))
397-
.send()
398-
.await)
399-
.is_err()
400-
{
401-
println!(
402-
"waiting for '{}' to start up...",
403-
working_directory.display()
404-
);
405-
sleep(Duration::from_millis(350)).await;
406-
}
407-
408-
port
409-
}
410-
411375
#[tokio::test(flavor = "multi_thread")]
412376
async fn rocket_hello_world_with_router_ip() {
413377

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

417380
let request_text = reqwest::Client::new()
418-
.get(format!("http://{ip:?}:{port}/hello"))
381+
.get(format!("http://0.0.0.0:{port}/hello"))
419382
.send()
420383
.await
421384
.unwrap()

0 commit comments

Comments
 (0)