Skip to content

Commit

Permalink
fix ut
Browse files Browse the repository at this point in the history
Signed-off-by: chengqinglin <chengqinglin@icloud.com>
  • Loading branch information
imuxin committed Sep 30, 2022
1 parent 2ce8885 commit ce2bb6b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 108 deletions.
71 changes: 71 additions & 0 deletions kube-client/src/discovery/apigroup.rs
Expand Up @@ -311,3 +311,74 @@ impl ApiGroup {
None
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_resources_by_stability() {
let ac = ApiCapabilities {
scope: Scope::Namespaced,
subresources: vec![],
operations: vec![],
};

let testlowversioncr_v1alpha1 = ApiResource {
group: String::from("kube.rs"),
version: String::from("v1alpha1"),
kind: String::from("TestLowVersionCr"),
api_version: String::from("kube.rs/v1alpha1"),
plural: String::from("testlowversioncrs"),
};

let testcr_v1 = ApiResource {
group: String::from("kube.rs"),
version: String::from("v1"),
kind: String::from("TestCr"),
api_version: String::from("kube.rs/v1"),
plural: String::from("testcrs"),
};

let testcr_v2alpha1 = ApiResource {
group: String::from("kube.rs"),
version: String::from("v2alpha1"),
kind: String::from("TestCr"),
api_version: String::from("kube.rs/v2alpha1"),
plural: String::from("testcrs"),
};

let group = ApiGroup {
name: "kube.rs".to_string(),
data: vec![
GroupVersionData {
version: "v1alpha1".to_string(),
resources: vec![(testlowversioncr_v1alpha1.clone(), ac.clone())],
},
GroupVersionData {
version: "v1".to_string(),
resources: vec![(testcr_v1.clone(), ac.clone())],
},
GroupVersionData {
version: "v2alpha1".to_string(),
resources: vec![(testcr_v2alpha1.clone(), ac.clone())],
},
],
preferred: Some(String::from("v1")),
};

let resources = group.resources_by_stability();
assert!(
resources
.iter()
.any(|(ar, _)| ar.kind == "TestCr" && ar.version == "v1"),
"wrong stable version"
);
assert!(
resources
.iter()
.any(|(ar, _)| ar.kind == "TestLowVersionCr" && ar.version == "v1alpha1"),
"lost low version resource"
);
}
}
108 changes: 0 additions & 108 deletions kube/src/lib.rs
Expand Up @@ -431,114 +431,6 @@ mod test {
Ok(())
}

#[tokio::test]
#[ignore] // needs cluster (fetches api resources, and lists all)
#[cfg(all(feature = "derive"))]
async fn derived_resources_by_stability_discoverable() -> Result<(), Box<dyn std::error::Error>> {
use crate::{
discovery::{self, Discovery},
runtime::wait::{await_condition, conditions},
};
use kube_core::crd::merge_crds;

mod v1alpha1 {
use super::*;

#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[kube(
group = "test.kube.rs",
version = "v1alpha1",
kind = "TestLowVersionCr",
namespaced
)]
#[kube(crates(kube_core = "crate::core"))] // for dev-dep test structure
pub struct TestLowVersionCrSpec {}
}

mod v1 {
use super::*;

#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[kube(group = "test.kube.rs", version = "v1", kind = "TestCr", namespaced)]
#[kube(crates(kube_core = "crate::core"))] // for dev-dep test structure
pub struct TestCrSpec {}
}

mod v2alpha1 {
use super::*;

#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[kube(group = "test.kube.rs", version = "v2alpha1", kind = "TestCr", namespaced)]
#[kube(crates(kube_core = "crate::core"))] // for dev-dep test structure
pub struct TestCrSpec {}
}

async fn apply_crd(
client: Client,
name: &str,
crd: CustomResourceDefinition,
) -> Result<(), Box<dyn std::error::Error>> {
let crds: Api<CustomResourceDefinition> = Api::all(client.clone());
let ssapply = PatchParams::apply("kube").force();
crds.patch(name, &ssapply, &Patch::Apply(&crd)).await?;
let establish = await_condition(crds.clone(), name, conditions::is_crd_established());
let _ = tokio::time::timeout(std::time::Duration::from_secs(10), establish).await?;
Ok(())
}

let client = Client::try_default().await?;
let test_lowversion_crd = v1alpha1::TestLowVersionCr::crd();
let testcrd_v1 = v1::TestCr::crd();
let testcrd_v2alpha1 = v2alpha1::TestCr::crd();
let all_crds = vec![testcrd_v1.clone(), testcrd_v2alpha1.clone()];

apply_crd(
client.clone(),
"testlowversioncrs.test.kube.rs",
test_lowversion_crd,
)
.await?;
apply_crd(
client.clone(),
"testcrs.test.kube.rs",
merge_crds(all_crds, "v1")?,
)
.await?;

// run (almost) full discovery
let discovery = Discovery::new(client.clone())
// only include test.kube.rs in discovery
.filter(&["test.kube.rs"])
.run()
.await?;

// check our custom resource first by resolving within groups
assert!(discovery.has_group("test.kube.rs"), "missing group kube.rs");

let group = discovery::group(&client, "test.kube.rs").await?;
let resources = group.resources_by_stability();
assert!(
resources
.iter()
.any(|(ar, _)| ar.kind == "TestCr" && ar.version == "v1"),
"wrong stable version"
);
assert!(
resources
.iter()
.any(|(ar, _)| ar.kind == "TestLowVersionCr" && ar.version == "v1alpha1"),
"lost low version resource"
);

// cleanup
let crds: Api<CustomResourceDefinition> = Api::all(client.clone());
crds.delete("testcrs.test.kube.rs", &DeleteParams::default())
.await?;
crds.delete("testlowversioncrs.test.kube.rs", &DeleteParams::default())
.await?;
Ok(())
}

#[tokio::test]
#[ignore] // needs cluster (will create await a pod)
#[cfg(all(feature = "runtime"))]
Expand Down

0 comments on commit ce2bb6b

Please sign in to comment.