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 <[email protected]>
Co-authored-by: Matthias Twardawski <[email protected]>
  • Loading branch information
2 people authored and mbfm committed Aug 28, 2024
1 parent 04e75a5 commit 12c5441
Showing 1 changed file with 50 additions and 24 deletions.
74 changes: 50 additions & 24 deletions opendut-carl/src/actions/peers/get_peer_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::resources::manager::{ResourcesManagerRef};
use opendut_carl_api::carl::peer::{GetPeerStateError};
use crate::resources::manager::ResourcesManagerRef;
use opendut_carl_api::carl::peer::GetPeerStateError;
use opendut_types::peer::state::PeerState;
use opendut_types::peer::{PeerDescriptor, PeerId};
use tracing::{debug, error, info};
Expand All @@ -21,12 +21,13 @@ pub async fn get_peer_state(params: GetPeerStateParams) -> Result<PeerState, Get

let peer_state = resources_manager.resources_mut(|resources| {
let peer_state = resources.get::<PeerState>(peer_id)
.map_err(|cause| GetPeerStateError::Internal { peer_id ,cause: cause.to_string() })?;
.map_err(|cause| GetPeerStateError::Internal { peer_id, cause: cause.to_string() })?;
match peer_state {
Some(peer_state) => { Ok(peer_state) }
None => {
match resources.get::<PeerDescriptor>(peer_id)
.map_err(|cause| GetPeerStateError::Internal { peer_id ,cause: cause.to_string() })? {
let peer_descriptor = resources.get::<PeerDescriptor>(peer_id)
.map_err(|cause| GetPeerStateError::Internal { peer_id, cause: cause.to_string() })?;
match peer_descriptor {
Some(_) => { Ok(PeerState::Down) }
None => { Err(GetPeerStateError::PeerNotFound { peer_id }) }
}
Expand All @@ -45,59 +46,84 @@ pub async fn get_peer_state(params: GetPeerStateParams) -> Result<PeerState, Get

#[cfg(test)]
mod tests {
use std::sync::Arc;
use crate::actions;
use crate::actions::peers::testing::{fixture, store_peer_descriptor_options, Fixture};
use crate::actions::{get_peer_state, GetPeerStateParams, StorePeerDescriptorOptions, StorePeerDescriptorParams};
use crate::resources::manager::{ResourcesManager, ResourcesManagerRef};
use googletest::prelude::*;
use rstest::rstest;
use opendut_carl_api::carl::peer::GetPeerStateError;
use opendut_types::peer::PeerId;
use opendut_types::peer::state::PeerState;
use crate::actions;
use crate::actions::{GetPeerStateParams, StorePeerDescriptorOptions, StorePeerDescriptorParams};
use crate::actions::peers::testing::{fixture, store_peer_descriptor_options, Fixture};
use crate::resources::manager::ResourcesManager;
use opendut_types::peer::{PeerDescriptor, PeerId};
use rstest::rstest;
use std::sync::Arc;

#[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
}

#[test_with::no_env(SKIP_DATABASE_CONTAINER_TESTS)]
#[rstest]
#[tokio::test]
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
}

#[test_with::no_env(SKIP_DATABASE_CONTAINER_TESTS)]
#[rstest]
#[tokio::test]
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_that!(peer_state_result, err(eq(GetPeerStateError::PeerNotFound { peer_id })));
assert_that!(peer_state_result, err(eq(GetPeerStateError::PeerNotFound { peer_id: not_existing_peer_id })));
Ok(())
}
}

0 comments on commit 12c5441

Please sign in to comment.