@@ -16,8 +16,7 @@ use tokio::process::Command;
16
16
use anyhow:: Result ;
17
17
use wadm:: consumers:: { CommandConsumer , ScopedMessage } ;
18
18
19
- const DEFAULT_WASMCLOUD_PORT : u16 = 4000 ;
20
- const DEFAULT_NATS_PORT : u16 = 4222 ;
19
+ pub const DEFAULT_NATS_PORT : u16 = 4222 ;
21
20
pub const ECHO_ACTOR_ID : & str = "MBCFOPM6JW2APJLXJD3Z5O4CN7CPYJ2B4FTKLJUR5YR5MITIU7HD3WD5" ;
22
21
pub const HTTP_SERVER_PROVIDER_ID : & str =
23
22
"VAG3QITQQ2ODAOWB5TTQSDJ53XK3SHBEIFNK4AYJ5RKAX2UNSCAPHA5M" ;
@@ -63,32 +62,19 @@ pub struct TestWashConfig {
63
62
64
63
/// Only connect to pre-existing NATS instance
65
64
pub nats_connect_only : bool ,
66
-
67
- /// Port on which to run wasmCloud (via `wash up`)
68
- pub wasmcloud_port : Option < u16 > ,
69
65
}
70
66
71
67
impl TestWashConfig {
72
68
/// Build a test wash configuration with randomized ports
73
69
pub async fn random ( ) -> Result < TestWashConfig > {
74
70
let nats_port = Some ( get_random_tcp_port ( ) ) ;
75
- let wasmcloud_port = Some ( get_random_tcp_port ( ) ) ;
76
71
77
72
Ok ( TestWashConfig {
78
73
nats_port,
79
- wasmcloud_port,
80
74
..TestWashConfig :: default ( )
81
75
} )
82
76
}
83
77
84
- /// Get the washboard URL for this config
85
- pub fn washboard_url ( & self ) -> String {
86
- format ! (
87
- "localhost:{}" ,
88
- self . wasmcloud_port. unwrap_or( DEFAULT_WASMCLOUD_PORT )
89
- )
90
- }
91
-
92
78
/// Get the NATS URL for this config
93
79
pub fn nats_url ( & self ) -> String {
94
80
format ! ( "127.0.0.1:{}" , self . nats_port. unwrap_or( DEFAULT_NATS_PORT ) )
@@ -98,22 +84,29 @@ impl TestWashConfig {
98
84
/// Start a local wash instance
99
85
async fn start_wash_instance ( cfg : & TestWashConfig ) -> Result < CleanupGuard > {
100
86
let nats_port = cfg. nats_port . unwrap_or ( DEFAULT_NATS_PORT ) . to_string ( ) ;
101
- let wasmcloud_port = cfg
102
- . wasmcloud_port
103
- . unwrap_or ( DEFAULT_WASMCLOUD_PORT )
104
- . to_string ( ) ;
87
+ let wash_host_key = nkeys:: KeyPair :: new_server ( ) ;
88
+
89
+ let seed = wash_host_key. seed ( ) . expect ( "seed to exist" ) . to_string ( ) ;
105
90
106
91
// Build args
107
- let mut args: Vec < & str > = Vec :: from ( [ "up" , "-d" , "--disable-wadm" , "--nats-port" , & nats_port] ) ;
92
+ let mut args: Vec < & str > = Vec :: from ( [
93
+ "up" ,
94
+ "-d" ,
95
+ "--disable-wadm" ,
96
+ "--nats-port" ,
97
+ & nats_port,
98
+ "--host-seed" ,
99
+ & seed,
100
+ "--wasmcloud-version" ,
101
+ "v0.78.0-rc3" ,
102
+ ] ) ;
108
103
if cfg. nats_connect_only {
109
104
args. push ( "--nats-connect-only" ) ;
110
105
}
111
106
112
107
// Build the command
113
108
let mut cmd = Command :: new ( "wash" ) ;
114
109
cmd. args ( & args)
115
- . env ( "WASMCLOUD_PORT" , & wasmcloud_port)
116
- . env ( "WASMCLOUD_DASHBOARD_PORT" , & wasmcloud_port)
117
110
. stderr ( std:: process:: Stdio :: null ( ) )
118
111
. stdout ( std:: process:: Stdio :: null ( ) ) ;
119
112
@@ -124,32 +117,45 @@ async fn start_wash_instance(cfg: &TestWashConfig) -> Result<CleanupGuard> {
124
117
} ;
125
118
126
119
let output = cmd. status ( ) . await . expect ( "Unable to run detached wash up" ) ;
127
-
128
120
assert ! ( output. success( ) , "Error trying to start host" , ) ;
129
121
130
- // Give the host just a bit more time to get totally ready
131
- tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 10 ) ) . await ;
122
+ // Run wash get inventory keypair.public_key() --nats-port {nats_port}
123
+ // and retry 5 times, sleeping for a second between tries, to ensure
124
+ // the host launched
125
+ let mut retries = 5 ;
126
+ while retries > 0 {
127
+ let output = Command :: new ( "wash" )
128
+ . args ( [
129
+ "get" ,
130
+ "inventory" ,
131
+ & wash_host_key. public_key ( ) ,
132
+ "--ctl-port" ,
133
+ & nats_port,
134
+ ] )
135
+ . output ( )
136
+ . await
137
+ . expect ( "Unable to run wash command" ) ;
138
+ if output. status . success ( ) {
139
+ break ;
140
+ }
141
+ retries -= 1 ;
142
+ if retries == 0 {
143
+ panic ! ( "Failed to launch host" ) ;
144
+ }
145
+ tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
146
+ }
132
147
133
148
Ok ( guard)
134
149
}
135
150
136
151
/// Set up and run a wash instance that can be used for a test
137
152
pub async fn setup_test_wash ( cfg : & TestWashConfig ) -> CleanupGuard {
138
- match tokio:: net:: TcpStream :: connect ( cfg. washboard_url ( ) ) . await {
139
- // NOTE: DO NOT use unwrap_or here. Otherwise we allocate a cleanup guard that then gets
140
- // dropped, which runs `wash down`
141
- #[ allow( clippy:: unnecessary_lazy_evaluations) ]
142
- Err ( _) => start_wash_instance ( cfg)
143
- . await
144
- . unwrap_or_else ( |_| CleanupGuard {
145
- child : None ,
146
- already_running : false ,
147
- } ) ,
148
- Ok ( _) => CleanupGuard {
153
+ start_wash_instance ( cfg)
154
+ . await
155
+ . unwrap_or_else ( |_| CleanupGuard {
149
156
child : None ,
150
- already_running : true ,
151
- } ,
152
- }
157
+ already_running : false ,
158
+ } )
153
159
}
154
160
155
161
pub async fn wait_for_server ( url : & str ) {
0 commit comments