1
1
use cargo_shuttle:: { Args , Command , ProjectArgs , RunArgs , Shuttle } ;
2
- use local_ip_address:: local_ip;
3
2
use portpicker:: pick_unused_port;
4
3
use reqwest:: StatusCode ;
5
- use std:: { fs:: canonicalize, process:: exit, time:: Duration } ;
4
+ use std:: { fs:: canonicalize, process:: exit, time:: Duration , net :: Ipv4Addr } ;
6
5
use tokio:: time:: sleep;
7
6
8
7
/// 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 {
10
9
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
+
11
17
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
+ } ;
13
24
14
25
let runner = Shuttle :: new ( ) . unwrap ( ) . run ( Args {
15
26
api_url : Some ( "http://shuttle.invalid:80" . to_string ( ) ) ,
@@ -36,7 +47,7 @@ async fn cargo_shuttle_run(working_directory: &str) -> u16 {
36
47
37
48
// Wait for service to be responsive
38
49
while ( reqwest:: Client :: new ( )
39
- . get ( format ! ( "http://localhost :{port}" ) )
50
+ . get ( format ! ( "http://{url} :{port}" ) )
40
51
. send ( )
41
52
. await )
42
53
. is_err ( )
@@ -53,7 +64,7 @@ async fn cargo_shuttle_run(working_directory: &str) -> u16 {
53
64
54
65
#[ tokio:: test( flavor = "multi_thread" ) ]
55
66
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 ;
57
68
58
69
let request_text = reqwest:: Client :: new ( )
59
70
. get ( format ! ( "http://localhost:{port}/hello" ) )
@@ -69,7 +80,7 @@ async fn rocket_hello_world() {
69
80
70
81
#[ tokio:: test( flavor = "multi_thread" ) ]
71
82
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 ;
73
84
74
85
let request_text = reqwest:: Client :: new ( )
75
86
. get ( format ! ( "http://localhost:{port}/secret" ) )
@@ -86,7 +97,7 @@ async fn rocket_secrets() {
86
97
// This example uses a shared Postgres. Thus local runs should create a docker container for it.
87
98
#[ tokio:: test( flavor = "multi_thread" ) ]
88
99
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 ;
90
101
let client = reqwest:: Client :: new ( ) ;
91
102
92
103
let post_text = client
@@ -115,7 +126,7 @@ async fn rocket_postgres() {
115
126
116
127
#[ tokio:: test( flavor = "multi_thread" ) ]
117
128
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 ;
119
130
let client = reqwest:: Client :: new ( ) ;
120
131
121
132
let public_text = client
@@ -171,7 +182,7 @@ async fn rocket_authentication() {
171
182
172
183
#[ tokio:: test( flavor = "multi_thread" ) ]
173
184
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 ;
175
186
176
187
let request_text = reqwest:: Client :: new ( )
177
188
. get ( format ! ( "http://localhost:{port}/hello" ) )
@@ -187,7 +198,7 @@ async fn actix_web_hello_world() {
187
198
188
199
#[ tokio:: test( flavor = "multi_thread" ) ]
189
200
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 ;
191
202
192
203
let request_text = reqwest:: Client :: new ( )
193
204
. get ( format ! ( "http://localhost:{port}/hello" ) )
@@ -203,7 +214,7 @@ async fn axum_hello_world() {
203
214
204
215
#[ tokio:: test( flavor = "multi_thread" ) ]
205
216
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 ;
207
218
208
219
let request_text = reqwest:: Client :: new ( )
209
220
. get ( format ! ( "http://localhost:{port}/hello" ) )
@@ -219,7 +230,7 @@ async fn tide_hello_world() {
219
230
220
231
#[ tokio:: test( flavor = "multi_thread" ) ]
221
232
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 ;
223
234
224
235
let request_text = reqwest:: Client :: new ( )
225
236
. get ( format ! ( "http://localhost:{port}/hello" ) )
@@ -235,7 +246,7 @@ async fn tower_hello_world() {
235
246
236
247
#[ tokio:: test( flavor = "multi_thread" ) ]
237
248
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 ;
239
250
240
251
let request_text = reqwest:: Client :: new ( )
241
252
. get ( format ! ( "http://localhost:{port}/hello" ) )
@@ -251,7 +262,7 @@ async fn warp_hello_world() {
251
262
252
263
#[ tokio:: test( flavor = "multi_thread" ) ]
253
264
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 ;
255
266
256
267
let request_text = reqwest:: Client :: new ( )
257
268
. get ( format ! ( "http://localhost:{port}/hello" ) )
@@ -268,7 +279,7 @@ async fn poem_hello_world() {
268
279
// This example uses a shared Postgres. Thus local runs should create a docker container for it.
269
280
#[ tokio:: test( flavor = "multi_thread" ) ]
270
281
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 ;
272
283
let client = reqwest:: Client :: new ( ) ;
273
284
274
285
let post_text = client
@@ -299,7 +310,7 @@ async fn poem_postgres() {
299
310
// This example uses a shared MongoDb. Thus local runs should create a docker container for it.
300
311
#[ tokio:: test( flavor = "multi_thread" ) ]
301
312
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 ;
303
314
let client = reqwest:: Client :: new ( ) ;
304
315
305
316
// Post a todo note and get the persisted todo objectId
@@ -331,7 +342,7 @@ async fn poem_mongodb() {
331
342
332
343
#[ tokio:: test( flavor = "multi_thread" ) ]
333
344
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 ;
335
346
336
347
let request_text = reqwest:: Client :: new ( )
337
348
. get ( format ! ( "http://localhost:{port}/hello" ) )
@@ -347,7 +358,7 @@ async fn salvo_hello_world() {
347
358
348
359
#[ tokio:: test( flavor = "multi_thread" ) ]
349
360
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 ;
351
362
352
363
let request_text = reqwest:: Client :: new ( )
353
364
. get ( format ! ( "http://localhost:{port}/hello" ) )
@@ -361,61 +372,13 @@ async fn thruster_hello_world() {
361
372
assert_eq ! ( request_text, "Hello, World!" ) ;
362
373
}
363
374
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
-
411
375
#[ tokio:: test( flavor = "multi_thread" ) ]
412
376
async fn rocket_hello_world_with_router_ip ( ) {
413
377
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 ;
416
379
417
380
let request_text = reqwest:: Client :: new ( )
418
- . get ( format ! ( "http://{ip:?} :{port}/hello" ) )
381
+ . get ( format ! ( "http://0.0.0.0 :{port}/hello" ) )
419
382
. send ( )
420
383
. await
421
384
. unwrap ( )
0 commit comments