Use only ConfigFileApplicationListener in server mini-application
In the server we use a SpringApplicationEnvironmentRepository to load the YAML and properties files from git (or locally). It creates a mini SpringApplication so as to faithfully replicate the way the Environment is created. Unfortunately that can have side effects on the server application itself (e.g. setting log levels). In particular if the mini SpringApplication fails to start then the log levels could be left in a "preInitialized" state with all log levels OFF by default. This change ensures that the server logs all errors when loading YAML and properties files, and also that the client logs the error response if it is JSON (as it should be). Fixes gh-66, fixes gh-67
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package org.springframework.cloud.config.server;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.cloud.config.Environment;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = ConfigServerApplication.class)
|
||||
@IntegrationTest({ "server.port:0", "spring.config.name:configserver" })
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles({ "test", "native" })
|
||||
public class NativeConfigServerIntegrationTests {
|
||||
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws IOException{
|
||||
ConfigServerTestUtils.prepareLocalRepo();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
Environment environment = new TestRestTemplate().getForObject("http://localhost:" + port + "/foo/development/", Environment.class);
|
||||
assertFalse(environment.getPropertySources().isEmpty());
|
||||
assertEquals("overrides", environment.getPropertySources().get(0).getName());
|
||||
assertEquals("{spring.cloud.config.enabled=true}", environment.getPropertySources().get(0).getSource().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void badYaml() {
|
||||
ResponseEntity<String> response = new TestRestTemplate().getForEntity("http://localhost:"
|
||||
+ port + "/bad/default/", String.class);
|
||||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ConfigServerApplication.class).profiles("native").properties(
|
||||
"spring.config.name=configserver").run(args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,21 +23,23 @@ import org.springframework.test.context.web.WebAppConfiguration;
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles("test")
|
||||
public class VanillaConfigServerIntegrationTests {
|
||||
|
||||
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws IOException{
|
||||
public static void init() throws IOException {
|
||||
ConfigServerTestUtils.prepareLocalRepo();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
Environment environment = new TestRestTemplate().getForObject("http://localhost:" + port + "/foo/development/", Environment.class);
|
||||
Environment environment = new TestRestTemplate().getForObject("http://localhost:"
|
||||
+ port + "/foo/development/", Environment.class);
|
||||
assertFalse(environment.getPropertySources().isEmpty());
|
||||
assertEquals("overrides", environment.getPropertySources().get(0).getName());
|
||||
assertEquals("{spring.cloud.config.enabled=true}", environment.getPropertySources().get(0).getSource().toString());
|
||||
assertEquals("{spring.cloud.config.enabled=true}", environment
|
||||
.getPropertySources().get(0).getSource().toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
4
spring-cloud-config-server/src/test/resources/bad.yml
Normal file
4
spring-cloud-config-server/src/test/resources/bad.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
foo:
|
||||
# whitespace error!
|
||||
bar: spam
|
||||
bucket: wham
|
||||
Reference in New Issue
Block a user