Commit 36533d6d authored by marcosnobrega's avatar marcosnobrega
Browse files

tests fixed for openstack4j 2.0.2

parent d0ffb17b
......@@ -77,7 +77,7 @@
<dependency>
<groupId>org.pacesys</groupId>
<artifactId>openstack4j</artifactId>
<version>2.0.0-SNAPSHOT</version>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
......
......@@ -6,6 +6,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import org.jamppa.client.XMPPClient;
import org.jamppa.client.plugin.xep0077.XEP0077;
......@@ -13,9 +14,9 @@ import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.IQ.Type;
public class AgentCommunicationComponent {
......@@ -81,14 +82,18 @@ public class AgentCommunicationComponent {
if (!from.toString().equals(prop.getProperty("xmpp.component"))) {
return false;
}
try {
String ns = packet.getElement().element("query")
.getNamespaceURI();
if (!ns.equals("org.fogbowcloud.green.GoToBed")) {
return false;
}
} catch (Exception e) {
LOGGER.info("There not a query element in the packet", e);
if (packet.getError() != null) {
LOGGER.warn("IAmAlive packet returned an error: " + packet.toXML());
return false;
}
Element queryEl = packet.getElement().element("query");
if (queryEl == null) {
LOGGER.info("There is no query element in the response packet");
return false;
}
String ns = queryEl.getNamespaceURI();
if (!ns.equals("org.fogbowcloud.green.GoToBed")) {
LOGGER.info("Query element has a different namespace: " + ns);
return false;
}
......@@ -110,7 +115,8 @@ public class AgentCommunicationComponent {
this.prop.getProperty("host.macAddress"));
query.addElement("hostName")
.setText(this.prop.getProperty("host.name"));
LOGGER.info("Sent I am alive signal");
client.getConnection().sendPacket(iq);
LOGGER.info("IAmAlive signal sent to " + iq.getTo());
}
public void start() {
......
......@@ -4,35 +4,41 @@ import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.fogbowcloud.green.server.core.greenStrategy.Host;
import org.fogbowcloud.green.server.core.plugins.CloudInfoPlugin;
import org.openstack4j.api.OSClient;
import org.openstack4j.api.compute.ext.ZoneService;
import org.openstack4j.model.compute.ext.AvailabilityZones.AvailabilityZone;
import org.openstack4j.model.compute.ext.AvailabilityZones.NovaService;
import org.openstack4j.model.compute.ext.AvailabilityZones.ZoneState;
import org.openstack4j.model.compute.ext.AvailabilityZone;
import org.openstack4j.model.compute.ext.AvailabilityZone.NovaService;
import org.openstack4j.model.compute.ext.AvailabilityZone.ZoneState;
import org.openstack4j.model.compute.ext.Hypervisor;
import org.openstack4j.openstack.OSFactory;
public class OpenStackInfoPlugin implements CloudInfoPlugin {
private OSClient os;
private String endpoint;
private String username;
private String password;
private String tenantname;
public OpenStackInfoPlugin(String endpoint, String username,
String password, String tenantname) {
this(OSFactory.builder().endpoint(endpoint)
.credentials(username, password).tenantName(tenantname)
.authenticate());
this.endpoint = endpoint;
this.username = username;
this.password = password;
this.tenantname = tenantname;
}
protected OpenStackInfoPlugin(OSClient os) {
this.os = os;
protected OSClient os() {
return OSFactory.builder().endpoint(endpoint)
.credentials(username, password).tenantName(tenantname)
.authenticate();
}
private List<String> getHostsName() {
List<? extends Hypervisor> hypervisors = os.compute().hypervisors()
List<? extends Hypervisor> hypervisors = os().compute().hypervisors()
.list();
List<String> hostsName = new LinkedList<String>();
for (Hypervisor hypervisor : hypervisors) {
......@@ -42,7 +48,7 @@ public class OpenStackInfoPlugin implements CloudInfoPlugin {
}
private HashMap<String, Integer> getAvailableRam() {
List<? extends Hypervisor> hypervisors = os.compute().hypervisors()
List<? extends Hypervisor> hypervisors = os().compute().hypervisors()
.list();
HashMap<String, Integer> availableRam = new HashMap<String, Integer>();
for (Hypervisor hypervisor : hypervisors) {
......@@ -53,7 +59,7 @@ public class OpenStackInfoPlugin implements CloudInfoPlugin {
}
private HashMap<String, Integer> getAvailableCPU() {
List<? extends Hypervisor> hypervisors = os.compute().hypervisors()
List<? extends Hypervisor> hypervisors = os().compute().hypervisors()
.list();
HashMap<String, Integer> availableCPU = new HashMap<String, Integer>();
for (Hypervisor hypervisor : hypervisors) {
......@@ -64,7 +70,7 @@ public class OpenStackInfoPlugin implements CloudInfoPlugin {
}
private HashMap<String, Integer> getRunningVM() {
List<? extends Hypervisor> hypervisors = os.compute().hypervisors()
List<? extends Hypervisor> hypervisors = os().compute().hypervisors()
.list();
HashMap<String, Integer> runningVM = new HashMap<String, Integer>();
for (Hypervisor hypervisor : hypervisors) {
......@@ -86,25 +92,24 @@ public class OpenStackInfoPlugin implements CloudInfoPlugin {
}
private HashMap<String, NovaHost> getNovaState(List<String> hostsName) {
ZoneService zones = os.compute().zones();
ZoneService zones = os().compute().zones();
HashMap<String, NovaHost> novaRunning = new HashMap<String, NovaHost>();
List<? extends AvailabilityZone> availabilityZoneList = zones
.getAvailabilityZones().getAvailabilityZoneList();
.list();
for (AvailabilityZone availabilityZone : availabilityZoneList) {
ZoneState zoneState = availabilityZone.getZoneState();
if (zoneState.getAvailable()) {
for (String host : hostsName) {
try {
HashMap<String, ? extends NovaService> hostService = availabilityZone
.getHosts().get(host.toLowerCase());
Map<String, Map<String, ? extends NovaService>> hostsInAvailabilityZone = availabilityZone.getHosts();
Map<String, ? extends NovaService> hostService = hostsInAvailabilityZone.get(host.toLowerCase());
NovaService ns = hostService.get("nova-compute");
if (ns != null) {
String active = ns.getStatusActive();
String available = ns.getAvailable();
boolean available = ns.getAvailable();
novaRunning.put(host,
new NovaHost(available.equals("true"),
active.equals("true")));
new NovaHost(available, active.equals("true")));
}
} catch (Exception e) {
// Ignoring exceptions for hosts in unavailable zones
......
package org.fogbowcloud.green.server.core.plugins.openstack;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.openstack4j.model.compute.ext.AvailabilityZones.AvailabilityZone;
import org.openstack4j.model.compute.ext.AvailabilityZones.NovaService;
import org.openstack4j.model.compute.ext.AvailabilityZones.ZoneState;
import org.openstack4j.model.compute.ext.AvailabilityZone;
public class AvailabilityZoneImpl implements AvailabilityZone {
......@@ -16,11 +13,11 @@ public class AvailabilityZoneImpl implements AvailabilityZone {
private static final long serialVersionUID = -2420582290244591446L;
private final ZoneState zoneState;
private final Map<String, HashMap<String, ? extends NovaService>> hosts;
private final Map<String, Map<String, ? extends NovaService>> hosts;
private final String zoneName;
public AvailabilityZoneImpl(ZoneState zoneState,
Map<String, HashMap<String, ? extends NovaService>> hosts,
Map<String, Map<String, ? extends NovaService>> hosts,
String zoneName) {
this.zoneState = zoneState;
this.hosts = hosts;
......@@ -33,7 +30,7 @@ public class AvailabilityZoneImpl implements AvailabilityZone {
}
@Override
public Map<String, HashMap<String, ? extends NovaService>> getHosts() {
public Map<String, Map<String, ? extends NovaService>> getHosts() {
return hosts;
}
......@@ -67,18 +64,18 @@ public class AvailabilityZoneImpl implements AvailabilityZone {
*
*/
private static final long serialVersionUID = 1L;
private String available;
private boolean available;
private String active;
private Date updateTime;
public NovaServiceImpl(String available, String active, Date updateTime) {
public NovaServiceImpl(boolean available, String active, Date updateTime) {
this.available = available;
this.active = active;
this.updateTime = updateTime;
}
@Override
public String getAvailable() {
public boolean getAvailable() {
return available;
}
......
......@@ -118,5 +118,11 @@ public class HypervisorTestImpl implements Hypervisor {
// TODO Auto-generated method stub
return null;
}
@Override
public CPUInfo getCPUInfo() {
// TODO Auto-generated method stub
return null;
}
}
......@@ -5,8 +5,8 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.fogbowcloud.green.server.core.greenStrategy.Host;
import org.fogbowcloud.green.server.core.plugins.openstack.OpenStackInfoPlugin;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
......@@ -16,9 +16,8 @@ import org.openstack4j.api.OSClient;
import org.openstack4j.api.compute.ComputeService;
import org.openstack4j.api.compute.ext.HypervisorService;
import org.openstack4j.api.compute.ext.ZoneService;
import org.openstack4j.model.compute.ext.AvailabilityZones;
import org.openstack4j.model.compute.ext.AvailabilityZones.AvailabilityZone;
import org.openstack4j.model.compute.ext.AvailabilityZones.NovaService;
import org.openstack4j.model.compute.ext.AvailabilityZone;
import org.openstack4j.model.compute.ext.AvailabilityZone.NovaService;
import org.openstack4j.model.compute.ext.Hypervisor;
public class TestOpenStackPlugin {
......@@ -38,27 +37,30 @@ public class TestOpenStackPlugin {
}
});
ZoneService zoneService = Mockito.mock(ZoneService.class);
AvailabilityZones availabilityZones = Mockito
.mock(AvailabilityZones.class);
Mockito.when(compute.zones()).thenReturn(zoneService);
Mockito.when(zoneService.getAvailabilityZones()).thenReturn(
availabilityZones);
Mockito.when(availabilityZones.getAvailabilityZoneList()).thenAnswer(
new Answer<List<AvailabilityZone>>() {
@Override
public List<AvailabilityZone> answer(
InvocationOnMock invocation) throws Throwable {
return zones;
}
});
Mockito.when(zoneService.list()).thenAnswer(new Answer<List<AvailabilityZone>>() {
@Override
public List<AvailabilityZone> answer(InvocationOnMock invocation)
throws Throwable {
return zones;
}
});
return os;
}
private OpenStackInfoPlugin createMockPlugin(final List<Hypervisor> hvs,
final List<AvailabilityZone> zones) {
OSClient osClient = createOSClientMock(new LinkedList<Hypervisor>(),
new LinkedList<AvailabilityZone>());
OpenStackInfoPlugin plugin = Mockito.spy(new OpenStackInfoPlugin(null, null, null, null));
Mockito.when(plugin.os()).thenReturn(osClient);
return plugin;
}
@Test
public void testNoZones() {
OSClient osClient = createOSClientMock(new LinkedList<Hypervisor>(),
new LinkedList<AvailabilityZones.AvailabilityZone>());
OpenStackInfoPlugin plugin = new OpenStackInfoPlugin(osClient);
OpenStackInfoPlugin plugin = createMockPlugin(new LinkedList<Hypervisor>(),
new LinkedList<AvailabilityZone>());
List<Host> hosts = plugin.getHostInformation();
Assert.assertTrue(hosts.isEmpty());
}
......@@ -67,23 +69,22 @@ public class TestOpenStackPlugin {
public void testEmptyZones() {
AvailabilityZoneImpl zone = new AvailabilityZoneImpl(
new AvailabilityZoneImpl.ZoneStateImpl(true),
new HashMap<String, HashMap<String, ? extends NovaService>>(),
new HashMap<String, Map<String, ? extends NovaService>>(),
"Zone");
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZones.AvailabilityZone>();
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZone>();
zones.add(zone);
OSClient osClient = createOSClientMock(new LinkedList<Hypervisor>(),
OpenStackInfoPlugin plugin = createMockPlugin(new LinkedList<Hypervisor>(),
zones);
OpenStackInfoPlugin plugin = new OpenStackInfoPlugin(osClient);
List<Host> hosts = plugin.getHostInformation();
Assert.assertTrue(hosts.isEmpty());
}
@Test
public void testUnavailableZone() {
Map<String, HashMap<String, ? extends NovaService>> servicesPerHost = new HashMap<String, HashMap<String, ? extends NovaService>>();
Map<String, Map<String, ? extends NovaService>> servicesPerHost = new HashMap<String, Map<String, ? extends NovaService>>();
HashMap<String, NovaService> services = new HashMap<String, NovaService>();
services.put("nova-compute", new AvailabilityZoneImpl.NovaServiceImpl(
"true", "true", new Date()));
true, "true", new Date()));
servicesPerHost.put("host1", services);
HypervisorTestImpl hp = new HypervisorTestImpl();
hp.setHostname("host1");
......@@ -93,10 +94,9 @@ public class TestOpenStackPlugin {
AvailabilityZoneImpl zone = new AvailabilityZoneImpl(
new AvailabilityZoneImpl.ZoneStateImpl(false), servicesPerHost,
"Zone");
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZones.AvailabilityZone>();
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZone>();
zones.add(zone);
OSClient osClient = createOSClientMock(hpList, zones);
OpenStackInfoPlugin plugin = new OpenStackInfoPlugin(osClient);
OpenStackInfoPlugin plugin = createMockPlugin(hpList, zones);
List<Host> hosts = plugin.getHostInformation();
Assert.assertTrue(hosts.isEmpty());
}
......@@ -105,23 +105,22 @@ public class TestOpenStackPlugin {
public void testUnavailableAndEmptyZone() {
AvailabilityZoneImpl zone = new AvailabilityZoneImpl(
new AvailabilityZoneImpl.ZoneStateImpl(false),
new HashMap<String, HashMap<String, ? extends NovaService>>(),
new HashMap<String, Map<String, ? extends NovaService>>(),
"Zone");
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZones.AvailabilityZone>();
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZone>();
zones.add(zone);
OSClient osClient = createOSClientMock(new LinkedList<Hypervisor>(),
OpenStackInfoPlugin plugin = createMockPlugin(new LinkedList<Hypervisor>(),
zones);
OpenStackInfoPlugin plugin = new OpenStackInfoPlugin(osClient);
List<Host> hosts = plugin.getHostInformation();
Assert.assertTrue(hosts.isEmpty());
}
@Test
public void testOneHost() {
Map<String, HashMap<String, ? extends NovaService>> servicesPerHost = new HashMap<String, HashMap<String, ? extends NovaService>>();
Map<String, Map<String, ? extends NovaService>> servicesPerHost = new HashMap<String, Map<String, ? extends NovaService>>();
HashMap<String, NovaService> services = new HashMap<String, NovaService>();
services.put("nova-compute", new AvailabilityZoneImpl.NovaServiceImpl(
"true", "true", new Date()));
true, "true", new Date()));
servicesPerHost.put("host1", services);
HypervisorTestImpl hp = new HypervisorTestImpl();
hp.setHostname("host1");
......@@ -131,20 +130,19 @@ public class TestOpenStackPlugin {
AvailabilityZoneImpl zone = new AvailabilityZoneImpl(
new AvailabilityZoneImpl.ZoneStateImpl(true), servicesPerHost,
"Zone");
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZones.AvailabilityZone>();
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZone>();
zones.add(zone);
OSClient osClient = createOSClientMock(hpList, zones);
OpenStackInfoPlugin plugin = new OpenStackInfoPlugin(osClient);
OpenStackInfoPlugin plugin = createMockPlugin(hpList, zones);
List<Host> hosts = plugin.getHostInformation();
Assert.assertEquals(1, hosts.size());
}
@Test
public void testHostWithComputerinDifferentZones() {
Map<String, HashMap<String, ? extends NovaService>> servicesPerHost = new HashMap<String, HashMap<String, ? extends NovaService>>();
Map<String, Map<String, ? extends NovaService>> servicesPerHost = new HashMap<String, Map<String, ? extends NovaService>>();
HashMap<String, NovaService> services = new HashMap<String, NovaService>();
services.put("nova-compute", new AvailabilityZoneImpl.NovaServiceImpl(
"true", "true", new Date()));
true, "true", new Date()));
servicesPerHost.put("host1", services);
HypervisorTestImpl hp = new HypervisorTestImpl();
hp.setHostname("host1");
......@@ -157,11 +155,10 @@ public class TestOpenStackPlugin {
AvailabilityZoneImpl zone2 = new AvailabilityZoneImpl(
new AvailabilityZoneImpl.ZoneStateImpl(true), servicesPerHost,
"Zone");
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZones.AvailabilityZone>();
LinkedList<AvailabilityZone> zones = new LinkedList<AvailabilityZone>();
zones.add(zone);
zones.add(zone2);
OSClient osClient = createOSClientMock(hpList, zones);
OpenStackInfoPlugin plugin = new OpenStackInfoPlugin(osClient);
OpenStackInfoPlugin plugin = createMockPlugin(hpList, zones);
List<Host> hosts = plugin.getHostInformation();
Assert.assertEquals(1, hosts.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