Commit 8a069158 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Added tests for turnOff class

parent 76d1d64f
......@@ -9,21 +9,36 @@ public class TurnOff {
private static final Logger LOGGER = Logger.getLogger(TurnOff.class);
private static final String DEFAULT_SUSPEND_COMMAND = "pm-suspend";
private Properties prop;
private ProcessBuilder pb;
public TurnOff(Properties prop) {
this.prop = prop;
}
public void suspend() {
protected ProcessBuilder getPb() {
return pb;
}
protected void startTurnOff() {
String command = prop.getProperty("green.TurnOffCommand");
if (command == null || command.isEmpty()){
command = DEFAULT_SUSPEND_COMMAND;
} else if (!command.equals("pm-hibernate") &&
!command.equals("pm-powersave")) {
LOGGER.warn("this comand may not be in our sudoers file, "
+ "it will not be executed as sudo");
pb = new ProcessBuilder(command);
return;
}
pb = new ProcessBuilder("sudo", "-S", command);
}
ProcessBuilder pb = new ProcessBuilder("sudo", "-S", command);
public void suspend() {
startTurnOff();
try {
Process process = pb.start();
process.waitFor();
System.out.println(process.getInputStream().read());
} catch (Exception e) {
LOGGER.warn("It was not possible to turn down this host", e);
}
......
package org.fogbowcloud.green.agent;
import java.util.Properties;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
public class TestTurnOff {
@Test
public void testTurnDownCommandIsNull() {
Properties prop = Mockito.mock(Properties.class);
TurnOff turnOff = new TurnOff(prop);
turnOff.startTurnOff();
Assert.assertEquals("sudo", turnOff.getPb().command().get(0));
Assert.assertEquals("-S", turnOff.getPb().command().get(1));
Assert.assertEquals("pm-suspend", turnOff.getPb().command().get(2));
}
@Test
public void testTurnDownCommandIsSettedandASudoOne() {
Properties prop = Mockito.mock(Properties.class);
Mockito.doReturn("pm-hibernate").when(prop)
.getProperty("green.TurnOffCommand");
TurnOff turnOff = new TurnOff(prop);
turnOff.startTurnOff();
Assert.assertEquals("sudo", turnOff.getPb().command().get(0));
Assert.assertEquals("-S", turnOff.getPb().command().get(1));
Assert.assertEquals("pm-hibernate", turnOff.getPb().command().get(2));
}
@Test
public void testTurnDownCommandisNotInSudoers() {
Properties prop = Mockito.mock(Properties.class);
Mockito.doReturn("other-suspend-command").when(prop)
.getProperty("green.TurnOffCommand");
TurnOff turnOff = new TurnOff(prop);
turnOff.startTurnOff();
Assert.assertEquals("other-suspend-command",
turnOff.getPb().command().get(0));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment