Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(policy): Reduce duplication in outbound policy API tests #13543

Merged
merged 9 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions policy-test/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,6 @@ impl OutboundPolicyClient {
Ok(rsp.into_inner())
}

pub async fn watch(
&mut self,
ns: &str,
svc: &k8s::Service,
port: u16,
) -> Result<tonic::Streaming<outbound::OutboundPolicy>, tonic::Status> {
let address = svc
.spec
.as_ref()
.expect("Service must have a spec")
.cluster_ip
.as_ref()
.expect("Service must have a cluster ip");
self.watch_ip(ns, address, port).await
}

pub async fn watch_ip(
&mut self,
ns: &str,
Expand Down
59 changes: 35 additions & 24 deletions policy-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod bb;
pub mod curl;
pub mod grpc;
pub mod outbound_api;
pub mod test_route;
pub mod web;

use kube::runtime::wait::Condition;
Expand All @@ -15,6 +16,7 @@ use linkerd_policy_controller_k8s_api::{
ResourceExt,
};
use maplit::{btreemap, convert_args};
use test_route::TestRoute;
use tokio::time;
use tracing::Instrument;

Expand Down Expand Up @@ -206,24 +208,25 @@ pub async fn await_pod_ip(client: &kube::Client, ns: &str, name: &str) -> std::n
.expect("pod IP must be valid")
}

// Waits until an HttpRoute with the given namespace and name has a status set
// on it, then returns the generic route status representation.
pub async fn await_route_status(
client: &kube::Client,
ns: &str,
name: &str,
) -> k8s::policy::httproute::RouteStatus {
use k8s::policy::httproute as api;
let route_status = await_condition(client, ns, name, |obj: Option<&api::HttpRoute>| -> bool {
obj.and_then(|route| route.status.as_ref()).is_some()
})
.await
.expect("must fetch route")
.status
.expect("route must contain a status representation")
.inner;
tracing::trace!(?route_status, name, ns, "got route status");
route_status
// Waits until an HttpRoute with the given namespace and name has been accepted.
pub async fn await_route_accepted<R: TestRoute>(client: &kube::Client, route: &R) {
await_condition(
client,
&route.namespace().unwrap(),
&route.name_unchecked(),
|obj: Option<&R>| -> bool {
obj.map_or(false, |route| {
let conditions = route
.conditions()
.unwrap_or_default()
.into_iter()
.cloned()
.collect::<Vec<_>>();
is_status_accepted(&conditions)
})
},
)
.await;
}

// Waits until an HttpRoute with the given namespace and name has a status set
Expand Down Expand Up @@ -591,17 +594,25 @@ pub fn mk_egress_net(ns: &str, name: &str) -> k8s::policy::EgressNetwork {
}

#[track_caller]
pub fn assert_resource_meta(meta: &Option<grpc::meta::Metadata>, resource: &Resource, port: u16) {
pub fn assert_resource_meta(
meta: &Option<grpc::meta::Metadata>,
parent_ref: ParentReference,
port: u16,
) {
println!("meta: {:?}", meta);
tracing::debug!(?meta, ?resource, port, "Asserting service metadata");
tracing::debug!(?meta, ?parent_ref, port, "Asserting parent metadata");
let mut group = parent_ref.group.unwrap();
if group.is_empty() {
group = "core".to_string();
}
assert_eq!(
meta,
&Some(grpc::meta::Metadata {
kind: Some(grpc::meta::metadata::Kind::Resource(grpc::meta::Resource {
group: resource.group(),
kind: resource.kind(),
name: resource.name(),
namespace: resource.namespace(),
group,
kind: parent_ref.kind.unwrap(),
name: parent_ref.name,
namespace: parent_ref.namespace.unwrap(),
section: "".to_string(),
port: port.into()
})),
Expand Down
Loading
Loading