Skip to content

Commit

Permalink
Support @testprofile in native mode #12974
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Dec 18, 2020
1 parent 1561bde commit 2478d22
Show file tree
Hide file tree
Showing 19 changed files with 629 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/native-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
{
"category": "Misc2",
"timeout": 65,
"test-modules": "tika hibernate-validator test-extension logging-gelf bootstrap-config mailer"
"test-modules": "tika hibernate-validator test-extension logging-gelf bootstrap-config mailer native-config-profile"
},
{
"category": "Misc3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
import io.quarkus.deployment.configuration.definition.RootDefinition;
import io.quarkus.deployment.logging.LoggingSetupBuildItem;
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.runtime.ConfigChangeRecorder;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.configuration.ConfigChangeRecorder;
import io.quarkus.runtime.configuration.ConfigurationRuntimeConfig;

public class ConfigGenerationBuildStep {

Expand Down Expand Up @@ -82,7 +83,8 @@ private List<String> getAdditionalBootstrapConfigSourceProviders(
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
public void checkForBuildTimeConfigChange(
ConfigChangeRecorder recorder, ConfigurationBuildItem configItem, LoggingSetupBuildItem loggingSetupBuildItem) {
ConfigChangeRecorder recorder, ConfigurationBuildItem configItem, LoggingSetupBuildItem loggingSetupBuildItem,
ConfigurationRuntimeConfig configurationConfig) {
BuildTimeConfigurationReader.ReadResult readResult = configItem.getReadResult();
Config config = ConfigProvider.getConfig();

Expand All @@ -96,7 +98,7 @@ public void checkForBuildTimeConfigChange(
}
}
values.remove("quarkus.profile");
recorder.handleConfigChange(values);
recorder.handleConfigChange(configurationConfig, values);
}

private void handleMembers(Config config, Map<String, String> values, Iterable<ClassDefinition.ClassMember> members,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.quarkus.runtime.configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;

import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.runtime.configuration.ConfigurationRuntimeConfig.BuildTimeMismatchAtRuntime;

@Recorder
public class ConfigChangeRecorder {

private static final Logger log = Logger.getLogger(ConfigChangeRecorder.class);

public void handleConfigChange(ConfigurationRuntimeConfig configurationConfig, Map<String, String> buildTimeConfig) {
Config configProvider = ConfigProvider.getConfig();
List<String> mismatches = null;
for (Map.Entry<String, String> entry : buildTimeConfig.entrySet()) {
Optional<String> val = configProvider.getOptionalValue(entry.getKey(), String.class);
if (val.isPresent()) {
if (!val.get().equals(entry.getValue())) {
if (mismatches == null) {
mismatches = new ArrayList<>();
}
mismatches.add(
" - " + entry.getKey() + " was '" + entry.getValue() + "' at build time and is now '" + val.get()
+ "'");
}
}
}
if (mismatches != null && !mismatches.isEmpty()) {
final String msg = "Build time property cannot be changed at runtime:\n"
+ mismatches.stream().collect(Collectors.joining("\n"));
switch (configurationConfig.buildTimeMismatchAtRuntime) {
case fail:
throw new IllegalStateException(msg);
case warn:
log.warn(msg);
break;
default:
throw new IllegalStateException("Unexpected " + BuildTimeMismatchAtRuntime.class.getName() + ": "
+ configurationConfig.buildTimeMismatchAtRuntime);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.runtime.configuration;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = "configuration", phase = ConfigPhase.RUN_TIME)
public class ConfigurationRuntimeConfig {

/**
* What should happen if the application is started with a different build time configuration than it was compiled
* against. This may be useful to prevent misconfiguration.
* <p>
* If this is set to {@code warn} the application will warn at start up.
* <p>
* If this is set to {@code fail} the application will fail at start up.
* <p>
* Native tests leveraging<code>@io.quarkus.test.junit.TestProfile</code> are always run with
* {@code quarkus.configuration.build-time-mismatch-at-runtime = fail}.
*/
@ConfigItem(defaultValue = "warn")
public BuildTimeMismatchAtRuntime buildTimeMismatchAtRuntime;

public enum BuildTimeMismatchAtRuntime {
warn,
fail
}

}
149 changes: 149 additions & 0 deletions integration-tests/native-config-profile/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-integration-tests-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-integration-test-native-config-profile</artifactId>
<name>Quarkus - Integration Tests - Native Configuration Profile</name>
<description>Native Configuration Profile</description>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
<configuration>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<phase/>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-image-it-main</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<configurationProfiles>
<configurationProfile>alt-profile</configurationProfile>
</configurationProfiles>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.it.nat.test.profile;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.quarkus.arc.Arc;

@Path("/native-config-profile")
public class NativeConfigProfileResource {

@ConfigProperty(name = "my.config.value")
String myConfigValue;

@Path("/myConfigValue")
@Produces("text/plain")
@GET
public String myConfigValue() {
return myConfigValue;
}

@Path("/unused-exists")
@Produces("text/plain")
@GET
public boolean unusedExists() {
return Arc.container().instance(UnusedRemovableBean.class).isAvailable();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.it.nat.test.profile;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class UnusedRemovableBean {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
quarkus.arc.remove-unused-beans = none
my.config.value = foo
%bar-profile.my.config.value = bar
%build-profile-change.quarkus.arc.remove-unused-beans = all
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.it.nat.test.profile;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.NativeImageTest;
import io.quarkus.test.junit.NativeTestExtension;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;

/**
* Run this manually via {@code mvn clean verify -Dnative -Dit.test=BuiltTimeProfileChangeManualITCase} to ensure that
* {@link NativeTestExtension#beforeEach(org.junit.jupiter.api.extension.ExtensionContext)} throws an exception caused
* by an application boot failure. The failure should happen because {@link NativeTestExtension} is setting
* {@code quarkus.configuration.build-time-mismatch-at-runtime = fail} and
* {@link BuildProfileChange#getConfigProfile()} returns a profile name that changes
* {@code quarkus.arc.remove-unused-beans} in {@code application.properties}.
*/
@NativeImageTest
@TestProfile(BuiltTimeProfileChangeManualIT.BuildProfileChange.class)
@Disabled("Manual testing only")
public class BuiltTimeProfileChangeManualIT {
@Test
public void unusedExists() {
Assertions.fail("Expected to fail in io.quarkus.test.junit.NativeTestExtension.beforeEach(ExtensionContext)");
}

public static class BuildProfileChange implements QuarkusTestProfile {
@Override
public String getConfigProfile() {
return "build-profile-change";
}
}
}
Loading

0 comments on commit 2478d22

Please sign in to comment.