Use abstract config data post processor (#715)

This commit is contained in:
Ryan Baxter
2021-03-31 07:03:01 -04:00
committed by GitHub
parent 92d47f6e80
commit f8ff3b5be7
4 changed files with 130 additions and 31 deletions

View File

@@ -17,6 +17,10 @@
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

View File

@@ -16,21 +16,18 @@
package org.springframework.cloud.consul.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor;
import org.springframework.cloud.consul.ConsulProperties;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils;
import org.springframework.core.env.Environment;
import static org.springframework.cloud.consul.config.ConsulConfigDataLocationResolver.PREFIX;
import static org.springframework.cloud.util.PropertyUtils.bootstrapEnabled;
import static org.springframework.cloud.util.PropertyUtils.useLegacyProcessing;
public class ConsulConfigDataMissingEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
public class ConsulConfigDataMissingEnvironmentPostProcessor extends ConfigDataMissingEnvironmentPostProcessor {
/**
* Order of post processor, set to run after
@@ -44,10 +41,10 @@ public class ConsulConfigDataMissingEnvironmentPostProcessor implements Environm
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
protected boolean shouldProcessEnvironment(Environment environment) {
// don't run if using bootstrap or legacy processing
if (bootstrapEnabled(environment) || useLegacyProcessing(environment)) {
return;
return false;
}
boolean coreEnabled = environment.getProperty(ConsulProperties.PREFIX + ".enabled", Boolean.class, true);
boolean configEnabled = environment.getProperty(ConsulConfigProperties.PREFIX + ".enabled", Boolean.class,
@@ -55,26 +52,14 @@ public class ConsulConfigDataMissingEnvironmentPostProcessor implements Environm
boolean importCheckEnabled = environment.getProperty(ConsulConfigProperties.PREFIX + ".import-check.enabled",
Boolean.class, true);
if (!coreEnabled || !configEnabled || !importCheckEnabled) {
return;
}
String property = environment.getProperty("spring.config.import");
if (!StringUtils.hasText(property)) {
throw new ImportException("No spring.config.import set", false);
}
if (!property.contains(PREFIX)) {
throw new ImportException("spring.config.import missing " + PREFIX, true);
return false;
}
return true;
}
static class ImportException extends RuntimeException {
final boolean missingPrefix;
ImportException(String message, boolean missingPrefix) {
super(message);
this.missingPrefix = missingPrefix;
}
@Override
protected String getPrefix() {
return PREFIX;
}
static class ImportExceptionFailureAnalyzer extends AbstractFailureAnalyzer<ImportException> {

View File

@@ -0,0 +1,106 @@
/*
* Copyright 2015-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.consul.config;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
/**
* @author Ryan Baxter
*/
class ConsulConfigDataMissingEnvironmentPostProcessorTests {
@Test
void noSpringConfigImport() {
MockEnvironment environment = new MockEnvironment();
SpringApplication app = mock(SpringApplication.class);
ConsulConfigDataMissingEnvironmentPostProcessor processor = new ConsulConfigDataMissingEnvironmentPostProcessor();
assertThatThrownBy(() -> processor.postProcessEnvironment(environment, app))
.isInstanceOf(ConsulConfigDataMissingEnvironmentPostProcessor.ImportException.class);
}
@Test
void boostrap() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.cloud.bootstrap.enabled", "true");
SpringApplication app = mock(SpringApplication.class);
ConsulConfigDataMissingEnvironmentPostProcessor processor = new ConsulConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void legacy() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.config.use-legacy-processing", "true");
SpringApplication app = mock(SpringApplication.class);
ConsulConfigDataMissingEnvironmentPostProcessor processor = new ConsulConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void configNotEnabled() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.cloud.consul.enabled", "false");
SpringApplication app = mock(SpringApplication.class);
ConsulConfigDataMissingEnvironmentPostProcessor processor = new ConsulConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void importCheckNotEnabled() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.cloud.consul.config.import-check.enabled", "false");
SpringApplication app = mock(SpringApplication.class);
ConsulConfigDataMissingEnvironmentPostProcessor processor = new ConsulConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void importSinglePropertySource() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.config.import", "consul:http://localhost:8888");
SpringApplication app = mock(SpringApplication.class);
ConsulConfigDataMissingEnvironmentPostProcessor processor = new ConsulConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void importMultiplePropertySource() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.config.import", "consul:http://localhost:8888,file:./app.properties");
SpringApplication app = mock(SpringApplication.class);
ConsulConfigDataMissingEnvironmentPostProcessor processor = new ConsulConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void importMultiplePropertySourceAsList() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.config.import[0]", "consul:http://localhost:8888");
environment.setProperty("spring.config.import[1]", "file:./app.properties");
SpringApplication app = mock(SpringApplication.class);
ConsulConfigDataMissingEnvironmentPostProcessor processor = new ConsulConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
}

View File

@@ -28,7 +28,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.cloud.consul.config.ConsulConfigDataMissingEnvironmentPostProcessor.ImportException;
import org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor;
import org.springframework.cloud.consul.test.ConsulTestcontainers;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
@@ -62,8 +62,10 @@ public class ConsulConfigDataNoImportIntegrationTests {
@Test
public void exceptionThrownIfNoImport(CapturedOutput output) {
Assertions.assertThatThrownBy(() -> new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE)
.run("--spring.application.name=" + APP_NAME)).isInstanceOf(ImportException.class);
Assertions
.assertThatThrownBy(() -> new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE)
.run("--spring.application.name=" + APP_NAME))
.isInstanceOf(ConfigDataMissingEnvironmentPostProcessor.ImportException.class);
assertThat(output).contains("No spring.config.import property has been defined")
.contains("Add a spring.config.import=consul: property to your configuration");
@@ -71,9 +73,11 @@ public class ConsulConfigDataNoImportIntegrationTests {
@Test
public void exceptionThrownIfImportMissingConsul(CapturedOutput output) {
Assertions.assertThatThrownBy(() -> new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE).run(
"--spring.config.import=optional:file:somefile.properties", "--spring.application.name=" + APP_NAME))
.isInstanceOf(ImportException.class);
Assertions
.assertThatThrownBy(() -> new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE).run(
"--spring.config.import=optional:file:somefile.properties",
"--spring.application.name=" + APP_NAME))
.isInstanceOf(ConfigDataMissingEnvironmentPostProcessor.ImportException.class);
assertThat(output).contains("spring.config.import property is missing a " + PREFIX)
.contains("Add a spring.config.import=consul: property to your configuration");