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

chore(crd-generator): Add approval tests for crd-generator #5943

Merged
merged 5 commits into from
Apr 27, 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
17 changes: 16 additions & 1 deletion crd-generator/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<artifactId>kubernetes-client-api</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jsonSchema</artifactId>
Expand Down Expand Up @@ -76,12 +76,27 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.approvaltests</groupId>
<artifactId>approvaltests</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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;

import io.fabric8.crd.example.multiple.v2.MultipleSpec;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.File;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThatIllegalStateException;

class MultipleStoredVersionsTest {

@Group("sample.fabric8.io")
@Version(value = "v3")
public static class Multiple extends CustomResource<MultipleSpec, Void> {
}

@ParameterizedTest(name = "{index}: version: {0}, parallel: {1}")
@DisplayName("Generate CRD for multiple stored versions throws exception")
@MethodSource("multipleCrdVersions")
void generateMultipleThrowsException(String crdVersion, boolean parallel, @TempDir File tmpDir) {
final CRDGenerator crdGenerator = new CRDGenerator()
.inOutputDir(tmpDir)
.withParallelGenerationEnabled(parallel)
.forCRDVersions(crdVersion)
.customResourceClasses(io.fabric8.crd.example.multiple.v2.Multiple.class, Multiple.class);
assertThatIllegalStateException()
.isThrownBy(crdGenerator::generate)
.withMessageContaining("Only one version can be marked as storage per custom resource.");
}

static Stream<Arguments> multipleCrdVersions() {
return Stream.of("v1", "v1beta1")
.flatMap(crdVersion -> Stream.of(
Arguments.of(crdVersion, false),
Arguments.of(crdVersion, true)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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;

import io.fabric8.crd.generator.CRDGenerator;
import io.fabric8.crd.generator.CRDInfo;
import io.fabric8.crd.generator.approvaltests.annotated.Annotated;
import io.fabric8.crd.generator.approvaltests.complex.Complex;
import io.fabric8.crd.generator.approvaltests.inherited.Child;
import io.fabric8.crd.generator.approvaltests.json.ContainingJson;
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.kubernetes.client.CustomResource;
import org.approvaltests.Approvals;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

class CRDGeneratorApprovalTest {

@TempDir
File tempDir;

@ParameterizedTest(name = "{1}.{2} parallel={3}")
@MethodSource("crdApprovalTests")
void approvalTest(
Class<? extends CustomResource<?, ?>>[] crClasses, String expectedCrd, String version, boolean parallel)
throws IOException {
Approvals.settings().allowMultipleVerifyCallsForThisMethod();
final Map<String, Map<String, CRDInfo>> result = new 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 String(Files.readAllBytes(new File(result.get(expectedCrd).get(version).getFilePath()).toPath())),
Approvals.NAMES.withParameters(expectedCrd, version));
}

static Stream<Arguments> crdApprovalTests() {
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));
}
}
return cases.stream().map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel));
}

private static final class TestCase {
private Class<? extends CustomResource<?, ?>>[] crClasses;
private String expectedCrd;
private String version;
private boolean parallel;

public TestCase(String expectedCrd, String version, boolean parallel, Class<? extends CustomResource<?, ?>>... crClasses) {
this.expectedCrd = expectedCrd;
this.version = version;
this.parallel = parallel;
this.crClasses = crClasses;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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;

import org.approvaltests.core.ApprovalFailureReporter;
import org.approvaltests.reporters.QuietReporter;

/**
* @see <a href=
* "https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/docs/explanations/BestConfigurationPractices.md">
* ApprovalTests - Best Configuration Practices</a>
*/
public class PackageSettings {
/**
* Disable Diff-Reporter
*/
@SuppressWarnings("unused")
public static ApprovalFailureReporter UseReporter = new QuietReporter();
public String ApprovalBaseDirectory = "../resources";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.annotated;

import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

@Group("samples.fabric8.io")
@Version("v1")
public class Annotated extends CustomResource<AnnotatedSpec, Void> {

}
Loading
Loading