Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog.trace.civisibility.utils

import datadog.communication.util.IOUtils
import datadog.trace.test.util.PortableCommand
import spock.lang.Specification
import spock.lang.TempDir

Expand All @@ -18,7 +19,7 @@ class ShellCommandExecutorTest extends Specification {
def shellCommandExecutor = new ShellCommandExecutor(temporaryFolder, SHELL_COMMAND_TIMEOUT)

when:
def output = shellCommandExecutor.executeCommand(IOUtils::readFully, "echo", "this is a test")
def output = shellCommandExecutor.executeCommand(IOUtils::readFully, *PortableCommand.echo("this is a test"))

then:
output.trim() == "this is a test"
Expand All @@ -29,7 +30,7 @@ class ShellCommandExecutorTest extends Specification {
def shellCommandExecutor = new ShellCommandExecutor(temporaryFolder, SHELL_COMMAND_TIMEOUT)

when:
def output = shellCommandExecutor.executeCommand(IOUtils::readFully, "this is a test".bytes, "cat")
def output = shellCommandExecutor.executeCommand(IOUtils::readFully, "this is a test".bytes, *PortableCommand.cat())

then:
output.trim() == "this is a test"
Expand All @@ -40,7 +41,7 @@ class ShellCommandExecutorTest extends Specification {
def shellCommandExecutor = new ShellCommandExecutor(temporaryFolder, 1_000)

when:
shellCommandExecutor.executeCommand(IOUtils::readFully, "sleep", "2")
shellCommandExecutor.executeCommand(IOUtils::readFully, *PortableCommand.sleep(2))

then:
thrown TimeoutException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package datadog.trace.util

import datadog.trace.test.util.DDSpecification
import datadog.trace.test.util.PortableCommand
import spock.util.concurrent.PollingConditions

// This test looks at the private "currentProcess" variable because the alternative
// would be calling "ps -e" repeatedly
class ProcessSupervisorTest extends DDSpecification {
ProcessBuilder createProcessBuilder() {
// Creates a process that never returns
return new ProcessBuilder("tail", "-f", "/dev/null")
// Creates a process that never returns on its own
return new ProcessBuilder(PortableCommand.runForever())
}

def "Process killed when supervisor closed"() {
Expand Down
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};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve literal echo values on POSIX

When value is option-like (for example -n, -e, or GNU --help), the native echo interprets it instead of printing it, while the Windows JVM implementation prints the literal value and a newline. This makes PortableCommand.echo(value) observably OS-dependent for valid inputs; use a literal-safe POSIX command such as printf or otherwise prevent native option parsing.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Preserve option-like echo values on POSIX

Tests that use option-like values can produce different results on POSIX and Windows.

Assertion details
  • Input: Call PortableCommand.echo("-n") on a POSIX system.
  • Expected: The command prints the literal value -n and a line separator on all operating systems.
  • Actual: The POSIX command is echo -n. The native utility treats -n as an option. It prints no value and no line separator.

Was this helpful? React 👍 or 👎
🤖 Datadog Autotest · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest · Open Bits AI session

}

@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));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Match the caller's charset in emulated echo

On Windows JVMs whose default charset is not UTF-8, such as typical JDK 8 installations, this writes UTF-8 bytes while the migrated ShellCommandExecutorTest consumes them through IOUtils.readFully, which decodes with Charset.defaultCharset(). A non-ASCII value such as é therefore becomes mojibake on the emulated path even though the API accepts arbitrary strings; encode using the inherited default charset or make callers explicitly decode UTF-8.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Use a compatible charset for Windows echo output

Tests with non-ASCII echo values can pass on POSIX and return incorrect output on Windows.

Assertion details
  • Input: Run PortableCommand.echo with a non-ASCII value on a Windows JVM whose default charset is not UTF-8, such as Java 8 with Cp1252.
  • Expected: The migrated consumer reads the same non-ASCII value that the caller supplies.
  • Actual: The child JVM writes UTF-8 bytes. The migrated consumer reads them with the default charset. On affected Windows JVMs, the consumer reads incorrect characters.

Was this helpful? React 👍 or 👎
🤖 Datadog Autotest · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest · Open Bits AI session

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]);
}
}
Loading
Loading