diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e9359daebb..377cbfa258 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -44,6 +44,7 @@ quarkus: - --initialize-at-run-time=io.github.hectorvent.floci.services.route53.Route53Service - --initialize-at-run-time=io.github.hectorvent.floci.services.kms.KmsService - --initialize-at-run-time=io.github.hectorvent.floci.services.cloudfront.CloudFrontService + - --initialize-at-run-time=io.github.hectorvent.floci.services.cloudmap.CloudMapService floci: diff --git a/src/test/java/io/github/hectorvent/floci/config/NativeImageRuntimeInitializationTest.java b/src/test/java/io/github/hectorvent/floci/config/NativeImageRuntimeInitializationTest.java new file mode 100644 index 0000000000..7abe2e097c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/config/NativeImageRuntimeInitializationTest.java @@ -0,0 +1,102 @@ +package io.github.hectorvent.floci.config; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Guards the GraalVM native-image build against the "Random/SecureRandom in the + * image heap" failure mode. + * + *
A {@code static} {@link java.security.SecureRandom}/{@link java.util.Random} + * field is initialized at build time, so native-image captures the + * instance — with a frozen seed — into the image heap and aborts with: + * + *
+ * Detected an instance of Random/SplittableRandom class in the image heap. + *+ * + * The remedy is to list the owning class under + * {@code quarkus.native.additional-build-args → --initialize-at-run-time} in + * {@code application.yml} so its static initializer runs at image runtime. + * + *
This failure only surfaces in the (slow, nightly) native build, never in
+ * the JVM CI jobs. This test moves the check into ordinary CI: every main-source
+ * class declaring such a static field MUST be registered for run-time
+ * initialization. Regression context: CloudMapService (#1217) added a static
+ * {@code SecureRandom} without the registration and broke the nightly (#1234).
+ */
+class NativeImageRuntimeInitializationTest {
+
+ private static final Path MAIN_SOURCE_ROOT = Path.of("src", "main", "java");
+ private static final Path APPLICATION_YML = Path.of("src", "main", "resources", "application.yml");
+
+ private static final String JAVA_SUFFIX = ".java";
+ private static final String INIT_AT_RUN_TIME_FLAG = "--initialize-at-run-time=";
+
+ /** A {@code static [final] [pkg.]SecureRandom|Random|SplittableRandom