Skip to content

Commit

Permalink
test(crd-generator): allow to add additional approvaltests for CRD-Ge…
Browse files Browse the repository at this point in the history
…nerator api-v2 only (6533)

Split approvaltests for CRD-Generator
---
Add approval test for PrinterColumn annotations
  • Loading branch information
baloo42 authored Nov 4, 2024
1 parent dbe132b commit 9dbc604
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends CustomResource<?, ?>>[] crClasses, String expectedCrd, String version, boolean parallel) {
Expand All @@ -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<? extends CustomResource<?, ?>>[] crClasses, String expectedCrd, String version, boolean parallel) {
Expand All @@ -122,18 +123,37 @@ void apiV2ApprovalTest(
new Namer(expectedCrd, version));
}

static Stream<Arguments> crdApprovalTests() {
/**
* Method source for test cases targeting CRD-Generator api-v1.
*
* @return the arguments for the test cases
*/
static Stream<Arguments> 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<Arguments> crdV1ApprovalTests() {
return crdApprovalBaseCases("v1")
/**
* Method source for test cases targeting CRD-Generator api-v2.
*
* @return the arguments for the test cases
*/
static Stream<Arguments> crdApprovalTestsApiV2() {
return Stream.concat(
crdApprovalCasesBase("v1"),
crdApprovalCasesApiV2("v1"))
.map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel));
}

static Stream<TestCase> 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<TestCase> crdApprovalCasesBase(String crdVersion) {
final List<TestCase> cases = new ArrayList<>();
for (boolean parallel : new boolean[] { false, true }) {
cases.add(new TestCase("annotateds.samples.fabric8.io", crdVersion, parallel, Annotated.class));
Expand All @@ -151,6 +171,20 @@ static Stream<TestCase> 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<TestCase> crdApprovalCasesApiV2(String crdVersion) {
final List<TestCase> 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<? extends CustomResource<?, ?>>[] crClasses;
private final String expectedCrd;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PrinterColumnSpec, Void> {
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9dbc604

Please sign in to comment.