Commit 284a59dc authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Added a test for checkHostLastSeen

parent f2f591b8
......@@ -16,7 +16,7 @@ import org.fogbowcloud.green.server.core.plugins.openstack.OpenStackInfoPlugin;
public class DefaultGreenStrategy implements GreenStrategy {
private CloudInfoPlugin openStackPlugin;
private List<? extends Host> allHosts;
private List<? extends Host> allWakedHosts;
private List<Host> lostHosts = new LinkedList<Host>();
private List<Host> nappingHosts = new LinkedList<Host>();
private List<Host> sleepingHosts = new LinkedList<Host>();
......@@ -46,38 +46,41 @@ public class DefaultGreenStrategy implements GreenStrategy {
.getProperty("greenstrategy.gracetime"));
this.lostHostTime = Long.parseLong(greenProperties
.getProperty("greenstrategy.lostAgentTime"));
this.allHosts = this.openStackPlugin.getHostInformation();
this.allWakedHosts = this.openStackPlugin.getHostInformation();
}
protected DefaultGreenStrategy(CloudInfoPlugin openStackPlugin,
long graceTime) {
this.openStackPlugin = openStackPlugin;
this.graceTime = graceTime;
this.allHosts = this.openStackPlugin.getHostInformation();
this.allWakedHosts = this.openStackPlugin.getHostInformation();
}
protected void setAllHosts() {
List<Host> nowHosts = new LinkedList<Host>();
nowHosts.addAll(this.allHosts);
this.allHosts = this.openStackPlugin.getHostInformation();
nowHosts.addAll(this.allWakedHosts);
this.allWakedHosts = this.openStackPlugin.getHostInformation();
/*
* Solution for eliminating hosts that don't send an
* "I am alive signal" but still are in the cloud information
*/
for (Host host : this.allHosts) {
if (!nowHosts.contains(host)) {
this.allHosts.remove(host);
if (!this.lostHosts.contains(host)) {
for (Host host : this.allWakedHosts) {
if (! nowHosts.contains(host)) {
this.allWakedHosts.remove(host);
if (! this.lostHosts.contains(host)) {
this.lostHosts.add(host);
}
if(this.nappingHosts.contains(host)){
this.nappingHosts.remove(host);
}
}
}
/*
* Solution for not loosing data when it is updated
*/
for (Host host : this.allHosts) {
for (Host host : this.allWakedHosts) {
Host fullHost = nowHosts.get(nowHosts.indexOf(host));
host.setIp(fullHost.getIp());
host.setJid(fullHost.getJid());
......@@ -106,6 +109,10 @@ public class DefaultGreenStrategy implements GreenStrategy {
public List<Host> getSleepingHosts() {
return sleepingHosts;
}
public List<? extends Host> getAllWakedHosts() {
return allWakedHosts;
}
public void receiveIamAliveInfo(String hostName, String jid, String ip,
String macAddress) {
......@@ -117,7 +124,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
}
for (Host host : this.allHosts) {
for (Host host : this.allWakedHosts) {
if (host.getName() == hostName) {
host.setJid(jid);
host.setIp(ip);
......@@ -129,7 +136,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
public void sendIdleHostsToBed() {
this.setAllHosts();
for (Host host : this.allHosts) {
for (Host host : this.allWakedHosts) {
if (host.isNovaEnable() && host.isNovaRunning()
&& (host.getRunningVM() == 0)) {
if (!this.getNappingHosts().contains(host)) {
......@@ -144,29 +151,36 @@ public class DefaultGreenStrategy implements GreenStrategy {
*/
if (nowTime - host.getNappingSince() > this.graceTime) {
scc.sendIdleHostToBed(host.getMacAddress());
this.getSleepingHosts().add(host);
this.getNappingHosts().remove(host);
this.sleepingHosts.add(host);
}
}
}
}
}
for (Host host : sleepingHosts) {
if (this.allWakedHosts.contains(host)){
this.allWakedHosts.remove(host);
}
if (this.nappingHosts.contains(host)) {
this.nappingHosts.remove(host);
}
}
}
public void checkHostsLastSeen() {
for (Host host : this.allHosts) {
if (host.getLastSeen() - this.lastUpdatedTime.getTime() > this.lostHostTime) {
allHosts.remove(host);
if (this.sleepingHosts.contains(host)) {
this.sleepingHosts.remove(host);
}
for (Host host : this.allWakedHosts) {
if (this.lastUpdatedTime.getTime() - host.getLastSeen() > this.lostHostTime) {
if (this.nappingHosts.contains(host)) {
this.nappingHosts.remove(host);
}
this.lostHosts.add(host);
}
}
for (Host host: this.lostHosts) {
if(this.allWakedHosts.contains(host)) {
this.allWakedHosts.remove(host);
}
}
}
public void wakeUpSleepingHost(int minCPU, int minRAM) {
......
......@@ -19,22 +19,35 @@ public class TestDefaultGreenStrategy {
Mockito.when(osip.getHostInformation()).thenReturn(hosts);
return osip;
}
private Date createDateMock(long Time){
private Date createDateMock(long Time) {
Date date = Mockito.mock(Date.class);
Mockito.when(date.getTime()).thenReturn(Time);
return date;
return date;
}
@Test
public void testReceivingLostHostsFromCloud(){
@Test
public void testCheckHostLastSeen() {
Host lost = new Host ("lost", 0, true, true, 0, 0, 0);
lost.setLastSeen(0);
Host still = new Host ("lost", 0, true, true, 0, 0, 0);
still.setLastSeen(1500000);
List<Host> hosts = new LinkedList<Host>();
hosts.add(lost);
hosts.add(still);
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
Date date = this.createDateMock(1500001);
dgs.setDate(date);
dgs.setLostHostTime(1500000);
dgs.checkHostsLastSeen();
Assert.assertEquals(1, dgs.getAllWakedHosts().size());
}
@Test
public void testOneHostNapping(){
Host napping = new Host ("host1", 0, true, true, 0, 0, 0);
List <Host> hosts = new LinkedList <Host> ();
@Test
public void testOneHostNapping() {
Host napping = new Host("host1", 0, true, true, 0, 0, 0);
List<Host> hosts = new LinkedList<Host>();
hosts.add(napping);
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
......@@ -43,15 +56,16 @@ public class TestDefaultGreenStrategy {
dgs.sendIdleHostsToBed();
Assert.assertEquals(1, dgs.getNappingHosts().size());
}
@Test
public void testOneHostSleeping(){
Host h1 = new Host ("host1", 0, true, true, 1800000,0,0);
public void testOneHostSleeping() {
Host h1 = new Host("host1", 0, true, true, 1800000, 0, 0);
h1.setMacAddress("mac");
List <Host> hosts = new LinkedList <Host> ();
List<Host> hosts = new LinkedList<Host>();
hosts.add(h1);
Date date = this.createDateMock(3600001);
ServerCommunicationComponent gscc = Mockito.mock(ServerCommunicationComponent.class);
ServerCommunicationComponent gscc = Mockito
.mock(ServerCommunicationComponent.class);
Mockito.doNothing().when(gscc).wakeUpHost(h1.getMacAddress());
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
......@@ -63,10 +77,10 @@ public class TestDefaultGreenStrategy {
Assert.assertEquals(1, dgs.getSleepingHosts().size());
Assert.assertEquals(0, dgs.getNappingHosts().size());
}
@Test
public void testNoHosts(){
List <Host> hosts = new LinkedList <Host> ();
@Test
public void testNoHosts() {
List<Host> hosts = new LinkedList<Host>();
Date date = this.createDateMock(3600001);
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
......@@ -115,32 +129,32 @@ public class TestDefaultGreenStrategy {
.getSleepingHosts().toArray());
}
@Test
public void testNoHostSleeping(){
List <Host> hosts = new LinkedList <Host> ();
public void testNoHostSleeping() {
List<Host> hosts = new LinkedList<Host>();
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
dgs.sendIdleHostsToBed();
dgs.sendIdleHostsToBed();
Assert.assertEquals(0, dgs.getSleepingHosts().size());
}
@Test
public void testMultipleWakableHosts(){
Host mustWake = new Host ("wake", 0, true, true, 1800000, 3, 8);
Host mustWake2 = new Host ("stil", 0, true, true, 1800000, 3, 5);
List <Host> hosts = new LinkedList <Host> ();
public void testMultipleWakableHosts() {
Host mustWake = new Host("wake", 0, true, true, 1800000, 3, 8);
Host mustWake2 = new Host("stil", 0, true, true, 1800000, 3, 5);
List<Host> hosts = new LinkedList<Host>();
hosts.add(mustWake);
hosts.add(mustWake2);
Date date = this.createDateMock(3600001);
ServerCommunicationComponent gscc = Mockito.mock(ServerCommunicationComponent.class);
ServerCommunicationComponent gscc = Mockito
.mock(ServerCommunicationComponent.class);
Mockito.doNothing().when(gscc).wakeUpHost("wake");
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
dgs.setDate(date);
dgs.setCommunicationComponent(gscc);
......@@ -149,8 +163,8 @@ public class TestDefaultGreenStrategy {
mustWake2.setNappingSince(1800000);
dgs.sendIdleHostsToBed();
dgs.wakeUpSleepingHost(2, 4);
Assert.assertEquals(1, dgs.getSleepingHosts().size());
}
}
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