Skip to content

Commit

Permalink
Issue-294: add database tests for getting peer state
Browse files Browse the repository at this point in the history
Co-authored-by: Anna Völker <anna.voelker@mercedes-benz.com>
Co-authored-by: Matthias Twardawski <matthias.twardawski@mercedes-benz.com>
  • Loading branch information
2 people authored and mbfm committed Aug 28, 2024
1 parent aedc4f2 commit 898b0f3
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions opendut-carl/src/actions/peers/get_peer_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,58 +47,86 @@ pub async fn get_peer_state(params: GetPeerStateParams) -> Result<PeerState, Get
mod tests {
use std::sync::Arc;
use googletest::assert_that;
use googletest::matchers::{eq};
use googletest::matchers::{eq, none};
use rstest::rstest;
use opendut_types::peer::PeerId;
use opendut_types::peer::{PeerDescriptor, PeerId};
use opendut_types::peer::state::PeerState;
use crate::actions;
use crate::actions::{GetPeerStateParams, StorePeerDescriptorOptions, StorePeerDescriptorParams};
use crate::actions::{get_peer_state, GetPeerStateParams, StorePeerDescriptorOptions, StorePeerDescriptorParams};
use crate::actions::peers::testing::{fixture, store_peer_descriptor_options, Fixture};
use crate::resources::manager::{ResourcesManager, ResourcesManagerRef};

#[rstest]
#[tokio::test]
async fn should_get_peer_state(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> {
async fn should_get_peer_state_down_in_memory(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> {
let resources_manager = ResourcesManager::new_in_memory();
should_get_peer_state(resources_manager, fixture, store_peer_descriptor_options).await
}

let resources_manager = fixture.resources_manager;
#[test_with::no_env(SKIP_DATABASE_CONTAINER_TESTS)]
#[rstest]
#[tokio::test]
#[ignore] //FIXME currently broken
async fn should_get_peer_state_down_in_database(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> {
let db = crate::persistence::database::testing::spawn_and_connect_resources_manager().await?;
should_get_peer_state(db.resources_manager, fixture, store_peer_descriptor_options).await
}

async fn should_get_peer_state(resources_manager: ResourcesManagerRef, fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> {
actions::store_peer_descriptor(StorePeerDescriptorParams {
resources_manager: Arc::clone(&resources_manager),
vpn: fixture.vpn,
peer_descriptor: fixture.peer_a_descriptor,
options: store_peer_descriptor_options,
}).await?;

let peer_state = actions::get_peer_state(GetPeerStateParams {

assert_that!(resources_manager.get::<PeerState>(fixture.peer_a_id).await?.as_ref(), none());

let peer_state = get_peer_state(GetPeerStateParams {
peer: fixture.peer_a_id,
resources_manager: Clone::clone(&resources_manager),
}).await?;

assert_that!(peer_state, eq(PeerState::Down));
assert_that!(resources_manager.get::<PeerState>(fixture.peer_a_id).await?.as_ref(), none());
Ok(())
}

#[rstest]
#[tokio::test]
async fn should_not_find_peer_for_id(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> {
async fn should_throw_error_if_peer_not_found_in_memory(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> {
let resources_manager = ResourcesManager::new_in_memory();
should_throw_error_if_peer_not_found(resources_manager, fixture, store_peer_descriptor_options).await
}

let resources_manager = fixture.resources_manager;

#[test_with::no_env(SKIP_DATABASE_CONTAINER_TESTS)]
#[rstest]
#[tokio::test]
#[ignore] //FIXME currently broken
async fn should_throw_error_if_peer_not_found_in_database(fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> {
let db = crate::persistence::database::testing::spawn_and_connect_resources_manager().await?;
should_throw_error_if_peer_not_found(db.resources_manager, fixture, store_peer_descriptor_options).await
}

async fn should_throw_error_if_peer_not_found(resources_manager: ResourcesManagerRef, fixture: Fixture, store_peer_descriptor_options: StorePeerDescriptorOptions) -> anyhow::Result<()> {
actions::store_peer_descriptor(StorePeerDescriptorParams {
resources_manager: Arc::clone(&resources_manager),
vpn: fixture.vpn,
peer_descriptor: fixture.peer_a_descriptor,
options: store_peer_descriptor_options,
}).await?;

let peer_id = PeerId::random();
let peer_state_result = actions::get_peer_state(GetPeerStateParams {
peer: peer_id,
let not_existing_peer_id = PeerId::random();
assert_that!(resources_manager.get::<PeerDescriptor>(not_existing_peer_id).await?.as_ref(), none());

let peer_state_result = get_peer_state(GetPeerStateParams {
peer: not_existing_peer_id,
resources_manager: Clone::clone(&resources_manager),
}).await;

assert!(peer_state_result.is_err());
let expected_error_message = format!("A peer with id <{peer_id}> could not be found!");
let expected_error_message = format!("A peer with id <{not_existing_peer_id}> could not be found!");
assert_that!(peer_state_result.unwrap_err().to_string(), eq(expected_error_message));
Ok(())
}
}
}

0 comments on commit 898b0f3

Please sign in to comment.