-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(crd-generator): Add approval tests for crd-generator
- Loading branch information
Showing
62 changed files
with
3,707 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...rc/test/java/io/fabric8/crd/generator/approvaltests/AbstractCRDGeneratorApprovalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package io.fabric8.crd.generator.approvaltests; | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
import io.fabric8.crd.generator.CRDGenerationInfo; | ||
import io.fabric8.crd.generator.CRDGenerator; | ||
import io.fabric8.crd.generator.CRDInfo; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import org.approvaltests.Approvals; | ||
import org.approvaltests.approvers.ApprovalApprover; | ||
import org.approvaltests.approvers.FileApprover; | ||
import org.approvaltests.core.Options; | ||
import org.approvaltests.namer.ApprovalNamer; | ||
import org.approvaltests.writers.ApprovalTextWriter; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
public abstract class AbstractCRDGeneratorApprovalTest { | ||
|
||
private static final String[] CRD_VERSIONS = new String[] { | ||
"v1", "v1beta1" | ||
}; | ||
|
||
protected abstract Class<? extends CustomResource<?, ?>>[] crClasses(); | ||
|
||
protected abstract String[] expectedCRDs(); | ||
|
||
@BeforeAll | ||
public void init() { | ||
// Allow using the same approval files for this class multiple times (required for parallel testing) | ||
String className = getClass().getName().replace('.', File.separatorChar); | ||
FileApprover.tracker.addAllowedDuplicates(f -> f.contains(className)); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
test(false); | ||
} | ||
|
||
@Test | ||
public void testParallel() { | ||
test(true); | ||
} | ||
|
||
public void test(boolean parallel) { | ||
Class<? extends CustomResource<?, ?>>[] crClasses = crClasses(); | ||
assertNotNull(crClasses); | ||
assertTrue(crClasses.length > 0); | ||
|
||
final File outputDir; | ||
try { | ||
outputDir = Files.createTempDirectory("crd-").toFile(); | ||
} catch (IOException e) { | ||
fail("Could not create temp directory", e); | ||
throw new RuntimeException(e); | ||
} | ||
|
||
// generate actual CRDs | ||
final CRDGenerationInfo crdGenerationInfo = new CRDGenerator() | ||
.withParallelGenerationEnabled(parallel) | ||
.inOutputDir(outputDir) | ||
.customResourceClasses(crClasses) | ||
.forCRDVersions(CRD_VERSIONS) | ||
.detailedGenerate(); | ||
|
||
for (String crdName : expectedCRDs()) { | ||
Map<String, Map<String, CRDInfo>> crdDetails = crdGenerationInfo.getCRDDetailsPerNameAndVersion(); | ||
Map<String, CRDInfo> crdInfos = crdDetails.get(crdName); | ||
assertNotNull(crdInfos, "Could not find expected CRD " + crdName | ||
+ " in results. Found instead: " + crdDetails.keySet()); | ||
for (String crdVersion : CRD_VERSIONS) { | ||
CRDInfo crdInfo = crdInfos.get(crdVersion); | ||
final File actualCRDFile = new File(crdInfo.getFilePath()); | ||
String crdFileContent = readFile(actualCRDFile); | ||
verify(crdName, crdVersion, crdFileContent); | ||
} | ||
} | ||
|
||
// only delete the generated files if the test is successful | ||
deleteDirectory(outputDir); | ||
} | ||
|
||
private void verify(String crdName, String crdVersion, String crdFileContent) { | ||
ApprovalTextWriter writer = new ApprovalTextWriter(crdFileContent, new Options() | ||
.forFile().withExtension(".yml")); | ||
ApprovalNamer approvalNamer = new CRDGeneratorApprovalNamer(AbstractCRDGeneratorApprovalTest.class, getClass()) | ||
.addAdditionalInformation(crdName) | ||
.addAdditionalInformation(crdVersion); | ||
ApprovalApprover approver = new FileApprover(writer, approvalNamer); | ||
Approvals.verify(approver); | ||
} | ||
|
||
private String readFile(File file) { | ||
try { | ||
return new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath()))) | ||
.lines().collect(Collectors.joining("\n")); | ||
} catch (IOException e) { | ||
fail(e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void deleteDirectory(File dir) { | ||
try (Stream<Path> fileStream = Files.walk(dir.toPath())) { | ||
fileStream.sorted(Comparator.reverseOrder()) | ||
.map(Path::toFile) | ||
.forEach(File::delete); | ||
} catch (IOException e) { | ||
fail("Could not delete output directory", e); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...r/api/src/test/java/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalNamer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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 com.spun.util.tests.StackTraceReflectionResult; | ||
import com.spun.util.tests.TestUtils; | ||
import org.approvaltests.namer.ApprovalNamer; | ||
import org.approvaltests.namer.AttributeStackSelector; | ||
import org.approvaltests.namer.NamerFactory; | ||
import org.approvaltests.writers.Writer; | ||
|
||
import java.io.File; | ||
|
||
class CRDGeneratorApprovalNamer implements ApprovalNamer { | ||
|
||
private final String implementationClassName; | ||
private final String baseDirectory; | ||
private final String additionalInformation; | ||
|
||
public CRDGeneratorApprovalNamer(Class<?> abstractClass, Class<?> implementationClass) { | ||
StackTraceReflectionResult info = TestUtils.getCurrentFileForMethod(new AttributeStackSelector()); | ||
additionalInformation = NamerFactory.getAndClearAdditionalInformation(); | ||
|
||
String abstractClassPath = abstractClass.getPackage().getName().replace('.', File.separatorChar); | ||
String implementationClassPath = implementationClass.getPackage().getName().replace('.', File.separatorChar); | ||
this.implementationClassName = implementationClass.getSimpleName(); | ||
|
||
String abstractClassAbsolutePath = info.getSourceFile().getAbsolutePath(); | ||
this.baseDirectory = abstractClassAbsolutePath.replace(abstractClassPath, | ||
File.separatorChar + implementationClassPath); | ||
} | ||
|
||
public CRDGeneratorApprovalNamer(String baseDirectory, | ||
String implementationClassName, | ||
String additionalInformation) { | ||
this.baseDirectory = baseDirectory; | ||
this.implementationClassName = implementationClassName; | ||
this.additionalInformation = additionalInformation; | ||
} | ||
|
||
@Override | ||
public String getApprovalName() { | ||
return String.format("%s%s", implementationClassName, additionalInformation); | ||
} | ||
|
||
@Override | ||
public String getSourceFilePath() { | ||
String sub = NamerFactory.getSubdirectory(); | ||
String subdirectory = sub != null && !sub.isEmpty() ? sub + File.separator : ""; | ||
return baseDirectory + File.separator + subdirectory; | ||
} | ||
|
||
@Override | ||
public File getReceivedFile(String extensionWithDot) { | ||
return new File( | ||
getSourceFilePath() + File.separatorChar + getApprovalName() + Writer.received + extensionWithDot); | ||
} | ||
|
||
@Override | ||
public File getApprovedFile(String extensionWithDot) { | ||
return new File( | ||
getSourceFilePath() + File.separatorChar + getApprovalName() + Writer.approved + extensionWithDot); | ||
} | ||
|
||
public ApprovalNamer addAdditionalInformation(String additionalInformation) { | ||
return new CRDGeneratorApprovalNamer(this.baseDirectory, this.implementationClassName, | ||
this.additionalInformation + "." + additionalInformation); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
crd-generator/api/src/test/java/io/fabric8/crd/generator/approvaltests/PackageSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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(); | ||
} |
Oops, something went wrong.