diff --git a/CHANGELOG.md b/CHANGELOG.md index 54ac667d788..ef7cb5dff07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ #### Bugs * Fix #6038: Support for Gradle configuration cache * Fix #6059: Swallow rejected execution from internal usage of the informer executor +* Fix #6068: KubernetesMockServer provides incomplete Configuration while creating test Config for KubernetesClient #### Improvements * Fix #6008: removing the optional dependency on bouncy castle diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServer.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServer.java index 0511cfcdf98..72d31aa3113 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServer.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServer.java @@ -19,6 +19,8 @@ import io.fabric8.kubernetes.api.model.APIResourceBuilder; import io.fabric8.kubernetes.api.model.APIResourceList; import io.fabric8.kubernetes.api.model.APIResourceListBuilder; +import io.fabric8.kubernetes.api.model.NamedContext; +import io.fabric8.kubernetes.api.model.NamedContextBuilder; import io.fabric8.kubernetes.api.model.RootPathsBuilder; import io.fabric8.kubernetes.client.Client; import io.fabric8.kubernetes.client.Config; @@ -205,12 +207,24 @@ public void clearExpectations() { } protected Config initConfig() { + final NamedContext mockServerContext = new NamedContextBuilder() + .withName("fabric8-mock-server-context") + .withNewContext() + .withNamespace("test") + .withCluster(String.format("localhost:%d", getPort())) + .withUser("fabric8-mock-server-user") + .endContext() + .build(); return new ConfigBuilder(Config.empty()) .withMasterUrl(url("/")) .withTrustCerts(true) .withTlsVersions(TlsVersion.TLS_1_2) .withNamespace("test") .withHttp2Disable(true) + .addToContexts(mockServerContext) + .withCurrentContext(mockServerContext) + .withUsername("fabric8-mock-server-user") + .withOauthToken("secret") .build(); } diff --git a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServerExtensionTest.java b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServerExtensionTest.java index 29f3580ee86..a1f3554ac2d 100644 --- a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServerExtensionTest.java +++ b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServerExtensionTest.java @@ -15,9 +15,9 @@ */ package io.fabric8.kubernetes.client.server.mock; +import io.fabric8.kubernetes.client.Client; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.VersionInfo; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -29,16 +29,22 @@ class KubernetesMockServerExtensionTest { KubernetesClient client; @Test - void testExample() { - Assertions.assertNotNull(client); - Assertions.assertNull(client.getConfiguration().getOauthToken()); - Assertions.assertNull(client.getConfiguration().getCurrentContext()); - Assertions.assertTrue(client.getConfiguration().getContexts().isEmpty()); + void mockServerConfiguration() { + assertThat(client) + .isNotNull() + .extracting(Client::getConfiguration) + .hasFieldOrPropertyWithValue("oauthToken", "secret") + .hasFieldOrPropertyWithValue("username", "fabric8-mock-server-user") + .hasFieldOrPropertyWithValue("currentContext.name", "fabric8-mock-server-context") + .hasFieldOrPropertyWithValue("currentContext.context.namespace", "test") + .hasFieldOrPropertyWithValue("currentContext.context.user", "fabric8-mock-server-user") + .satisfies(c -> assertThat(c.getCurrentContext().getContext().getCluster()).startsWith("localhost:")) + .satisfies(c -> assertThat(c.getContexts()).hasSize(1)); } @Test @DisplayName("KubernetesMockServerExtension uses KubernetesMixedDispatcher and provides expectation for GET /version") - void testGetKubernetesVersion() { + void getKubernetesVersion() { // When final VersionInfo result = client.getKubernetesVersion(); // Then diff --git a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/MockServerKubeconfigTest.java b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/MockServerKubeconfigTest.java index b94d6bfe3a6..01fc3ebc520 100644 --- a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/MockServerKubeconfigTest.java +++ b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/MockServerKubeconfigTest.java @@ -15,14 +15,17 @@ */ package io.fabric8.kubernetes.client.server.mock; +import io.fabric8.kubernetes.api.model.NamedContext; import io.fabric8.kubernetes.client.KubernetesClient; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.Objects; +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + class MockServerKubeconfigTest { @BeforeEach @@ -39,10 +42,16 @@ void mockServerShouldNotPickTokenAndNameContextIfKubeConfigFound() { KubernetesClient client = server.createClient(); // Then - Assertions.assertNotNull(client); - Assertions.assertNull(client.getConfiguration().getOauthToken()); - Assertions.assertNull(client.getConfiguration().getCurrentContext()); - Assertions.assertTrue(client.getConfiguration().getContexts().isEmpty()); + assertThat(client).isNotNull(); + assertThat(client.getConfiguration()) + .isNotNull() + .satisfies(c -> assertThat(c.getCurrentContext().getName()).isNotEqualTo("default/api-crc-testing:6443/kubeadmin")) + .satisfies(c -> assertThat(c.getOauthToken()).isNotEqualTo("sha256~iYtvbJNJEE0_QSxYE0Wl1MJJxpSvDUsNyYfzkCIoDkw")) + .satisfies(c -> assertThat(c.getContexts()) + .hasSize(1) + .singleElement(InstanceOfAssertFactories.type(NamedContext.class)) + .extracting(NamedContext::getName) + .isNotEqualTo("default/api-crc-testing:6443/kubeadmin")); } @AfterEach diff --git a/junit/openshift-server-mock/pom.xml b/junit/openshift-server-mock/pom.xml index a804f7c800f..44f93f1b38a 100644 --- a/junit/openshift-server-mock/pom.xml +++ b/junit/openshift-server-mock/pom.xml @@ -39,6 +39,11 @@ openshift-client + + org.assertj + assertj-core + test + org.junit.jupiter junit-jupiter-api diff --git a/junit/openshift-server-mock/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtensionStaticTests.java b/junit/openshift-server-mock/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtensionStaticTests.java index 6eecb8fb21c..09cd7d1e689 100644 --- a/junit/openshift-server-mock/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtensionStaticTests.java +++ b/junit/openshift-server-mock/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtensionStaticTests.java @@ -15,12 +15,11 @@ */ package io.fabric8.openshift.client.server.mock; +import io.fabric8.kubernetes.client.Client; import io.fabric8.openshift.client.OpenShiftClient; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; @EnableOpenShiftMockClient(crud = true) class OpenShiftMockServerExtensionStaticTests { @@ -28,9 +27,15 @@ class OpenShiftMockServerExtensionStaticTests { @Test void testStaticOpenShiftClientGetsInitialized() { - assertNotNull(openShiftClient); - assertNull(openShiftClient.getConfiguration().getOauthToken()); - assertNull(openShiftClient.getConfiguration().getCurrentContext()); - assertTrue(openShiftClient.getConfiguration().getContexts().isEmpty()); + assertThat(openShiftClient) + .isNotNull() + .extracting(Client::getConfiguration) + .hasFieldOrPropertyWithValue("oauthToken", "secret") + .hasFieldOrPropertyWithValue("username", "fabric8-mock-server-user") + .hasFieldOrPropertyWithValue("currentContext.name", "fabric8-mock-server-context") + .hasFieldOrPropertyWithValue("currentContext.context.namespace", "test") + .hasFieldOrPropertyWithValue("currentContext.context.user", "fabric8-mock-server-user") + .satisfies(c -> assertThat(c.getCurrentContext().getContext().getCluster()).startsWith("localhost:")) + .satisfies(c -> assertThat(c.getContexts()).hasSize(1)); } } diff --git a/junit/openshift-server-mock/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtensionTests.java b/junit/openshift-server-mock/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtensionTests.java index b87b1f47c28..f4fa10f038d 100644 --- a/junit/openshift-server-mock/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtensionTests.java +++ b/junit/openshift-server-mock/src/test/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtensionTests.java @@ -15,12 +15,11 @@ */ package io.fabric8.openshift.client.server.mock; +import io.fabric8.kubernetes.client.Client; import io.fabric8.openshift.client.OpenShiftClient; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; @EnableOpenShiftMockClient(crud = true) class OpenShiftMockServerExtensionTests { @@ -28,9 +27,15 @@ class OpenShiftMockServerExtensionTests { @Test void testOpenShiftClientGetsInitialized() { - assertNotNull(client); - assertNull(client.getConfiguration().getOauthToken()); - assertNull(client.getConfiguration().getCurrentContext()); - assertTrue(client.getConfiguration().getContexts().isEmpty()); + assertThat(client) + .isNotNull() + .extracting(Client::getConfiguration) + .hasFieldOrPropertyWithValue("oauthToken", "secret") + .hasFieldOrPropertyWithValue("username", "fabric8-mock-server-user") + .hasFieldOrPropertyWithValue("currentContext.name", "fabric8-mock-server-context") + .hasFieldOrPropertyWithValue("currentContext.context.namespace", "test") + .hasFieldOrPropertyWithValue("currentContext.context.user", "fabric8-mock-server-user") + .satisfies(c -> assertThat(c.getCurrentContext().getContext().getCluster()).startsWith("localhost:")) + .satisfies(c -> assertThat(c.getContexts()).hasSize(1)); } } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java index eac70a81351..f7abf6151ee 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java @@ -52,6 +52,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -332,7 +333,7 @@ public Config(String masterUrl, String apiVersion, String namespace, boolean tru userAgent, tlsVersions, websocketPingInterval, proxyUsername, proxyPassword, trustStoreFile, trustStorePassphrase, keyStoreFile, keyStorePassphrase, impersonateUsername, impersonateGroups, impersonateExtras, null, null, DEFAULT_REQUEST_RETRY_BACKOFFLIMIT, DEFAULT_REQUEST_RETRY_BACKOFFINTERVAL, - DEFAULT_UPLOAD_REQUEST_TIMEOUT, false); + DEFAULT_UPLOAD_REQUEST_TIMEOUT, false, null, Collections.emptyList()); } @Buildable(builderPackage = "io.fabric8.kubernetes.api.builder", editableEnabled = false) @@ -347,7 +348,8 @@ public Config(String masterUrl, String apiVersion, String namespace, boolean tru String proxyPassword, String trustStoreFile, String trustStorePassphrase, String keyStoreFile, String keyStorePassphrase, String impersonateUsername, String[] impersonateGroups, Map> impersonateExtras, OAuthTokenProvider oauthTokenProvider, Map customHeaders, int requestRetryBackoffLimit, - int requestRetryBackoffInterval, int uploadRequestTimeout, boolean onlyHttpWatches) { + int requestRetryBackoffInterval, int uploadRequestTimeout, boolean onlyHttpWatches, NamedContext currentContext, + List contexts) { this.apiVersion = apiVersion; this.namespace = namespace; this.trustCerts = trustCerts; @@ -396,6 +398,8 @@ public Config(String masterUrl, String apiVersion, String namespace, boolean tru this.maxConcurrentRequestsPerHost = maxConcurrentRequestsPerHost; this.autoOAuthToken = autoOAuthToken; this.onlyHttpWatches = onlyHttpWatches; + this.contexts = contexts; + this.currentContext = currentContext; } public static void configFromSysPropsOrEnvVars(Config config) { diff --git a/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftConfig.java b/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftConfig.java index c37600bf29d..a7562ad5683 100644 --- a/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftConfig.java +++ b/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftConfig.java @@ -18,6 +18,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; +import io.fabric8.kubernetes.api.model.NamedContext; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.OAuthTokenProvider; import io.fabric8.kubernetes.client.http.TlsVersion; @@ -87,7 +88,7 @@ public OpenShiftConfig(String openShiftUrl, String oapiVersion, String masterUrl String[] impersonateGroups, Map> impersonateExtras, OAuthTokenProvider oauthTokenProvider, Map customHeaders, int requestRetryBackoffLimit, int requestRetryBackoffInterval, int uploadRequestTimeout, boolean onlyHttpWatches, long buildTimeout, - boolean disableApiGroupCheck) { + boolean disableApiGroupCheck, NamedContext currentContext, List contexts) { super(masterUrl, apiVersion, namespace, trustCerts, disableHostnameVerification, caCertFile, caCertData, clientCertFile, clientCertData, clientKeyFile, clientKeyData, clientKeyAlgo, clientKeyPassphrase, username, password, @@ -99,7 +100,7 @@ public OpenShiftConfig(String openShiftUrl, String oapiVersion, String masterUrl impersonateExtras, oauthTokenProvider, customHeaders, requestRetryBackoffLimit, requestRetryBackoffInterval, - uploadRequestTimeout, onlyHttpWatches); + uploadRequestTimeout, onlyHttpWatches, currentContext, contexts); this.setOapiVersion(oapiVersion); this.setBuildTimeout(buildTimeout); this.setDisableApiGroupCheck(disableApiGroupCheck); @@ -141,7 +142,9 @@ public OpenShiftConfig(Config kubernetesConfig, String openShiftUrl, String oapi kubernetesConfig.getUploadRequestTimeout(), kubernetesConfig.isOnlyHttpWatches(), buildTimeout, - false); + false, + kubernetesConfig.getCurrentContext(), + kubernetesConfig.getContexts()); } public static OpenShiftConfig wrap(Config config) {