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,114 @@
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 portable command lines for simple test utilities.
*
* <ul>
* <li>POSIX uses native system commands.
* <li>Windows uses {@link PortableCommandRunner} in a child JVM to emulate their behavior.
* </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};
Comment thread
AlexeyKuznetsov-DD marked this conversation as resolved.
Comment thread
AlexeyKuznetsov-DD marked this conversation as resolved.
}

@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.Charset.defaultCharset;

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(defaultCharset()));
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