Repository navigation
JAVA-7407 DO NOT MERGE Run PVF with the SemSitter frontend #6584
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
Open
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9d22fce
JAVA-7407 DO NOT MERGE Tell the parser providers which product runs t…
rombirli 5a0b82b
JAVA-7407 DO NOT MERGE Bundle the Linux SemSitter servers so PVF and …
rombirli 334ede0
JAVA-7407 DO NOT MERGE Skip the sensor tests, which run with the Linu…
rombirli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/BundledServer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.semsitter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.util.Locale; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import javax.annotation.Nullable; | ||
| import org.sonar.api.SonarEdition; | ||
| import org.sonar.api.SonarRuntime; | ||
|
|
||
| /** | ||
| * The SemSitter servers shipped inside this plugin, one per Linux architecture, for the runners that have no server | ||
| * installed: the performance-validation benchmark and SonarQube Cloud, whose analysis service runs on arm64. A server is | ||
| * used only there. Every other analysis keeps the default frontend unless an executable is configured. | ||
| * | ||
| * <p>Where a server is expected, it must be usable: a host that is not Linux amd64 or arm64, or a server that cannot be | ||
| * extracted, fails the analysis instead of silently leaving it to the default frontend. | ||
| */ | ||
| final class BundledServer { | ||
|
|
||
| static final String RESOURCE_X86_64 = "udg-java-server-x86_64-unknown-linux-musl"; | ||
| static final String RESOURCE_AARCH64 = "udg-java-server-aarch64-unknown-linux-musl"; | ||
| static final String PERFORMANCE_VALIDATION_CONTEXT = "sonar.java.context"; | ||
| private static final AtomicReference<Path> COPY = new AtomicReference<>(); | ||
|
|
||
| private BundledServer() { | ||
| } | ||
|
|
||
| static Optional<Path> extract(SonarRuntime runtime) { | ||
| if (!performanceValidation() && runtime.getEdition() != SonarEdition.SONARCLOUD) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(copyOnce(resourceFor(System.getProperty("os.name", ""), System.getProperty("os.arch", "")))); | ||
| } | ||
|
|
||
| /** The server that runs on the given host, or a failure for a host none of them runs on. */ | ||
| static String resourceFor(String osName, String architecture) { | ||
| if (!osName.toLowerCase(Locale.ROOT).contains("linux")) { | ||
| throw new IllegalStateException("The bundled SemSitter server only runs on Linux, not on " + osName); | ||
| } | ||
| return switch (architecture.toLowerCase(Locale.ROOT)) { | ||
| case "amd64", "x86_64" -> RESOURCE_X86_64; | ||
| case "aarch64", "arm64" -> RESOURCE_AARCH64; | ||
| default -> throw new IllegalStateException("No SemSitter server is bundled for the architecture " + architecture); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Copies the server once per JVM, to a file of the user that no concurrent analysis on the same host writes to. A | ||
| * server that cannot be extracted fails the analysis, so that a performance validation never silently runs ECJ. | ||
| */ | ||
| private static synchronized Path copyOnce(String resource) { | ||
| Path copy = COPY.get(); | ||
| if (copy != null) { | ||
| return copy; | ||
| } | ||
| try (InputStream in = BundledServer.class.getResourceAsStream(resource)) { | ||
| copy = install(in, resource, installDirectory(System.getProperty("user.home", ""), System.getProperty("java.io.tmpdir"))); | ||
| COPY.set(copy); | ||
| return copy; | ||
| } catch (IOException failure) { | ||
| throw new IllegalStateException("The bundled SemSitter server could not be extracted: " + failure.getMessage(), failure); | ||
| } | ||
| } | ||
|
|
||
| /** The directory of the user's home for the server, or else the temporary directory when the home does not exist or cannot be written, as in a container. */ | ||
| static Path installDirectory(String home, String temporaryDirectory) throws IOException { | ||
| Path homeDirectory = Path.of(home, ".sonar", "semsitter"); | ||
| return isUsable(homeDirectory) ? homeDirectory : Files.createDirectories(Path.of(temporaryDirectory)); | ||
| } | ||
|
|
||
| private static boolean isUsable(Path directory) { | ||
| try { | ||
| Files.createDirectories(directory); | ||
| return Files.isWritable(directory); | ||
| } catch (IOException | RuntimeException failure) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** Writes the server to a new executable file of {@code directory}, deleted when the JVM exits or right away if the copy fails. */ | ||
| static Path install(@Nullable InputStream in, String resource, Path directory) throws IOException { | ||
| if (in == null) { | ||
| throw new IllegalStateException("The SemSitter server is not bundled in this plugin."); | ||
| } | ||
| Path destination = Files.createTempFile(directory, resource, ""); | ||
| if (!copyTo(in, destination)) { | ||
| delete(destination); | ||
| throw new IllegalStateException("The bundled SemSitter server could not be copied to " + destination); | ||
| } | ||
| Runtime.getRuntime().addShutdownHook(new Thread(() -> delete(destination))); | ||
| return destination; | ||
| } | ||
|
|
||
| private static boolean copyTo(InputStream in, Path destination) { | ||
| try { | ||
| Files.copy(in, destination, StandardCopyOption.REPLACE_EXISTING); | ||
| return destination.toFile().setExecutable(true); | ||
| } catch (IOException failure) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static boolean delete(Path path) { | ||
| try { | ||
| return Files.deleteIfExists(path); | ||
| } catch (IOException failure) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static boolean performanceValidation() { | ||
| return PERFORMANCE_VALIDATION_CONTEXT.equals(System.getProperty("sensor.context.key")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+17.5 MB
...er/src/main/resources/org/sonar/java/semsitter/udg-java-server-aarch64-unknown-linux-musl
Binary file not shown.
Binary file added
BIN
+21.3 MB
...ter/src/main/resources/org/sonar/java/semsitter/udg-java-server-x86_64-unknown-linux-musl
Binary file not shown.
141 changes: 141 additions & 0 deletions
141
java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/BundledServerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.semsitter; | ||
|
|
||
| import com.sonarsource.scanner.engine.sensor.test.fixtures.TestSonarRuntime; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.sonar.api.SonarEdition; | ||
| import org.sonar.api.SonarQubeSide; | ||
| import org.sonar.api.SonarRuntime; | ||
| import org.sonar.api.utils.Version; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
|
|
||
| class BundledServerTest { | ||
|
|
||
| @TempDir | ||
| Path project; | ||
|
|
||
| @Test | ||
| void bundled_server_stays_unused_outside_performance_validation_and_sonarqube_cloud() { | ||
| System.clearProperty("sensor.context.key"); | ||
|
|
||
| assertThat(BundledServer.extract(runtime(SonarEdition.COMMUNITY))).isEmpty(); | ||
| assertThat(BundledServer.extract(runtime(SonarEdition.ENTERPRISE))).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void the_server_follows_the_architecture_of_the_linux_host() { | ||
| assertThat(BundledServer.resourceFor("Linux", "amd64")).isEqualTo(BundledServer.RESOURCE_X86_64); | ||
| assertThat(BundledServer.resourceFor("Linux", "x86_64")).isEqualTo(BundledServer.RESOURCE_X86_64); | ||
| assertThat(BundledServer.resourceFor("Linux", "aarch64")).isEqualTo(BundledServer.RESOURCE_AARCH64); | ||
| assertThat(BundledServer.resourceFor("Linux", "arm64")).isEqualTo(BundledServer.RESOURCE_AARCH64); | ||
| } | ||
|
|
||
| @Test | ||
| void a_host_without_a_bundled_server_fails() { | ||
| assertThatThrownBy(() -> BundledServer.resourceFor("Mac OS X", "aarch64")).isInstanceOf(IllegalStateException.class).hasMessageContaining("only runs on Linux"); | ||
| assertThatThrownBy(() -> BundledServer.resourceFor("Linux", "riscv64")).isInstanceOf(IllegalStateException.class).hasMessageContaining("riscv64"); | ||
| } | ||
|
|
||
| @Test | ||
| void both_linux_servers_are_bundled() throws Exception { | ||
| for (String resource : new String[] {BundledServer.RESOURCE_X86_64, BundledServer.RESOURCE_AARCH64}) { | ||
| try (InputStream in = BundledServer.class.getResourceAsStream(resource)) { | ||
| assertThat(in).isNotNull(); | ||
| assertThat(in.readNBytes(4)).containsExactly(0x7f, 'E', 'L', 'F'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void the_installed_copy_is_executable() throws Exception { | ||
| Path copy = BundledServer.install(new ByteArrayInputStream(new byte[] {1, 2, 3}), "server", project); | ||
|
|
||
| assertThat(Files.isExecutable(copy)).isTrue(); | ||
| assertThat(Files.readAllBytes(copy)).containsExactly(1, 2, 3); | ||
| } | ||
|
|
||
| @Test | ||
| void a_failed_copy_fails_and_leaves_no_file_behind() throws Exception { | ||
| InputStream broken = new InputStream() { | ||
| @Override | ||
| public int read() throws IOException { | ||
| throw new IOException("broken"); | ||
| } | ||
| }; | ||
|
|
||
| assertThatThrownBy(() -> BundledServer.install(broken, "server", project)).isInstanceOf(IllegalStateException.class).hasMessageContaining("could not be copied"); | ||
| try (var files = Files.list(project)) { | ||
| assertThat(files).isEmpty(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void the_server_goes_to_the_home_of_the_user() throws Exception { | ||
| Path directory = BundledServer.installDirectory(project.toString(), project.resolve("tmp").toString()); | ||
|
|
||
| assertThat(directory).isEqualTo(project.resolve(".sonar").resolve("semsitter")); | ||
| } | ||
|
|
||
| @Test | ||
| void the_server_goes_to_the_temporary_directory_when_the_home_is_unusable() throws Exception { | ||
| Path notADirectory = Files.writeString(project.resolve("home"), "file"); | ||
| Path temporary = project.resolve("tmp"); | ||
|
|
||
| assertThat(BundledServer.installDirectory(notADirectory.toString(), temporary.toString())).isEqualTo(temporary); | ||
| } | ||
|
|
||
| @Test | ||
| void a_missing_bundled_server_fails() { | ||
| assertThatThrownBy(() -> BundledServer.install(null, "server", project)).hasMessage("The SemSitter server is not bundled in this plugin."); | ||
| } | ||
|
|
||
| @Test | ||
| void bundled_linux_server_speaks_the_protocol() throws Exception { | ||
| String os = System.getProperty("os.name", "").toLowerCase(Locale.ROOT); | ||
| String architecture = System.getProperty("os.arch", ""); | ||
| assumeTrue(os.contains("linux") && List.of("amd64", "x86_64", "aarch64", "arm64").contains(architecture)); | ||
| System.setProperty("sensor.context.key", BundledServer.PERFORMANCE_VALIDATION_CONTEXT); | ||
| try { | ||
| Optional<Path> server = BundledServer.extract(runtime(SonarEdition.COMMUNITY)); | ||
| assertThat(server).isPresent(); | ||
|
|
||
| try (SemSitterClient client = SemSitterClient.start(server.get(), project)) { | ||
| assertThat(client).isNotNull(); | ||
| } | ||
| assertThat(Files.isExecutable(server.get())).isTrue(); | ||
| } finally { | ||
| System.clearProperty("sensor.context.key"); | ||
| } | ||
| } | ||
|
|
||
| private static SonarRuntime runtime(SonarEdition edition) { | ||
| return TestSonarRuntime.forSonarQube(Version.create(10, 0), SonarQubeSide.SCANNER, edition); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.