quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.8k stars 2.68k forks source link

Use a mock server with Initialization events #7232

Open alphamikevictor opened 4 years ago

alphamikevictor commented 4 years ago

Describe the bug Maybe it is not a bug, maybe it is a feature to implement.

So far I am writing a sort of Operator with Quarkus, so when Quarkus starts I am placing code to deal with Kubernetes, while performing tests it is complaining because when trying to get CRDs it receives a 404.

This is an extract of the code:

    void onStart(@Observes StartupEvent ev) {
        KubernetesClient oc = new DefaultKubernetesClient();
        CustomResourceDefinition crd = oc.customResourceDefinitions()
                .list().getItems().stream()
                    .filter( ob -> ob.getMetadata().getName().equals("types.openshift.example-cloud.com"))
                    .findFirst()
                    .orElseThrow( () -> new IllegalArgumentException("Can not find TYPE CRD"));

Expected behavior I was expecting to reach the mockServer set up for the test and return the list of CRDs declared on the mock:

@QuarkusTestResource(KubernetesMockServerTestResource.class)
@QuarkusTest
class TestAIRController {

    @MockServer
    KubernetesMockServer mockServer;

    private CustomResourceDefinition crd;
    private CustomResourceDefinitionList crdlist;

    @BeforeEach
    public void before() {
        crd = new CustomResourceDefinitionBuilder()
            .withApiVersion("apiextensions.k8s.io/v1beta1")
            .withNewMetadata().withName("types.openshift.example-cloud.com")
            .endMetadata()
            .withNewSpec()
            .withNewNames()
            .withKind("Type")
            .withPlural("types")
            .endNames()
            .withGroup("openshift.example-cloud.com")
            .withVersion("v1")
            .withScope("Namespaced")
            .endSpec()
            .build();
        crdlist = new CustomResourceDefinitionListBuilder().withItems(crd).build();
        mockServer.expect().get().withPath("/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions")
            .andReturn(200, crdlist)
            .always();
    }
}

Actual behavior Instead of getting the list of CRDs in the logs I can see this:

2020-02-17 11:04:12,148 INFO  [okh.moc.MockWebServer] (MockWebServer /127.0.0.1:53048) MockWebServer[57577] received request: GET /apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions HTTP/1.1 and responded: HTTP/1.1 404 Client Error

In fact in a Dummy REST resource I've written it is able to retrieve the list of CRDs from the mock server and perform the stuff it is supposed to do.

Environment (please complete the following information):

irawlinson commented 3 years ago

Have there been any updates on this feature?

irawlinson commented 3 years ago

Found the solution to my problem in #15144, though I don't know if it addresses this feature.

Adding this to my application.properties fixed my issue

quarkus.arc.test.disable-application-lifecycle-observers=true