Skip to content

Commit

Permalink
Update existing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alpeb committed Nov 13, 2024
1 parent 60f25b7 commit fc99d3a
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 46 deletions.
15 changes: 15 additions & 0 deletions policy-controller/k8s/index/src/inbound/tests/ratelimit_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,20 @@ fn mk_ratelimit(
identity: None,
overrides: Some(overrides),
},
status: Some(k8s::policy::HTTPLocalRateLimitPolicyStatus {
conditions: vec![k8s::Condition {
last_transition_time: k8s::Time(chrono::DateTime::<chrono::Utc>::MIN_UTC),
message: "".to_string(),
observed_generation: None,
reason: "".to_string(),
status: "True".to_string(),
type_: "Accepted".to_string(),
}],
target_ref: LocalTargetRef {
group: Some("policy.linkerd.io".to_string()),
kind: "Server".to_string(),
name: server_name.to_string(),
},
}),
}
}
32 changes: 32 additions & 0 deletions policy-controller/k8s/status/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use linkerd_policy_controller_core::IpNet;
use linkerd_policy_controller_k8s_api::{self as k8s_core_api, policy as linkerd_k8s_api};
mod egress_network;
mod ratelimit;
mod routes;

pub fn default_cluster_networks() -> Vec<IpNet> {
Expand All @@ -11,3 +13,33 @@ pub fn default_cluster_networks() -> Vec<IpNet> {
"fd00::/8".parse().unwrap(),
]
}

pub fn make_server(
namespace: impl ToString,
name: impl ToString,
port: u16,
srv_labels: impl IntoIterator<Item = (&'static str, &'static str)>,
pod_labels: impl IntoIterator<Item = (&'static str, &'static str)>,
proxy_protocol: Option<linkerd_k8s_api::server::ProxyProtocol>,
) -> linkerd_k8s_api::Server {
let port = linkerd_k8s_api::server::Port::Number(port.try_into().unwrap());
linkerd_k8s_api::Server {
metadata: k8s_core_api::ObjectMeta {
namespace: Some(namespace.to_string()),
name: Some(name.to_string()),
labels: Some(
srv_labels
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
),
..Default::default()
},
spec: linkerd_k8s_api::ServerSpec {
port,
selector: linkerd_k8s_api::server::Selector::Pod(pod_labels.into_iter().collect()),
proxy_protocol,
access_policy: None,
},
}
}
30 changes: 0 additions & 30 deletions policy-controller/k8s/status/src/tests/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,3 @@ fn make_egress_network(
}),
}
}

fn make_server(
namespace: impl ToString,
name: impl ToString,
port: u16,
srv_labels: impl IntoIterator<Item = (&'static str, &'static str)>,
pod_labels: impl IntoIterator<Item = (&'static str, &'static str)>,
proxy_protocol: Option<linkerd_k8s_api::server::ProxyProtocol>,
) -> linkerd_k8s_api::Server {
let port = linkerd_k8s_api::server::Port::Number(port.try_into().unwrap());
linkerd_k8s_api::Server {
metadata: k8s_core_api::ObjectMeta {
namespace: Some(namespace.to_string()),
name: Some(name.to_string()),
labels: Some(
srv_labels
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
),
..Default::default()
},
spec: linkerd_k8s_api::ServerSpec {
port,
selector: linkerd_k8s_api::server::Selector::Pod(pod_labels.into_iter().collect()),
proxy_protocol,
access_policy: None,
},
}
}
6 changes: 3 additions & 3 deletions policy-controller/k8s/status/src/tests/routes/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
route_conflicted, POLICY_API_GROUP,
},
resource_id::NamespaceGroupKindName,
tests::default_cluster_networks,
tests::{default_cluster_networks, make_server},
Index, IndexMetrics,
};
use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -564,7 +564,7 @@ fn route_accepted_after_server_create() {
assert_eq!(patch, update.patch);

// Apply the server
let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down Expand Up @@ -685,7 +685,7 @@ fn route_rejected_after_server_delete() {
default_cluster_networks(),
);

let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down
10 changes: 5 additions & 5 deletions policy-controller/k8s/status/src/tests/routes/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
POLICY_API_GROUP,
},
resource_id::NamespaceGroupKindName,
tests::default_cluster_networks,
tests::{default_cluster_networks, make_server},
Index, IndexMetrics,
};
use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -1209,7 +1209,7 @@ fn linkerd_route_accepted_after_server_create() {
assert_eq!(patch, update.patch);

// Apply the server
let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down Expand Up @@ -1370,7 +1370,7 @@ fn gateway_route_accepted_after_server_create() {
assert_eq!(patch, update.patch);

// Apply the server
let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down Expand Up @@ -1491,7 +1491,7 @@ fn linkerd_route_rejected_after_server_delete() {
default_cluster_networks(),
);

let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down Expand Up @@ -1669,7 +1669,7 @@ fn gateway_route_rejected_after_server_delete() {
default_cluster_networks(),
);

let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down
6 changes: 3 additions & 3 deletions policy-controller/k8s/status/src/tests/routes/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
POLICY_API_GROUP,
},
resource_id::NamespaceGroupKindName,
tests::default_cluster_networks,
tests::{default_cluster_networks, make_server},
Index, IndexMetrics,
};
use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -556,7 +556,7 @@ fn route_accepted_after_server_create() {
assert_eq!(patch, update.patch);

// Apply the server
let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down Expand Up @@ -677,7 +677,7 @@ fn route_rejected_after_server_delete() {
default_cluster_networks(),
);

let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down
6 changes: 3 additions & 3 deletions policy-controller/k8s/status/src/tests/routes/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
route_conflicted, POLICY_API_GROUP,
},
resource_id::NamespaceGroupKindName,
tests::default_cluster_networks,
tests::{default_cluster_networks, make_server},
Index, IndexMetrics,
};
use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -556,7 +556,7 @@ fn route_accepted_after_server_create() {
assert_eq!(patch, update.patch);

// Apply the server
let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down Expand Up @@ -677,7 +677,7 @@ fn route_rejected_after_server_delete() {
default_cluster_networks(),
);

let server = super::make_server(
let server = make_server(
"ns-0",
"srv-8080",
8080,
Expand Down
20 changes: 18 additions & 2 deletions policy-test/tests/admit_http_local_ratelimit_policy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use k8s_openapi::chrono;
use linkerd_policy_controller_k8s_api::{
self as api,
policy::{
HTTPLocalRateLimitPolicy, Limit, LocalTargetRef, NamespacedTargetRef, Override,
RateLimitPolicySpec,
HTTPLocalRateLimitPolicy, HTTPLocalRateLimitPolicyStatus, Limit, LocalTargetRef,
NamespacedTargetRef, Override, RateLimitPolicySpec,
},
};
use linkerd_policy_test::admission;
Expand Down Expand Up @@ -90,5 +91,20 @@ fn mk_ratelimiter(
}),
overrides: Some(overrides),
},
status: Some(HTTPLocalRateLimitPolicyStatus {
conditions: vec![api::Condition {
last_transition_time: api::Time(chrono::DateTime::<chrono::Utc>::MIN_UTC),
message: "".to_string(),
observed_generation: None,
reason: "".to_string(),
status: "True".to_string(),
type_: "Accepted".to_string(),
}],
target_ref: LocalTargetRef {
group: Some("policy.linkerd.io".to_string()),
kind: "Server".to_string(),
name: "linkerd-admin".to_string(),
},
}),
}
}
16 changes: 16 additions & 0 deletions policy-test/tests/inbound_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::time::Duration;

use futures::prelude::*;
use k8s_openapi::chrono;
use kube::ResourceExt;
use linkerd_policy_controller_core::{Ipv4Net, Ipv6Net};
use linkerd_policy_controller_k8s_api as k8s;
Expand Down Expand Up @@ -355,6 +356,21 @@ async fn http_local_rate_limit_policy() {
}],
}]),
},
status: Some(k8s::policy::HTTPLocalRateLimitPolicyStatus {
conditions: vec![k8s::Condition {
last_transition_time: k8s::Time(chrono::DateTime::<chrono::Utc>::MIN_UTC),
message: "".to_string(),
observed_generation: None,
reason: "".to_string(),
status: "True".to_string(),
type_: "Accepted".to_string(),
}],
target_ref: k8s::policy::LocalTargetRef {
group: Some("policy.linkerd.io".to_string()),
kind: "Server".to_string(),
name: "linkerd-admin".to_string(),
},
}),
},
)
.await;
Expand Down

0 comments on commit fc99d3a

Please sign in to comment.