Skip to content

Commit

Permalink
add test and drop unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjohn committed Aug 1, 2024
1 parent f851be1 commit 47ff2a3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ impl DemandProxyState {
return None;
}
self.fetch_on_demand(uid.clone()).await;
self.state.read().unwrap().workloads.find_uid(uid)
self.read().workloads.find_uid(uid)
}

pub async fn fetch_upstream(
Expand All @@ -707,7 +707,7 @@ impl DemandProxyState {
self.fetch_address(&network_addr(network.clone(), addr.ip()))
.await;
let upstream = {
self.state.read().unwrap().find_upstream(
self.read().find_upstream(
network,
source_workload,
addr,
Expand Down Expand Up @@ -766,7 +766,7 @@ impl DemandProxyState {
)
}
Destination::Hostname(host) => {
let state = self.state.read().unwrap();
let state = self.read();
match state.find_hostname(host) {
Some(Address::Service(s)) => state.find_upstream_from_service(
source_workload,
Expand Down
24 changes: 24 additions & 0 deletions src/test_helpers/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,18 @@ impl<'a> TestServiceBuilder<'a> {
self
}

/// Set the service waypoint by hostname
pub fn waypoint_hostname(mut self, hostname: &str) -> Self {
self.s.waypoint = Some(GatewayAddress {
destination: gatewayaddress::Destination::Hostname(NamespacedHostname {
namespace: strng::literal!("default"),
hostname: hostname.into(),
}),
hbone_mtls_port: 15008,
});
self
}

/// Finish building the service.
pub async fn register(self) -> anyhow::Result<()> {
self.manager
Expand Down Expand Up @@ -411,6 +423,18 @@ impl<'a> TestWorkloadBuilder<'a> {
self
}

/// Set the service waypoint by hostname
pub fn waypoint_hostname(mut self, hostname: &str) -> Self {
self.w.workload.waypoint = Some(GatewayAddress {
destination: gatewayaddress::Destination::Hostname(NamespacedHostname {
namespace: strng::literal!("default"),
hostname: hostname.into(),
}),
hbone_mtls_port: 15008,
});
self
}

/// Set a waypoint to the workload
pub fn mutate_workload(mut self, f: impl FnOnce(&mut Workload)) -> Self {
f(&mut self.w.workload);
Expand Down
74 changes: 74 additions & 0 deletions tests/namespaced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,80 @@ mod namespaced {
Ok(())
}

#[tokio::test]
async fn service_waypoint_hostname() -> anyhow::Result<()> {
let mut manager = setup_netns_test!(Shared);

let zt = manager.deploy_ztunnel(DEFAULT_NODE).await?;

manager
.service_builder("waypoint")
.addresses(vec![NetworkAddress {
network: strng::EMPTY,
address: TEST_VIP.parse::<IpAddr>()?,
}])
.ports(HashMap::from([(15008u16, 15008u16)]))
.register()
.await?;
let waypoint = manager
.workload_builder("waypoint", DEFAULT_NODE)
.uncaptured()
.service(
"default/waypoint.default.svc.cluster.local",80, SERVER_PORT
)
.register()
.await?;
run_hbone_server(waypoint, "waypoint")?;

manager
.workload_builder("server", DEFAULT_NODE)
.waypoint_hostname("waypoint.default.svc.cluster.local")
.register()
.await?;
let client = manager
.workload_builder("client", DEFAULT_NODE)
.register()
.await?;

let server_ip = manager.resolver().resolve("server")?;
let waypoint_pod_ip = manager.resolver().resolve("waypoint")?;
run_tcp_to_hbone_client(client, manager.resolver(), "server")?;

let metrics = [
(CONNECTIONS_OPENED, 1),
(CONNECTIONS_CLOSED, 1),
(BYTES_RECV, REQ_SIZE),
(BYTES_SENT, HBONE_REQ_SIZE),
];
verify_metrics(&zt, &metrics, &source_labels()).await;

let sent = format!("{REQ_SIZE}");
let recv = format!("{HBONE_REQ_SIZE}");
let hbone_addr = format!("{server_ip}:8080");
let dst_addr = format!("{waypoint_pod_ip}:15008");
let want = HashMap::from([
("scope", "access"),
("src.workload", "client"),
("dst.workload", "waypoint"),
("dst.hbone_addr", &hbone_addr),
("dst.addr", &dst_addr),
("bytes_sent", &sent),
("bytes_recv", &recv),
("direction", "outbound"),
("message", "connection complete"),
(
"src.identity",
"spiffe://cluster.local/ns/default/sa/client",
),
(
"dst.identity",
"spiffe://cluster.local/ns/default/sa/waypoint",
),
]);
telemetry::testing::assert_contains(want);
Ok(())
}

#[tokio::test]
async fn sandwich_waypoint_plain() -> anyhow::Result<()> {
let mut manager = setup_netns_test!(Shared);
Expand Down

0 comments on commit 47ff2a3

Please sign in to comment.