Skip to content

Commit 502e55d

Browse files
committed
test(stackable-versioned): Add conversion roundtrip integration test
1 parent 7bfc2bb commit 502e55d

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"kind": "ConversionReview",
3+
"apiVersion": "apiextensions.k8s.io/v1",
4+
"request": {
5+
"uid": "c4e55572-ee1f-4e94-9097-28936985d45f",
6+
"desiredAPIVersion": "test.stackable.tech/v1alpha1",
7+
"objects": [
8+
{
9+
"apiVersion": "test.stackable.tech/v3",
10+
"kind": "Person",
11+
"metadata": {},
12+
"spec": {
13+
"username": "jdoe",
14+
"firstName": "John",
15+
"lastName": "Doe",
16+
"gender": "Male",
17+
"socials": {
18+
"email": "[email protected]",
19+
"mastodon": "@[email protected]"
20+
}
21+
},
22+
"status": {
23+
"changedValues": {
24+
"downgrades": {},
25+
"upgrades": {}
26+
}
27+
}
28+
}
29+
]
30+
}
31+
}

crates/stackable-versioned/tests/person.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::{fs::File, path::Path};
22

3-
use kube::{CustomResource, core::conversion::ConversionReview};
3+
use kube::{
4+
CustomResource,
5+
core::conversion::{ConversionRequest, ConversionReview},
6+
};
47
use schemars::JsonSchema;
58
use serde::{Deserialize, Serialize};
69
use stackable_versioned::versioned;
@@ -18,6 +21,24 @@ pub fn convert_via_file(path: &Path) -> (ConversionReview, ConversionReview) {
1821
(request, response)
1922
}
2023

24+
#[allow(dead_code)]
25+
pub fn roundtrip_conversion_review(
26+
response_review: ConversionReview,
27+
desired_api_version: PersonVersion,
28+
) -> ConversionReview {
29+
let response = response_review.response.unwrap();
30+
ConversionReview {
31+
types: response_review.types,
32+
request: Some(ConversionRequest {
33+
desired_api_version: desired_api_version.as_api_version_str().to_owned(),
34+
objects: response.converted_objects,
35+
types: response.types,
36+
uid: response.uid,
37+
}),
38+
response: None,
39+
}
40+
}
41+
2142
#[versioned(
2243
version(name = "v1alpha1"),
2344
version(name = "v1alpha2"),
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use insta::glob;
2+
use kube::core::response::StatusSummary;
3+
4+
use crate::person::{Person, PersonVersion};
5+
6+
mod person;
7+
8+
#[test]
9+
fn person_v3_v1alpha1_v3() {
10+
glob!("./inputs/roundtrip", "*.json", |path| {
11+
// Convert from v3 to v1alpha1
12+
// NOTE (@Techassi): It should be noted that the input conversion review
13+
// contains a status with empty changedValues to be able to assert_eq
14+
// the objects at the end. As mentioned in the actual macro code, we
15+
// should avoid "polluting" the status if it is empty.
16+
let (request_v1alpha1, response_v1alpha1) = person::convert_via_file(path);
17+
let response = response_v1alpha1
18+
.response
19+
.as_ref()
20+
.expect("v1alpha1 review must have a response");
21+
22+
assert_eq!(response.result.status, Some(StatusSummary::Success));
23+
24+
// Construct the roundtrip review
25+
let roundtrip_review =
26+
person::roundtrip_conversion_review(response_v1alpha1, PersonVersion::V3);
27+
28+
// Convert back to v3 from v1alpha1
29+
let response_v3 = Person::try_convert(roundtrip_review);
30+
let response = response_v3
31+
.response
32+
.as_ref()
33+
.expect("v3 review must have a response");
34+
35+
assert_eq!(response.result.status, Some(StatusSummary::Success));
36+
37+
// Now let compare the object how it started out with the object which
38+
// was produced through the conversion roundtrip. They must match.
39+
let original_object = request_v1alpha1
40+
.request
41+
.as_ref()
42+
.expect("v1alpha1 review must have a request")
43+
.objects
44+
.first()
45+
.expect("there must be at least one object");
46+
47+
let converted_object = response
48+
.converted_objects
49+
.first()
50+
.expect("there must be at least one object");
51+
52+
assert_eq!(original_object, converted_object);
53+
});
54+
}

0 commit comments

Comments
 (0)