Skip to content

Commit ed4430c

Browse files
chore(test): properly run Rust host
Signed-off-by: Brooks Townsend <brooks@cosmonic.com> try actually care about port wasmcloud 0.78 sotp
1 parent 71cd136 commit ed4430c

File tree

2 files changed

+87
-46
lines changed

2 files changed

+87
-46
lines changed

tests/event_consumer_integration.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,13 @@ async fn test_event_stream() -> Result<()> {
8989
let mut stream = get_event_consumer(config.nats_url()).await;
9090

9191
// NOTE: the first heartbeat doesn't come for 30s so we are ignoring it for now
92+
let ctl_port = config
93+
.nats_port
94+
.unwrap_or(crate::helpers::DEFAULT_NATS_PORT)
95+
.to_string();
9296

9397
// Start an actor
94-
helpers::run_wash_command(["start", "actor", ECHO_REFERENCE]).await;
98+
helpers::run_wash_command(["start", "actor", ECHO_REFERENCE, "--ctl-port", &ctl_port]).await;
9599

96100
let mut evt = wait_for_event(&mut stream, DEFAULT_TIMEOUT_DURATION).await;
97101
if let Event::ActorStarted(actor) = evt.as_ref() {
@@ -106,7 +110,14 @@ async fn test_event_stream() -> Result<()> {
106110
evt.ack().await.expect("Should be able to ack event");
107111

108112
// Start a provider
109-
helpers::run_wash_command(["start", "provider", HTTP_SERVER_REFERENCE]).await;
113+
helpers::run_wash_command([
114+
"start",
115+
"provider",
116+
HTTP_SERVER_REFERENCE,
117+
"--ctl-port",
118+
&ctl_port,
119+
])
120+
.await;
110121

111122
let mut evt = wait_for_event(&mut stream, DEFAULT_TIMEOUT_DURATION).await;
112123
if let Event::ProviderStarted(provider) = evt.as_ref() {
@@ -128,6 +139,8 @@ async fn test_event_stream() -> Result<()> {
128139
ECHO_ACTOR_ID,
129140
HTTP_SERVER_PROVIDER_ID,
130141
CONTRACT_ID,
142+
"--ctl-port",
143+
&ctl_port,
131144
])
132145
.await;
133146

@@ -153,7 +166,15 @@ async fn test_event_stream() -> Result<()> {
153166
evt.ack().await.expect("Should be able to ack event");
154167

155168
// Delete link
156-
helpers::run_wash_command(["link", "del", ECHO_ACTOR_ID, CONTRACT_ID]).await;
169+
helpers::run_wash_command([
170+
"link",
171+
"del",
172+
ECHO_ACTOR_ID,
173+
CONTRACT_ID,
174+
"--ctl-port",
175+
&ctl_port,
176+
])
177+
.await;
157178

158179
let mut evt = wait_for_event(&mut stream, LINK_OPERATION_TIMEOUT_DURATION).await;
159180
if let Event::LinkdefDeleted(link) = evt.as_ref() {
@@ -174,7 +195,7 @@ async fn test_event_stream() -> Result<()> {
174195

175196
// Stop provider
176197
let host_id = serde_json::from_slice::<HostResponse>(
177-
&helpers::run_wash_command(["get", "hosts", "-o", "json"]).await,
198+
&helpers::run_wash_command(["get", "hosts", "-o", "json", "--ctl-port", &ctl_port]).await,
178199
)
179200
.unwrap()
180201
.hosts[0]
@@ -190,6 +211,8 @@ async fn test_event_stream() -> Result<()> {
190211
HTTP_SERVER_PROVIDER_ID,
191212
LINK_NAME,
192213
CONTRACT_ID,
214+
"--ctl-port",
215+
&ctl_port,
193216
])
194217
.await;
195218

@@ -206,7 +229,15 @@ async fn test_event_stream() -> Result<()> {
206229
evt.ack().await.expect("Should be able to ack event");
207230

208231
// Stop an actor
209-
helpers::run_wash_command(["stop", "actor", &host_id, ECHO_ACTOR_ID]).await;
232+
helpers::run_wash_command([
233+
"stop",
234+
"actor",
235+
&host_id,
236+
ECHO_ACTOR_ID,
237+
"--ctl-port",
238+
&ctl_port,
239+
])
240+
.await;
210241

211242
let mut evt = wait_for_event(&mut stream, DEFAULT_TIMEOUT_DURATION).await;
212243
if let Event::ActorStopped(actor) = evt.as_ref() {
@@ -221,7 +252,7 @@ async fn test_event_stream() -> Result<()> {
221252
evt.ack().await.expect("Should be able to ack event");
222253

223254
// Stop the host
224-
helpers::run_wash_command(["stop", "host", &host_id]).await;
255+
helpers::run_wash_command(["stop", "host", &host_id, "--ctl-port", &ctl_port]).await;
225256

226257
let mut evt = wait_for_event(&mut stream, DEFAULT_TIMEOUT_DURATION).await;
227258
if let Event::HostStopped(host) = evt.as_ref() {
@@ -250,8 +281,12 @@ async fn test_nack_and_rereceive() -> Result<()> {
250281

251282
let mut stream = get_event_consumer(config.nats_url()).await;
252283

284+
let ctl_port = config
285+
.nats_port
286+
.unwrap_or(crate::helpers::DEFAULT_NATS_PORT)
287+
.to_string();
253288
// Start an actor
254-
helpers::run_wash_command(["start", "actor", ECHO_REFERENCE]).await;
289+
helpers::run_wash_command(["start", "actor", ECHO_REFERENCE, "--ctl-port", &ctl_port]).await;
255290

256291
// Get the event and then nack it
257292
let mut evt = wait_for_event(&mut stream, DEFAULT_TIMEOUT_DURATION).await;

tests/helpers.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use tokio::process::Command;
1616
use anyhow::Result;
1717
use wadm::consumers::{CommandConsumer, ScopedMessage};
1818

19-
const DEFAULT_WASMCLOUD_PORT: u16 = 4000;
20-
const DEFAULT_NATS_PORT: u16 = 4222;
19+
pub const DEFAULT_NATS_PORT: u16 = 4222;
2120
pub const ECHO_ACTOR_ID: &str = "MBCFOPM6JW2APJLXJD3Z5O4CN7CPYJ2B4FTKLJUR5YR5MITIU7HD3WD5";
2221
pub const HTTP_SERVER_PROVIDER_ID: &str =
2322
"VAG3QITQQ2ODAOWB5TTQSDJ53XK3SHBEIFNK4AYJ5RKAX2UNSCAPHA5M";
@@ -63,32 +62,19 @@ pub struct TestWashConfig {
6362

6463
/// Only connect to pre-existing NATS instance
6564
pub nats_connect_only: bool,
66-
67-
/// Port on which to run wasmCloud (via `wash up`)
68-
pub wasmcloud_port: Option<u16>,
6965
}
7066

7167
impl TestWashConfig {
7268
/// Build a test wash configuration with randomized ports
7369
pub async fn random() -> Result<TestWashConfig> {
7470
let nats_port = Some(get_random_tcp_port());
75-
let wasmcloud_port = Some(get_random_tcp_port());
7671

7772
Ok(TestWashConfig {
7873
nats_port,
79-
wasmcloud_port,
8074
..TestWashConfig::default()
8175
})
8276
}
8377

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-
9278
/// Get the NATS URL for this config
9379
pub fn nats_url(&self) -> String {
9480
format!("127.0.0.1:{}", self.nats_port.unwrap_or(DEFAULT_NATS_PORT))
@@ -98,22 +84,29 @@ impl TestWashConfig {
9884
/// Start a local wash instance
9985
async fn start_wash_instance(cfg: &TestWashConfig) -> Result<CleanupGuard> {
10086
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();
10590

10691
// 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+
]);
108103
if cfg.nats_connect_only {
109104
args.push("--nats-connect-only");
110105
}
111106

112107
// Build the command
113108
let mut cmd = Command::new("wash");
114109
cmd.args(&args)
115-
.env("WASMCLOUD_PORT", &wasmcloud_port)
116-
.env("WASMCLOUD_DASHBOARD_PORT", &wasmcloud_port)
117110
.stderr(std::process::Stdio::null())
118111
.stdout(std::process::Stdio::null());
119112

@@ -124,32 +117,45 @@ async fn start_wash_instance(cfg: &TestWashConfig) -> Result<CleanupGuard> {
124117
};
125118

126119
let output = cmd.status().await.expect("Unable to run detached wash up");
127-
128120
assert!(output.success(), "Error trying to start host",);
129121

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+
}
132147

133148
Ok(guard)
134149
}
135150

136151
/// Set up and run a wash instance that can be used for a test
137152
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 {
149156
child: None,
150-
already_running: true,
151-
},
152-
}
157+
already_running: false,
158+
})
153159
}
154160

155161
pub async fn wait_for_server(url: &str) {

0 commit comments

Comments
 (0)