Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP authored and manusa committed Dec 24, 2021
1 parent 0d58835 commit 191481f
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,149 +56,149 @@ class CRDGeneratorTest {

private final TestCRDOutput output = new TestCRDOutput();

// @Test
// void choosingCRDVersionsShouldWork() {
// CRDGenerator generator = new CRDGenerator();
// assertTrue(generator.getHandlers().isEmpty());
//
// generator.forCRDVersions();
// assertTrue(generator.getHandlers().isEmpty());
//
// generator.forCRDVersions((List<String>) null);
// assertTrue(generator.getHandlers().isEmpty());
//
// generator.forCRDVersions((String[]) null);
// assertTrue(generator.getHandlers().isEmpty());
//
// generator.forCRDVersions("v2");
// assertTrue(generator.getHandlers().isEmpty());
//
// String version = "v1";
// generator.forCRDVersions(version);
// Map<String, AbstractCustomResourceHandler> handlers = generator.getHandlers();
// assertEquals(1, handlers.size());
// assertTrue(handlers.containsKey(version));
//
// generator.forCRDVersions(version, "v1beta1", version, "v3", null);
// handlers = generator.getHandlers();
// assertEquals(2, handlers.size());
// assertTrue(handlers.containsKey(version));
// assertTrue(handlers.containsKey("v1beta1"));
// }
//
// @Test
// void addingCustomResourceInfosShouldWork() {
// CRDGenerator generator = new CRDGenerator();
// assertTrue(generator.getCustomResourceInfos().isEmpty());
//
// generator.customResourceClasses();
// assertTrue(generator.getCustomResourceInfos().isEmpty());
//
// generator.customResources();
// assertTrue(generator.getCustomResourceInfos().isEmpty());
//
// generator.customResources(null);
// assertTrue(generator.getCustomResourceInfos().isEmpty());
//
// generator.customResources(null, null);
// assertTrue(generator.getCustomResourceInfos().isEmpty());
//
// generator.customResourceClasses(Simplest.class);
// assertEquals(1, generator.getCustomResourceInfos().size());
// assertTrue(generator.getCustomResourceInfos().stream().allMatch(cri -> cri.crClassName().equals(Simplest.class.getName())));
//
// generator.customResourceClasses(Child.class);
// assertEquals(2, generator.getCustomResourceInfos().size());
// CustomResourceInfo simplest = CustomResourceInfo.fromClass(Simplest.class);
// assertTrue(generator.getCustomResourceInfos().contains(simplest));
// CustomResourceInfo child = CustomResourceInfo.fromClass(Child.class);
// assertTrue(generator.getCustomResourceInfos().contains(child));
//
// generator.customResources(CustomResourceInfo.fromClass(Child.class));
// assertEquals(2, generator.getCustomResourceInfos().size());
//
// CustomResourceInfo joke = CustomResourceInfo.fromClass(Joke.class);
// CustomResourceInfo jr = CustomResourceInfo.fromClass(JokeRequest.class);
// generator.customResources(joke, jr);
// Set<CustomResourceInfo> infos = generator.getCustomResourceInfos();
// assertEquals(4, infos.size());
// assertTrue(infos.contains(simplest));
// assertTrue(infos.contains(child));
// assertTrue(infos.contains(joke));
// assertTrue(infos.contains(jr));
// }
//
// @Test
// void shouldProperlyRecordNumberOfGeneratedCRDs() {
// CRDGenerator generator = new CRDGenerator();
// assertEquals(0, generator.generate());
// assertEquals(0, generator.detailedGenerate().numberOfGeneratedCRDs());
//
// final CRDGenerationInfo info = generator
// .customResourceClasses(Simplest.class, Child.class, Joke.class, JokeRequest.class)
// .forCRDVersions("v1", "v1beta1")
// .withOutput(output).detailedGenerate();
//
// assertEquals(4 * 2, info.numberOfGeneratedCRDs());
// final Map<String, Map<String, CRDInfo>> details = info.getCRDDetailsPerNameAndVersion();
// assertEquals(4, details.size());
// assertTrue(details.containsKey(CustomResource.getCRDName(Simplest.class)));
// assertTrue(details.containsKey(CustomResource.getCRDName(Child.class)));
// assertTrue(details.containsKey(CustomResource.getCRDName(Joke.class)));
// final String crdName = CustomResource.getCRDName(JokeRequest.class);
// assertTrue(details.containsKey(crdName));
// final Map<String, CRDInfo> jokeRequestInfos = info.getCRDInfos(crdName);
// assertEquals(2, jokeRequestInfos.size());
// assertTrue(jokeRequestInfos.containsKey("v1"));
// assertTrue(jokeRequestInfos.containsKey("v1beta1"));
// }
//
// @Test
// void shouldProperlyGenerateMultipleVersionsOfCRDs() {
// CRDGenerator generator = new CRDGenerator();
// final String specVersion = "v1beta1";
// final CRDGenerationInfo info = generator
// .customResourceClasses(Multiple.class, io.fabric8.crd.example.multiple.v2.Multiple.class)
// .forCRDVersions(specVersion)
// .withOutput(output).detailedGenerate();
//
// assertEquals(1, info.numberOfGeneratedCRDs());
// final Map<String, Map<String, CRDInfo>> details = info.getCRDDetailsPerNameAndVersion();
// assertEquals(1, details.size());
// // check multiple versions for same CR
// final String crdName = CustomResource.getCRDName(Multiple.class);
// assertTrue(details.containsKey(crdName));
// final Map<String, CRDInfo> infos = info.getCRDInfos(crdName);
// assertEquals(1, infos.size());
// assertTrue(infos.containsKey(specVersion));
//
// final String outputName = CRDGenerator.getOutputName(crdName, specVersion);
// CustomResourceDefinition definition = output.definition(outputName);
// assertNotNull(definition);
// assertEquals("apiextensions.k8s.io/" + specVersion, definition.getApiVersion());
//
// CustomResourceDefinitionSpec spec = definition.getSpec();
// final List<CustomResourceDefinitionVersion> versions = spec.getVersions();
// assertEquals(2, versions.size());
// assertTrue(versions.stream().filter(v -> v.getName().equals("v1")).count() == 1);
// assertTrue(versions.stream().filter(v -> v.getName().equals("v2")).count() == 1);
//
//
// Class<?>[] mustContainTraversedClasses = {Multiple.class, MultipleSpec.class, io.fabric8.crd.example.multiple.v2.Multiple.class, io.fabric8.crd.example.multiple.v2.MultipleSpec.class};
// final Set<String> dependentClassNames = infos.get(specVersion).getDependentClassNames();
// Arrays.stream(mustContainTraversedClasses).map(Class::getCanonicalName).forEach(c -> assertTrue(dependentClassNames.contains(c), "should contain " + c));
// }
//
// @Test void notDefiningOutputShouldNotGenerateAnything() {
// CRDGenerator generator = new CRDGenerator();
// assertEquals(0, generator.generate());
//
// CustomResourceInfo joke = CustomResourceInfo.fromClass(Joke.class);
// CustomResourceInfo jr = CustomResourceInfo.fromClass(JokeRequest.class);
// generator.customResources(joke, jr);
// assertEquals(0, generator.generate());
// }
//
@Test
void choosingCRDVersionsShouldWork() {
CRDGenerator generator = new CRDGenerator();
assertTrue(generator.getHandlers().isEmpty());

generator.forCRDVersions();
assertTrue(generator.getHandlers().isEmpty());

generator.forCRDVersions((List<String>) null);
assertTrue(generator.getHandlers().isEmpty());

generator.forCRDVersions((String[]) null);
assertTrue(generator.getHandlers().isEmpty());

generator.forCRDVersions("v2");
assertTrue(generator.getHandlers().isEmpty());

String version = "v1";
generator.forCRDVersions(version);
Map<String, AbstractCustomResourceHandler> handlers = generator.getHandlers();
assertEquals(1, handlers.size());
assertTrue(handlers.containsKey(version));

generator.forCRDVersions(version, "v1beta1", version, "v3", null);
handlers = generator.getHandlers();
assertEquals(2, handlers.size());
assertTrue(handlers.containsKey(version));
assertTrue(handlers.containsKey("v1beta1"));
}

@Test
void addingCustomResourceInfosShouldWork() {
CRDGenerator generator = new CRDGenerator();
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResourceClasses();
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResources();
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResources(null);
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResources(null, null);
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResourceClasses(Simplest.class);
assertEquals(1, generator.getCustomResourceInfos().size());
assertTrue(generator.getCustomResourceInfos().stream().allMatch(cri -> cri.crClassName().equals(Simplest.class.getName())));

generator.customResourceClasses(Child.class);
assertEquals(2, generator.getCustomResourceInfos().size());
CustomResourceInfo simplest = CustomResourceInfo.fromClass(Simplest.class);
assertTrue(generator.getCustomResourceInfos().contains(simplest));
CustomResourceInfo child = CustomResourceInfo.fromClass(Child.class);
assertTrue(generator.getCustomResourceInfos().contains(child));

generator.customResources(CustomResourceInfo.fromClass(Child.class));
assertEquals(2, generator.getCustomResourceInfos().size());

CustomResourceInfo joke = CustomResourceInfo.fromClass(Joke.class);
CustomResourceInfo jr = CustomResourceInfo.fromClass(JokeRequest.class);
generator.customResources(joke, jr);
Set<CustomResourceInfo> infos = generator.getCustomResourceInfos();
assertEquals(4, infos.size());
assertTrue(infos.contains(simplest));
assertTrue(infos.contains(child));
assertTrue(infos.contains(joke));
assertTrue(infos.contains(jr));
}

@Test
void shouldProperlyRecordNumberOfGeneratedCRDs() {
CRDGenerator generator = new CRDGenerator();
assertEquals(0, generator.generate());
assertEquals(0, generator.detailedGenerate().numberOfGeneratedCRDs());

final CRDGenerationInfo info = generator
.customResourceClasses(Simplest.class, Child.class, Joke.class, JokeRequest.class)
.forCRDVersions("v1", "v1beta1")
.withOutput(output).detailedGenerate();

assertEquals(4 * 2, info.numberOfGeneratedCRDs());
final Map<String, Map<String, CRDInfo>> details = info.getCRDDetailsPerNameAndVersion();
assertEquals(4, details.size());
assertTrue(details.containsKey(CustomResource.getCRDName(Simplest.class)));
assertTrue(details.containsKey(CustomResource.getCRDName(Child.class)));
assertTrue(details.containsKey(CustomResource.getCRDName(Joke.class)));
final String crdName = CustomResource.getCRDName(JokeRequest.class);
assertTrue(details.containsKey(crdName));
final Map<String, CRDInfo> jokeRequestInfos = info.getCRDInfos(crdName);
assertEquals(2, jokeRequestInfos.size());
assertTrue(jokeRequestInfos.containsKey("v1"));
assertTrue(jokeRequestInfos.containsKey("v1beta1"));
}

@Test
void shouldProperlyGenerateMultipleVersionsOfCRDs() {
CRDGenerator generator = new CRDGenerator();
final String specVersion = "v1beta1";
final CRDGenerationInfo info = generator
.customResourceClasses(Multiple.class, io.fabric8.crd.example.multiple.v2.Multiple.class)
.forCRDVersions(specVersion)
.withOutput(output).detailedGenerate();

assertEquals(1, info.numberOfGeneratedCRDs());
final Map<String, Map<String, CRDInfo>> details = info.getCRDDetailsPerNameAndVersion();
assertEquals(1, details.size());
// check multiple versions for same CR
final String crdName = CustomResource.getCRDName(Multiple.class);
assertTrue(details.containsKey(crdName));
final Map<String, CRDInfo> infos = info.getCRDInfos(crdName);
assertEquals(1, infos.size());
assertTrue(infos.containsKey(specVersion));

final String outputName = CRDGenerator.getOutputName(crdName, specVersion);
CustomResourceDefinition definition = output.definition(outputName);
assertNotNull(definition);
assertEquals("apiextensions.k8s.io/" + specVersion, definition.getApiVersion());

CustomResourceDefinitionSpec spec = definition.getSpec();
final List<CustomResourceDefinitionVersion> versions = spec.getVersions();
assertEquals(2, versions.size());
assertTrue(versions.stream().filter(v -> v.getName().equals("v1")).count() == 1);
assertTrue(versions.stream().filter(v -> v.getName().equals("v2")).count() == 1);


Class<?>[] mustContainTraversedClasses = {Multiple.class, MultipleSpec.class, io.fabric8.crd.example.multiple.v2.Multiple.class, io.fabric8.crd.example.multiple.v2.MultipleSpec.class};
final Set<String> dependentClassNames = infos.get(specVersion).getDependentClassNames();
Arrays.stream(mustContainTraversedClasses).map(Class::getCanonicalName).forEach(c -> assertTrue(dependentClassNames.contains(c), "should contain " + c));
}

@Test void notDefiningOutputShouldNotGenerateAnything() {
CRDGenerator generator = new CRDGenerator();
assertEquals(0, generator.generate());

CustomResourceInfo joke = CustomResourceInfo.fromClass(Joke.class);
CustomResourceInfo jr = CustomResourceInfo.fromClass(JokeRequest.class);
generator.customResources(joke, jr);
assertEquals(0, generator.generate());
}

@Test void generatingACycleShouldFail() {
final CRDGenerator generator = new CRDGenerator()
.customResourceClasses(Cyclic.class)
Expand Down Expand Up @@ -237,31 +237,31 @@ private void outputCRDIfFailed(Class<? extends CustomResource<?, ?>> customResou
throw e;
}
}
// @Test
// void simplestCRDShouldWork() {
// outputCRDIfFailed(Simplest.class, (customResource) -> {
// final CustomResourceDefinitionVersion version = checkCRD(customResource, "Simplest", "simplests",
// Scope.CLUSTER, SimplestSpec.class, SimplestStatus.class);
// assertNotNull(version.getSubresources());
// });
//
// }
//
// @Test
// void inheritedCRDShouldWork() {
// outputCRDIfFailed(Child.class, (customResource) -> {
// final CustomResourceDefinitionVersion version = checkCRD(customResource, "Child", "children",
// Scope.NAMESPACED, ChildSpec.class, ChildStatus.class, BaseSpec.class, BaseStatus.class);
// assertNotNull(version.getSubresources());
// final Map<String, JSONSchemaProps> specProps = version.getSchema().getOpenAPIV3Schema()
// .getProperties().get("spec").getProperties();
// assertEquals(4, specProps.size());
// assertEquals("integer", specProps.get("baseInt").getType());
// checkMapProp(specProps, "unsupported");
// checkMapProp(specProps, "unsupported2");
// checkMapProp(specProps, "supported");
// });
// }
@Test
void simplestCRDShouldWork() {
outputCRDIfFailed(Simplest.class, (customResource) -> {
final CustomResourceDefinitionVersion version = checkCRD(customResource, "Simplest", "simplests",
Scope.CLUSTER, SimplestSpec.class, SimplestStatus.class);
assertNotNull(version.getSubresources());
});

}

@Test
void inheritedCRDShouldWork() {
outputCRDIfFailed(Child.class, (customResource) -> {
final CustomResourceDefinitionVersion version = checkCRD(customResource, "Child", "children",
Scope.NAMESPACED, ChildSpec.class, ChildStatus.class, BaseSpec.class, BaseStatus.class);
assertNotNull(version.getSubresources());
final Map<String, JSONSchemaProps> specProps = version.getSchema().getOpenAPIV3Schema()
.getProperties().get("spec").getProperties();
assertEquals(4, specProps.size());
assertEquals("integer", specProps.get("baseInt").getType());
checkMapProp(specProps, "unsupported");
checkMapProp(specProps, "unsupported2");
checkMapProp(specProps, "supported");
});
}

private void checkMapProp(Map<String, JSONSchemaProps> specProps, String name) {
final JSONSchemaProps props = specProps.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@Version(value="v1alpha1", storage=false)
@Group("io.zookeeper")
public class Zookeeper extends CustomResource<ZookeeperSpec, ZookeeperStatus> implements Namespaced {

private ZookeeperSpec spec;
private ZookeeperStatus status;
}

0 comments on commit 191481f

Please sign in to comment.