From fc99d3adf68cf97e885824c7fa12649255f4a720 Mon Sep 17 00:00:00 2001 From: Alejandro Pedraza Date: Tue, 12 Nov 2024 18:49:53 -0500 Subject: [PATCH] Update existing unit tests --- .../src/inbound/tests/ratelimit_policy.rs | 15 +++++++++ policy-controller/k8s/status/src/tests.rs | 32 +++++++++++++++++++ .../k8s/status/src/tests/routes.rs | 30 ----------------- .../k8s/status/src/tests/routes/grpc.rs | 6 ++-- .../k8s/status/src/tests/routes/http.rs | 10 +++--- .../k8s/status/src/tests/routes/tcp.rs | 6 ++-- .../k8s/status/src/tests/routes/tls.rs | 6 ++-- .../admit_http_local_ratelimit_policy.rs | 20 ++++++++++-- policy-test/tests/inbound_api.rs | 16 ++++++++++ 9 files changed, 95 insertions(+), 46 deletions(-) diff --git a/policy-controller/k8s/index/src/inbound/tests/ratelimit_policy.rs b/policy-controller/k8s/index/src/inbound/tests/ratelimit_policy.rs index ab06f98b1ac7f..9c576f082d371 100644 --- a/policy-controller/k8s/index/src/inbound/tests/ratelimit_policy.rs +++ b/policy-controller/k8s/index/src/inbound/tests/ratelimit_policy.rs @@ -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::::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(), + }, + }), } } diff --git a/policy-controller/k8s/status/src/tests.rs b/policy-controller/k8s/status/src/tests.rs index 6d7b0e5dba07d..5aa196b6f9550 100644 --- a/policy-controller/k8s/status/src/tests.rs +++ b/policy-controller/k8s/status/src/tests.rs @@ -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 { @@ -11,3 +13,33 @@ pub fn default_cluster_networks() -> Vec { "fd00::/8".parse().unwrap(), ] } + +pub fn make_server( + namespace: impl ToString, + name: impl ToString, + port: u16, + srv_labels: impl IntoIterator, + pod_labels: impl IntoIterator, + proxy_protocol: Option, +) -> 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, + }, + } +} diff --git a/policy-controller/k8s/status/src/tests/routes.rs b/policy-controller/k8s/status/src/tests/routes.rs index 58f0943723550..b3063cd2f844a 100644 --- a/policy-controller/k8s/status/src/tests/routes.rs +++ b/policy-controller/k8s/status/src/tests/routes.rs @@ -88,33 +88,3 @@ fn make_egress_network( }), } } - -fn make_server( - namespace: impl ToString, - name: impl ToString, - port: u16, - srv_labels: impl IntoIterator, - pod_labels: impl IntoIterator, - proxy_protocol: Option, -) -> 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, - }, - } -} diff --git a/policy-controller/k8s/status/src/tests/routes/grpc.rs b/policy-controller/k8s/status/src/tests/routes/grpc.rs index fab43ea42746d..4330562a94e45 100644 --- a/policy-controller/k8s/status/src/tests/routes/grpc.rs +++ b/policy-controller/k8s/status/src/tests/routes/grpc.rs @@ -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}; @@ -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, @@ -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, diff --git a/policy-controller/k8s/status/src/tests/routes/http.rs b/policy-controller/k8s/status/src/tests/routes/http.rs index d9f4d24b9bbe5..f9d41d93c7c3d 100644 --- a/policy-controller/k8s/status/src/tests/routes/http.rs +++ b/policy-controller/k8s/status/src/tests/routes/http.rs @@ -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}; @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/policy-controller/k8s/status/src/tests/routes/tcp.rs b/policy-controller/k8s/status/src/tests/routes/tcp.rs index ba5a041c64238..49c55959098cf 100644 --- a/policy-controller/k8s/status/src/tests/routes/tcp.rs +++ b/policy-controller/k8s/status/src/tests/routes/tcp.rs @@ -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}; @@ -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, @@ -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, diff --git a/policy-controller/k8s/status/src/tests/routes/tls.rs b/policy-controller/k8s/status/src/tests/routes/tls.rs index 5689d994bcc02..d5d2148084cc2 100644 --- a/policy-controller/k8s/status/src/tests/routes/tls.rs +++ b/policy-controller/k8s/status/src/tests/routes/tls.rs @@ -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}; @@ -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, @@ -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, diff --git a/policy-test/tests/admit_http_local_ratelimit_policy.rs b/policy-test/tests/admit_http_local_ratelimit_policy.rs index 5e4de499420be..71194242d3115 100644 --- a/policy-test/tests/admit_http_local_ratelimit_policy.rs +++ b/policy-test/tests/admit_http_local_ratelimit_policy.rs @@ -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; @@ -90,5 +91,20 @@ fn mk_ratelimiter( }), overrides: Some(overrides), }, + status: Some(HTTPLocalRateLimitPolicyStatus { + conditions: vec![api::Condition { + last_transition_time: api::Time(chrono::DateTime::::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(), + }, + }), } } diff --git a/policy-test/tests/inbound_api.rs b/policy-test/tests/inbound_api.rs index 2efeb2c3b0c41..b17880c0ea449 100644 --- a/policy-test/tests/inbound_api.rs +++ b/policy-test/tests/inbound_api.rs @@ -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; @@ -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::::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;