|
1 | 1 | package io.javaoperatorsdk.operator.baseapi.informerremotecluster;
|
2 | 2 |
|
| 3 | +import java.util.Map; |
| 4 | + |
3 | 5 | import org.junit.jupiter.api.Test;
|
4 | 6 | import org.junit.jupiter.api.extension.RegisterExtension;
|
5 | 7 |
|
6 |
| -import io.javaoperatorsdk.jenvtest.junit.EnableKubeAPIServer; |
7 |
| -import io.javaoperatorsdk.operator.baseapi.labelselector.LabelSelectorTestReconciler; |
| 8 | +import io.fabric8.kubeapitest.junit.EnableKubeAPIServer; |
| 9 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 10 | +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; |
| 11 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 12 | +import io.fabric8.kubernetes.client.KubernetesClient; |
8 | 13 | import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
|
| 14 | +import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers; |
| 15 | + |
| 16 | +import static io.javaoperatorsdk.operator.baseapi.informerremotecluster.InformerRemoteClusterReconciler.DATA_KEY; |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.awaitility.Awaitility.await; |
| 19 | + |
| 20 | +@EnableKubeAPIServer |
| 21 | +class InformerRemoteClusterIT { |
9 | 22 |
|
10 |
| -@EnableKubeAPIServer(apiServerFlags = {"--min-request-timeout", "1"}) |
11 |
| -public class InformerRemoteClusterIT { |
| 23 | + public static final String NAME = "test1"; |
| 24 | + public static final String CONFIG_MAP_NAME = "testcm"; |
| 25 | + public static final String INITIAL_VALUE = "initial_value"; |
| 26 | + public static final String CHANGED_VALUE = "changed_value"; |
| 27 | + public static final String CM_NAMESPACE = "default"; |
| 28 | + |
| 29 | + static KubernetesClient kubernetesClient; |
12 | 30 |
|
13 | 31 | @RegisterExtension
|
14 | 32 | LocallyRunOperatorExtension extension =
|
15 |
| - LocallyRunOperatorExtension.builder().withReconciler(new LabelSelectorTestReconciler()) |
| 33 | + LocallyRunOperatorExtension.builder() |
| 34 | + .withReconciler(new InformerRemoteClusterReconciler(kubernetesClient)) |
16 | 35 | .build();
|
17 | 36 |
|
18 | 37 | @Test
|
19 | 38 | void testRemoteClusterInformer() {
|
| 39 | + var r = extension.create(testCustomResource()); |
| 40 | + |
| 41 | + var cm = kubernetesClient.configMaps() |
| 42 | + .resource(remoteConfigMap(r.getMetadata().getName(), |
| 43 | + r.getMetadata().getNamespace())) |
| 44 | + .create(); |
| 45 | + |
| 46 | + // config map not exists on the primary resource cluster |
| 47 | + assertThat(extension.getKubernetesClient().configMaps() |
| 48 | + .inNamespace(CM_NAMESPACE) |
| 49 | + .withName(CONFIG_MAP_NAME).get()).isNull(); |
| 50 | + |
| 51 | + await().untilAsserted(() -> { |
| 52 | + var cr = extension.get(InformerRemoteClusterCustomResource.class, NAME); |
| 53 | + assertThat(cr.getStatus()).isNotNull(); |
| 54 | + assertThat(cr.getStatus().getRemoteConfigMapMessage()).isEqualTo(INITIAL_VALUE); |
| 55 | + }); |
| 56 | + |
| 57 | + cm.getData().put(DATA_KEY, CHANGED_VALUE); |
| 58 | + kubernetesClient.configMaps().resource(cm).update(); |
| 59 | + |
| 60 | + await().untilAsserted(() -> { |
| 61 | + var cr = extension.get(InformerRemoteClusterCustomResource.class, NAME); |
| 62 | + assertThat(cr.getStatus().getRemoteConfigMapMessage()).isEqualTo(CHANGED_VALUE); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + InformerRemoteClusterCustomResource testCustomResource() { |
| 67 | + var res = new InformerRemoteClusterCustomResource(); |
| 68 | + res.setMetadata(new ObjectMetaBuilder() |
| 69 | + .withName(NAME) |
| 70 | + .build()); |
| 71 | + return res; |
| 72 | + } |
20 | 73 |
|
| 74 | + ConfigMap remoteConfigMap(String ownerName, String ownerNamespace) { |
| 75 | + return new ConfigMapBuilder() |
| 76 | + .withMetadata(new ObjectMetaBuilder() |
| 77 | + .withName(CONFIG_MAP_NAME) |
| 78 | + .withNamespace(CM_NAMESPACE) |
| 79 | + .withAnnotations(Map.of( |
| 80 | + Mappers.DEFAULT_ANNOTATION_FOR_NAME, ownerName, |
| 81 | + Mappers.DEFAULT_ANNOTATION_FOR_NAMESPACE, ownerNamespace)) |
| 82 | + .build()) |
| 83 | + .withData(Map.of(DATA_KEY, INITIAL_VALUE)) |
| 84 | + .build(); |
21 | 85 | }
|
22 | 86 |
|
23 | 87 | }
|
0 commit comments