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

Can not mock K8S api /version #23023

Closed
synox opened this issue Jan 19, 2022 · 8 comments
Closed

Can not mock K8S api /version #23023

synox opened this issue Jan 19, 2022 · 8 comments
Labels
area/kubernetes kind/bug Something isn't working triage/wontfix This will not be worked on

Comments

@synox
Copy link

synox commented Jan 19, 2022

Describe the bug

Cannot mock the /version api. I followed the guide https://quarkus.io/guides/kubernetes-client#testing.

Mocking /version works on 2.4.2.Final, but fails on 2.5.4.Final and 2.6.2.Final.

The following code should throw an exception when the version path is accessed, but it does not thrown an error for v2.5.4 or higher:

import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.VersionInfo;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.kubernetes.client.KubernetesTestServer;
import io.quarkus.test.kubernetes.client.WithKubernetesTestServer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.net.HttpURLConnection;


@QuarkusTest
@WithKubernetesTestServer(crud = false)
class K8sTest {

    @KubernetesTestServer
    KubernetesServer server;


    @Test
    public void fail() {
        server.expect().get().withPath("/version")
                .andReturn(HttpURLConnection.HTTP_INTERNAL_ERROR, "error")
                .always();

        System.out.println("http://" + server.getKubernetesMockServer().getHostName() + ":" + server.getKubernetesMockServer().getPort());

        NamespacedKubernetesClient client = server.getClient();
        Assertions.assertThrows(KubernetesClientException.class, () -> {

            VersionInfo response = client.getKubernetesVersion();
            System.out.println(response);
        });
    }
}

However the following code works as expected with Quarkus 2.4.2 (note there are some api changes).

import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.VersionInfo;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.kubernetes.client.KubernetesTestServer;
import io.quarkus.test.kubernetes.client.WithKubernetesTestServer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.net.HttpURLConnection;


@QuarkusTest
@WithKubernetesTestServer(crud = false)
class K8sTest {

    @KubernetesTestServer
    KubernetesServer server;


    @Test
    public void fail() {
        server.expect().get().withPath("/version")
                .andReturn(HttpURLConnection.HTTP_INTERNAL_ERROR, "error")
                .always();

        System.out.println("http://" + server.getMockServer().getHostName() + ":" + server.getMockServer().getPort());

        NamespacedKubernetesClient client = server.getClient();
        Assertions.assertThrows(KubernetesClientException.class, () -> {

            VersionInfo response = client.getVersion();
            System.out.println(response);
        });
    }
}

Expected behavior

Should allow to mock /version api.

Actual behavior

Mock of /version does not have any impact.

How to Reproduce?

See code above.

Output of uname -a or ver

Darwin 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:41 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T6000 arm64

Output of java -version

openjdk version "17.0.1" 2021-10-19

GraalVM version (if different from Java)

not relevant

Quarkus version or git rev

2.6.2

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

@synox synox added the kind/bug Something isn't working label Jan 19, 2022
@quarkus-bot
Copy link

quarkus-bot bot commented Jan 19, 2022

/cc @geoand, @iocanel

@synox synox changed the title Can not mock K8S api Can not mock K8S api /version Jan 19, 2022
@geoand
Copy link
Contributor

geoand commented Jan 19, 2022

cc @manusa

@geoand
Copy link
Contributor

geoand commented Jan 20, 2022

So is this still an issue with Quarkus 2.7.0.CR1?

@manusa
Copy link
Contributor

manusa commented Jan 20, 2022

The /version default expectation was added especially for JOSDK (and in general for the standard functionality of any Kubernetes Application). Overriding this value seems like an edge case.

The expectation is based on a defaulted versionInfo within the KubernetesMockServer class. However, this field is not accessible (maybe we need to change this).

The recommended approach (which works with any version), is to clear the default expectations on the test setup:

  @BeforeEach
  void setUp() {
    server.getKubernetesMockServer().clearExpectations();
  }

I'm still not sure if we should expose the default versionInfo field, and allow users to modify it. Or just recommend the current workaround/best practice. (Input is welcome)

@manusa
Copy link
Contributor

manusa commented Jan 20, 2022

So is this still an issue with Quarkus 2.7.0.CR1?

Yes, however, as explained, I would call it feature instead of issue 😄

@geoand
Copy link
Contributor

geoand commented Jan 20, 2022

Gotcha, so let's close this then :)

@geoand geoand closed this as completed Jan 20, 2022
@geoand geoand added the triage/wontfix This will not be worked on label Jan 20, 2022
@synox
Copy link
Author

synox commented Jan 21, 2022

Thank you all for providing the workaround and link to the resolved ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubernetes kind/bug Something isn't working triage/wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants