From e4a89537aff6c45d2ac60cfc383e0711df11b6d9 Mon Sep 17 00:00:00 2001 From: Michal Szynkiewicz Date: Sat, 12 Sep 2020 21:37:30 +0200 Subject: [PATCH] NCL-6053: pig - .bacon dir in a temp loc --- .../org/jboss/pnc/bacon/test/AbstractTest.java | 16 +++++++++++----- .../org/jboss/pnc/bacon/test/CLIExecutor.java | 8 +++++--- .../pnc/bacon/test/pig/PigFunctionalTest.java | 7 +++++-- .../org/jboss/pnc/bacon/test/pig/PigTest.java | 5 +++-- .../org/jboss/pnc/bacon/pig/impl/PigContext.java | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/integration-tests/src/test/java/org/jboss/pnc/bacon/test/AbstractTest.java b/integration-tests/src/test/java/org/jboss/pnc/bacon/test/AbstractTest.java index b287cd98a..709186624 100644 --- a/integration-tests/src/test/java/org/jboss/pnc/bacon/test/AbstractTest.java +++ b/integration-tests/src/test/java/org/jboss/pnc/bacon/test/AbstractTest.java @@ -14,6 +14,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.nio.file.Path; import java.util.Base64; import java.util.Random; @@ -64,28 +65,33 @@ protected static String getRandomString() { } protected void execute(String... args) { - ExecutionResult result = executor.runCommand(args); + ExecutionResult result = executor.runCommand(null, args); assertThat(result.getOutput()).isEmpty(); assertThat(result.getError()).isEmpty(); assertThat(result.getRetval()).isZero(); } protected T executeAndDeserialize(Class clazz, String... args) throws JsonProcessingException { - ExecutionResult result = executor.runCommand(args); + ExecutionResult result = executor.runCommand(null, args); log.debug("stderr:{}{}", System.lineSeparator(), result.getError()); assertThat(result.getRetval()).isZero(); return result.fromYAML(clazz); } - protected T executeAndDeserializeJSON(Class clazz, String... args) throws JsonProcessingException { - ExecutionResult result = executor.runCommand(args); + protected T executeAndDeserializeJSON(Class clazz, Path workingDir, String... args) + throws JsonProcessingException { + ExecutionResult result = executor.runCommand(workingDir, args); log.debug("stderr:{}{}", System.lineSeparator(), result.getError()); assertThat(result.getRetval()).isZero(); return result.fromYAML(clazz); } protected ExecutionResult executeAndGetResult(String... args) { - return executor.runCommand(args); + return executor.runCommand(null, args); + } + + protected ExecutionResult executeAndGetResult(Path workingDir, String... args) { + return executor.runCommand(workingDir, args); } } diff --git a/integration-tests/src/test/java/org/jboss/pnc/bacon/test/CLIExecutor.java b/integration-tests/src/test/java/org/jboss/pnc/bacon/test/CLIExecutor.java index 356ac6fa7..7608f3ee8 100644 --- a/integration-tests/src/test/java/org/jboss/pnc/bacon/test/CLIExecutor.java +++ b/integration-tests/src/test/java/org/jboss/pnc/bacon/test/CLIExecutor.java @@ -3,6 +3,7 @@ import org.jboss.pnc.bacon.common.Constant; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -22,9 +23,9 @@ */ public class CLIExecutor { private static final Path BACON_JAR = Paths.get("..", "cli", "target", "bacon.jar").toAbsolutePath().normalize(); - public static final Path CONFIG_LOCATION = Paths.get("target", "test-config"); + public static final Path CONFIG_LOCATION = Paths.get("target", "test-config").toAbsolutePath().normalize(); - public ExecutionResult runCommand(String... args) { + public ExecutionResult runCommand(Path workingDirectory, String... args) { try { checkExecutable(); @@ -38,7 +39,8 @@ public ExecutionResult runCommand(String... args) { System.out.println( "Running command: " + Arrays.stream(cmdarray).collect(Collectors.joining("' '", "'", "'")) + "\n\twith env " + Arrays.toString(env)); - Process process = Runtime.getRuntime().exec(cmdarray, env); + File workingDirectoryFile = workingDirectory != null ? workingDirectory.toFile() : null; + Process process = Runtime.getRuntime().exec(cmdarray, env, workingDirectoryFile); CompletableFuture output = CompletableFuture .supplyAsync(() -> readInputStream(process.getInputStream())); diff --git a/integration-tests/src/test/java/org/jboss/pnc/bacon/test/pig/PigFunctionalTest.java b/integration-tests/src/test/java/org/jboss/pnc/bacon/test/pig/PigFunctionalTest.java index e958ee26b..a60745888 100644 --- a/integration-tests/src/test/java/org/jboss/pnc/bacon/test/pig/PigFunctionalTest.java +++ b/integration-tests/src/test/java/org/jboss/pnc/bacon/test/pig/PigFunctionalTest.java @@ -2,7 +2,6 @@ import org.apache.commons.lang3.RandomStringUtils; import org.jboss.pnc.bacon.common.Constant; -import org.jboss.pnc.bacon.common.exception.FatalException; import org.jboss.pnc.bacon.config.Config; import org.jboss.pnc.bacon.config.PigConfig; import org.jboss.pnc.bacon.config.Validate; @@ -15,6 +14,8 @@ import java.nio.file.Path; import java.util.Optional; +import static org.jboss.pnc.bacon.common.Constant.PIG_CONTEXT_DIR; + @Tag(TestType.REAL_SERVICE_ONLY) public abstract class PigFunctionalTest { static final String emptyNameBase1 = "michalszynkiewicz-et-%s"; @@ -29,13 +30,15 @@ static String init(Path configDir, boolean clean, Optional releaseStorag String suffix = prepareSuffix(); // todo release storage url mocking if (configDir == null) { - throw new FatalException("You need to specify the configuration directory!"); + throw new RuntimeException("You need to specify the configuration directory!"); } + System.setProperty(PIG_CONTEXT_DIR, targetDirectory.toString()); // validate the PiG config PigConfig pig = Config.instance().getActiveProfile().getPig(); if (pig == null) { throw new Validate.ConfigMissingException("Pig configuration missing"); } + pig.validate(); PigContext.init(clean, configDir, targetDirectory.toAbsolutePath().toString(), releaseStorageUrl); diff --git a/integration-tests/src/test/java/org/jboss/pnc/bacon/test/pig/PigTest.java b/integration-tests/src/test/java/org/jboss/pnc/bacon/test/pig/PigTest.java index 8b0353042..fa1fda1f3 100644 --- a/integration-tests/src/test/java/org/jboss/pnc/bacon/test/pig/PigTest.java +++ b/integration-tests/src/test/java/org/jboss/pnc/bacon/test/pig/PigTest.java @@ -19,6 +19,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.io.TempDir; import java.io.IOException; import java.nio.file.Files; @@ -68,7 +69,7 @@ class PigTest extends AbstractTest { @Test @Order(1) - void shouldCreateProduct() throws IOException { + void shouldCreateProduct(@TempDir Path workingDir) throws IOException { final Path configFile = CONFIG_LOCATION; replaceSuffixInConfigFile(configFile.resolve("build-config.yaml")); @@ -143,7 +144,7 @@ void shouldCreateProduct() throws IOException { .post(GROUP_CONFIG_BUILD_CONFIGS.apply(UNIVERSAL_ID)) .then() .getEntity(GROUP_CONFIG, groupConfigWithBuildConfig); - ExecutionResult output = executeAndGetResult("pig", "configure", configFile.toString()); + ExecutionResult output = executeAndGetResult(workingDir, "pig", "configure", configFile.toString()); assertThat(output.getOutput()).contains("name: \"Product Foobar " + SUFFIX + "\""); } diff --git a/pig/src/main/java/org/jboss/pnc/bacon/pig/impl/PigContext.java b/pig/src/main/java/org/jboss/pnc/bacon/pig/impl/PigContext.java index 90a6f451c..0048fcec6 100644 --- a/pig/src/main/java/org/jboss/pnc/bacon/pig/impl/PigContext.java +++ b/pig/src/main/java/org/jboss/pnc/bacon/pig/impl/PigContext.java @@ -196,7 +196,7 @@ private static PigContext readContext(boolean clean, Path configDir) { String sha = hashDirectory(configDir); PigContext result; - String ctxLocationEnv = System.getenv(PIG_CONTEXT_DIR); + String ctxLocationEnv = System.getProperty(PIG_CONTEXT_DIR, System.getenv(PIG_CONTEXT_DIR)); Path contextDir = ctxLocationEnv == null ? Paths.get(".bacon") : Paths.get(ctxLocationEnv); Path contextJson = contextDir.resolve("pig-context.json"); if (!clean && Files.exists(contextJson)) {