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

feat(tests): client from ConfigurationService should match injected one #637

Merged
merged 1 commit into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,62 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.quarkiverse.operatorsdk.runtime.KubernetesClientSerializationCustomizer;
import io.quarkiverse.operatorsdk.runtime.QuarkusConfigurationService;
import io.quarkiverse.operatorsdk.test.sources.SimpleCR;
import io.quarkiverse.operatorsdk.test.sources.SimpleSpec;
import io.quarkiverse.operatorsdk.test.sources.SimpleStatus;
import io.quarkus.jackson.ObjectMapperCustomizer;
import io.quarkus.kubernetes.client.KubernetesClientObjectMapperCustomizer;
import io.quarkus.test.QuarkusUnitTest;

public class KubernetesClientSerializationCustomizerTest {

@Inject
KubernetesClient kubernetesClient;
@Inject
QuarkusConfigurationService service;

@Test
public void kubernetesClientUsesCustomizedObjectMapper() {
final var result = kubernetesClient.getKubernetesSerialization()
.unmarshal("{\"quarkusName\":\"the-name\"}", ObjectMeta.class);
assertEquals("the-name", result.getName());
assertEquals(service.getKubernetesClient(), kubernetesClient);
var serialization = kubernetesClient.getKubernetesSerialization();
var result = serialization
.unmarshal("{\"spec\": {\"mixin\": \"fromMixin\"},\"status\":{\"mixin\": \"fromMixin\"}}", SimpleCR.class);
assertEquals("fromMixin", result.getSpec().value);
assertEquals("fromMixin", result.getStatus().value);

serialization = service.getKubernetesClient().getKubernetesSerialization();
result = serialization
.unmarshal("{\"spec\": {\"mixin\": \"fromMixin\"},\"status\":{\"mixin\": \"fromMixin\"}}", SimpleCR.class);
assertEquals("fromMixin", result.getSpec().value);
assertEquals("fromMixin", result.getStatus().value);
}

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar.addClasses(Customizer.class));
.withApplicationRoot(jar -> jar.addClasses(Customizer.class, Customizer2.class, ValueMixIn.class, SimpleCR.class,
SimpleSpec.class, SimpleStatus.class));

@Singleton
@KubernetesClientSerializationCustomizer
public static class Customizer implements ObjectMapperCustomizer {
@Override
public void customize(ObjectMapper objectMapper) {
objectMapper.addMixIn(ObjectMeta.class, ObjectMetaMixin.class);
objectMapper.addMixIn(SimpleSpec.class, ValueMixIn.class);
}
}

private static final class ObjectMetaMixin {
@JsonProperty("quarkusName")
String name;
private static final class ValueMixIn {
@JsonProperty("mixin")
String value;
}

@Singleton
public static class Customizer2 implements KubernetesClientObjectMapperCustomizer {
public void customize(ObjectMapper objectMapper) {
objectMapper.addMixIn(SimpleStatus.class, ValueMixIn.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.quarkiverse.operatorsdk.test.sources;

public class SimpleSpec {

public String value;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.quarkiverse.operatorsdk.test.sources;

public class SimpleStatus {

public String value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public class BuildTimeControllerConfiguration {
* <p>
* Note that this is provided as a means to quickly deploy a specific controller to test it by applying the generated
* manifests to the target cluster. If empty, no manifests will be generated. The namespace in which the controller will be
* deployed will
* be the currently configured namespace as specified by your {@code .kube/config} file, unless you specify the target
* deployment namespace using the {@code quarkus.kubernetes.namespace} property.
* deployed will be the currently configured namespace as specified by your {@code .kube/config} file, unless you specify
* the target deployment namespace using the {@code quarkus.kubernetes.namespace} property.
* </p>
*
* <p>
Expand Down