Skip to content

Commit

Permalink
fix(mock-server): KubernetesMockServer provides a valid, complete con…
Browse files Browse the repository at this point in the history
…figuration (6069)

fix (kubernetes-server-mock) : Add opinionated current context to KubernetesMockServer's initConfig

Add values for username, oauthToken and currentContext to Config created
by the Kubernetes Mock Server for KubernetesClient used in tests

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
---
review: kubernetes-mock-server config

Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
rohanKanojia committed Jun 26, 2024
1 parent e6fd2e2 commit b9ee9b0
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions junit/openshift-server-mock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>openshift-client</artifactId>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@
*/
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 {
static OpenShiftClient openShiftClient;

@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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@
*/
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 {
OpenShiftClient client;

@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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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<String, List<String>> impersonateExtras,
OAuthTokenProvider oauthTokenProvider, Map<String, String> customHeaders, int requestRetryBackoffLimit,
int requestRetryBackoffInterval, int uploadRequestTimeout, boolean onlyHttpWatches) {
int requestRetryBackoffInterval, int uploadRequestTimeout, boolean onlyHttpWatches, NamedContext currentContext,
List<NamedContext> contexts) {
this.apiVersion = apiVersion;
this.namespace = namespace;
this.trustCerts = trustCerts;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,7 +88,7 @@ public OpenShiftConfig(String openShiftUrl, String oapiVersion, String masterUrl
String[] impersonateGroups, Map<String, List<String>> impersonateExtras, OAuthTokenProvider oauthTokenProvider,
Map<String, String> customHeaders, int requestRetryBackoffLimit, int requestRetryBackoffInterval,
int uploadRequestTimeout, boolean onlyHttpWatches, long buildTimeout,
boolean disableApiGroupCheck) {
boolean disableApiGroupCheck, NamedContext currentContext, List<NamedContext> contexts) {
super(masterUrl, apiVersion, namespace, trustCerts, disableHostnameVerification, caCertFile, caCertData,
clientCertFile,
clientCertData, clientKeyFile, clientKeyData, clientKeyAlgo, clientKeyPassphrase, username, password,
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b9ee9b0

Please sign in to comment.