Skip to content

Commit

Permalink
Merge pull request #1543 from input-output-hk/ensemble/1524/improve-h…
Browse files Browse the repository at this point in the history
…ttp-tests

Improve aggregator HTTP server tests
  • Loading branch information
dlachaume authored Mar 4, 2024
2 parents 3f93e35 + e8dcecf commit 6e95b18
Show file tree
Hide file tree
Showing 20 changed files with 916 additions and 369 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/mithril-persistence/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-persistence"
version = "0.1.1"
version = "0.1.2"
description = "Common types, interfaces, and utilities to persist data for Mithril nodes."
authors = { workspace = true }
edition = { workspace = true }
Expand Down
33 changes: 29 additions & 4 deletions internal/mithril-persistence/src/store/adapter/dumb_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use super::{AdapterError, StoreAdapter};
use anyhow::anyhow;
use async_trait::async_trait;

/// A [StoreAdapter] that store one fixed data record, for testing purpose.
pub struct DumbStoreAdapter<K, R> {
last_key: Option<K>,
last_value: Option<R>,
error: Option<String>,
}

impl<K, R> DumbStoreAdapter<K, R> {
Expand All @@ -13,6 +15,15 @@ impl<K, R> DumbStoreAdapter<K, R> {
Self {
last_key: None,
last_value: None,
error: None,
}
}

/// DumbStoreAdapter factory that returns error when 'get_record' is called.
pub fn new_failing_adapter(error: &str) -> Self {
Self {
error: Some(error.to_string()),
..Self::new()
}
}
}
Expand Down Expand Up @@ -47,10 +58,15 @@ where
}

async fn get_record(&self, key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> {
if self.record_exists(key).await? {
Ok(self.last_value.as_ref().cloned())
} else {
Ok(None)
match &self.error {
Some(error) => Err(AdapterError::GeneralError(anyhow!(error.clone()))),
None => {
if self.record_exists(key).await? {
Ok(self.last_value.as_ref().cloned())
} else {
Ok(None)
}
}
}
}

Expand Down Expand Up @@ -208,4 +224,13 @@ mod tests {

assert_eq!(0, records.count());
}

#[tokio::test]
async fn test_return_error_calling_get_record() {
let adapter: DumbStoreAdapter<String, String> =
DumbStoreAdapter::new_failing_adapter("error");
let result = adapter.get_record(&"key".to_string()).await;

assert!(result.is_err());
}
}
2 changes: 1 addition & 1 deletion mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-aggregator"
version = "0.4.43"
version = "0.4.44"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ pub mod tests {
};
use mithril_persistence::sqlite::HydrationError;
use serde_json::Value::Null;
use warp::{http::Method, test::request};
use warp::{
http::{Method, StatusCode},
test::request,
};

use super::*;

Expand Down Expand Up @@ -150,7 +153,9 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::OK,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -179,7 +184,9 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::INTERNAL_SERVER_ERROR,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -216,11 +223,13 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::OK,
)
.unwrap();
}

#[tokio::test]
async fn test_cardano_transaction_ok_norecord() {
async fn test_cardano_transaction_return_404_not_found_when_no_record() {
let mut mock_http_message_service = MockMessageService::new();
mock_http_message_service
.expect_get_cardano_transaction_message()
Expand All @@ -245,7 +254,9 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::NOT_FOUND,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -274,6 +285,8 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::INTERNAL_SERVER_ERROR,
)
.unwrap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ pub mod tests {
};
use mithril_persistence::sqlite::HydrationError;
use serde_json::Value::Null;
use warp::{http::Method, test::request};
use warp::{
http::{Method, StatusCode},
test::request,
};

use super::*;

Expand Down Expand Up @@ -151,7 +154,9 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::OK,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -180,7 +185,9 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::INTERNAL_SERVER_ERROR,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -217,11 +224,13 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::OK,
)
.unwrap();
}

#[tokio::test]
async fn test_mithril_stake_distribution_ok_norecord() {
async fn test_mithril_stake_distribution_returns_404_no_found_when_no_record() {
let mut mock_http_message_service = MockMessageService::new();
mock_http_message_service
.expect_get_mithril_stake_distribution_message()
Expand All @@ -246,7 +255,9 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::NOT_FOUND,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -275,6 +286,8 @@ pub mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::INTERNAL_SERVER_ERROR,
)
.unwrap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ mod tests {
};
use mithril_persistence::sqlite::HydrationError;
use serde_json::Value::Null;
use warp::{http::Method, test::request};
use warp::{
http::{Method, StatusCode},
test::request,
};

use super::*;

Expand Down Expand Up @@ -282,7 +285,9 @@ mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::OK,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -311,7 +316,9 @@ mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::INTERNAL_SERVER_ERROR,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -348,11 +355,13 @@ mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::OK,
)
.unwrap();
}

#[tokio::test]
async fn test_snapshot_digest_get_ok_nosnapshot() {
async fn test_snapshot_digest_returns_404_not_found_when_no_snapshot() {
let mut mock_http_message_service = MockMessageService::new();
mock_http_message_service
.expect_get_snapshot_message()
Expand All @@ -377,7 +386,9 @@ mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::NOT_FOUND,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -406,11 +417,13 @@ mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::INTERNAL_SERVER_ERROR,
)
.unwrap();
}

#[tokio::test]
async fn test_snapshot_download_get_ok() {
async fn test_snapshot_local_download_returns_302_found_when_the_snapshot_exists() {
let signed_entity = create_signed_entities(
SignedEntityType::CardanoImmutableFilesFull(Beacon::default()),
fake_data::snapshots(1),
Expand All @@ -435,18 +448,19 @@ mod tests {
.reply(&setup_router(Arc::new(dependency_manager)))
.await;

APISpec::verify_conformity(
APISpec::get_all_spec_files(),
method,
path,
"application/gzip",
&Null,
&response,
assert_eq!(response.status(), StatusCode::FOUND);
let location = std::str::from_utf8(response.headers()["location"].as_bytes())
.unwrap()
.to_string();
assert!(
location.contains(&format!("/{SERVER_BASE_PATH}/snapshot_download/testnet")),
"Expected value '/{SERVER_BASE_PATH}/snapshot_download/testnet' not found in {}",
location
);
}

#[tokio::test]
async fn test_snapshot_download_get_ok_nosnapshot() {
async fn test_snapshot_download_returns_404_not_found_when_no_snapshot() {
let mut mock_signed_entity_service = MockSignedEntityService::new();
mock_signed_entity_service
.expect_get_signed_snapshot_by_id()
Expand All @@ -471,7 +485,9 @@ mod tests {
"application/gzip",
&Null,
&response,
);
&StatusCode::NOT_FOUND,
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -500,6 +516,8 @@ mod tests {
"application/json",
&Null,
&response,
);
&StatusCode::INTERNAL_SERVER_ERROR,
)
.unwrap();
}
}
Loading

0 comments on commit 6e95b18

Please sign in to comment.