-
Notifications
You must be signed in to change notification settings - Fork 355
Make child-process tests portable across operating systems #12355
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package datadog.trace.test.util; | ||
|
|
||
| import datadog.environment.OperatingSystem; | ||
| import datadog.trace.api.internal.VisibleForTesting; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.security.CodeSource; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Builds command lines for a small set of command-line utilities so tests can spawn them without | ||
| * depending on the host operating system. | ||
| * | ||
| * <p>On POSIX platforms the native utilities are used directly. Windows has no usable equivalent | ||
| * for any of them — {@code echo} is a {@code cmd.exe} builtin rather than an executable, {@code | ||
| * type} cannot read standard input, and {@code timeout} refuses to run when standard input is | ||
| * redirected — so there the commands are emulated by {@link PortableCommandRunner} in a child JVM. | ||
| * | ||
| * <p>POSIX deliberately keeps the native utilities instead of emulating everywhere: forking a JVM | ||
| * is far more expensive than spawning a small native binary, in both startup time and memory, and | ||
| * effectively all CI runs on Linux — so the cheap path is the one that matters. It is also exactly | ||
| * what these tests spawned before this class existed, which leaves CI behavior unchanged. | ||
| * | ||
| * <p>Both paths are observably identical: {@code cat} copies bytes exactly, {@code sleep} takes a | ||
| * duration in seconds, and {@code echo} terminates its output with the platform line separator. | ||
| * Callers therefore never need to branch on the operating system. | ||
| * | ||
| * <p>Supported commands: | ||
| * | ||
| * <ul> | ||
| * <li>{@link #echo(String)} writes a value followed by the platform line separator. | ||
| * <li>{@link #cat()} copies standard input to standard output. | ||
| * <li>{@link #sleep(long)} waits for the requested number of seconds, then exits with 0. | ||
| * <li>{@link #runForever()} never exits on its own and must be destroyed by the caller. | ||
| * </ul> | ||
| */ | ||
| public final class PortableCommand { | ||
| private static final String MIN_HEAP = "-Xms8m"; | ||
| private static final String MAX_HEAP = "-Xmx16m"; | ||
|
|
||
| /** Windows has no usable native equivalent for any of these commands. */ | ||
| private static final boolean EMULATED = OperatingSystem.isWindows(); | ||
|
|
||
| private PortableCommand() {} | ||
|
|
||
| public static String[] echo(String value) { | ||
| return echo(value, EMULATED); | ||
| } | ||
|
|
||
| public static String[] cat() { | ||
| return cat(EMULATED); | ||
| } | ||
|
|
||
| public static String[] sleep(long durationSec) { | ||
| return sleep(durationSec, EMULATED); | ||
| } | ||
|
|
||
| public static String[] runForever() { | ||
| return runForever(EMULATED); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String[] echo(String value, boolean emulated) { | ||
| return emulated ? emulate("echo", value) : new String[] {"echo", value}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Tests that use option-like values can produce different results on POSIX and Windows. Assertion details
Was this helpful? React 👍 or 👎 |
||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String[] cat(boolean emulated) { | ||
| return emulated ? emulate("cat") : new String[] {"cat"}; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String[] sleep(long durationSec, boolean emulated) { | ||
| if (durationSec < 0) { | ||
| throw new IllegalArgumentException("Sleep duration must not be negative: " + durationSec); | ||
| } | ||
| // The native sleep takes seconds; the emulated runner takes milliseconds. | ||
| return emulated | ||
| ? emulate("sleep", Long.toString(durationSec * 1000)) | ||
| : new String[] {"sleep", Long.toString(durationSec)}; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String[] runForever(boolean emulated) { | ||
| return emulated | ||
| ? emulate("sleep", Long.toString(Long.MAX_VALUE)) | ||
| : new String[] {"tail", "-f", "/dev/null"}; | ||
| } | ||
|
|
||
| private static String[] emulate(String... arguments) { | ||
| Path executable = javaExecutable(); | ||
| Path classpath = classpathEntry(); | ||
|
|
||
| List<String> command = new ArrayList<>(); | ||
| command.add(executable.toString()); | ||
| command.add(MIN_HEAP); | ||
| command.add(MAX_HEAP); | ||
| command.add("-cp"); | ||
| command.add(classpath.toString()); | ||
| command.add(PortableCommandRunner.class.getName()); | ||
| command.addAll(Arrays.asList(arguments)); | ||
| return command.toArray(new String[0]); | ||
| } | ||
|
|
||
| private static Path javaExecutable() { | ||
| return javaExecutable(Paths.get(System.getProperty("java.home"))); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static Path javaExecutable(Path javaHome) { | ||
| Path bin = javaHome.resolve("bin"); | ||
| for (String name : new String[] {"java", "java.exe"}) { | ||
| Path candidate = bin.resolve(name); | ||
| if (Files.isRegularFile(candidate)) { | ||
| return candidate; | ||
| } | ||
| } | ||
| throw new IllegalStateException("Could not find a Java executable under " + bin); | ||
| } | ||
|
|
||
| private static Path classpathEntry() { | ||
| CodeSource source = PortableCommand.class.getProtectionDomain().getCodeSource(); | ||
| try { | ||
| return Paths.get(source.getLocation().toURI()); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException( | ||
| "Cannot determine the classpath of " + PortableCommand.class.getName(), e); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package datadog.trace.test.util; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
|
||
| import datadog.trace.api.internal.VisibleForTesting; | ||
| import de.thetaphi.forbiddenapis.SuppressForbidden; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
|
|
||
| /** | ||
| * Emulates the utilities described by {@link PortableCommand} inside a JVM, for platforms that have | ||
| * no usable native equivalent. Spawned as a child process; not meant to be called directly. | ||
| */ | ||
| public final class PortableCommandRunner { | ||
| private PortableCommandRunner() {} | ||
|
|
||
| @SuppressForbidden | ||
| public static void main(String[] arguments) throws IOException, InterruptedException { | ||
| execute(arguments, System.in, System.out); | ||
| System.out.flush(); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static void execute(String[] arguments, InputStream input, OutputStream output) | ||
| throws IOException, InterruptedException { | ||
| if (arguments.length == 0) { | ||
| throw new IllegalArgumentException("Missing command"); | ||
| } | ||
| switch (arguments[0]) { | ||
| case "echo": | ||
| output.write((argument(arguments) + System.lineSeparator()).getBytes(UTF_8)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On Windows JVMs whose default charset is not UTF-8, such as typical JDK 8 installations, this writes UTF-8 bytes while the migrated Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Tests with non-ASCII echo values can pass on POSIX and return incorrect output on Windows. Assertion details
Was this helpful? React 👍 or 👎 |
||
| break; | ||
| case "cat": | ||
| byte[] buffer = new byte[8192]; | ||
| int read; | ||
| while ((read = input.read(buffer)) != -1) { | ||
| output.write(buffer, 0, read); | ||
| } | ||
| break; | ||
| case "sleep": | ||
| Thread.sleep(Long.parseLong(argument(arguments))); | ||
| break; | ||
| default: | ||
| throw new IllegalArgumentException("Unknown command: " + arguments[0]); | ||
| } | ||
| } | ||
|
|
||
| private static String argument(String[] arguments) { | ||
| if (arguments.length < 2) { | ||
| throw new IllegalArgumentException("Command '" + arguments[0] + "' requires an argument"); | ||
| } | ||
| return arguments[1]; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package datadog.trace.test.util; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.InputStream; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Unit tests for the command dispatch. In production the runner only ever executes as a child | ||
| * process, where its behavior is covered end to end by the emulated cases of {@link | ||
| * PortableCommandTest} — but a child JVM is opaque to both the coverage report and to assertions | ||
| * about why a command misbehaved, so the dispatch is driven directly here. | ||
| */ | ||
| class PortableCommandRunnerTest { | ||
| @Test | ||
| void echoes() throws Exception { | ||
| ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
|
|
||
| PortableCommandRunner.execute(new String[] {"echo", "value"}, emptyInput(), output); | ||
|
|
||
| assertEquals("value" + System.lineSeparator(), new String(output.toByteArray(), UTF_8)); | ||
| } | ||
|
|
||
| @Test | ||
| void copiesInput() throws Exception { | ||
| ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
| InputStream input = new ByteArrayInputStream("payload".getBytes(UTF_8)); | ||
|
|
||
| PortableCommandRunner.execute(new String[] {"cat"}, input, output); | ||
|
|
||
| assertEquals("payload", new String(output.toByteArray(), UTF_8)); | ||
| } | ||
|
|
||
| @Test | ||
| void sleeps() throws Exception { | ||
| long start = System.nanoTime(); | ||
|
|
||
| PortableCommandRunner.execute( | ||
| new String[] {"sleep", "50"}, emptyInput(), new ByteArrayOutputStream()); | ||
|
|
||
| assertTrue((System.nanoTime() - start) / 1_000_000 >= 40, "sleep returned immediately"); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsMissingCommand() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> | ||
| PortableCommandRunner.execute( | ||
| new String[0], emptyInput(), new ByteArrayOutputStream())); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsUnknownCommand() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> | ||
| PortableCommandRunner.execute( | ||
| new String[] {"rm"}, emptyInput(), new ByteArrayOutputStream())); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsMissingArgument() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> | ||
| PortableCommandRunner.execute( | ||
| new String[] {"echo"}, emptyInput(), new ByteArrayOutputStream())); | ||
| } | ||
|
|
||
| private static InputStream emptyInput() { | ||
| return new ByteArrayInputStream(new byte[0]); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
valueis option-like (for example-n,-e, or GNU--help), the nativeechointerprets it instead of printing it, while the Windows JVM implementation prints the literal value and a newline. This makesPortableCommand.echo(value)observably OS-dependent for valid inputs; use a literal-safe POSIX command such asprintfor otherwise prevent native option parsing.Useful? React with 👍 / 👎.