Refine GraalVM tracing agent detection

This commit refines how GraalVM tracing agent detection works
for both test and application executions.

It rolls back the introduction of TestAotDetector done in 111309605c
and instead updates AotDetector.useGeneratedArtifacts()
to only detect "buildtime" and "runtime" imagecode system
property values by leveraging a new method
NativeDetector.inNativeImage(NativeDetector.Context...).

This commit also adds a workaround for
https://github.com/oracle/graal/issues/6691.

Closes gh-30511
This commit is contained in:
Sébastien Deleuze
2023-05-25 13:50:03 +02:00
parent 8b8d147480
commit 1cf6d1dd9d
16 changed files with 107 additions and 166 deletions

View File

@@ -1,76 +0,0 @@
/*
* 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

@@ -18,6 +18,7 @@ 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;
@@ -27,7 +28,6 @@ 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 (TestAotDetector.useGeneratedArtifacts()) {
if (AotDetector.useGeneratedArtifacts()) {
assertThat(aotAttributes.getString(stringKey))
.as("AOT String attribute must already be present during AOT run-time execution")
.isEqualTo("org.example.Main");

View File

@@ -19,6 +19,7 @@ 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;
@@ -27,7 +28,6 @@ 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 (TestAotDetector.useGeneratedArtifacts()) {
if (AotDetector.useGeneratedArtifacts()) {
return null;
}
if (testClass.getName().startsWith("org.springframework.test.context.aot.samples") &&