Skip to content

Commit cc4d851

Browse files
committed
test: add testing against upstream standard CRDs
Signed-off-by: Shane Utt <[email protected]>
1 parent 334c199 commit cc4d851

File tree

1 file changed

+102
-12
lines changed

1 file changed

+102
-12
lines changed

gateway-api/src/lib.rs

Lines changed: 102 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use apis::experimental;
1212
mod tests {
1313
use std::process::Command;
1414

15-
use anyhow::Error;
15+
use anyhow::{Error, Ok};
1616
use hyper_util::client::legacy::Client as HTTPClient;
1717
use hyper_util::rt::TokioExecutor;
1818
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, Time};
@@ -35,7 +35,8 @@ mod tests {
3535
},
3636
apis::standard::gatewayclasses::{GatewayClass, GatewayClassSpec},
3737
apis::standard::gateways::{
38-
Gateway, GatewaySpec, GatewayStatus, GatewayStatusAddresses, GatewayStatusListeners,
38+
Gateway, GatewayListeners, GatewaySpec, GatewayStatus, GatewayStatusAddresses,
39+
GatewayStatusListeners,
3940
},
4041
apis::standard::grpcroutes::GrpcRouteSpec,
4142
apis::standard::httproutes::HttpRouteSpec,
@@ -44,25 +45,57 @@ mod tests {
4445
},
4546
};
4647

48+
const DEFAULT_GATEWAY_API_VERSION: &str = "v1.4.0";
49+
4750
// -------------------------------------------------------------------------
4851
// Tests
4952
// -------------------------------------------------------------------------
5053

5154
#[ignore]
5255
#[tokio::test]
53-
async fn deploy_resources() -> Result<(), Error> {
54-
let (client, cluster) = get_client().await?;
56+
async fn test_deploy_resources() -> Result<(), Error> {
57+
let (client, cluster) = get_client(false).await?;
58+
let info = client.apiserver_version().await?;
59+
60+
println!(
61+
"kind cluster {} is running, server version: {}",
62+
cluster.name, info.git_version
63+
);
64+
65+
test_resource_deployment(client).await?;
66+
67+
println!("cleaning up kind cluster {}", cluster.name);
68+
69+
Ok(())
70+
}
71+
72+
#[ignore]
73+
#[tokio::test]
74+
async fn test_deploy_resources_upstream_crds() -> Result<(), Error> {
75+
let (client, cluster) = get_client(true).await?;
5576
let info = client.apiserver_version().await?;
5677

5778
println!(
5879
"kind cluster {} is running, server version: {}",
5980
cluster.name, info.git_version
6081
);
6182

83+
test_resource_deployment(client).await?;
84+
85+
println!("cleaning up kind cluster {}", cluster.name);
86+
87+
Ok(())
88+
}
89+
90+
// -------------------------------------------------------------------------
91+
// Test Resources
92+
// -------------------------------------------------------------------------
93+
94+
async fn test_resource_deployment(client: kube::Client) -> Result<(), Error> {
6295
let mut gwc = GatewayClass {
6396
metadata: ObjectMeta::default(),
6497
spec: GatewayClassSpec {
65-
controller_name: "test-controller".to_string(),
98+
controller_name: "example.com/gateway-controller".to_string(),
6699
description: None,
67100
parameters_ref: None,
68101
},
@@ -83,7 +116,16 @@ mod tests {
83116
.metadata
84117
.name
85118
.ok_or(Error::msg("could not find GatewayClass name"))?,
86-
..Default::default()
119+
listeners: vec![GatewayListeners {
120+
name: "http".to_string(),
121+
port: 80,
122+
protocol: "HTTP".to_string(),
123+
hostname: None,
124+
allowed_routes: None,
125+
tls: None,
126+
}],
127+
addresses: None,
128+
infrastructure: None,
87129
},
88130
status: None,
89131
};
@@ -96,9 +138,12 @@ mod tests {
96138
assert!(gw.metadata.uid.is_some());
97139

98140
let gw_status = GatewayStatus {
99-
addresses: Some(vec![GatewayStatusAddresses::default()]),
141+
addresses: Some(vec![GatewayStatusAddresses {
142+
r#type: Some("IPAddress".to_string()),
143+
value: "10.0.0.1".to_string(),
144+
}]),
100145
listeners: Some(vec![GatewayStatusListeners {
101-
name: "tcp".into(),
146+
name: "http".into(),
102147
attached_routes: 0,
103148
supported_kinds: vec![],
104149
conditions: vec![Condition {
@@ -171,7 +216,7 @@ mod tests {
171216
section_name: None,
172217
port: None,
173218
},
174-
controller_name: "test-controller".to_string(),
219+
controller_name: "example.com/gateway-controller".to_string(),
175220
conditions: vec![Condition {
176221
last_transition_time: Time(Utc::now()),
177222
message: "testing http route".to_string(),
@@ -232,7 +277,7 @@ mod tests {
232277
section_name: None,
233278
port: None,
234279
},
235-
controller_name: "test-controller".to_string(),
280+
controller_name: "example.com/gateway-controller".to_string(),
236281
conditions: vec![Condition {
237282
last_transition_time: Time(Utc::now()),
238283
message: "testing grpc route".to_string(),
@@ -310,7 +355,7 @@ mod tests {
310355
}
311356
}
312357

313-
async fn get_client() -> Result<(kube::Client, Cluster), Error> {
358+
async fn get_client(upstream: bool) -> Result<(kube::Client, Cluster), Error> {
314359
let cluster = create_kind_cluster()?;
315360
let kubeconfig_yaml = get_kind_kubeconfig(&cluster.name)?;
316361
let kubeconfig = Kubeconfig::from_yaml(&kubeconfig_yaml)?;
@@ -327,11 +372,56 @@ mod tests {
327372

328373
let client = KubeClient::new(service, config.default_namespace);
329374

330-
deploy_crds(client.clone()).await?;
375+
if upstream {
376+
deploy_crds_upstream(&cluster.name).await?;
377+
} else {
378+
deploy_crds(client.clone()).await?;
379+
}
331380

332381
Ok((client, cluster))
333382
}
334383

384+
async fn deploy_crds_upstream(cluster_name: &str) -> Result<(), Error> {
385+
let version = std::env::var("GATEWAY_API_VERSION")
386+
.unwrap_or_else(|_| DEFAULT_GATEWAY_API_VERSION.to_string());
387+
388+
let semver_pattern = regex::Regex::new(r"^v?\d+\.\d+\.\d+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$")
389+
.map_err(|e| Error::msg(format!("Failed to compile regex: {}", e)))?;
390+
if !semver_pattern.is_match(&version) {
391+
return Err(Error::msg(format!(
392+
"GATEWAY_API_VERSION '{}' is not a valid semver version",
393+
version
394+
)));
395+
}
396+
397+
let kubeconfig_yaml = get_kind_kubeconfig(cluster_name)?;
398+
let temp_dir = std::env::temp_dir();
399+
let kubeconfig_path = temp_dir.join(format!("kubeconfig-{}", cluster_name));
400+
std::fs::write(&kubeconfig_path, kubeconfig_yaml)?;
401+
402+
let url = format!(
403+
"https://github.com/kubernetes-sigs/gateway-api/releases/download/{}/standard-install.yaml",
404+
version
405+
);
406+
407+
let output = Command::new("kubectl")
408+
.arg("--kubeconfig")
409+
.arg(&kubeconfig_path)
410+
.arg("apply")
411+
.arg("-f")
412+
.arg(&url)
413+
.output()?;
414+
415+
if !output.status.success() {
416+
return Err(Error::msg(format!(
417+
"Failed to apply CRDs: {}",
418+
String::from_utf8_lossy(&output.stderr)
419+
)));
420+
}
421+
422+
Ok(())
423+
}
424+
335425
async fn deploy_crds(client: kube::Client) -> Result<(), Error> {
336426
let mut gwc_crd = GatewayClass::crd();
337427
gwc_crd.metadata.annotations = Some(std::collections::BTreeMap::from([(

0 commit comments

Comments
 (0)