Skip to content

Commit

Permalink
Implementing Keywords ExecuteBash and ExecuteCmd
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromecomte committed Jun 22, 2020
1 parent 5a2c5ee commit 451a870
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;

import ch.exense.commons.processes.ManagedProcess;
import ch.exense.commons.processes.ManagedProcess.ManagedProcessException;
import step.grid.io.Attachment;
import step.grid.io.AttachmentHelper;
import step.handlers.javahandler.AbstractKeyword;
Expand Down Expand Up @@ -67,10 +69,21 @@ protected void executeManagedCommand(String cmd, int timeoutMs, OutputConfigurat
executeManagedCommand(cmd, timeoutMs, outputConfiguration, null);
}

protected void executeManagedCommand(List<String> cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer<ManagedProcess> postProcess) throws Exception {
ManagedProcess process = new ManagedProcess(cmd);
executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process);
}

protected void executeManagedCommand(String cmd, int timeoutMs, OutputConfiguration outputConfiguration, Consumer<ManagedProcess> postProcess) throws Exception {
boolean hasError = false;
ManagedProcess process = new ManagedProcess(cmd);
executeManagedCommand(timeoutMs, outputConfiguration, postProcess, process);
}

protected void executeManagedCommand(int timeoutMs, OutputConfiguration outputConfiguration,
Consumer<ManagedProcess> postProcess, ManagedProcess process)
throws ManagedProcessException, InterruptedException, IOException {
try {
boolean hasError = false;
process.start();
try {
int exitCode = process.waitFor(timeoutMs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.exense.step.library.kw.system;

import java.util.ArrayList;

import step.handlers.javahandler.Keyword;

public class ProcessKeywords extends AbstractProcessKeyword {
Expand All @@ -23,6 +25,37 @@ public void executeSystemCommand() throws Exception {
readInputs();
executeManagedCommand(command, timeoutInMillis, outputConfiguration);
}

@Keyword(name = "ExecuteBash", schema = "{\"properties\":{\"" + TIMEOUT_MS + "\":{\"type\":\"string\"},"
+ "\"" + MAX_OUTPUT_PAYLOAD_SIZE + "\":{\"type\":\"string\"},\""
+ MAX_OUTPUT_ATTACHMENT_SIZE + "\":{\"type\":\"string\"},\""
+ CHECK_EXIT_CODE + "\":{\"type\":\"boolean\"},"
+ "\"" + COMMAND + "\":{\"type\":\"string\"}},\"required\":[\"" + COMMAND + "\"]}")
public void executeBashCommand() throws Exception {
readInputs();

ArrayList<String> cmd = new ArrayList<String>();
cmd.add("bash");
cmd.add("-c");
cmd.add(command);
executeManagedCommand(cmd, timeoutInMillis, outputConfiguration, null);
}


@Keyword(name = "ExecuteCmd", schema = "{\"properties\":{\"" + TIMEOUT_MS + "\":{\"type\":\"string\"},"
+ "\"" + MAX_OUTPUT_PAYLOAD_SIZE + "\":{\"type\":\"string\"},\""
+ MAX_OUTPUT_ATTACHMENT_SIZE + "\":{\"type\":\"string\"},\""
+ CHECK_EXIT_CODE + "\":{\"type\":\"boolean\"},"
+ "\"" + COMMAND + "\":{\"type\":\"string\"}},\"required\":[\"" + COMMAND + "\"]}")
public void executeCmdCommand() throws Exception {
readInputs();

ArrayList<String> cmd = new ArrayList<String>();
cmd.add("cmd");
cmd.add("/C");
cmd.add(command);
executeManagedCommand(cmd, timeoutInMillis, outputConfiguration, null);
}

protected void readInputs() {
command = input.getString(COMMAND,"");
Expand Down

0 comments on commit 451a870

Please sign in to comment.