Skip to content

Commit

Permalink
Merge pull request #1369 from cberkhoff/shard-infra-4
Browse files Browse the repository at this point in the history
[Sharding 4] Base testing support for sharding
  • Loading branch information
cberkhoff authored Oct 29, 2024
2 parents 4fd5e2b + 29258c5 commit 4d50a64
Show file tree
Hide file tree
Showing 6 changed files with 627 additions and 193 deletions.
24 changes: 18 additions & 6 deletions ipa-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,27 +532,39 @@ mod tests {
};

const URI_1: &str = "http://localhost:3000";
const URI_1S: &str = "http://localhost:6000";
const URI_2: &str = "http://localhost:3001";
const URI_2S: &str = "http://localhost:6001";
const URI_3: &str = "http://localhost:3002";
const URI_3S: &str = "http://localhost:6002";

#[test]
fn parse_config() {
let conf = TestConfigBuilder::with_http_and_default_test_ports().build();

let uri1 = URI_1.parse::<Uri>().unwrap();
let id1 = HelperIdentity::try_from(1usize).unwrap();
let value1 = &conf.network.peers()[id1];
assert_eq!(value1.url, uri1);
let ring_value1 = &conf.leaders_ring().network.peers()[id1];
assert_eq!(ring_value1.url, uri1);
let uri1s = URI_1S.parse::<Uri>().unwrap();
let sharding_value1 = conf.get_shards_for_helper(id1).network.get_peer(0).unwrap();
assert_eq!(sharding_value1.url, uri1s);

let uri2 = URI_2.parse::<Uri>().unwrap();
let id2 = HelperIdentity::try_from(2usize).unwrap();
let value2 = &conf.network.peers()[id2];
assert_eq!(value2.url, uri2);
let ring_value2 = &conf.leaders_ring().network.peers()[id2];
assert_eq!(ring_value2.url, uri2);
let uri2s = URI_2S.parse::<Uri>().unwrap();
let sharding_value2 = conf.get_shards_for_helper(id2).network.get_peer(0).unwrap();
assert_eq!(sharding_value2.url, uri2s);

let uri3 = URI_3.parse::<Uri>().unwrap();
let id3 = HelperIdentity::try_from(3usize).unwrap();
let value3 = &conf.network.peers()[id3];
assert_eq!(value3.url, uri3);
let ring_value3 = &conf.leaders_ring().network.peers()[id3];
assert_eq!(ring_value3.url, uri3);
let uri3s = URI_3S.parse::<Uri>().unwrap();
let sharding_value3 = conf.get_shards_for_helper(id3).network.get_peer(0).unwrap();
assert_eq!(sharding_value3.url, uri3s);
}

#[test]
Expand Down
29 changes: 28 additions & 1 deletion ipa-core/src/net/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use pin_project::pin_project;
use rustls::RootCertStore;
use tracing::error;

use super::{ConnectionFlavor, Helper};
use super::{ConnectionFlavor, Helper, Shard};
use crate::{
config::{
ClientConfig, HyperClientConfigurator, NetworkConfig, OwnedCertificate, OwnedPrivateKey,
Expand Down Expand Up @@ -469,6 +469,33 @@ impl MpcHelperClient<Helper> {
}
}

impl MpcHelperClient<Shard> {
/// This is a mirror of [`MpcHelperClient<Helper>::from_config`] but for Shards. This creates
/// set of Shard clients in the supplied helper network configuration, which can be used to
/// talk to each of the shards in this helper.
///
/// `identity` configures whether and how the client will authenticate to the server. It is for
/// the shard making the calls, so the same one is used for all three of the clients.
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn shards_from_conf(
runtime: &IpaRuntime,
conf: &NetworkConfig<Shard>,
identity: &ClientIdentity<Shard>,
) -> Vec<Self> {
conf.peers_iter()
.map(|peer_conf| {
Self::new(
runtime.clone(),
&conf.client,
peer_conf.clone(),
identity.clone_with_key(),
)
})
.collect()
}
}

fn make_http_connector() -> HttpConnector {
let mut connector = HttpConnector::new();
// IPA uses HTTP2 and it is sensitive to those delays especially in high-latency network
Expand Down
10 changes: 5 additions & 5 deletions ipa-core/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,23 @@ pub fn parse_certificate_and_private_key_bytes(
mod tests {
use std::io::ErrorKind;

use crate::net::test;
use super::test::get_test_certificate_and_key;
use crate::sharding::ShardedHelperIdentity;

const NOTHING: &[u8] = b" ";
const GARBAGE: &[u8] = b"ksjdhfskjdfhsdf";

#[test]
fn parse_cert_pk_happy_path() {
let mut c = test::TEST_CERTS[0];
let mut pk = test::TEST_KEYS[0];
let (mut c, mut pk) = get_test_certificate_and_key(ShardedHelperIdentity::ONE_FIRST);
super::parse_certificate_and_private_key_bytes(&mut c, &mut pk).unwrap();
}

#[test]
#[should_panic(expected = "No certificates found")]
fn parse_cert_pk_no_cert() {
let mut c = NOTHING;
let mut pk = test::TEST_KEYS[0];
let (_, mut pk) = get_test_certificate_and_key(ShardedHelperIdentity::ONE_FIRST);
let r = super::parse_certificate_and_private_key_bytes(&mut c, &mut pk);
assert_eq!(r.as_ref().unwrap_err().kind(), ErrorKind::Other);
r.unwrap();
Expand All @@ -140,7 +140,7 @@ mod tests {
#[test]
#[should_panic(expected = "No private key")]
fn parse_cert_pk_no_pk() {
let mut c = test::TEST_CERTS[0];
let (mut c, _) = get_test_certificate_and_key(ShardedHelperIdentity::ONE_FIRST);
let mut pk = NOTHING;
let r = super::parse_certificate_and_private_key_bytes(&mut c, &mut pk);
assert_eq!(r.as_ref().unwrap_err().kind(), ErrorKind::Other);
Expand Down
Loading

0 comments on commit 4d50a64

Please sign in to comment.