Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop TestResourceManager using System props #16748

Merged
merged 1 commit into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static Consumer<BuildChainBuilder> loadStepsFrom(ClassLoader classLoader,
final BuildTimeConfigurationReader reader = new BuildTimeConfigurationReader(roots);

// now prepare & load the build configuration
final SmallRyeConfigBuilder builder = ConfigUtils.configBuilder(false);
final SmallRyeConfigBuilder builder = ConfigUtils.configBuilder(false, launchMode);

final DefaultValuesConfigurationSource ds1 = new DefaultValuesConfigurationSource(
reader.getBuildTimePatternMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.quarkus.gizmo.MethodDescriptor;
import io.quarkus.gizmo.ResultHandle;
import io.quarkus.gizmo.TryBlock;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.configuration.AbstractRawDefaultConfigSource;
import io.quarkus.runtime.configuration.ConfigDiagnostic;
Expand Down Expand Up @@ -149,10 +150,14 @@ public final class RunTimeConfigurationGenerator {
static final MethodDescriptor CU_SORTED_SET_FACTORY = MethodDescriptor.ofMethod(ConfigUtils.class, "sortedSetFactory",
IntFunction.class);
static final MethodDescriptor CU_CONFIG_BUILDER = MethodDescriptor.ofMethod(ConfigUtils.class, "configBuilder",
SmallRyeConfigBuilder.class, boolean.class);
SmallRyeConfigBuilder.class, boolean.class, LaunchMode.class);
static final MethodDescriptor CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED = MethodDescriptor.ofMethod(ConfigUtils.class,
"configBuilder",
SmallRyeConfigBuilder.class, boolean.class, boolean.class);
SmallRyeConfigBuilder.class, boolean.class, boolean.class, LaunchMode.class);
static final MethodDescriptor CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED_AND_BOOTSRAP = MethodDescriptor.ofMethod(
ConfigUtils.class,
"configBuilder",
SmallRyeConfigBuilder.class, boolean.class, boolean.class, boolean.class, LaunchMode.class);
static final MethodDescriptor CU_ADD_SOURCE_PROVIDER = MethodDescriptor.ofMethod(ConfigUtils.class, "addSourceProvider",
void.class, SmallRyeConfigBuilder.class, ConfigSourceProvider.class);
static final MethodDescriptor CU_ADD_SOURCE_PROVIDERS = MethodDescriptor.ofMethod(ConfigUtils.class, "addSourceProviders",
Expand Down Expand Up @@ -243,7 +248,7 @@ private RunTimeConfigurationGenerator() {
public static void generate(
final BuildTimeConfigurationReader.ReadResult readResult,
final ClassOutput classOutput,
final boolean devMode,
LaunchMode launchMode,
final Map<String, String> runTimeDefaults,
final List<Class<?>> additionalTypes,
final List<String> additionalStaticInitConfigSourceProviders,
Expand All @@ -252,7 +257,7 @@ public static void generate(
new GenerateOperation.Builder()
.setBuildTimeReadResult(readResult)
.setClassOutput(classOutput)
.setDevMode(devMode)
.setLaunchMode(launchMode)
.setRunTimeDefaults(runTimeDefaults)
.setAdditionalTypes(additionalTypes)
.setAdditionalStaticInitConfigSourceProviders(additionalStaticInitConfigSourceProviders)
Expand All @@ -263,6 +268,7 @@ public static void generate(

static final class GenerateOperation implements AutoCloseable {
final boolean devMode;
final LaunchMode launchMode;
final AccessorFinder accessorFinder;
final ClassOutput classOutput;
final ClassCreator cc;
Expand Down Expand Up @@ -307,7 +313,8 @@ static final class GenerateOperation implements AutoCloseable {
int converterIndex = 0;

GenerateOperation(Builder builder) {
this.devMode = builder.devMode;
this.launchMode = builder.launchMode;
this.devMode = builder.launchMode == LaunchMode.DEVELOPMENT;
final BuildTimeConfigurationReader.ReadResult buildTimeReadResult = builder.buildTimeReadResult;
buildTimeConfigResult = Assert.checkNotNullParam("buildTimeReadResult", buildTimeReadResult);
specifiedRunTimeDefaultValues = Assert.checkNotNullParam("specifiedRunTimeDefaultValues",
Expand Down Expand Up @@ -380,7 +387,7 @@ static final class GenerateOperation implements AutoCloseable {

// the build time config, which is for user use only (not used by us other than for loading converters)
final ResultHandle buildTimeBuilder = clinit.invokeStaticMethod(CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED,
clinit.load(true), clinit.load(false));
clinit.load(true), clinit.load(false), clinit.load(launchMode));
final ResultHandle array = clinit.newArray(ConfigSource[].class, 2);
// build time values
clinit.writeArrayValue(array, 0, buildTimeConfigSource);
Expand Down Expand Up @@ -440,7 +447,8 @@ public void run() {
final ResultHandle buildTimeConfigSource = reinit.newInstance(PCS_NEW, buildTimeValues,
reinit.load("Build time config = Reloaded"), reinit.load(100));
// the build time config, which is for user use only (not used by us other than for loading converters)
final ResultHandle buildTimeBuilder = reinit.invokeStaticMethod(CU_CONFIG_BUILDER, reinit.load(true));
final ResultHandle buildTimeBuilder = reinit.invokeStaticMethod(CU_CONFIG_BUILDER, reinit.load(true),
reinit.load(launchMode));
final ResultHandle array = reinit.newArray(ConfigSource[].class, 2);
// build time values
reinit.writeArrayValue(array, 0, buildTimeConfigSource);
Expand Down Expand Up @@ -490,12 +498,14 @@ public void run() {
// create the bootstrap config if necessary
ResultHandle bootstrapBuilder = null;
if (bootstrapConfigSetupNeeded()) {
bootstrapBuilder = readBootstrapConfig.invokeStaticMethod(CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED,
readBootstrapConfig.load(false), readBootstrapConfig.load(false));
bootstrapBuilder = readBootstrapConfig.invokeStaticMethod(CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED_AND_BOOTSRAP,
readBootstrapConfig.load(false), readBootstrapConfig.load(true), readBootstrapConfig.load(false),
readBootstrapConfig.load(launchMode));
}

// create the run time config
final ResultHandle runTimeBuilder = readConfig.invokeStaticMethod(CU_CONFIG_BUILDER, readConfig.load(true));
final ResultHandle runTimeBuilder = readConfig.invokeStaticMethod(CU_CONFIG_BUILDER, readConfig.load(true),
readConfig.load(launchMode));

// add in our run time only config source provider
readConfig.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, runTimeBuilder, readConfig.newInstance(
Expand Down Expand Up @@ -1586,7 +1596,7 @@ public void close() {
}

static final class Builder {
private boolean devMode;
private LaunchMode launchMode;
private ClassOutput classOutput;
private BuildTimeConfigurationReader.ReadResult buildTimeReadResult;
private Map<String, String> runTimeDefaults;
Expand Down Expand Up @@ -1633,12 +1643,12 @@ Builder setAdditionalTypes(final List<Class<?>> additionalTypes) {
return this;
}

boolean isDevMode() {
return devMode;
public LaunchMode getLaunchMode() {
return launchMode;
}

Builder setDevMode(boolean devMode) {
this.devMode = devMode;
public Builder setLaunchMode(LaunchMode launchMode) {
this.launchMode = launchMode;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.deployment.steps;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -12,6 +13,7 @@
import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.deployment.GeneratedClassGizmoAdaptor;
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
Expand All @@ -29,11 +31,12 @@
import io.quarkus.deployment.configuration.definition.ClassDefinition;
import io.quarkus.deployment.configuration.definition.RootDefinition;
import io.quarkus.deployment.logging.LoggingSetupBuildItem;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.configuration.ConfigChangeRecorder;
import io.quarkus.runtime.configuration.ConfigurationRuntimeConfig;
import io.quarkus.runtime.configuration.RuntimeOverrideConfigSource;

public class ConfigGenerationBuildStep {

Expand Down Expand Up @@ -63,7 +66,7 @@ void generateConfigClass(ConfigurationBuildItem configItem, List<RunTimeConfigur

ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClass, false);
RunTimeConfigurationGenerator.generate(readResult, classOutput,
launchModeBuildItem.getLaunchMode() == LaunchMode.DEVELOPMENT, defaults, additionalConfigTypes,
launchModeBuildItem.getLaunchMode(), defaults, additionalConfigTypes,
getAdditionalStaticInitConfigSourceProviders(additionalStaticInitConfigSourceProviders),
getAdditionalBootstrapConfigSourceProviders(additionalBootstrapConfigSourceProviders));
}
Expand Down Expand Up @@ -116,6 +119,19 @@ public void checkForBuildTimeConfigChange(
recorder.handleConfigChange(configurationConfig, values);
}

@BuildStep(onlyIfNot = { IsNormal.class })
public void setupConfigOverride(
BuildProducer<GeneratedClassBuildItem> generatedClassBuildItemBuildProducer) {

ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClassBuildItemBuildProducer, true);

try (ClassCreator clazz = ClassCreator.builder().classOutput(classOutput)
.className(RuntimeOverrideConfigSource.GENERATED_CLASS_NAME).build()) {
clazz.getFieldCreator(RuntimeOverrideConfigSource.FIELD_NAME, Map.class)
.setModifiers(Modifier.STATIC | Modifier.PUBLIC | Modifier.VOLATILE);
}
}

private void handleMembers(Config config, Map<String, String> values, Iterable<ClassDefinition.ClassMember> members,
String prefix) {
for (ClassDefinition.ClassMember member : members) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.quarkus.deployment.configuration.RunTimeConfigurationGenerator;
import io.quarkus.dev.appstate.ApplicationStateNotification;
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.configuration.RuntimeOverrideConfigSource;

public class StartupActionImpl implements StartupAction {

Expand Down Expand Up @@ -141,6 +142,11 @@ public void close() throws IOException {

}

@Override
public void overrideConfig(Map<String, String> config) {
RuntimeOverrideConfigSource.setConfig(runtimeClassLoader, config);
}

/**
* Runs the application, and returns a handle that can be used to shut it down.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
import org.jboss.logging.Logger;

import io.quarkus.runtime.LaunchMode;
import io.smallrye.config.ConfigSourceInterceptor;
import io.smallrye.config.ConfigSourceInterceptorContext;
import io.smallrye.config.ConfigSourceInterceptorFactory;
Expand Down Expand Up @@ -62,8 +63,8 @@ public static <T> IntFunction<SortedSet<T>> sortedSetFactory() {
return size -> new TreeSet<>();
}

public static SmallRyeConfigBuilder configBuilder(final boolean runTime) {
return configBuilder(runTime, true);
public static SmallRyeConfigBuilder configBuilder(final boolean runTime, LaunchMode launchMode) {
return configBuilder(runTime, true, launchMode);
}

/**
Expand All @@ -73,12 +74,29 @@ public static SmallRyeConfigBuilder configBuilder(final boolean runTime) {
* @param addDiscovered {@code true} if the ConfigSource and Converter objects should be auto-discovered
* @return the configuration builder
*/
public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final boolean addDiscovered) {
public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final boolean addDiscovered,
LaunchMode launchMode) {
return configBuilder(runTime, false, addDiscovered, launchMode);
}

/**
* Get the basic configuration builder.
*
* @param runTime {@code true} if the configuration is run time, {@code false} if build time
* @param addDiscovered {@code true} if the ConfigSource and Converter objects should be auto-discovered
* @return the configuration builder
*/
public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final boolean bootstrap,
final boolean addDiscovered,
LaunchMode launchMode) {
final SmallRyeConfigBuilder builder = emptyConfigBuilder();

final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
builder.withSources(new ApplicationPropertiesConfigSourceLoader.InFileSystem().getConfigSources(classLoader));
builder.withSources(new ApplicationPropertiesConfigSourceLoader.InClassPath().getConfigSources(classLoader));
if (launchMode.isDevOrTest() && (runTime || bootstrap)) {
builder.withSources(new RuntimeOverrideConfigSource(classLoader));
}
if (runTime) {
builder.addDefaultSources();
builder.withSources(dotEnvSources(classLoader));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.quarkus.runtime.configuration;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.microprofile.config.spi.ConfigSource;

/**
* Config source that is used to handle {@code io.quarkus.bootstrap.app.StartupAction#overrideConfig(java.util.Map)}
*
*/
public class RuntimeOverrideConfigSource implements ConfigSource {

public static final String FIELD_NAME = "CONFIG";
public static final String GENERATED_CLASS_NAME = RuntimeOverrideConfigSource.class.getName() + "$$GeneratedMapHolder";

final Map<String, String> values;

public RuntimeOverrideConfigSource(ClassLoader classLoader) {
try {
Class<?> cls = classLoader.loadClass(GENERATED_CLASS_NAME);
Map<String, String> values = (Map<String, String>) cls.getDeclaredField(FIELD_NAME).get(null);
this.values = values == null ? Collections.emptyMap() : values;
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException exception) {
throw new RuntimeException(exception);
}
}

public static void setConfig(ClassLoader runtimeClassLoader, Map<String, String> config) {
try {
Class<?> cls = runtimeClassLoader.loadClass(GENERATED_CLASS_NAME);
cls.getDeclaredField(FIELD_NAME).set(null, new HashMap<>(config));
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException exception) {
throw new RuntimeException(exception);
}
}

@Override
public Map<String, String> getProperties() {
return new HashMap<>(values);
}

@Override
public Set<String> getPropertyNames() {
return values.keySet();
}

@Override
public int getOrdinal() {
return 399; //one less that system properties
}

@Override
public String getValue(String s) {
return values.get(s);
}

@Override
public String getName() {
return "Config Override Config Source";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -334,8 +335,10 @@ public void testConfigDefaultValuesSourceOrdinal() {
assertNotNull(defaultValues);
assertEquals(Integer.MIN_VALUE + 100, defaultValues.getOrdinal());

// Should be the first
ConfigSource applicationProperties = config.getConfigSources().iterator().next();
// Should be the second, after RuntimeOverrideConfigSource
Iterator<ConfigSource> iterator = config.getConfigSources().iterator();
iterator.next();
ConfigSource applicationProperties = iterator.next();
assertNotNull(applicationProperties);
assertEquals(1000, applicationProperties.getOrdinal());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DSAPublicKey getPublicKey() {
}

public void setPublicKey(DSAPublicKey publicKey) {
log.infof("setPublicKey, key=%s", publicKey);
log.debugf("setPublicKey, key=%s", publicKey);
this.publicKey = publicKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;
import io.quarkus.deployment.configuration.BuildTimeConfigurationReader;
import io.quarkus.deployment.configuration.DefaultValuesConfigurationSource;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.configuration.ConfigUtils;
import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SmallRyeConfig;
Expand Down Expand Up @@ -105,7 +106,7 @@ private void clearProperty(String key) {
private void whenPublishImageInfo() {
BuildTimeConfigurationReader reader = new BuildTimeConfigurationReader(
Collections.singletonList(ContainerImageConfig.class));
SmallRyeConfigBuilder builder = ConfigUtils.configBuilder(false);
SmallRyeConfigBuilder builder = ConfigUtils.configBuilder(false, LaunchMode.NORMAL);

DefaultValuesConfigurationSource ds = new DefaultValuesConfigurationSource(
reader.getBuildTimePatternMap());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package io.quarkus.bootstrap.app;

import java.util.Map;
import java.util.function.Consumer;

public interface StartupAction {

/**
* Overrides runtime config.
*/
void overrideConfig(Map<String, String> config);

RunningQuarkusApplication run(String... args) throws Exception;

ClassLoader getClassLoader();
Expand Down