3939import org .springframework .boot .test .system .OutputCaptureExtension ;
4040import org .springframework .cloud .kubernetes .commons .KubernetesNamespaceProvider ;
4141import org .springframework .cloud .kubernetes .commons .config .ConfigMapConfigProperties ;
42- import org .springframework .cloud .kubernetes .commons .config .Constants ;
4342import org .springframework .cloud .kubernetes .commons .config .RetryProperties ;
4443import org .springframework .core .env .CompositePropertySource ;
45- import org .springframework .core .env .MapPropertySource ;
4644import org .springframework .core .env .PropertySource ;
4745import 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}
0 commit comments