mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Script cleanup
- new negative tests - some copy-paste replacement in the code Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
This commit is contained in:
parent
6617187193
commit
5f800f2d22
@ -19,11 +19,6 @@
|
|||||||
|
|
||||||
package com.cloud.utils.script;
|
package com.cloud.utils.script;
|
||||||
|
|
||||||
import com.cloud.utils.PropertiesUtil;
|
|
||||||
import com.cloud.utils.concurrency.NamedThreadFactory;
|
|
||||||
import com.cloud.utils.script.OutputInterpreter.TimedOutLogger;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@ -43,6 +38,13 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import com.cloud.utils.PropertiesUtil;
|
||||||
|
import com.cloud.utils.concurrency.NamedThreadFactory;
|
||||||
|
import com.cloud.utils.script.OutputInterpreter.TimedOutLogger;
|
||||||
|
|
||||||
public class Script implements Callable<String> {
|
public class Script implements Callable<String> {
|
||||||
private static final Logger s_logger = Logger.getLogger(Script.class);
|
private static final Logger s_logger = Logger.getLogger(Script.class);
|
||||||
|
|
||||||
@ -167,6 +169,16 @@ public class Script implements Callable<String> {
|
|||||||
return buildCommandLine(command);
|
return buildCommandLine(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String stackTraceAsString(Throwable throwable) {
|
||||||
|
//TODO: a StringWriter is bit to heavy weight
|
||||||
|
try(StringWriter out = new StringWriter(); PrintWriter writer = new PrintWriter(out);) {
|
||||||
|
throwable.printStackTrace(writer);
|
||||||
|
return out.toString();
|
||||||
|
} catch (IOException e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public String execute(OutputInterpreter interpreter) {
|
public String execute(OutputInterpreter interpreter) {
|
||||||
String[] command = _command.toArray(new String[_command.size()]);
|
String[] command = _command.toArray(new String[_command.size()]);
|
||||||
|
|
||||||
@ -259,28 +271,15 @@ public class Script implements Callable<String> {
|
|||||||
return error;
|
return error;
|
||||||
} catch (SecurityException ex) {
|
} catch (SecurityException ex) {
|
||||||
_logger.warn("Security Exception....not running as root?", ex);
|
_logger.warn("Security Exception....not running as root?", ex);
|
||||||
StringWriter writer = new StringWriter();
|
return stackTraceAsString(ex);
|
||||||
ex.printStackTrace(new PrintWriter(writer));
|
|
||||||
return writer.toString();
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
_logger.warn("Exception: " + buildCommandLine(command), ex);
|
_logger.warn("Exception: " + buildCommandLine(command), ex);
|
||||||
StringWriter writer = new StringWriter();
|
return stackTraceAsString(ex);
|
||||||
ex.printStackTrace(new PrintWriter(writer));
|
|
||||||
return writer.toString();
|
|
||||||
} finally {
|
} finally {
|
||||||
if (_process != null) {
|
if (_process != null) {
|
||||||
try {
|
IOUtils.closeQuietly(_process.getErrorStream());
|
||||||
_process.getErrorStream().close();
|
IOUtils.closeQuietly(_process.getOutputStream());
|
||||||
} catch (IOException ex) {
|
IOUtils.closeQuietly(_process.getInputStream());
|
||||||
}
|
|
||||||
try {
|
|
||||||
_process.getOutputStream().close();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
_process.getInputStream().close();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
}
|
|
||||||
_process.destroy();
|
_process.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -318,23 +317,15 @@ public class Script implements Callable<String> {
|
|||||||
try {
|
try {
|
||||||
result = interpreter.interpret(reader);
|
result = interpreter.interpret(reader);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
StringWriter writer = new StringWriter();
|
result = stackTraceAsString(ex);
|
||||||
ex.printStackTrace(new PrintWriter(writer));
|
|
||||||
result = writer.toString();
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
StringWriter writer = new StringWriter();
|
result = stackTraceAsString(ex);
|
||||||
ex.printStackTrace(new PrintWriter(writer));
|
|
||||||
result = writer.toString();
|
|
||||||
} finally {
|
} finally {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
done = true;
|
done = true;
|
||||||
notifyAll();
|
notifyAll();
|
||||||
}
|
}
|
||||||
try {
|
IOUtils.closeQuietly(reader);
|
||||||
reader.close();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
}
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,9 @@
|
|||||||
|
|
||||||
package com.cloud.utils;
|
package com.cloud.utils;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.commons.lang.SystemUtils;
|
import org.apache.commons.lang.SystemUtils;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@ -68,7 +71,8 @@ public class ScriptTest {
|
|||||||
script.add("foo");
|
script.add("foo");
|
||||||
script.add("bar", "baz");
|
script.add("bar", "baz");
|
||||||
script.set("blah", "blah");
|
script.set("blah", "blah");
|
||||||
Assert.assertEquals("/bin/echo foo bar baz blah blah ", script.toString());
|
Assert.assertEquals("/bin/echo foo bar baz blah blah ",
|
||||||
|
script.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -86,13 +90,40 @@ public class ScriptTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRunSimpleBashScript() {
|
public void testRunSimpleBashScript() {
|
||||||
Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
|
Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
|
||||||
Assert.assertEquals("hello world!", Script.runSimpleBashScript("echo 'hello world!'"));
|
Assert.assertEquals("hello world!",
|
||||||
|
Script.runSimpleBashScript("echo 'hello world!'"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void executeWithOutputInterpreter() {
|
||||||
|
Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
|
||||||
|
Script script = new Script("/bin/bash");
|
||||||
|
script.add("-c");
|
||||||
|
script.add("echo 'hello world!'");
|
||||||
|
String value = script.execute(new OutputInterpreter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String interpret(BufferedReader reader) throws IOException {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// it is a stack trace in this case as string
|
||||||
|
Assert.assertNotNull(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void runSimpleBashScriptNotExisting() {
|
||||||
|
Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
|
||||||
|
String output = Script.runSimpleBashScript("/not/existing/scripts/"
|
||||||
|
+ System.currentTimeMillis());
|
||||||
|
Assert.assertNull(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRunSimpleBashScriptWithTimeout() {
|
public void testRunSimpleBashScriptWithTimeout() {
|
||||||
Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
|
Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
|
||||||
Assert.assertEquals("hello world!", Script.runSimpleBashScript("echo 'hello world!'", 1000));
|
Assert.assertEquals("hello world!",
|
||||||
|
Script.runSimpleBashScript("echo 'hello world!'", 1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user