-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: KubernetesMockServer + JUnit5 extension support clint builder c…
…ustomization Signed-off-by: Marc Nuri <marc@marcnuri.com>
- Loading branch information
Showing
7 changed files
with
166 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...io/fabric8/kubernetes/client/server/mock/KubernetesMockClientKubernetesClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* 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.server.mock; | ||
|
||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.ConfigBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.fabric8.kubernetes.client.http.TlsVersion; | ||
|
||
import java.util.function.Function; | ||
|
||
public class KubernetesMockClientKubernetesClientBuilder implements Function<String, KubernetesClientBuilder> { | ||
|
||
@Override | ||
public KubernetesClientBuilder apply(String url) { | ||
return new KubernetesClientBuilder().withConfig(initConfig(url)); | ||
} | ||
|
||
protected Config initConfig(String url) { | ||
return new ConfigBuilder(Config.empty()) | ||
.withMasterUrl(url) | ||
.withTrustCerts(true) | ||
.withTlsVersions(TlsVersion.TLS_1_2) | ||
.withNamespace("test") | ||
.withHttp2Disable(true) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...bernetes/client/server/mock/KubernetesMockServerExtensionKubernetesClientBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* 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.server.mock; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.kubernetes.api.model.Pod; | ||
import io.fabric8.kubernetes.api.model.PodBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.fabric8.kubernetes.client.utils.KubernetesSerialization; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@EnableKubernetesMockClient(crud = true, kubernetesClientBuilder = KubernetesMockServerExtensionKubernetesClientBuilderTest.CustomSerialization.class) | ||
class KubernetesMockServerExtensionKubernetesClientBuilderTest { | ||
|
||
KubernetesClient client; | ||
|
||
@Test | ||
void usesCustomMapper() { | ||
// Given | ||
final Pod pod = new PodBuilder().withNewMetadata().withName("name").endMetadata().build(); | ||
// When | ||
client.pods().resource(pod).create(); | ||
// Then | ||
assertThat(client.pods()) | ||
.returns(null, pr -> pr.withName("name").get()) | ||
.extracting(pr -> pr.withName("name-extended").get()) | ||
.isNotNull(); | ||
} | ||
|
||
public static final class CustomSerialization extends KubernetesMockClientKubernetesClientBuilder { | ||
@Override | ||
public KubernetesClientBuilder apply(String url) { | ||
final KubernetesClientBuilder kubernetesClientBuilder = super.apply(url); | ||
final ObjectMapper customMapper = new ObjectMapper(); | ||
customMapper.addMixIn(ObjectMeta.class, ObjectMetaMixin.class); | ||
kubernetesClientBuilder.withKubernetesSerialization(new KubernetesSerialization(customMapper, true)); | ||
return kubernetesClientBuilder; | ||
} | ||
|
||
private static final class ObjectMetaMixin { | ||
@JsonSerialize(using = StringAppenderSerializer.class) | ||
@JsonProperty("name") | ||
String name; | ||
} | ||
|
||
private static final class StringAppenderSerializer extends StdSerializer<String> { | ||
|
||
private StringAppenderSerializer() { | ||
super(String.class); | ||
} | ||
|
||
@Override | ||
public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | ||
jsonGenerator.writeString(s + "-extended"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters