Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
fogbow
fogbow-green-sitter
Commits
8a069158
Commit
8a069158
authored
Apr 08, 2015
by
aninhacostaribeiro
Browse files
Added tests for turnOff class
parent
76d1d64f
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/fogbowcloud/green/agent/TurnOff.java
View file @
8a069158
...
...
@@ -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
);
}
...
...
src/test/java/org/fogbowcloud/green/agent/TestTurnOff.java
0 → 100644
View file @
8a069158
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
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment