-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow manually specifying CRDs in test extension (#2569)
* feat: allow manually specifying CRDs in test extension This is useful when using a contract-first approach where the Java classes are generated from the CRD instead of the reverse. Fixes #2561 Signed-off-by: Chris Laprun <claprun@redhat.com> * refactor: register CRDs via resource name instead of class Signed-off-by: Chris Laprun <claprun@redhat.com> --------- Signed-off-by: Chris Laprun <claprun@redhat.com>
- Loading branch information
Showing
3 changed files
with
177 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: tests.crd.example | ||
spec: | ||
group: crd.example | ||
names: | ||
kind: Test | ||
singular: test | ||
plural: tests | ||
scope: Namespaced | ||
versions: | ||
- name: v1 | ||
schema: | ||
openAPIV3Schema: | ||
properties: | ||
type: "object" | ||
served: true | ||
storage: true |
57 changes: 57 additions & 0 deletions
57
...ator-framework/src/test/java/io/javaoperatorsdk/operator/CRDMappingInTestExtensionIT.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,57 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import java.time.Duration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.Namespaced; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.fabric8.kubernetes.model.annotation.Group; | ||
import io.fabric8.kubernetes.model.annotation.Kind; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; | ||
import io.javaoperatorsdk.operator.api.reconciler.Reconciler; | ||
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class CRDMappingInTestExtensionIT { | ||
private final KubernetesClient client = new KubernetesClientBuilder().build(); | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension operator = | ||
LocallyRunOperatorExtension.builder() | ||
.withReconciler(new TestReconciler()) | ||
.withCRDMapping("tests.crd.example", "src/test/crd/test.crd") | ||
.build(); | ||
|
||
@Test | ||
void correctlyAppliesManuallySpecifiedCRD() { | ||
operator.applyCrd(TestCR.class); | ||
|
||
final var crdClient = client.apiextensions().v1().customResourceDefinitions(); | ||
await().pollDelay(Duration.ofMillis(150)) | ||
.untilAsserted(() -> assertThat(crdClient.withName("tests.crd.example").get()).isNotNull()); | ||
} | ||
|
||
@Group("crd.example") | ||
@Version("v1") | ||
@Kind("Test") | ||
private static class TestCR extends CustomResource<Void, Void> implements Namespaced { | ||
} | ||
|
||
@ControllerConfiguration | ||
private static class TestReconciler implements Reconciler<TestCR> { | ||
@Override | ||
public UpdateControl<TestCR> reconcile(TestCR resource, Context<TestCR> context) | ||
throws Exception { | ||
return null; | ||
} | ||
} | ||
} |