Support Test AOT processing with GraalVM tracing agent and NBT

Prior to this commit, test AOT processing failed when using the GraalVM
tracing agent and GraalVM Native Build Tools (NBT) plugins for Maven
and Gradle.

The reason is that the AOT support in the TestContext framework (TCF)
relied on AotDetector.useGeneratedArtifacts() which delegates
internally to NativeDetector.inNativeImage() which does not
differentiate between values stored in the
"org.graalvm.nativeimage.imagecode" JVM system property.

This commit addresses this issue by introducing a TestAotDetector
utility that is specific to the TCF. This detector considers the
current runtime to be in "AOT runtime mode" if the "spring.aot.enabled"
Spring property is set to "true" or the GraalVM
"org.graalvm.nativeimage.imagecode" JVM system property is set to any
non-empty value other than "agent".

Closes gh-30281
This commit is contained in:
Sam Brannen
2023-05-10 17:31:19 +02:00
parent 89bcee68bb
commit 111309605c
11 changed files with 165 additions and 37 deletions

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2002-2023 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.test.context.aot;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Tests for error cases in {@link TestContextAotGenerator}.
*
* @author Sam Brannen
* @since 6.0.9
*/
class TestContextAotGeneratorErrorCaseTests {
@ParameterizedTest
@CsvSource(delimiter = '=', textBlock = """
'spring.aot.enabled' = 'true'
'org.graalvm.nativeimage.imagecode' = 'buildtime'
'org.graalvm.nativeimage.imagecode' = 'runtime'
'org.graalvm.nativeimage.imagecode' = 'bogus'
""")
void attemptToProcessWhileRunningInAotMode(String property, String value) {
try {
System.setProperty(property, value);
assertThatIllegalStateException()
.isThrownBy(() -> generator().processAheadOfTime(Stream.empty()))
.withMessage("Cannot perform AOT processing during AOT run-time execution");
}
finally {
System.clearProperty(property);
}
}
@Test
void attemptToProcessWhileRunningInGraalVmNativeBuildToolsAgentMode() {
final String IMAGECODE = "org.graalvm.nativeimage.imagecode";
try {
System.setProperty(IMAGECODE, "AgenT");
assertThatNoException().isThrownBy(() -> generator().processAheadOfTime(Stream.empty()));
}
finally {
System.clearProperty(IMAGECODE);
}
}
private static TestContextAotGenerator generator() {
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
return new TestContextAotGenerator(generatedFiles);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -18,7 +18,6 @@ package org.springframework.test.context.aot.samples.basic;
import org.junit.runner.RunWith;
import org.springframework.aot.AotDetector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
@@ -28,6 +27,7 @@ import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.aot.AotTestAttributes;
import org.springframework.test.context.aot.TestAotDetector;
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests.CustomXmlBootstrapper;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.junit4.SpringRunner;
@@ -78,7 +78,7 @@ public class BasicSpringVintageTests {
String booleanKey1 = "@SpringBootConfiguration-" + mergedConfig.getTestClass().getName() + "-active1";
String booleanKey2 = "@SpringBootConfiguration-" + mergedConfig.getTestClass().getName() + "-active2";
AotTestAttributes aotAttributes = AotTestAttributes.getInstance();
if (AotDetector.useGeneratedArtifacts()) {
if (TestAotDetector.useGeneratedArtifacts()) {
assertThat(aotAttributes.getString(stringKey))
.as("AOT String attribute must already be present during AOT run-time execution")
.isEqualTo("org.example.Main");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -19,7 +19,6 @@ package org.springframework.test.context.aot.samples.basic;
import java.util.Arrays;
import java.util.List;
import org.springframework.aot.AotDetector;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.annotation.Import;
@@ -28,6 +27,7 @@ import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.aot.TestAotDetector;
/**
* Emulates {@code ImportsContextCustomizerFactory} from Spring Boot's testing support.
@@ -41,7 +41,7 @@ class ImportsContextCustomizerFactory implements ContextCustomizerFactory {
public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
if (AotDetector.useGeneratedArtifacts()) {
if (TestAotDetector.useGeneratedArtifacts()) {
return null;
}
if (testClass.getName().startsWith("org.springframework.test.context.aot.samples") &&