Skip to content

Commit 8da2f10

Browse files
committed
use omicron_test_utils::dev::poll
1 parent f5d4ec7 commit 8da2f10

File tree

5 files changed

+31
-50
lines changed

5 files changed

+31
-50
lines changed

Cargo.lock

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

end-to-end-tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ anyhow = { version = "1.0.58", features = ["backtrace"] }
99
base64 = "0.13.0"
1010
futures = "0.3.21"
1111
omicron-sled-agent = { path = "../sled-agent" }
12+
omicron-test-utils = { path = "../test-utils" }
1213
oxide-client = { path = "../oxide-client" }
1314
rand = "0.8.5"
1415
reqwest = { version = "0.11.11", default-features = false }

end-to-end-tests/src/bin/bootstrap.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
use anyhow::{bail, Result};
22
use end_to_end_tests::helpers::ctx::{build_client, nexus_addr, Context};
3-
use end_to_end_tests::helpers::{generate_name, try_loop};
3+
use end_to_end_tests::helpers::generate_name;
4+
use omicron_test_utils::dev::poll::{wait_for_condition, CondCheckError};
45
use oxide_client::types::{
56
ByteCount, DiskCreate, DiskSource, IpPoolCreate, IpRange, Ipv4Range,
67
};
78
use oxide_client::{ClientDisksExt, ClientIpPoolsExt, ClientOrganizationsExt};
89
use std::net::IpAddr;
910
use std::time::Duration;
10-
use tokio::time::sleep;
1111

1212
#[tokio::main]
1313
async fn main() -> Result<()> {
1414
let client = build_client()?;
1515

1616
// ===== ENSURE NEXUS IS UP ===== //
1717
eprintln!("waiting for nexus to come up...");
18-
try_loop(
18+
wait_for_condition(
1919
|| async {
20-
sleep(Duration::from_secs(1)).await;
21-
client.organization_list().send().await
20+
client
21+
.organization_list()
22+
.send()
23+
.await
24+
.map_err(|_| CondCheckError::<oxide_client::Error>::NotYet)
2225
},
23-
Duration::from_secs(300),
26+
&Duration::from_secs(1),
27+
&Duration::from_secs(300),
2428
)
2529
.await?;
2630

@@ -60,9 +64,8 @@ async fn main() -> Result<()> {
6064
eprintln!("ensuring datasets are ready...");
6165
let ctx = Context::from_client(client).await?;
6266
let disk_name = generate_name("disk")?;
63-
try_loop(
67+
wait_for_condition(
6468
|| async {
65-
sleep(Duration::from_secs(1)).await;
6669
ctx.client
6770
.disk_create()
6871
.organization_name(ctx.org_name.clone())
@@ -71,17 +74,16 @@ async fn main() -> Result<()> {
7174
name: disk_name.clone(),
7275
description: String::new(),
7376
disk_source: DiskSource::Blank {
74-
block_size: 512
75-
.try_into()
76-
.map_err(anyhow::Error::msg)?,
77+
block_size: 512.try_into().unwrap(),
7778
},
7879
size: ByteCount(1024 * 1024 * 1024),
7980
})
8081
.send()
8182
.await
82-
.map_err(anyhow::Error::from)
83+
.map_err(|_| CondCheckError::<oxide_client::Error>::NotYet)
8384
},
84-
Duration::from_secs(120),
85+
&Duration::from_secs(1),
86+
&Duration::from_secs(120),
8587
)
8688
.await?;
8789
ctx.client

end-to-end-tests/src/helpers/mod.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
pub mod ctx;
22

3-
use anyhow::{Context, Result};
3+
use anyhow::Result;
44
use oxide_client::types::Name;
55
use rand::{thread_rng, Rng};
6-
use std::future::Future;
7-
use std::time::{Duration, Instant};
86

97
pub fn generate_name(prefix: &str) -> Result<Name> {
108
format!("{}-{:x}", prefix, thread_rng().gen_range(0..0xfff_ffff_ffffu64))
119
.try_into()
1210
.map_err(anyhow::Error::msg)
1311
}
14-
15-
pub async fn try_loop<F, Fut, T, E>(mut f: F, timeout: Duration) -> Result<T>
16-
where
17-
F: FnMut() -> Fut,
18-
Fut: Future<Output = Result<T, E>>,
19-
Result<T, E>: Context<T, E>,
20-
{
21-
let start = Instant::now();
22-
loop {
23-
match f().await {
24-
Ok(t) => return Ok(t),
25-
Err(err) => {
26-
if Instant::now() - start > timeout {
27-
return Err(err).with_context(|| {
28-
format!(
29-
"try_loop timed out after {} seconds",
30-
timeout.as_secs_f64()
31-
)
32-
});
33-
}
34-
}
35-
}
36-
}
37-
}

end-to-end-tests/src/instance_launch.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#![cfg(test)]
22

3-
use crate::helpers::{ctx::Context, generate_name, try_loop};
3+
use crate::helpers::{ctx::Context, generate_name};
44
use anyhow::{ensure, Context as _, Result};
55
use futures::future::Ready;
6+
use omicron_test_utils::dev::poll::{wait_for_condition, CondCheckError};
67
use oxide_client::types::{
78
ByteCount, DiskCreate, DiskSource, Distribution, ExternalIpCreate,
89
GlobalImageCreate, ImageSource, InstanceCpuCount, InstanceCreate,
@@ -113,9 +114,11 @@ async fn instance_launch() -> Result<()> {
113114
// poll serial for login prompt, waiting 5 min max
114115
// (pulling disk blocks over HTTP is slow)
115116
eprintln!("waiting for serial console");
116-
let serial = try_loop(
117+
let serial = wait_for_condition(
117118
|| async {
118-
sleep(Duration::from_secs(5)).await;
119+
type Error =
120+
CondCheckError<oxide_client::Error<oxide_client::types::Error>>;
121+
119122
let data = String::from_utf8_lossy(
120123
&ctx.client
121124
.instance_serial_console()
@@ -129,14 +132,14 @@ async fn instance_launch() -> Result<()> {
129132
.data,
130133
)
131134
.into_owned();
132-
ensure!(
133-
data.contains("localshark login:"),
134-
"not yet booted\n{}",
135-
data
136-
);
137-
Ok(data)
135+
if data.contains("localshark login:") {
136+
Ok(data)
137+
} else {
138+
Err(Error::NotYet)
139+
}
138140
},
139-
Duration::from_secs(300),
141+
&Duration::from_secs(5),
142+
&Duration::from_secs(300),
140143
)
141144
.await?;
142145

0 commit comments

Comments
 (0)