Skip to content

Commit

Permalink
chore: Fix broken unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed May 28, 2024
1 parent 21546cf commit f8df7be
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 25 deletions.
17 changes: 17 additions & 0 deletions drivers/rust/driver/Cargo.lock

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

1 change: 1 addition & 0 deletions drivers/rust/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ base64 = "0.21.7"
[dev-dependencies]
env_logger = "0.11.3"
expectest = "0.12.0"
pretty_assertions = "1.4.0"
tempdir = "0.3.7"
test-log = "0.2.15"

Expand Down
60 changes: 38 additions & 22 deletions drivers/rust/driver/src/catalogue_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ pub struct CatalogueEntry {
pub values: HashMap<String, String>
}

impl Default for CatalogueEntry {
fn default() -> Self {
CatalogueEntry {
entry_type: CatalogueEntryType::CONTENT_MATCHER,
provider_type: CatalogueEntryProviderType::CORE,
plugin: None,
key: "".to_string(),
values: Default::default(),
}
}
}

/// Register the entries in the global catalogue
pub fn register_plugin_entries(plugin: &PactPluginManifest, catalogue_list: Vec<CatalogueEntry>) {
trace!("register_plugin_entries({:?}, {:?})", plugin, catalogue_list);
Expand Down Expand Up @@ -251,10 +263,8 @@ pub fn all_entries() -> Vec<CatalogueEntry> {

#[cfg(test)]
mod tests {
use expectest::prelude::*;
use maplit::hashmap;

use crate::proto::catalogue_entry;
use pretty_assertions::assert_eq;

use super::*;

Expand All @@ -266,25 +276,31 @@ mod tests {
.. PactPluginManifest::default()
};
let entries = vec![
ProtoCatalogueEntry {
r#type: catalogue_entry::EntryType::ContentMatcher as i32,
CatalogueEntry {
entry_type: CatalogueEntryType::CONTENT_MATCHER,
provider_type: CatalogueEntryProviderType::PLUGIN,
key: "protobuf".to_string(),
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() },
.. CatalogueEntry::default()
},
ProtoCatalogueEntry {
r#type: catalogue_entry::EntryType::ContentGenerator as i32,
CatalogueEntry {
entry_type: CatalogueEntryType::CONTENT_GENERATOR,
provider_type: CatalogueEntryProviderType::PLUGIN,
key: "protobuf".to_string(),
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() },
.. CatalogueEntry::default()
},
ProtoCatalogueEntry {
r#type: catalogue_entry::EntryType::Transport as i32,
CatalogueEntry {
entry_type: CatalogueEntryType::TRANSPORT,
provider_type: CatalogueEntryProviderType::PLUGIN,
key: "grpc".to_string(),
values: hashmap!{}
values: hashmap!{},
.. CatalogueEntry::default()
}
];

// When
register_plugin_entries(&manifest, &entries);
register_plugin_entries(&manifest, entries);

// Then
let matcher_entry = lookup_entry("content-matcher/protobuf");
Expand All @@ -293,26 +309,26 @@ mod tests {

remove_plugin_entries("sets_plugin_catalogue_entries_correctly");

expect!(matcher_entry).to(be_some().value(CatalogueEntry {
assert_eq!(matcher_entry.unwrap(), CatalogueEntry {
entry_type: CatalogueEntryType::CONTENT_MATCHER,
provider_type: CatalogueEntryProviderType::PLUGIN,
plugin: Some(manifest.clone()),
plugin: None,
key: "protobuf".to_string(),
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
}));
expect!(generator_entry).to(be_some().value(CatalogueEntry {
});
assert_eq!(generator_entry.unwrap(), CatalogueEntry {
entry_type: CatalogueEntryType::CONTENT_GENERATOR,
provider_type: CatalogueEntryProviderType::PLUGIN,
plugin: Some(manifest.clone()),
plugin: None,
key: "protobuf".to_string(),
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
}));
expect!(transport_entry).to(be_some().value(CatalogueEntry {
});
assert_eq!(transport_entry.unwrap(), CatalogueEntry {
entry_type: CatalogueEntryType::TRANSPORT,
provider_type: CatalogueEntryProviderType::PLUGIN,
plugin: Some(manifest.clone()),
plugin: None,
key: "grpc".to_string(),
values: hashmap!{}
}));
});
}
}
139 changes: 138 additions & 1 deletion drivers/rust/driver/src/grpc_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use std::collections::HashMap;
use std::path::PathBuf;
use std::process::Stdio;
#[cfg(windows)] use std::process::Command;
use std::process::Stdio;
use std::str::from_utf8;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -828,5 +828,142 @@ pub(crate) async fn start_plugin_process(manifest: &PactPluginManifest) -> anyho

#[cfg(test)]
pub(crate) mod tests {
use async_trait::async_trait;
use expectest::prelude::*;
use maplit::hashmap;

use crate::catalogue_manager::{CatalogueEntry, CatalogueEntryProviderType, CatalogueEntryType, lookup_entry, remove_plugin_entries};
use crate::grpc_plugin::{init_handshake, PactPluginRpc};
use crate::plugin_models::PactPluginManifest;
use crate::proto::{
Catalogue,
catalogue_entry,
CatalogueEntry as ProtoCatalogueEntry,
CompareContentsRequest,
CompareContentsResponse,
ConfigureInteractionRequest,
ConfigureInteractionResponse,
GenerateContentRequest,
GenerateContentResponse,
InitPluginRequest,
InitPluginResponse,
MockServerRequest,
MockServerResults,
ShutdownMockServerRequest,
ShutdownMockServerResponse,
StartMockServerRequest,
StartMockServerResponse,
VerificationPreparationRequest,
VerificationPreparationResponse,
VerifyInteractionRequest,
VerifyInteractionResponse
};

#[derive(Default, Debug, Clone)]
struct MockPlugin {}

#[async_trait]
impl PactPluginRpc for MockPlugin {
async fn init_plugin(&mut self, request: InitPluginRequest) -> anyhow::Result<InitPluginResponse> {
let entries = vec![
ProtoCatalogueEntry {
r#type: catalogue_entry::EntryType::ContentMatcher as i32,
key: "protobuf".to_string(),
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
},
ProtoCatalogueEntry {
r#type: catalogue_entry::EntryType::ContentGenerator as i32,
key: "protobuf".to_string(),
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
},
ProtoCatalogueEntry {
r#type: catalogue_entry::EntryType::Transport as i32,
key: "grpc".to_string(),
values: hashmap!{}
}
];
Ok(InitPluginResponse {
catalogue: entries
})
}

async fn compare_contents(&self, _request: CompareContentsRequest) -> anyhow::Result<CompareContentsResponse> {
todo!()
}

async fn configure_interaction(&self, _request: ConfigureInteractionRequest) -> anyhow::Result<ConfigureInteractionResponse> {
todo!()
}

async fn generate_content(&self, _request: GenerateContentRequest) -> anyhow::Result<GenerateContentResponse> {
todo!()
}

async fn start_mock_server(&self, _request: StartMockServerRequest) -> anyhow::Result<StartMockServerResponse> {
todo!()
}

async fn shutdown_mock_server(&self, _request: ShutdownMockServerRequest) -> anyhow::Result<ShutdownMockServerResponse> {
todo!()
}

async fn get_mock_server_results(&self, _request: MockServerRequest) -> anyhow::Result<MockServerResults> {
todo!()
}

async fn prepare_interaction_for_verification(&self, _request: VerificationPreparationRequest) -> anyhow::Result<VerificationPreparationResponse> {
todo!()
}

async fn verify_interaction(&self, _request: VerifyInteractionRequest) -> anyhow::Result<VerifyInteractionResponse> {
todo!()
}

async fn update_catalogue(&self, _request: Catalogue) -> anyhow::Result<()> {
todo!()
}
}

#[tokio::test]
async fn init_handshake_sets_plugin_catalogue_entries_correctly() {
// Given
let manifest = PactPluginManifest {
name: "init_handshake_sets_plugin_catalogue_entries_correctly".to_string(),
.. PactPluginManifest::default()
};

let mut mock_plugin = MockPlugin {};

// When
init_handshake(&manifest, &mut mock_plugin).await.unwrap();

// Then
let matcher_entry = lookup_entry("content-matcher/protobuf");
let generator_entry = lookup_entry("content-generator/protobuf");
let transport_entry = lookup_entry("transport/grpc");

remove_plugin_entries("init_handshake_sets_plugin_catalogue_entries_correctly");

expect!(matcher_entry).to(be_some().value(CatalogueEntry {
entry_type: CatalogueEntryType::CONTENT_MATCHER,
provider_type: CatalogueEntryProviderType::PLUGIN,
plugin: Some(manifest.clone()),
key: "protobuf".to_string(),
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
}));
expect!(generator_entry).to(be_some().value(CatalogueEntry {
entry_type: CatalogueEntryType::CONTENT_GENERATOR,
provider_type: CatalogueEntryProviderType::PLUGIN,
plugin: Some(manifest.clone()),
key: "protobuf".to_string(),
values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
}));
expect!(transport_entry).to(be_some().value(CatalogueEntry {
entry_type: CatalogueEntryType::TRANSPORT,
provider_type: CatalogueEntryProviderType::PLUGIN,
plugin: Some(manifest.clone()),
key: "grpc".to_string(),
values: hashmap!{}
}));
}
}
4 changes: 2 additions & 2 deletions drivers/rust/driver/src/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ async fn initialise_plugin<'a>(
}
"lua" => {
#[cfg(feature = "lua")] {
let mut plugin = start_lua_plugin(manifest)?;
let plugin = start_lua_plugin(manifest)?;
debug!("Plugin started OK ({:?}), sending init message", plugin);

plugin.init()?;

// This causes a deadlock
// TODO: This causes a deadlock
//publish_updated_catalogue();

let arc = Arc::new(plugin);
Expand Down

0 comments on commit f8df7be

Please sign in to comment.