|
| 1 | +package org.graalvm.internal.tck; |
| 2 | + |
| 3 | +import org.graalvm.internal.tck.utils.InteractiveTaskUtils; |
| 4 | +import org.graalvm.internal.tck.utils.MetadataGenerationUtils; |
| 5 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 6 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | +import com.fasterxml.jackson.core.util.DefaultIndenter; |
| 8 | +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 11 | +import org.graalvm.internal.tck.model.MetadataVersionsIndexEntry; |
| 12 | +import org.gradle.api.DefaultTask; |
| 13 | +import org.gradle.api.file.ProjectLayout; |
| 14 | +import org.gradle.api.tasks.Input; |
| 15 | +import org.gradle.api.tasks.TaskAction; |
| 16 | +import org.gradle.api.tasks.options.Option; |
| 17 | +import org.gradle.process.ExecOperations; |
| 18 | +import org.gradle.api.GradleException; |
| 19 | + |
| 20 | +import javax.inject.Inject; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.File; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.regex.Pattern; |
| 28 | + |
| 29 | +public abstract class FixTestNativeImageRun extends DefaultTask { |
| 30 | + private static final String GRADLEW = "gradlew"; |
| 31 | + |
| 32 | + private String testLibraryCoordinates; |
| 33 | + private String newLibraryVersion; |
| 34 | + |
| 35 | + @Inject |
| 36 | + protected abstract ProjectLayout getLayout(); |
| 37 | + |
| 38 | + @Inject |
| 39 | + protected abstract ExecOperations getExecOperations(); |
| 40 | + |
| 41 | + @Option(option = "newLibraryVersion", description = "New version of library that is failing") |
| 42 | + public void setNewLibraryVersion(String newLibraryVersion) { |
| 43 | + this.newLibraryVersion = newLibraryVersion; |
| 44 | + } |
| 45 | + |
| 46 | + @Input |
| 47 | + public String getNewLibraryVersion() { |
| 48 | + return newLibraryVersion; |
| 49 | + } |
| 50 | + |
| 51 | + @Option(option = "testLibraryCoordinates", description = "Coordinates in the form of group:artifact:version") |
| 52 | + public void setTestLibraryCoordinates(String testLibraryCoordinates) { |
| 53 | + this.testLibraryCoordinates = testLibraryCoordinates; |
| 54 | + } |
| 55 | + |
| 56 | + @Input |
| 57 | + public String getTestLibraryCoordinates() { |
| 58 | + return testLibraryCoordinates; |
| 59 | + } |
| 60 | + |
| 61 | + @TaskAction |
| 62 | + public void run() throws IOException { |
| 63 | + Path testsDirectory = MetadataGenerationUtils.computeTestsDirectory(getLayout(), testLibraryCoordinates); |
| 64 | + Path gradlewPath = MetadataGenerationUtils.getPathFromProject(getLayout(), GRADLEW); |
| 65 | + |
| 66 | + Coordinates baseCoords = Coordinates.parse(testLibraryCoordinates); |
| 67 | + String newCoordsString = baseCoords.group() + ":" + baseCoords.artifact() + ":" + newLibraryVersion; |
| 68 | + Coordinates newCoords = Coordinates.parse(newCoordsString); |
| 69 | + updateIndexJson(newCoords); |
| 70 | + |
| 71 | + Path metadataDirectory = MetadataGenerationUtils.computeMetadataDirectory(getLayout(), newCoordsString); |
| 72 | + Files.createDirectories(metadataDirectory); |
| 73 | + |
| 74 | + // Ensure the tests build.gradle has an agent block; if not, create user-code-filter.json and add the agent block. |
| 75 | + Path buildFilePath = testsDirectory.resolve("build.gradle"); |
| 76 | + if (!Files.isRegularFile(buildFilePath)) { |
| 77 | + throw new RuntimeException("Cannot find tests build file at: " + buildFilePath); |
| 78 | + } |
| 79 | + String buildGradle = Files.readString(buildFilePath, java.nio.charset.StandardCharsets.UTF_8); |
| 80 | + boolean hasAgentBlock = Pattern.compile("(?s)\\bagent\\s*\\{").matcher(buildGradle).find(); |
| 81 | + if (!hasAgentBlock) { |
| 82 | + MetadataGenerationUtils.addUserCodeFilterFile(testsDirectory, java.util.List.of(baseCoords.group())); |
| 83 | + MetadataGenerationUtils.addAgentConfigBlock(testsDirectory); |
| 84 | + } |
| 85 | + |
| 86 | + MetadataGenerationUtils.collectMetadata(getExecOperations(), testsDirectory, getLayout(), newCoordsString, gradlewPath, newLibraryVersion); |
| 87 | + |
| 88 | + MetadataGenerationUtils.createIndexJsonSpecificVersion(metadataDirectory); |
| 89 | + |
| 90 | + // At the end, attempt to run tests with the new library version. |
| 91 | + // If the build fails, it can be due agent's non-deterministic nature. |
| 92 | + InteractiveTaskUtils.printUserInfo("Running the test with updated metadata"); |
| 93 | + var execOutput = new java.io.ByteArrayOutputStream(); |
| 94 | + var testResult = runTestWithVersion(gradlewPath, testLibraryCoordinates, newLibraryVersion, execOutput); |
| 95 | + if (testResult.getExitValue() != 0) { |
| 96 | + InteractiveTaskUtils.printUserInfo("Test run failed, running agent again"); |
| 97 | + MetadataGenerationUtils.collectMetadata(getExecOperations(), testsDirectory, getLayout(), newCoordsString, gradlewPath); |
| 98 | + testResult = runTestWithVersion(gradlewPath, testLibraryCoordinates, newLibraryVersion, execOutput); |
| 99 | + if (testResult.getExitValue() != 0) { |
| 100 | + throw new GradleException("Test run failed. See output:\n" + execOutput); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + private org.gradle.process.ExecResult runTestWithVersion(Path gradlewPath, String coords, String version, java.io.ByteArrayOutputStream execOutput) { |
| 107 | + return getExecOperations().exec(execSpec -> { |
| 108 | + execSpec.setExecutable(gradlewPath.toString()); |
| 109 | + execSpec.setArgs(java.util.List.of("clean", "test", "-Pcoordinates=" + coords)); |
| 110 | + execSpec.environment("GVM_TCK_LV", version); |
| 111 | + execSpec.setStandardOutput(execOutput); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + private void updateIndexJson(Coordinates newCoords) throws IOException { |
| 116 | + String indexPathTemplate = "metadata/$group$/$artifact$/index.json"; |
| 117 | + File indexFile = MetadataGenerationUtils.getPathFromProject(getLayout(), CoordinateUtils.replace(indexPathTemplate, newCoords)).toFile(); |
| 118 | + |
| 119 | + ObjectMapper objectMapper = new ObjectMapper() |
| 120 | + .enable(SerializationFeature.INDENT_OUTPUT) |
| 121 | + .setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 122 | + |
| 123 | + // Read existing entries if file exists, otherwise start fresh |
| 124 | + List<MetadataVersionsIndexEntry> entries = new ArrayList<>(); |
| 125 | + if (indexFile.exists()) { |
| 126 | + entries = objectMapper.readValue(indexFile, new TypeReference<>() {}); |
| 127 | + } |
| 128 | + |
| 129 | + // Remove 'latest' flag from any existing latest entry |
| 130 | + for (int i = 0; i < entries.size(); i++) { |
| 131 | + MetadataVersionsIndexEntry entry = entries.get(i); |
| 132 | + if (Boolean.TRUE.equals(entry.latest())) { |
| 133 | + entries.set(i, new MetadataVersionsIndexEntry( |
| 134 | + null, // latest removed |
| 135 | + entry.override(), |
| 136 | + entry.module(), |
| 137 | + entry.defaultFor(), |
| 138 | + entry.metadataVersion(), |
| 139 | + entry.testedVersions() |
| 140 | + )); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // Add the new entry and mark it as latest |
| 145 | + String moduleName = newCoords.group() + ":" + newCoords.artifact(); |
| 146 | + List<String> testedVersions = new ArrayList<>(); |
| 147 | + testedVersions.add(newCoords.version()); |
| 148 | + |
| 149 | + MetadataVersionsIndexEntry newEntry = new MetadataVersionsIndexEntry( |
| 150 | + Boolean.TRUE, |
| 151 | + null, |
| 152 | + moduleName, |
| 153 | + null, |
| 154 | + newCoords.version(), |
| 155 | + testedVersions |
| 156 | + ); |
| 157 | + entries.add(newEntry); |
| 158 | + |
| 159 | + DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); |
| 160 | + prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE); |
| 161 | + String json = objectMapper.writer(prettyPrinter).writeValueAsString(entries); |
| 162 | + if (!json.endsWith(System.lineSeparator())) { |
| 163 | + json = json + System.lineSeparator(); |
| 164 | + } |
| 165 | + Files.writeString(indexFile.toPath(), json, java.nio.charset.StandardCharsets.UTF_8); |
| 166 | + } |
| 167 | +} |
0 commit comments