Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(crd-generator): approval tests for crd-generator-api-v2 #5991

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crd-generator/test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-api</artifactId>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-api-v2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void tearDown() {
@ParameterizedTest(name = "{1}.{2} parallel={3}")
@MethodSource("crdApprovalTests")
@DisplayName("CRD Generator V1 Approval Tests")
void v1ApprovalTest(
void apiV1ApprovalTest(
Class<? extends CustomResource<?, ?>>[] crClasses, String expectedCrd, String version, boolean parallel) {
final Map<String, Map<String, CRDInfo>> result = new CRDGenerator()
.withParallelGenerationEnabled(parallel)
Expand All @@ -94,23 +94,58 @@ void v1ApprovalTest(
new Namer(expectedCrd, version));
}

@ParameterizedTest(name = "{1}.{2} parallel={3}")
@MethodSource("crdV1ApprovalTests")
@DisplayName("CRD Generator V2 Approval Tests")
void apiV2ApprovalTest(
Class<? extends CustomResource<?, ?>>[] crClasses, String expectedCrd, String version, boolean parallel) {
Approvals.settings().allowMultipleVerifyCallsForThisMethod();
final Map<String, Map<String, io.fabric8.crdv2.generator.CRDInfo>> result = new io.fabric8.crdv2.generator.CRDGenerator()
.withParallelGenerationEnabled(parallel)
.inOutputDir(tempDir)
.customResourceClasses(crClasses)
.forCRDVersions(version)
.detailedGenerate()
.getCRDDetailsPerNameAndVersion();

assertThat(result)
.withFailMessage(() -> "Could not find expected CRD " + expectedCrd
+ " in results. Found instead: " + result.keySet())
.containsKey(expectedCrd)
.extractingByKey(expectedCrd)
.isNotNull();

Approvals.verify(
new FileApprovalWriter(new File(result.get(expectedCrd).get(version).getFilePath())),
new Namer(expectedCrd, version));
}

static Stream<Arguments> crdApprovalTests() {
return Stream.concat(
crdApprovalBaseCases("v1"),
crdApprovalBaseCases("v1beta1")).map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel));
}

static Stream<Arguments> crdV1ApprovalTests() {
return crdApprovalBaseCases("v1")
.map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel));
}

static Stream<TestCase> crdApprovalBaseCases(String crdVersion) {
final List<TestCase> cases = new ArrayList<>();
for (String crdVersion : new String[] { "v1", "v1beta1" }) {
for (boolean parallel : new boolean[] { false, true }) {
cases.add(new TestCase("annotateds.samples.fabric8.io", crdVersion, parallel, Annotated.class));
cases.add(new TestCase("complexkinds.samples.fabric8.io", crdVersion, parallel, Complex.class));
cases.add(new TestCase("children.sample.fabric8.io", crdVersion, parallel, Child.class));
cases.add(new TestCase("containingjsons.sample.fabric8.io", crdVersion, parallel, ContainingJson.class));
cases.add(new TestCase("k8svalidations.samples.fabric8.io", crdVersion, parallel, K8sValidation.class));
cases.add(new TestCase("containingmaps.sample.fabric8.io", crdVersion, parallel, ContainingMaps.class));
cases.add(new TestCase("multiples.sample.fabric8.io", crdVersion, parallel,
io.fabric8.crd.generator.approvaltests.multipleversions.v1.Multiple.class,
io.fabric8.crd.generator.approvaltests.multipleversions.v2.Multiple.class));
cases.add(new TestCase("nocyclics.sample.fabric8.io", crdVersion, parallel, NoCyclic.class));
}
for (boolean parallel : new boolean[] { false, true }) {
cases.add(new TestCase("annotateds.samples.fabric8.io", crdVersion, parallel, Annotated.class));
cases.add(new TestCase("complexkinds.samples.fabric8.io", crdVersion, parallel, Complex.class));
cases.add(new TestCase("children.sample.fabric8.io", crdVersion, parallel, Child.class));
cases.add(new TestCase("containingjsons.sample.fabric8.io", crdVersion, parallel, ContainingJson.class));
cases.add(new TestCase("k8svalidations.samples.fabric8.io", crdVersion, parallel, K8sValidation.class));
cases.add(new TestCase("containingmaps.sample.fabric8.io", crdVersion, parallel, ContainingMaps.class));
cases.add(new TestCase("multiples.sample.fabric8.io", crdVersion, parallel,
io.fabric8.crd.generator.approvaltests.multipleversions.v1.Multiple.class,
io.fabric8.crd.generator.approvaltests.multipleversions.v2.Multiple.class));
cases.add(new TestCase("nocyclics.sample.fabric8.io", crdVersion, parallel, NoCyclic.class));
}
return cases.stream().map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel));
return cases.stream();
}

private static final class TestCase {
Expand All @@ -119,6 +154,7 @@ private static final class TestCase {
private final String version;
private final boolean parallel;

@SafeVarargs
public TestCase(String expectedCrd, String version, boolean parallel, Class<? extends CustomResource<?, ?>>... crClasses) {
this.expectedCrd = expectedCrd;
this.version = version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ public class ContainingJsonSpec {

private JsonNode free;

private Foo foo;

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ spec:
properties:
field:
type: "integer"
foo:
properties:
configAsMap:
additionalProperties:
type: "object"
type: "object"
x-kubernetes-preserve-unknown-fields: true
type: "object"
x-kubernetes-preserve-unknown-fields: true
free:
x-kubernetes-preserve-unknown-fields: true
type: "object"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ spec:
properties:
field:
type: "integer"
foo:
properties:
configAsMap:
additionalProperties:
type: "object"
type: "object"
x-kubernetes-preserve-unknown-fields: true
type: "object"
x-kubernetes-preserve-unknown-fields: true
free:
x-kubernetes-preserve-unknown-fields: true
type: "object"
Expand Down
Loading