From 9dbc604d9bdd6975ac43c9d3d79ff631bc2413fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernhard=20Str=C3=A4hle?= Date: Mon, 4 Nov 2024 15:09:05 +0100 Subject: [PATCH] test(crd-generator): allow to add additional approvaltests for CRD-Generator api-v2 only (6533) Split approvaltests for CRD-Generator --- Add approval test for PrinterColumn annotations --- .../CRDGeneratorApprovalTest.java | 50 ++++++++++++++--- .../printercolum/PrinterColumn.java | 27 +++++++++ .../printercolum/PrinterColumnSpec.java | 46 +++++++++++++++ ...rcolumns.sample.fabric8.io.v1.approved.yml | 56 +++++++++++++++++++ 4 files changed, 171 insertions(+), 8 deletions(-) create mode 100644 crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/printercolum/PrinterColumn.java create mode 100644 crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/printercolum/PrinterColumnSpec.java create mode 100644 crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.printercolumns.sample.fabric8.io.v1.approved.yml diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.java index 77af9556817..4056d242379 100644 --- a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.java +++ b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.java @@ -27,6 +27,7 @@ import io.fabric8.crd.generator.approvaltests.k8svalidation.K8sValidation; import io.fabric8.crd.generator.approvaltests.map.ContainingMaps; import io.fabric8.crd.generator.approvaltests.nocyclic.NoCyclic; +import io.fabric8.crd.generator.approvaltests.printercolum.PrinterColumn; import io.fabric8.crd.generator.approvaltests.replica.Replica; import io.fabric8.kubernetes.client.CustomResource; import io.sundr.utils.Strings; @@ -72,7 +73,7 @@ void tearDown() { } @ParameterizedTest(name = "{1}.{2} parallel={3}") - @MethodSource("crdApprovalTests") + @MethodSource("crdApprovalTestsApiV1") @DisplayName("CRD Generator V1 Approval Tests") void apiV1ApprovalTest( Class>[] crClasses, String expectedCrd, String version, boolean parallel) { @@ -97,7 +98,7 @@ void apiV1ApprovalTest( } @ParameterizedTest(name = "{1}.{2} parallel={3}") - @MethodSource("crdV1ApprovalTests") + @MethodSource("crdApprovalTestsApiV2") @DisplayName("CRD Generator V2 Approval Tests") void apiV2ApprovalTest( Class>[] crClasses, String expectedCrd, String version, boolean parallel) { @@ -122,18 +123,37 @@ void apiV2ApprovalTest( new Namer(expectedCrd, version)); } - static Stream crdApprovalTests() { + /** + * Method source for test cases targeting CRD-Generator api-v1. + * + * @return the arguments for the test cases + */ + static Stream crdApprovalTestsApiV1() { return Stream.concat( - crdApprovalBaseCases("v1"), - crdApprovalBaseCases("v1beta1")).map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel)); + crdApprovalCasesBase("v1"), + crdApprovalCasesBase("v1beta1")) + .map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel)); } - static Stream crdV1ApprovalTests() { - return crdApprovalBaseCases("v1") + /** + * Method source for test cases targeting CRD-Generator api-v2. + * + * @return the arguments for the test cases + */ + static Stream crdApprovalTestsApiV2() { + return Stream.concat( + crdApprovalCasesBase("v1"), + crdApprovalCasesApiV2("v1")) .map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel)); } - static Stream crdApprovalBaseCases(String crdVersion) { + /** + * Test cases for CRD-Generator api-v1 and api-v2 which must have the exact same results. + * + * @param crdVersion the CRD version + * @return the test cases + */ + static Stream crdApprovalCasesBase(String crdVersion) { final List cases = new ArrayList<>(); for (boolean parallel : new boolean[] { false, true }) { cases.add(new TestCase("annotateds.samples.fabric8.io", crdVersion, parallel, Annotated.class)); @@ -151,6 +171,20 @@ static Stream crdApprovalBaseCases(String crdVersion) { return cases.stream(); } + /** + * Test cases for CRD-Generator api-v2 only. + * + * @param crdVersion the CRD version + * @return the test cases + */ + static Stream crdApprovalCasesApiV2(String crdVersion) { + final List cases = new ArrayList<>(); + for (boolean parallel : new boolean[] { false, true }) { + cases.add(new TestCase("printercolumns.sample.fabric8.io", crdVersion, parallel, PrinterColumn.class)); + } + return cases.stream(); + } + private static final class TestCase { private final Class>[] crClasses; private final String expectedCrd; diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/printercolum/PrinterColumn.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/printercolum/PrinterColumn.java new file mode 100644 index 00000000000..33a803a2cda --- /dev/null +++ b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/printercolum/PrinterColumn.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.crd.generator.approvaltests.printercolum; + +import io.fabric8.crd.generator.annotation.AdditionalPrinterColumn; +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Version; + +@Version("v1alpha1") +@Group("sample.fabric8.io") +@AdditionalPrinterColumn(jsonPath = ".spec.deepLevel1.name") +public class PrinterColumn extends CustomResource { +} diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/printercolum/PrinterColumnSpec.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/printercolum/PrinterColumnSpec.java new file mode 100644 index 00000000000..7a3e8101259 --- /dev/null +++ b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/printercolum/PrinterColumnSpec.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.crd.generator.approvaltests.printercolum; + +import io.fabric8.crd.generator.annotation.PrinterColumn; +import lombok.Data; + +@Data +public class PrinterColumnSpec { + + @PrinterColumn + private String id; + + private DeepLevel1 deepLevel1; + + @Data + static class DeepLevel1 { + // targeted from @AdditionalPrinterColumn + private String name; + + @PrinterColumn + private Integer fromLevel1; + + private DeepLevel2 deepLevel2; + } + + @Data + static class DeepLevel2 { + @PrinterColumn + private Boolean fromLevel2; + } + +} diff --git a/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.printercolumns.sample.fabric8.io.v1.approved.yml b/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.printercolumns.sample.fabric8.io.v1.approved.yml new file mode 100644 index 00000000000..99a40df8091 --- /dev/null +++ b/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.printercolumns.sample.fabric8.io.v1.approved.yml @@ -0,0 +1,56 @@ +# Generated by Fabric8 CRDGenerator, manual edits might get overwritten! +apiVersion: "apiextensions.k8s.io/v1" +kind: "CustomResourceDefinition" +metadata: + name: "printercolumns.sample.fabric8.io" +spec: + group: "sample.fabric8.io" + names: + kind: "PrinterColumn" + plural: "printercolumns" + singular: "printercolumn" + scope: "Cluster" + versions: + - additionalPrinterColumns: + - jsonPath: ".spec.deepLevel1.deepLevel2.fromLevel2" + name: "FROMLEVEL2" + priority: 0 + type: "boolean" + - jsonPath: ".spec.deepLevel1.fromLevel1" + name: "FROMLEVEL1" + priority: 0 + type: "integer" + - jsonPath: ".spec.deepLevel1.name" + name: "NAME" + priority: 0 + type: "string" + - jsonPath: ".spec.id" + name: "ID" + priority: 0 + type: "string" + name: "v1alpha1" + schema: + openAPIV3Schema: + properties: + spec: + properties: + deepLevel1: + properties: + deepLevel2: + properties: + fromLevel2: + type: "boolean" + type: "object" + fromLevel1: + type: "integer" + name: + type: "string" + type: "object" + id: + type: "string" + type: "object" + status: + type: "object" + type: "object" + served: true + storage: true