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:
@@ -30,21 +30,26 @@ public class ApplicationTests {
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
|
||||
private static ConfigurableApplicationContext server;
|
||||
|
||||
@BeforeClass
|
||||
public static void startConfigServer() throws IOException {
|
||||
String repo = ConfigServerTestUtils.prepareLocalRepo();
|
||||
ConfigurableApplicationContext context = SpringApplication.run(
|
||||
server = SpringApplication.run(
|
||||
org.springframework.cloud.config.server.ConfigServerApplication.class,
|
||||
"--server.port=" + configPort, "--spring.config.name=server",
|
||||
"--spring.cloud.config.server.git.uri=" + repo);
|
||||
configPort = ((EmbeddedWebApplicationContext) context)
|
||||
configPort = ((EmbeddedWebApplicationContext) server)
|
||||
.getEmbeddedServletContainer().getPort();
|
||||
System.setProperty("config.port", "" + configPort);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void close() {
|
||||
System.clearProperty("config.port");
|
||||
System.clearProperty("config.port");
|
||||
if (server!=null) {
|
||||
server.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package sample;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.cloud.config.server.ConfigServerTestUtils;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@IntegrationTest({"server.port:0", "spring.application.name:bad"})
|
||||
@WebAppConfiguration
|
||||
public class ServerNativeApplicationTests {
|
||||
|
||||
private static int configPort = 0;
|
||||
|
||||
@Autowired
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
|
||||
private static ConfigurableApplicationContext server;
|
||||
|
||||
@BeforeClass
|
||||
public static void startConfigServer() throws IOException {
|
||||
String repo = ConfigServerTestUtils.prepareLocalRepo();
|
||||
server = SpringApplication.run(
|
||||
org.springframework.cloud.config.server.ConfigServerApplication.class,
|
||||
"--server.port=" + configPort, "--spring.config.name=server",
|
||||
"--spring.cloud.config.server.git.uri=" + repo, "--spring.profiles.active=native");
|
||||
configPort = ((EmbeddedWebApplicationContext) server)
|
||||
.getEmbeddedServletContainer().getPort();
|
||||
System.setProperty("config.port", "" + configPort);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void close() {
|
||||
System.clearProperty("config.port");
|
||||
if (server!=null) {
|
||||
server.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
// The remote config was bad so there is no bootstrap
|
||||
assertTrue(((Map)environment.getPropertySources().get("bootstrap").getSource()).isEmpty());
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
configPort = 8888;
|
||||
startConfigServer();
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
4
spring-cloud-config-sample/src/test/resources/bad.yml
Normal file
4
spring-cloud-config-sample/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