Skip to content

Commit

Permalink
refactor: make e2e tests assertions more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraynaud committed Dec 20, 2024
1 parent 92470a7 commit 2475894
Showing 1 changed file with 61 additions and 14 deletions.
75 changes: 61 additions & 14 deletions mithril-test-lab/mithril-end-to-end/src/assertions/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ pub async fn assert_node_producing_mithril_stake_distribution(
let url = format!("{aggregator_endpoint}/artifact/mithril-stake-distributions");
info!("Waiting for the aggregator to produce a mithril stake distribution");

// todo: reduce the number of attempts if we can reduce the delay between two immutables
match attempt!(45, Duration::from_millis(2000), {
async fn assert_attempt(url: String) -> StdResult<Option<String>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<MithrilStakeDistributionListMessage>().await.as_deref() {
Expand All @@ -37,6 +36,11 @@ pub async fn assert_node_producing_mithril_stake_distribution(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

// todo: reduce the number of attempts if we can reduce the delay between two immutables
match attempt!(45, Duration::from_millis(2000), {
assert_attempt(url.clone()).await
}) {
AttemptResult::Ok(hash) => {
info!("Aggregator produced a mithril stake distribution"; "hash" => &hash);
Expand All @@ -61,7 +65,7 @@ pub async fn assert_signer_is_signing_mithril_stake_distribution(
expected_epoch_min
);

match attempt!(10, Duration::from_millis(1000), {
async fn assert_attempt(url: String, expected_epoch_min:Epoch) -> StdResult<Option<MithrilStakeDistributionMessage>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<MithrilStakeDistributionMessage>().await {
Expand All @@ -78,6 +82,10 @@ pub async fn assert_signer_is_signing_mithril_stake_distribution(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

match attempt!(10, Duration::from_millis(1000), {
assert_attempt(url.clone(), expected_epoch_min).await
}) {
AttemptResult::Ok(stake_distribution) => {
// todo: assert that the mithril stake distribution is really signed
Expand All @@ -95,8 +103,7 @@ pub async fn assert_node_producing_snapshot(aggregator_endpoint: &str) -> StdRes
let url = format!("{aggregator_endpoint}/artifact/snapshots");
info!("Waiting for the aggregator to produce a snapshot");

// todo: reduce the number of attempts if we can reduce the delay between two immutables
match attempt!(45, Duration::from_millis(2000), {
async fn assert_attempt(url: String) -> StdResult<Option<String>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<Vec<SnapshotMessage>>().await.as_deref() {
Expand All @@ -108,6 +115,11 @@ pub async fn assert_node_producing_snapshot(aggregator_endpoint: &str) -> StdRes
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

// todo: reduce the number of attempts if we can reduce the delay between two immutables
match attempt!(45, Duration::from_millis(2000), {
assert_attempt(url.clone()).await
}) {
AttemptResult::Ok(digest) => {
info!("Aggregator produced a snapshot"; "digest" => &digest);
Expand All @@ -132,7 +144,7 @@ pub async fn assert_signer_is_signing_snapshot(
expected_epoch_min
);

match attempt!(10, Duration::from_millis(1000), {
async fn assert_attempt(url: String, expected_epoch_min:Epoch) -> StdResult<Option<SnapshotMessage>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<SnapshotMessage>().await {
Expand All @@ -149,6 +161,10 @@ pub async fn assert_signer_is_signing_snapshot(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

match attempt!(10, Duration::from_millis(1000), {
assert_attempt(url.clone(), expected_epoch_min).await
}) {
AttemptResult::Ok(snapshot) => {
// todo: assert that the snapshot is really signed
Expand All @@ -168,8 +184,7 @@ pub async fn assert_node_producing_cardano_database_snapshot(
let url = format!("{aggregator_endpoint}/artifact/cardano-database");
info!("Waiting for the aggregator to produce a Cardano database snapshot");

// todo: reduce the number of attempts if we can reduce the delay between two immutables
match attempt!(45, Duration::from_millis(2000), {
async fn assert_attempt(url: String) -> StdResult<Option<String>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response
Expand All @@ -187,6 +202,11 @@ pub async fn assert_node_producing_cardano_database_snapshot(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

// todo: reduce the number of attempts if we can reduce the delay between two immutables
match attempt!(45, Duration::from_millis(2000), {
assert_attempt(url.clone()).await
}) {
AttemptResult::Ok(merkle_root) => {
info!("Aggregator produced a Cardano database snapshot"; "merkle_root" => &merkle_root);
Expand All @@ -211,7 +231,10 @@ pub async fn assert_signer_is_signing_cardano_database_snapshot(
expected_epoch_min
);

match attempt!(10, Duration::from_millis(1000), {
async fn assert_attempt(
url: String,
expected_epoch_min: Epoch,
) -> StdResult<Option<CardanoDatabaseSnapshotMessage>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<CardanoDatabaseSnapshotMessage>().await {
Expand All @@ -228,6 +251,10 @@ pub async fn assert_signer_is_signing_cardano_database_snapshot(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

match attempt!(10, Duration::from_millis(1000), {
assert_attempt(url.clone(), expected_epoch_min).await
}) {
AttemptResult::Ok(snapshot) => {
info!("Signer signed a snapshot"; "certificate_hash" => &snapshot.certificate_hash);
Expand All @@ -246,7 +273,7 @@ pub async fn assert_node_producing_cardano_transactions(
let url = format!("{aggregator_endpoint}/artifact/cardano-transactions");
info!("Waiting for the aggregator to produce a Cardano transactions artifact");

match attempt!(45, Duration::from_millis(2000), {
async fn assert_attempt(url: String) -> StdResult<Option<String>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response
Expand All @@ -264,6 +291,10 @@ pub async fn assert_node_producing_cardano_transactions(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

match attempt!(45, Duration::from_millis(2000), {
assert_attempt(url.clone()).await
}) {
AttemptResult::Ok(hash) => {
info!("Aggregator produced a Cardano transactions artifact"; "hash" => &hash);
Expand All @@ -288,7 +319,7 @@ pub async fn assert_signer_is_signing_cardano_transactions(
expected_epoch_min
);

match attempt!(10, Duration::from_millis(1000), {
async fn assert_attempt(url: String, expected_epoch_min:Epoch) -> StdResult<Option<CardanoTransactionSnapshotMessage>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<CardanoTransactionSnapshotMessage>().await {
Expand All @@ -305,6 +336,10 @@ pub async fn assert_signer_is_signing_cardano_transactions(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

match attempt!(10, Duration::from_millis(1000), {
assert_attempt(url.clone(), expected_epoch_min).await
}) {
AttemptResult::Ok(artifact) => {
info!("Signer signed a Cardano transactions artifact"; "certificate_hash" => &artifact.certificate_hash);
Expand All @@ -323,7 +358,7 @@ pub async fn assert_node_producing_cardano_stake_distribution(
let url = format!("{aggregator_endpoint}/artifact/cardano-stake-distributions");
info!("Waiting for the aggregator to produce a Cardano stake distribution");

match attempt!(45, Duration::from_millis(2000), {
async fn assert_attempt(url: String) -> StdResult<Option<(String,Epoch)>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<CardanoStakeDistributionListMessage>().await.as_deref() {
Expand All @@ -335,6 +370,10 @@ pub async fn assert_node_producing_cardano_stake_distribution(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

match attempt!(45, Duration::from_millis(2000), {
assert_attempt(url.clone()).await
}) {
AttemptResult::Ok((hash, epoch)) => {
info!("Aggregator produced a Cardano stake distribution"; "hash" => &hash, "epoch" => #?epoch);
Expand All @@ -359,7 +398,7 @@ pub async fn assert_signer_is_signing_cardano_stake_distribution(
expected_epoch_min
);

match attempt!(10, Duration::from_millis(1000), {
async fn assert_attempt(url: String, expected_epoch_min:Epoch) -> StdResult<Option<CardanoStakeDistributionMessage>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<CardanoStakeDistributionMessage>().await {
Expand All @@ -376,6 +415,10 @@ pub async fn assert_signer_is_signing_cardano_stake_distribution(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

match attempt!(10, Duration::from_millis(1000), {
assert_attempt(url.clone(), expected_epoch_min).await
}) {
AttemptResult::Ok(cardano_stake_distribution) => {
info!("Signer signed a Cardano stake distribution"; "certificate_hash" => &cardano_stake_distribution.certificate_hash);
Expand All @@ -395,7 +438,7 @@ pub async fn assert_is_creating_certificate_with_enough_signers(
) -> StdResult<()> {
let url = format!("{aggregator_endpoint}/certificate/{certificate_hash}");

match attempt!(10, Duration::from_millis(1000), {
async fn assert_attempt(url: String) -> StdResult<Option<CertificateMessage>> {
match reqwest::get(url.clone()).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<CertificateMessage>().await {
Expand All @@ -407,6 +450,10 @@ pub async fn assert_is_creating_certificate_with_enough_signers(
},
Err(err) => Err(anyhow!(err).context(format!("Request to `{url}` failed"))),
}
}

match attempt!(10, Duration::from_millis(1000), {
assert_attempt(url.clone()).await
}) {
AttemptResult::Ok(certificate) => {
info!("Aggregator produced a certificate"; "certificate" => ?certificate);
Expand Down

0 comments on commit 2475894

Please sign in to comment.