Skip to content

Commit

Permalink
Add integration tests for SimpleProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Jan 25, 2024
1 parent 29686d4 commit fd356b6
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.junit.jupiter.api.io.TempDir;

import com.exasol.mavenprojectversiongetter.MavenProjectVersionGetter;
import com.exasol.projectkeeper.sources.analyze.golang.SimpleProcess;
import com.exasol.projectkeeper.sources.analyze.generic.SimpleProcess;
import com.exasol.projectkeeper.test.GolangProjectFixture;
import com.exasol.projectkeeper.test.MavenProjectFixture;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.nio.file.Path;

import com.exasol.projectkeeper.sources.analyze.golang.SimpleProcess;

/**
* Enable to execute a {@link ShellCommand} and hence to mock this in tests.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.exasol.projectkeeper.sources.analyze.golang;
package com.exasol.projectkeeper.sources.analyze.generic;

import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -26,7 +26,7 @@ public class SimpleProcess {
private final List<String> command;
private final Instant startTime;

private SimpleProcess(final Process process, final CollectingConsumer outputStreamConsumer,
SimpleProcess(final Process process, final CollectingConsumer outputStreamConsumer,
final CollectingConsumer errorStreamConsumer, final Path workingDirectory, final List<String> command,
final Instant startTime) {
this.process = process;
Expand Down Expand Up @@ -97,7 +97,7 @@ public void waitUntilFinished(final Duration executionTimeout) {
+ " Output:\n{{std out}}\n" //
+ "Error output:\n{{std error}}", //
formatCommand(), this.workingDirectory, exitCode, duration, //
getOutputStreamContent(), getErrorStreamContent())
getOutputStreamContent().trim(), getErrorStreamContent().trim())
.toString());
}
LOGGER.finest(() -> "Command '" + formatCommand() + "' finished successfully after " + duration);
Expand All @@ -123,7 +123,7 @@ public String getOutputStreamContent() {
*/
public String getErrorStreamContent() {
try {
return this.errorStreamConsumer.getContent(Duration.ofMillis(500));
return this.errorStreamConsumer.getContent(Duration.ofSeconds(5));
} catch (final InterruptedException exception) {
throw handleInterruptedException(exception);
}
Expand All @@ -132,8 +132,8 @@ public String getErrorStreamContent() {
private void waitForExecutionFinished(final Duration executionTimeout) {
try {
if (!this.process.waitFor(executionTimeout.toMillis(), TimeUnit.MILLISECONDS)) {
final String outputStreamContentUntilNow = this.outputStreamConsumer.getCurrentContent();
final String errorStreamContentUntilNow = this.errorStreamConsumer.getCurrentContent();
final String outputStreamContentUntilNow = this.outputStreamConsumer.getCurrentContent().trim();
final String errorStreamContentUntilNow = this.errorStreamConsumer.getCurrentContent().trim();
throw new IllegalStateException(ExaError.messageBuilder("E-PK-CORE-128")
.message("Timeout while waiting {{timeout|u}}ms for command {{executed command}}." //
+ " Output was {{std output}}\nError output: {{std error}}", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import com.exasol.errorreporting.ExaError;
import com.exasol.projectkeeper.OsCheck;
import com.exasol.projectkeeper.sources.analyze.generic.CommandExecutor;
import com.exasol.projectkeeper.sources.analyze.generic.ShellCommand;
import com.exasol.projectkeeper.sources.analyze.generic.*;

/**
* Represents an executable go binary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,4 @@ void installDependencies(final Path projectPath) {
.build();
this.executor.execute(sc, projectPath);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.exasol.projectkeeper.sources.analyze.generic;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.time.Duration;
import java.util.List;

import org.junit.jupiter.api.Test;

class SimpleProcessIT {

private static final Duration TIMEOUT = Duration.ofMillis(10);

@Test
void outputStream() {
final SimpleProcess process = SimpleProcess.start(List.of("bash", "-c", "echo output"));
process.waitUntilFinished(TIMEOUT);
assertThat(process.getOutputStreamContent(), equalTo("output\n"));
assertThat(process.getErrorStreamContent(), emptyString());
}

@Test
void errorStream() {
final SimpleProcess process = SimpleProcess.start(List.of("bash", "-c", ">&2 echo error"));
process.waitUntilFinished(TIMEOUT);
assertThat(process.getOutputStreamContent(), emptyString());
assertThat(process.getErrorStreamContent(), equalTo("error\n"));
}

@Test
void outputAndErrorStream() {
final SimpleProcess process = SimpleProcess.start(List.of("bash", "-c", "echo output && >&2 echo error"));
process.waitUntilFinished(TIMEOUT);
assertThat(process.getOutputStreamContent(), equalTo("output\n"));
assertThat(process.getErrorStreamContent(), equalTo("error\n"));
}

@Test
void processFails() {
final SimpleProcess process = SimpleProcess
.start(List.of("bash", "-c", "echo output && >&2 echo error && exit 1"));
final IllegalStateException exception = assertThrows(IllegalStateException.class,
() -> process.waitUntilFinished(TIMEOUT));
assertThat(exception.getMessage(), allOf(startsWith(
"E-PK-CORE-126: Failed to run command 'bash -c echo output && >&2 echo error && exit 1' in <null>, exit code was 1 after PT"),
endsWith("Output:\n'output'\n" + //
"Error output:\n'error'")));
}

@Test
void processTimeout() {
final SimpleProcess process = SimpleProcess
.start(List.of("bash", "-c", "echo output && >&2 echo error && sleep 1"));
final IllegalStateException exception = assertThrows(IllegalStateException.class,
() -> process.waitUntilFinished(TIMEOUT));
assertThat(exception.getMessage(), equalTo(
"E-PK-CORE-128: Timeout while waiting 10ms for command 'bash -c echo output && >&2 echo error && sleep 1'. Output was 'output'\n"
+ "Error output: 'error'"));
}

@Test
void executeFails() {
final List<String> command = List.of("no-such-binary");
final IllegalStateException exception = assertThrows(IllegalStateException.class,
() -> SimpleProcess.start(command));
assertThat(exception.getMessage(), equalTo(
"E-PK-CORE-125: Error executing command 'no-such-binary'. Verify that the 'no-such-binary' executable is on the PATH."));
}
}

0 comments on commit fd356b6

Please sign in to comment.