Skip to content

Commit c97dc85

Browse files
committed
fix issue
1 parent 63c5834 commit c97dc85

File tree

3 files changed

+78
-112
lines changed

3 files changed

+78
-112
lines changed

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapErrorOnReadingSourceTests.java

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939
import org.springframework.boot.test.system.OutputCaptureExtension;
4040
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
4141
import org.springframework.cloud.kubernetes.commons.config.ConfigMapConfigProperties;
42-
import org.springframework.cloud.kubernetes.commons.config.Constants;
4342
import org.springframework.cloud.kubernetes.commons.config.RetryProperties;
4443
import org.springframework.core.env.CompositePropertySource;
45-
import org.springframework.core.env.MapPropertySource;
4644
import org.springframework.core.env.PropertySource;
4745
import org.springframework.mock.env.MockEnvironment;
4846

@@ -100,12 +98,11 @@ public void afterEach() {
10098

10199
/**
102100
* <pre>
103-
* we try to read all config maps in a namespace and fail,
104-
* thus generate a well defined name for the source.
101+
* we try to read all config maps in a namespace and fail.
105102
* </pre>
106103
*/
107104
@Test
108-
void namedSingleConfigMapFails() {
105+
void namedSingleConfigMapFails(CapturedOutput output) {
109106
String name = "my-config";
110107
String namespace = "spring-k8s";
111108
String path = "/api/v1/namespaces/" + namespace + "/configmaps";
@@ -120,13 +117,10 @@ void namedSingleConfigMapFails() {
120117
configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment()));
121118

122119
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
123-
MapPropertySource mapPropertySource = (MapPropertySource) propertySource.getPropertySources()
124-
.stream()
125-
.findAny()
126-
.orElseThrow();
127120

128-
assertThat(mapPropertySource.getName()).isEqualTo("configmap..spring-k8s");
129-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
121+
assertThat(propertySource.getPropertySources()).isEmpty();
122+
assertThat(output.getOut()).contains("Failure in reading named sources");
123+
assertThat(output.getOut()).contains("Failed to load source: { config-map name : 'Optional[my-config]'");
130124

131125
}
132126

@@ -168,11 +162,12 @@ void namedTwoConfigMapsOneFails(CapturedOutput output) {
168162
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
169163
List<String> names = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
170164

171-
// two sources are present, one being empty
172-
assertThat(names).containsExactly("configmap.two.default", "configmap..default");
173-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
165+
// one property source is present
166+
assertThat(names).containsExactly("configmap.two.default");
174167
assertThat(output.getOut())
175168
.doesNotContain("sourceName : two was requested, but not found in namespace : default");
169+
assertThat(output.getOut()).contains("Failure in reading named sources");
170+
assertThat(output.getOut()).contains("Failed to load source: { config-map name : 'Optional[one]'");
176171

177172
}
178173

@@ -212,20 +207,19 @@ void namedTwoConfigMapsBothFail(CapturedOutput output) {
212207
configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment()));
213208

214209
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
215-
List<String> names = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
216210

217-
assertThat(names).containsExactly("configmap..default");
218-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
211+
assertThat(propertySource.getPropertySources()).isEmpty();
219212
assertThat(output.getOut())
220213
.doesNotContain("sourceName : one was requested, but not found in namespace : default");
221214
assertThat(output.getOut())
222215
.doesNotContain("sourceName : two was requested, but not found in namespace : default");
216+
assertThat(output.getOut()).contains("Failure in reading named sources");
217+
assertThat(output.getOut()).contains("Failed to load source: { config-map name : 'Optional[one]'");
223218
}
224219

225220
/**
226221
* <pre>
227-
* we try to read all config maps in a namespace and fail,
228-
* thus generate a well defined name for the source.
222+
* we try to read all config maps in a namespace and fail.
229223
* </pre>
230224
*/
231225
@Test
@@ -256,12 +250,11 @@ void labeledSingleConfigMapFails(CapturedOutput output) {
256250
configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment()));
257251

258252
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
259-
List<String> sourceNames = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
260253

261-
assertThat(sourceNames).containsExactly("configmap..spring-k8s");
262-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
263-
assertThat(output).contains("failure in reading labeled sources");
264-
assertThat(output).contains("failure in reading named sources");
254+
assertThat(propertySource.getPropertySources()).isEmpty();
255+
assertThat(output.getOut()).contains("Failure in reading labeled sources");
256+
assertThat(output.getOut()).contains("Failure in reading named sources");
257+
assertThat(output.getOut()).contains("Failed to load source: { config map labels : '{a=b}'");
265258
}
266259

267260
/**
@@ -311,12 +304,11 @@ void labeledTwoConfigMapsOneFails(CapturedOutput output) {
311304
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
312305
List<String> names = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
313306

314-
// two sources are present, one being empty
315-
assertThat(names).containsExactly("configmap.two.default", "configmap..default");
316-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
317-
318-
assertThat(output).contains("failure in reading labeled sources");
319-
assertThat(output).contains("failure in reading named sources");
307+
// one source is present
308+
assertThat(names).containsExactly("configmap.two.default");
309+
assertThat(output.getOut()).contains("Failure in reading labeled sources");
310+
assertThat(output.getOut()).contains("Failure in reading named sources");
311+
assertThat(output.getOut()).contains("Failed to load source: { config map labels : '{one=1}'");
320312

321313
}
322314

@@ -364,15 +356,12 @@ void labeledTwoConfigMapsBothFail(CapturedOutput output) {
364356
configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment()));
365357

366358
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
367-
List<String> names = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
368-
369-
// all 3 sources ('application' named source, and two labeled sources)
370-
assertThat(names).containsExactly("configmap..default");
371-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
372-
373-
assertThat(output).contains("failure in reading labeled sources");
374-
assertThat(output).contains("failure in reading named sources");
375359

360+
assertThat(propertySource.getPropertySources()).isEmpty();
361+
assertThat(output).contains("Failure in reading labeled sources");
362+
assertThat(output).contains("Failure in reading named sources");
363+
assertThat(output.getOut()).contains("Failed to load source: { config map labels : '{one=1}'");
364+
assertThat(output.getOut()).contains("Failed to load source: { config map labels : '{two=2}'");
376365
}
377366

378367
}

spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapErrorOnReadingSourceTests.java

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
import org.springframework.boot.test.system.OutputCaptureExtension;
3434
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
3535
import org.springframework.cloud.kubernetes.commons.config.ConfigMapConfigProperties;
36-
import org.springframework.cloud.kubernetes.commons.config.Constants;
3736
import org.springframework.cloud.kubernetes.commons.config.RetryProperties;
3837
import org.springframework.core.env.CompositePropertySource;
39-
import org.springframework.core.env.MapPropertySource;
4038
import org.springframework.core.env.PropertySource;
4139
import org.springframework.mock.env.MockEnvironment;
4240

@@ -62,11 +60,11 @@ static void beforeAll() {
6260
/**
6361
* <pre>
6462
* we try to read all config maps in a namespace and fail,
65-
* thus generate a well defined name for the source.
63+
* thus the composite property source is empty.
6664
* </pre>
6765
*/
6866
@Test
69-
void namedSingleConfigMapFails() {
67+
void namedSingleConfigMapFails(CapturedOutput output) {
7068
String name = "my-config";
7169
String namespace = "spring-k8s";
7270
String path = "/api/v1/namespaces/" + namespace + "/configmaps";
@@ -80,13 +78,8 @@ void namedSingleConfigMapFails() {
8078
configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment()));
8179

8280
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
83-
MapPropertySource mapPropertySource = (MapPropertySource) propertySource.getPropertySources()
84-
.stream()
85-
.findAny()
86-
.orElseThrow();
87-
88-
assertThat(mapPropertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
89-
81+
assertThat(propertySource.getPropertySources()).isEmpty();
82+
assertThat(output.getOut()).contains("Failed to load source: { config-map name : 'Optional[my-config]'");
9083
}
9184

9285
/**
@@ -96,7 +89,7 @@ void namedSingleConfigMapFails() {
9689
* </pre>
9790
*/
9891
@Test
99-
void namedTwoConfigMapsOneFails() {
92+
void namedTwoConfigMapsOneFails(CapturedOutput output) {
10093
String configMapNameOne = "one";
10194
String configMapNameTwo = "two";
10295
String namespace = "default";
@@ -123,10 +116,9 @@ void namedTwoConfigMapsOneFails() {
123116
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
124117
List<String> names = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
125118

126-
// two sources are present
127-
assertThat(names).containsExactly("configmap.two.default", "configmap..default");
128-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
129-
119+
// one source is present
120+
assertThat(names).containsExactly("configmap.two.default");
121+
assertThat(output.getOut()).contains("Failed to load source: { config-map name : 'Optional[one]'");
130122
}
131123

132124
/**
@@ -136,7 +128,7 @@ void namedTwoConfigMapsOneFails() {
136128
* </pre>
137129
*/
138130
@Test
139-
void namedTwoConfigMapsBothFail() {
131+
void namedTwoConfigMapsBothFail(CapturedOutput output) {
140132
String configMapNameOne = "one";
141133
String configMapNameTwo = "two";
142134
String namespace = "default";
@@ -158,15 +150,14 @@ void namedTwoConfigMapsBothFail() {
158150
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
159151
List<String> names = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
160152

161-
assertThat(names).containsExactly("configmap..default");
162-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
163-
153+
assertThat(propertySource.getPropertySources()).isEmpty();
154+
assertThat(output.getOut()).contains("Failed to load source: { config-map name : 'Optional[one]'");
155+
assertThat(output.getOut()).contains("Failed to load source: { config-map name : 'Optional[two]'");
164156
}
165157

166158
/**
167159
* <pre>
168-
* we try to read all config maps in a namespace and fail,
169-
* thus generate a well defined name for the source.
160+
* we try to read all config maps in a namespace and fail.
170161
* </pre>
171162
*/
172163
@Test
@@ -190,10 +181,8 @@ void labeledSingleConfigMapFails(CapturedOutput output) {
190181
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
191182
List<String> sourceNames = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
192183

193-
assertThat(sourceNames).containsExactly("configmap..spring-k8s");
194-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
195-
assertThat(output).contains("failure in reading labeled sources");
196-
assertThat(output).contains("failure in reading named sources");
184+
assertThat(propertySource.getPropertySources()).isEmpty();
185+
assertThat(output.getOut()).contains("Failed to load source: { config map labels : '{a=b}'");
197186
}
198187

199188
/**
@@ -236,12 +225,11 @@ void labeledTwoConfigMapsOneFails(CapturedOutput output) {
236225
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
237226
List<String> names = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
238227

239-
// two sources are present, one being empty
240-
assertThat(names).containsExactly("configmap.two.default", "configmap..default");
241-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
242-
243-
assertThat(output).contains("failure in reading labeled sources");
244-
assertThat(output).contains("failure in reading named sources");
228+
// one property source is present
229+
assertThat(names).containsExactly("configmap.two.default");
230+
assertThat(output.getOut()).contains("Failure in reading labeled sources");
231+
assertThat(output.getOut()).contains("Failure in reading named sources");
232+
assertThat(output.getOut()).contains("Failed to load source: { config map labels : '{one=1}'");
245233

246234
}
247235

@@ -274,14 +262,12 @@ void labeledTwoConfigMapsBothFail(CapturedOutput output) {
274262
configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment()));
275263

276264
CompositePropertySource propertySource = (CompositePropertySource) locator.locate(new MockEnvironment());
277-
List<String> names = propertySource.getPropertySources().stream().map(PropertySource::getName).toList();
278-
279-
assertThat(names).containsExactly("configmap..default");
280-
assertThat(propertySource.getProperty(Constants.ERROR_PROPERTY)).isEqualTo("true");
281-
282-
assertThat(output).contains("failure in reading labeled sources");
283-
assertThat(output).contains("failure in reading named sources");
284265

266+
assertThat(propertySource.getPropertySources()).isEmpty();
267+
assertThat(output.getOut()).contains("Failure in reading labeled sources");
268+
assertThat(output.getOut()).contains("Failure in reading named sources");
269+
assertThat(output.getOut()).contains("Failed to load source: { config map labels : '{two=2}'");
270+
assertThat(output.getOut()).contains("Failed to load source: { config map labels : '{one=1}'");
285271
}
286272

287273
private ConfigMap configMap(String name, Map<String, String> labels) {

0 commit comments

Comments
 (0)