Skip to content

Commit 1676bc0

Browse files
committed
fabric8 implemented
Signed-off-by: wind57 <[email protected]>
1 parent 391413e commit 1676bc0

11 files changed

+117
-249
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @author wind57
4141
*/
4242
@EnableKubernetesMockClient(crud = true, https = false)
43-
class Fabric8ConfigUtilsNamespacedBatchReadTests {
43+
class Fabric8ConfigUtilsBatchReadTests {
4444

4545
private KubernetesClient client;
4646

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @author wind57
4040
*/
4141
@EnableKubernetesMockClient(crud = true, https = false)
42-
class Fabric8ConfigUtilsNonNamespacedBatchReadTests {
42+
class Fabric8ConfigUtilsSingleReadTests {
4343

4444
private KubernetesClient client;
4545

@@ -62,7 +62,7 @@ void testSecretDataByLabelsSecretNotFound() {
6262
.resource(new SecretBuilder().withMetadata(new ObjectMetaBuilder().withName("my-secret").build()).build())
6363
.create();
6464
MultipleSourcesContainer result = Fabric8ConfigUtils.secretsByLabels(client, "spring-k8s",
65-
Map.of("color", "red"), new MockEnvironment(), ReadType.BATCH);
65+
Map.of("color", "red"), new MockEnvironment(), ReadType.SINGLE);
6666
Assertions.assertThat(result.data()).isEmpty();
6767
}
6868

@@ -83,7 +83,7 @@ void testSecretDataByLabelsSecretFound() {
8383
.create();
8484

8585
MultipleSourcesContainer result = Fabric8ConfigUtils.secretsByLabels(client, "spring-k8s",
86-
Map.of("color", "pink"), new MockEnvironment(), ReadType.BATCH);
86+
Map.of("color", "pink"), new MockEnvironment(), ReadType.SINGLE);
8787
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-secret");
8888

8989
Map<String, Object> data = result.data().get("my-secret");
@@ -109,7 +109,7 @@ void testSecretDataByLabelsSecretFoundWithPropertyFile() {
109109
.create();
110110

111111
MultipleSourcesContainer result = Fabric8ConfigUtils.secretsByLabels(client, "spring-k8s",
112-
Map.of("color", "pink"), new MockEnvironment(), ReadType.BATCH);
112+
Map.of("color", "pink"), new MockEnvironment(), ReadType.SINGLE);
113113
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-secret");
114114

115115
Map<String, Object> data = result.data().get("my-secret");
@@ -143,7 +143,7 @@ void testSecretDataByLabelsTwoSecretsFound() {
143143
.create();
144144

145145
MultipleSourcesContainer result = Fabric8ConfigUtils.secretsByLabels(client, "spring-k8s",
146-
Map.of("color", "pink"), new MockEnvironment(), ReadType.BATCH);
146+
Map.of("color", "pink"), new MockEnvironment(), ReadType.SINGLE);
147147
Assertions.assertThat(result.data().keySet()).contains("my-secret");
148148
Assertions.assertThat(result.data().keySet()).contains("my-secret-2");
149149

@@ -211,7 +211,7 @@ void testSecretDataByLabelsThreeSecretsFound() {
211211
.create();
212212

213213
MultipleSourcesContainer result = Fabric8ConfigUtils.secretsByLabels(client, "spring-k8s",
214-
Map.of("tag", "fit", "color", "blue"), new MockEnvironment(), ReadType.BATCH);
214+
Map.of("tag", "fit", "color", "blue"), new MockEnvironment(), ReadType.SINGLE);
215215

216216
Assertions.assertThat(result.data().keySet()).contains("blue-circle-secret");
217217
Assertions.assertThat(result.data().keySet()).contains("blue-square-secret");
@@ -238,7 +238,7 @@ void testSecretDataByNameSecretNotFound() {
238238
LinkedHashSet<String> names = new LinkedHashSet<>();
239239
names.add("nope");
240240
MultipleSourcesContainer result = Fabric8ConfigUtils.secretsByName(client, "spring-k8s", names,
241-
new MockEnvironment(), ReadType.BATCH);
241+
new MockEnvironment(), ReadType.SINGLE);
242242
Assertions.assertThat(result.data()).isEmpty();
243243
}
244244

@@ -259,7 +259,7 @@ void testSecretDataByNameSecretFound() {
259259
names.add("my-secret");
260260

261261
MultipleSourcesContainer result = Fabric8ConfigUtils.secretsByName(client, "spring-k8s", names,
262-
new MockEnvironment(), ReadType.BATCH);
262+
new MockEnvironment(), ReadType.SINGLE);
263263
Assertions.assertThat(result.data().keySet()).hasSize(1);
264264

265265
Map<String, Object> data = result.data().get("my-secret");
@@ -283,7 +283,7 @@ void testConfigMapsDataByNameFoundNo() {
283283
names.add("my-config-map");
284284

285285
MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsByName(client, "spring-k8s", names,
286-
new MockEnvironment(), ReadType.BATCH);
286+
new MockEnvironment(), ReadType.SINGLE);
287287
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-config-map");
288288

289289
Map<String, Object> data = result.data().get("my-config-map");
@@ -305,7 +305,7 @@ void testConfigMapsByNameNotFound() {
305305
LinkedHashSet<String> names = new LinkedHashSet<>();
306306
names.add("my-config-map-not-found");
307307
MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsByName(client, "spring-k8s", names,
308-
new MockEnvironment(), ReadType.BATCH);
308+
new MockEnvironment(), ReadType.SINGLE);
309309
Assertions.assertThat(result.data()).isEmpty();
310310
}
311311

@@ -327,7 +327,7 @@ void testConfigMapDataByNameFound() {
327327
names.add("my-config-map");
328328

329329
MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsByName(client, "spring-k8s", names,
330-
new MockEnvironment(), ReadType.BATCH);
330+
new MockEnvironment(), ReadType.SINGLE);
331331
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-config-map");
332332

333333
Map<String, Object> data = result.data().get("my-config-map");
@@ -355,7 +355,7 @@ void testConfigMapDataByNameFoundWithPropertyFile() {
355355
names.add("my-config-map");
356356

357357
MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsByName(client, "spring-k8s", names,
358-
new MockEnvironment(), ReadType.BATCH);
358+
new MockEnvironment(), ReadType.SINGLE);
359359
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-config-map");
360360

361361
Map<String, Object> data = result.data().get("my-config-map");
@@ -390,7 +390,7 @@ void testConfigMapDataByNameTwoFound() {
390390
names.add("my-config-map-2");
391391

392392
MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsByName(client, "spring-k8s", names,
393-
new MockEnvironment(), ReadType.BATCH);
393+
new MockEnvironment(), ReadType.SINGLE);
394394
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-config-map", "my-config-map-2");
395395

396396
Assertions.assertThat(result.data()).hasSize(2);
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@
2626
import io.fabric8.kubernetes.client.Config;
2727
import io.fabric8.kubernetes.client.KubernetesClient;
2828
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
29+
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
2930
import org.assertj.core.api.Assertions;
3031
import org.junit.jupiter.api.AfterEach;
3132
import org.junit.jupiter.api.BeforeAll;
3233
import org.junit.jupiter.api.Test;
33-
import org.junit.jupiter.api.extension.ExtendWith;
3434

35-
import org.springframework.boot.test.system.CapturedOutput;
36-
import org.springframework.boot.test.system.OutputCaptureExtension;
3735
import org.springframework.cloud.kubernetes.commons.config.ConfigUtils;
3836
import org.springframework.cloud.kubernetes.commons.config.LabeledConfigMapNormalizedSource;
3937
import org.springframework.cloud.kubernetes.commons.config.NormalizedSource;
@@ -47,8 +45,7 @@
4745
* @author wind57
4846
*/
4947
@EnableKubernetesMockClient(crud = true, https = false)
50-
@ExtendWith(OutputCaptureExtension.class)
51-
class LabeledConfigMapContextToSourceDataProviderNamespacedBatchReadTests {
48+
class LabeledConfigMapContextToSourceDataProviderBatchReadTests {
5249

5350
private static final String NAMESPACE = "default";
5451

@@ -62,14 +59,14 @@ class LabeledConfigMapContextToSourceDataProviderNamespacedBatchReadTests {
6259

6360
private static KubernetesClient mockClient;
6461

65-
static {
66-
LABELS.put("label2", "value2");
67-
LABELS.put("label1", "value1");
68-
}
62+
private static KubernetesMockServer mockServer;
6963

7064
@BeforeAll
7165
static void beforeAll() {
7266

67+
LABELS.put("label2", "value2");
68+
LABELS.put("label1", "value1");
69+
7370
// Configure the kubernetes master url to point to the mock server
7471
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl());
7572
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
@@ -499,7 +496,7 @@ void searchWithLabelsTwoConfigMapsFound() {
499496
* </pre>
500497
*/
501498
@Test
502-
void cache(CapturedOutput output) {
499+
void cache() {
503500
ConfigMap redConfigMap = new ConfigMapBuilder().withNewMetadata()
504501
.withName("red-configmap")
505502
.withLabels(Collections.singletonMap("color", "red"))
@@ -527,8 +524,9 @@ void cache(CapturedOutput output) {
527524
SourceData redSourceData = redData.apply(redContext);
528525

529526
Assertions.assertThat(redSourceData.sourceData().size()).isEqualTo(1);
530-
Assertions.assertThat(redSourceData.sourceData().get("red-configmap.one")).isEqualTo("1");
531-
Assertions.assertThat(output.getAll()).contains("Loaded all config maps in namespace '" + NAMESPACE + "'");
527+
528+
// delete the configmap, if caching is not present, the test would fail
529+
mockClient.configMaps().inNamespace(NAMESPACE).withName(greenConfigmap.getMetadata().getName()).delete();
532530

533531
NormalizedSource greenNormalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE,
534532
Collections.singletonMap("color", "green"), true, ConfigUtils.Prefix.DELAYED, true);
@@ -540,14 +538,6 @@ void cache(CapturedOutput output) {
540538
Assertions.assertThat(greenSourceData.sourceData().size()).isEqualTo(1);
541539
Assertions.assertThat(greenSourceData.sourceData().get("green-configmap.two")).isEqualTo("2");
542540

543-
// meaning there is a single entry with such a log statement
544-
String[] out = output.getAll().split("Loaded all config maps in namespace");
545-
Assertions.assertThat(out.length).isEqualTo(2);
546-
547-
// meaning that the second read was done from the cache
548-
out = output.getAll().split("Loaded \\(from cache\\) all config maps in namespace");
549-
Assertions.assertThat(out.length).isEqualTo(2);
550-
551541
}
552542

553543
}
Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030
import org.junit.jupiter.api.AfterEach;
3131
import org.junit.jupiter.api.BeforeAll;
3232
import org.junit.jupiter.api.Test;
33-
import org.junit.jupiter.api.extension.ExtendWith;
3433

35-
import org.springframework.boot.test.system.CapturedOutput;
36-
import org.springframework.boot.test.system.OutputCaptureExtension;
3734
import org.springframework.cloud.kubernetes.commons.config.ConfigUtils;
3835
import org.springframework.cloud.kubernetes.commons.config.LabeledConfigMapNormalizedSource;
3936
import org.springframework.cloud.kubernetes.commons.config.NormalizedSource;
@@ -45,8 +42,7 @@
4542
* @author wind57
4643
*/
4744
@EnableKubernetesMockClient(crud = true, https = false)
48-
@ExtendWith(OutputCaptureExtension.class)
49-
class LabeledConfigMapContextToSourceDataProviderNonNamespacedBatchReadTests {
45+
class LabeledConfigMapContextToSourceDataProviderSingleReadTests {
5046

5147
private static final String NAMESPACE = "default";
5248

@@ -60,14 +56,12 @@ class LabeledConfigMapContextToSourceDataProviderNonNamespacedBatchReadTests {
6056

6157
private static KubernetesClient mockClient;
6258

63-
static {
64-
LABELS.put("label2", "value2");
65-
LABELS.put("label1", "value1");
66-
}
67-
6859
@BeforeAll
6960
static void beforeAll() {
7061

62+
LABELS.put("label2", "value2");
63+
LABELS.put("label1", "value1");
64+
7165
// Configure the kubernetes master url to point to the mock server
7266
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl());
7367
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
@@ -102,7 +96,7 @@ void singleConfigMapMatchAgainstLabels() {
10296

10397
NormalizedSource normalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE, LABELS, true, false);
10498
Fabric8ConfigContext context = new Fabric8ConfigContext(mockClient, normalizedSource, NAMESPACE,
105-
new MockEnvironment(), ReadType.BATCH);
99+
new MockEnvironment(), ReadType.SINGLE);
106100

107101
Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
108102
SourceData sourceData = data.apply(context);
@@ -146,7 +140,7 @@ void twoConfigMapsMatchAgainstLabels() {
146140

147141
NormalizedSource normalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE, RED_LABEL, true, false);
148142
Fabric8ConfigContext context = new Fabric8ConfigContext(mockClient, normalizedSource, NAMESPACE,
149-
new MockEnvironment(), ReadType.BATCH);
143+
new MockEnvironment(), ReadType.SINGLE);
150144

151145
Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
152146
SourceData sourceData = data.apply(context);
@@ -175,7 +169,7 @@ void configMapNoMatch() {
175169

176170
NormalizedSource normalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE, BLUE_LABEL, true, false);
177171
Fabric8ConfigContext context = new Fabric8ConfigContext(mockClient, normalizedSource, NAMESPACE,
178-
new MockEnvironment(), ReadType.BATCH);
172+
new MockEnvironment(), ReadType.SINGLE);
179173

180174
Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
181175
SourceData sourceData = data.apply(context);
@@ -206,7 +200,7 @@ void namespaceMatch() {
206200
NormalizedSource normalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE + "nope", LABELS, true,
207201
false);
208202
Fabric8ConfigContext context = new Fabric8ConfigContext(mockClient, normalizedSource, NAMESPACE,
209-
new MockEnvironment(), ReadType.BATCH);
203+
new MockEnvironment(), ReadType.SINGLE);
210204

211205
Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
212206
SourceData sourceData = data.apply(context);
@@ -236,7 +230,7 @@ void testWithPrefix() {
236230
NormalizedSource normalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE,
237231
Collections.singletonMap("color", "blue"), true, mePrefix, false);
238232
Fabric8ConfigContext context = new Fabric8ConfigContext(mockClient, normalizedSource, NAMESPACE,
239-
new MockEnvironment(), ReadType.BATCH);
233+
new MockEnvironment(), ReadType.SINGLE);
240234

241235
Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
242236
SourceData sourceData = data.apply(context);
@@ -278,7 +272,7 @@ void testTwoConfigmapsWithPrefix() {
278272
NormalizedSource normalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE,
279273
Collections.singletonMap("color", "blue"), true, ConfigUtils.Prefix.DELAYED, false);
280274
Fabric8ConfigContext context = new Fabric8ConfigContext(mockClient, normalizedSource, NAMESPACE,
281-
new MockEnvironment(), ReadType.BATCH);
275+
new MockEnvironment(), ReadType.SINGLE);
282276

283277
Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
284278
SourceData sourceData = data.apply(context);
@@ -328,7 +322,7 @@ void searchWithLabelsNoConfigmapsFound() {
328322
NormalizedSource normalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE,
329323
Collections.singletonMap("color", "red"), true, ConfigUtils.Prefix.DEFAULT, true);
330324
Fabric8ConfigContext context = new Fabric8ConfigContext(mockClient, normalizedSource, NAMESPACE, environment,
331-
ReadType.BATCH);
325+
ReadType.SINGLE);
332326

333327
Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
334328
SourceData sourceData = data.apply(context);
@@ -365,7 +359,7 @@ void searchWithLabelsOneConfigMapFound() {
365359
NormalizedSource normalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE,
366360
Collections.singletonMap("color", "blue"), true, ConfigUtils.Prefix.DEFAULT, true);
367361
Fabric8ConfigContext context = new Fabric8ConfigContext(mockClient, normalizedSource, NAMESPACE, environment,
368-
ReadType.BATCH);
362+
ReadType.SINGLE);
369363

370364
Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
371365
SourceData sourceData = data.apply(context);
@@ -433,7 +427,7 @@ void searchWithLabelsTwoConfigMapsFound() {
433427
NormalizedSource normalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE,
434428
Collections.singletonMap("color", "blue"), true, ConfigUtils.Prefix.DELAYED, true);
435429
Fabric8ConfigContext context = new Fabric8ConfigContext(mockClient, normalizedSource, NAMESPACE, environment,
436-
ReadType.BATCH);
430+
ReadType.SINGLE);
437431

438432
Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
439433
SourceData sourceData = data.apply(context);
@@ -455,7 +449,7 @@ void searchWithLabelsTwoConfigMapsFound() {
455449
* </pre>
456450
*/
457451
@Test
458-
void nonCache(CapturedOutput output) {
452+
void nonCache() {
459453
ConfigMap redConfigMap = new ConfigMapBuilder().withNewMetadata()
460454
.withName("red-configmap")
461455
.withLabels(Collections.singletonMap("color", "red"))
@@ -478,34 +472,32 @@ void nonCache(CapturedOutput output) {
478472
NormalizedSource redNormalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE,
479473
Collections.singletonMap("color", "red"), true, ConfigUtils.Prefix.DELAYED, true);
480474
Fabric8ConfigContext redContext = new Fabric8ConfigContext(mockClient, redNormalizedSource, NAMESPACE,
481-
environment, ReadType.BATCH);
475+
environment, ReadType.SINGLE);
482476
Fabric8ContextToSourceData redData = new LabeledConfigMapContextToSourceDataProvider().get();
483477
SourceData redSourceData = redData.apply(redContext);
484478

485479
Assertions.assertThat(redSourceData.sourceData()).hasSize(1);
486480
Assertions.assertThat(redSourceData.sourceData().get("red-configmap.one")).isEqualTo("1");
487481

488-
Assertions.assertThat(output.getAll())
489-
.doesNotContain("Loaded all config maps in namespace '" + NAMESPACE + "'");
490-
Assertions.assertThat(output.getOut()).contains("Will read individual configmaps in namespace");
491-
492482
NormalizedSource greenNormalizedSource = new LabeledConfigMapNormalizedSource(NAMESPACE,
493483
Collections.singletonMap("color", "green"), true, ConfigUtils.Prefix.DELAYED, true);
494484
Fabric8ConfigContext greenContext = new Fabric8ConfigContext(mockClient, greenNormalizedSource, NAMESPACE,
495-
environment, ReadType.BATCH);
485+
environment, ReadType.SINGLE);
496486
Fabric8ContextToSourceData greenData = new LabeledConfigMapContextToSourceDataProvider().get();
497487
SourceData greenSourceData = greenData.apply(greenContext);
498488

499489
Assertions.assertThat(greenSourceData.sourceData()).hasSize(1);
500490
Assertions.assertThat(greenSourceData.sourceData().get("green-configmap.two")).isEqualTo("2");
501491

502-
// meaning there is a single entry with such a log statement
503-
String[] out = output.getAll().split("Loaded all config maps in namespace");
504-
Assertions.assertThat(out.length).isEqualTo(1);
492+
// since we do SINGLE reads, thus no caching is involved
493+
// when we remove the configmap, nothing is found
494+
mockClient.configMaps().inNamespace(NAMESPACE).resource(greenConfigmap).delete();
495+
496+
greenData = new LabeledConfigMapContextToSourceDataProvider().get();
497+
greenSourceData = greenData.apply(greenContext);
505498

506-
// meaning that both reads were non cached
507-
out = output.getAll().split("Will read individual configmaps in namespace");
508-
Assertions.assertThat(out.length).isEqualTo(3);
499+
Assertions.assertThat(greenSourceData.sourceData()).hasSize(0);
500+
Assertions.assertThat(greenSourceData.sourceData().get("green-configmap.two")).isNull();
509501

510502
}
511503

0 commit comments

Comments
 (0)