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

@QuarkusIntegrationTest starts native executable in prod profile instead of test profile #25519

Closed
Karm opened this issue May 11, 2022 · 12 comments · Fixed by #25537
Closed

@QuarkusIntegrationTest starts native executable in prod profile instead of test profile #25519

Karm opened this issue May 11, 2022 · 12 comments · Fixed by #25537
Assignees
Labels
area/mandrel area/testing kind/bug Something isn't working triage/invalid This doesn't seem right
Milestone

Comments

@Karm
Copy link
Member

Karm commented May 11, 2022

Describe the bug

I was struggling with database test data population during native-image test in a project and it boiled down to the simple fact that the native-executable is started in prod profile (hence expecting %prod values) instead of the test profile the JVM test correctly uses.

Expected behavior

native-image executable is started in test profile during test execution.

Actual behavior

native-image executable is started in prod profile during test execution.

How to Reproduce?

I reproduced it with this tiny example downloaded from code.quarkus.io:

$ curl -O -J  https://code.quarkus.io/d?e=io.quarkus:quarkus-resteasy
$ unzip code-with-quarkus.zip 
$ cd code-with-quarkus 

Patch with this patch:

diff --git a/src/main/java/org/acme/GreetingResource.java b/src/main/java/org/acme/GreetingResource.java
index 055f53c..b4e1e1e 100644
--- a/src/main/java/org/acme/GreetingResource.java
+++ b/src/main/java/org/acme/GreetingResource.java
@@ -1,5 +1,7 @@
 package org.acme;
 
+import org.eclipse.microprofile.config.inject.ConfigProperty;
+
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
@@ -8,9 +10,12 @@ import javax.ws.rs.core.MediaType;
 @Path("/hello")
 public class GreetingResource {
 
+    @ConfigProperty(name = "my.property")
+    String property;
+
     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String hello() {
-        return "Hello RESTEasy";
+        return property;
     }
 }
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index e69de29..7f9990a 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -0,0 +1,2 @@
+%test.my.property=Hello, test!
+my.property=Hello.
diff --git a/src/test/java/org/acme/GreetingResourceTest.java b/src/test/java/org/acme/GreetingResourceTest.java
index 87fbd5c..e58f732 100644
--- a/src/test/java/org/acme/GreetingResourceTest.java
+++ b/src/test/java/org/acme/GreetingResourceTest.java
@@ -15,7 +15,7 @@ public class GreetingResourceTest {
           .when().get("/hello")
           .then()
              .statusCode(200)
-             .body(is("Hello RESTEasy"));
+             .body(is("Hello, test!"));
     }
 
 }

Running the tests as:

./mvnw  verify -Pnative

Passes the JVM mode:

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.953 s - in org.acme.GreetingResourceTest

And fails with native:

[ERROR] org.acme.GreetingResourceIT.testHelloEndpoint  Time elapsed: 1.452 s  <<< FAILURE!
java.lang.AssertionError: 
1 expectation failed.
Response body doesn't match expectation.
Expected: is "Hello, test!"
  Actual: Hello.

Note how the Quarkus app starts:

Executing "/tmp/code-with-quarkus/target/code-with-quarkus-1.0.0-SNAPSHOT-runner -Dquarkus.http.port=8081 -Dquarkus.http.ssl-port=8444 -Dtest.url=http://localhost:8081 -Dquarkus.log.file.path=/tmp/code-with-quarkus/target/quarkus.log -Dquarkus.log.file.enable=true"
__  ____  __  _____   ___  __ ____  ______ 
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
INFO  [io.quarkus] (main) code-with-quarkus 1.0.0-SNAPSHOT native (powered by Quarkus 2.9.0.Final) started in 0.020s. Listening on: http://0.0.0.0:8081
INFO  [io.quarkus] (main) Profile prod activated. 
INFO  [io.quarkus] (main) Installed features: [cdi, resteasy, smallrye-context-propagation, vertx]

i.e. the profile should be test, not prod.

Output of uname -a or ver

Linux amd64

Output of java -version

OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7)

GraalVM version (if different from Java)

native-image 22.1.0.0-Final Mandrel Distribution (Java Version 17.0.3+7)

Quarkus version or git rev

2.9.0.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.8.4

Additional information

No response

@Karm Karm added the kind/bug Something isn't working label May 11, 2022
@quarkus-bot
Copy link

quarkus-bot bot commented May 11, 2022

/cc @galderz, @geoand, @zakkak

@Karm
Copy link
Member Author

Karm commented May 11, 2022

I'll try to make the test framework to append -Dquarkus-profile=test and see. Unless this behavior is intentional for some reason...

@Karm
Copy link
Member Author

Karm commented May 11, 2022

Appending it explicitly to the maven command makes it work:

./mvnw  verify -Pnative -Dquarkus-profile=test

Adds the profile here DefaultNativeImageLauncher.java

So...I don't know. Close this "issue"? It's somewhat annoying that the Jar Launcher doesn't need it set explicitly then.

@Karm Karm self-assigned this May 11, 2022
@geoand
Copy link
Contributor

geoand commented May 12, 2022

This is the intended behavior

@geoand geoand closed this as completed May 12, 2022
@geoand geoand added the triage/invalid This doesn't seem right label May 12, 2022
@Karm
Copy link
Member Author

Karm commented May 12, 2022

@geoand Ack.

I'll open a doc PR though.

@geoand
Copy link
Contributor

geoand commented May 12, 2022

Great, thanks!

@Karm
Copy link
Member Author

Karm commented May 12, 2022

@geoand I took a look at the building-native-image.adoc#L275, but I don't know how to explain there why in one case -Dquarkus.test.native-image-profile=test is O.K. and in another it is not and you have to do -Dquarkus-profile=test. It doesn't feel right.

If you could take a look at this small reproducer (just a Hello world with 1 tiny entity):

$ git clone https://github.com/Karm/dev-null.git
$ cd dev-null/issue-25519/
$ export JAVA_HOME=/home/karm/workspaceRH/mandrel-release/CPU/mandrel-java11-22.1.0.0-Final/;export GRAALVM_HOME=${JAVA_HOME};export PATH=${JAVA_HOME}/bin:${PATH}

PASS ✔️

$ ./mvnw verify -Pnative -Dquarkus-profile=test

FAIL ❌

$ ./mvnw verify -Pnative -Dquarkus.test.native-image-profile=test

You can see that in both cases, the %test prefixed string property is injected as expected, so far so good.
Although the database test data import is not executed with -Dquarkus.test.native-image-profile=test and the test fails. You have to run it with -Dquarkus-profile=test to have the database populated.

Weirdly enough, the failing scenario executed with -Dquarkus.test.native-image-profile=test, as documentation tells you, has a -Dquarkus.profile=test appended to the execution args of the native, see:

screenshot

I can carry on with my project, using $ ./mvnw verify -Pnative -Dquarkus-profile=test, but I still think this is at least confusing to a random user out there :(

@geoand
Copy link
Contributor

geoand commented May 12, 2022

This is a tricky one, but it comes down to the fact that unless otherwise specified (i.e. -Dquarkus.profile is set at build time), the native image binary is built with the prod profile. The profile the application is built with is different from the one it is run with...
So when mvn verify -Dquarkus.test.native-image-profile=test is used, the application is built with the prod profile, which in turn leads to import-dev.sql not being included in native image.
When the application is then run using the test the load script cannot be found and it thus not executed.

@Karm
Copy link
Member Author

Karm commented May 12, 2022

@geoand Ah, it all clicked together now. I have this in my CI that deploys to a server:

      - name: Package
        # We don't want the native executable to be built wth test profile (e.g. including superfluous test resources)
        run: |
          ./mvnw clean verify -Pnative -Dquarkus-profile=test && ./mvnw package -DskipTests -Pnative

I know enough to update the doc now. Thx. 👍

@geoand
Copy link
Contributor

geoand commented May 12, 2022

🎉

@gian1200
Copy link
Contributor

Isn't it supposed to be -Dquarkus.profile=test, with dot instead of hyphen?

@geoand
Copy link
Contributor

geoand commented Jun 29, 2023

You are right!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/mandrel area/testing kind/bug Something isn't working triage/invalid This doesn't seem right
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants