Skip to content

Commit

Permalink
test: KubernetesClientBuilder withConfig tests
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa authored Jan 27, 2025
1 parent eb7fee6 commit 5018807
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
*/
public class KubernetesClientBuilder {

private static final String DEFAULT_IMPLEMENTATION = "io.fabric8.kubernetes.client.impl.KubernetesClientImpl";

@FunctionalInterface
public interface ExecutorSupplier extends Supplier<Executor> {

Expand All @@ -51,12 +53,11 @@ default void onClose(Executor executor) {

public KubernetesClientBuilder() {
// basically the same logic as in KubernetesResourceUtil for finding list types
String className = "io.fabric8.kubernetes.client.impl.KubernetesClientImpl";
try {
clazz = (Class<KubernetesClient>) Thread.currentThread().getContextClassLoader().loadClass(className);
clazz = (Class<KubernetesClient>) Thread.currentThread().getContextClassLoader().loadClass(DEFAULT_IMPLEMENTATION);
} catch (ClassNotFoundException | ClassCastException | NullPointerException e) {
try {
clazz = (Class<KubernetesClient>) KubernetesClient.class.getClassLoader().loadClass(className);
clazz = (Class<KubernetesClient>) KubernetesClient.class.getClassLoader().loadClass(DEFAULT_IMPLEMENTATION);
} catch (Exception ex) {
throw KubernetesClientException.launderThrowable(ex);
}
Expand Down Expand Up @@ -93,24 +94,22 @@ HttpClient getHttpClient() {
return builder.build();
}

public KubernetesClientBuilder withConfig(Config config) {
this.config = config;
public KubernetesClientBuilder withKubernetesSerialization(KubernetesSerialization kubernetesSerialization) {
this.kubernetesSerialization = Utils.checkNotNull(kubernetesSerialization, "kubernetesSerialization must not be null");
return this;
}

public KubernetesClientBuilder withKubernetesSerialization(KubernetesSerialization kubernetesSerialization) {
this.kubernetesSerialization = Utils.checkNotNull(kubernetesSerialization, "kubernetesSerialization must not be null");
public KubernetesClientBuilder withConfig(Config config) {
this.config = config;
return this;
}

public KubernetesClientBuilder withConfig(String config) {
this.config = kubernetesSerialization.unmarshal(config, Config.class);
return this;
return withConfig(kubernetesSerialization.unmarshal(config, Config.class));
}

public KubernetesClientBuilder withConfig(InputStream config) {
this.config = kubernetesSerialization.unmarshal(config, Config.class);
return this;
return withConfig(kubernetesSerialization.unmarshal(config, Config.class));
}

public KubernetesClientBuilder withHttpClientFactory(HttpClient.Factory factory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,30 @@
package io.fabric8.kubernetes.client;

import io.fabric8.kubernetes.client.KubernetesClientBuilder.ConfigNested;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.http.HttpClient.Factory;
import io.fabric8.kubernetes.client.http.TestStandardHttpClient;
import io.fabric8.kubernetes.client.http.TestStandardHttpClientFactory;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class KubernetesClientBuilderTest {

@Test
void testHttpClientConfiguration() {
void httpClientConfiguration() {
KubernetesClientBuilder builder = new KubernetesClientBuilder(null);
Factory mockFactory = Mockito.mock(HttpClient.Factory.class);
HttpClient.Builder mockBuilder = Mockito.mock(HttpClient.Builder.class);
Mockito.when(mockFactory.newBuilder(Mockito.any())).thenReturn(mockBuilder);
builder.withHttpClientFactory(mockFactory).withHttpClientBuilderConsumer(b -> b.proxyAuthorization("something"));
builder.getHttpClient();
Mockito.verify(mockBuilder).proxyAuthorization("something");
builder.withConfig(Config.empty());
builder.withHttpClientFactory(new TestStandardHttpClientFactory());
builder.withHttpClientBuilderConsumer(b -> b.tag("string-tag-value"));
assertThat(builder.getHttpClient())
.asInstanceOf(InstanceOfAssertFactories.type(TestStandardHttpClient.class))
.returns("string-tag-value", c -> c.getTag(String.class));
}

@Test
void testConfigNested() {
void nestedConfigPreservesOriginalValues() {
KubernetesClientBuilder builder = new KubernetesClientBuilder(null);
builder.withConfig(new ConfigBuilder().withWatchReconnectLimit(600).build());
builder.editOrNewConfig().withApiVersion("x.y").endConfig();
Expand All @@ -65,4 +66,5 @@ void testNullContextClassLoader() {
Thread.currentThread().setContextClassLoader(currContextClassLoader);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.behavior;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;

class KubernetesClientBuilderTest {

@Nested
class WithConfig {

@Test
void fromStringDoesntAutoconfigure() {
final var config = "{\"masterUrl\":\"https://example.com\"}";
try (var client = new KubernetesClientBuilder().withConfig(config).build()) {
assertThat(client.getConfiguration())
.returns(false, c -> Boolean.TRUE.equals(c.getAutoConfigure()))
.returns("https://example.com/", Config::getMasterUrl);
}
}

@Test
void fromInputStreamDoesntAutoconfigure() {
final var config = new ByteArrayInputStream("{\"masterUrl\":\"https://example.com\"}".getBytes(StandardCharsets.UTF_8));
try (var client = new KubernetesClientBuilder().withConfig(config).build()) {
assertThat(client.getConfiguration())
.returns(false, c -> Boolean.TRUE.equals(c.getAutoConfigure()))
.returns("https://example.com/", Config::getMasterUrl);
}
}
}
}

0 comments on commit 5018807

Please sign in to comment.