Skip to content
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

feat (jkube-kit/common) : Add workDir configuration option in ExternalCommand #2308

Merged
Merged
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
Expand Up @@ -15,6 +15,7 @@

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -35,13 +36,19 @@
*/
public abstract class ExternalCommand {
protected final KitLogger log;
private final File workDir;

private final ExecutorService executor = Executors.newFixedThreadPool(2);

private int statusCode;

protected ExternalCommand(KitLogger log) {
this(log, null);
}

protected ExternalCommand(KitLogger log, File dir) {
this.log = log;
this.workDir = dir;
}

public void execute() throws IOException {
Expand Down Expand Up @@ -109,7 +116,7 @@ private void inputStreamPump(OutputStream outputStream, String processInput) {

private Process startProcess() throws IOException {
try {
return Runtime.getRuntime().exec(getArgs());
return Runtime.getRuntime().exec(getArgs(), null, workDir);
} catch (IOException e) {
throw new IOException(String.format("Failed to start '%s' : %s",
getCommandAsString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIOException;

class ExternalCommandTest {
private final KitLogger kitLogger = new KitLogger.SilentLogger();

@Test
void execute_whenCommandCompletedSuccessfully_thenPrintResult() throws IOException {
// Given
TestCommand testCommand = new TestCommand(kitLogger, new String[] {"echo", "hello"});

// When
testCommand.execute();

// Then
assertThat(testCommand.getResult()).isEqualTo("hello");
}

@Test
void execute_whenCommandFailed_thenThrowException() {
// Given
TestCommand testCommand = new TestCommand(kitLogger, new String[] {"ls", "idontexist"});

// When + Then
assertThatIOException()
.isThrownBy(testCommand::execute)
.withMessage("Process 'ls idontexist' exited with status 2");
}

@Test
void execute_whenWorkDirProvided_thenUseWorkDir(@TempDir File temporaryFolder) throws IOException {
// Given
TestCommand testCommand = new TestCommand(kitLogger, new String[] {"touch", "foo"}, temporaryFolder);

// When
testCommand.execute();

// Then
assertThat(new File(temporaryFolder, "foo")).exists();
}


private static class TestCommand extends ExternalCommand {
private String result;
private final String[] args;
public TestCommand(KitLogger kitLogger, String[] args) {
this(kitLogger, args, null);
}

public TestCommand(KitLogger kitLogger, String[] args, File dir) {
super(kitLogger, dir);
this.args = args;
}

@Override
protected String[] getArgs() {
return args;
}

@Override
protected void processLine(String line) {
result = line;
}

public String getResult() {
return result;
}
}
}