mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-02 11:52:28 +01:00
Mock Scanner, instead of scan the computer running the test. (#3173)
* Mock Scanner, instead of scan the computer running the test. This allows non linux machines to run the tests without scanning for a non existing /proc/meminfo. * test fixes on 'other' platforms libvirt wrapper unit tests (#3)
This commit is contained in:
parent
379d779a72
commit
8f7b27bbdc
@ -30,6 +30,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
import java.util.UUID;
|
||||
import java.util.Vector;
|
||||
|
||||
@ -46,7 +47,7 @@ import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.CpuTuneDef;
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.joda.time.Duration;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.libvirt.Connect;
|
||||
@ -64,7 +65,9 @@ import org.mockito.Matchers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
@ -186,7 +189,8 @@ import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(value = {MemStat.class})
|
||||
public class LibvirtComputingResourceTest {
|
||||
|
||||
@Mock
|
||||
@ -198,6 +202,19 @@ public class LibvirtComputingResourceTest {
|
||||
|
||||
String hyperVisorType = "kvm";
|
||||
Random random = new Random();
|
||||
final String memInfo = "MemTotal: 5830236 kB\n" +
|
||||
"MemFree: 156752 kB\n" +
|
||||
"Buffers: 326836 kB\n" +
|
||||
"Cached: 2606764 kB\n" +
|
||||
"SwapCached: 0 kB\n" +
|
||||
"Active: 4260808 kB\n" +
|
||||
"Inactive: 949392 kB\n";
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
Scanner scanner = new Scanner(memInfo);
|
||||
PowerMockito.whenNew(Scanner.class).withAnyArguments().thenReturn(scanner);
|
||||
}
|
||||
|
||||
/**
|
||||
This test tests if the Agent can handle a vmSpec coming
|
||||
@ -424,10 +441,11 @@ public class LibvirtComputingResourceTest {
|
||||
public void testGetNicStats() {
|
||||
//this test is only working on linux because of the loopback interface name
|
||||
//also the tested code seems to work only on linux
|
||||
Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
|
||||
final LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource();
|
||||
final Pair<Double, Double> stats = libvirtComputingResource.getNicStats("lo");
|
||||
assertNotNull(stats);
|
||||
if(SystemUtils.IS_OS_LINUX) {
|
||||
final LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource();
|
||||
final Pair<Double, Double> stats = libvirtComputingResource.getNicStats("lo");
|
||||
assertNotNull(stats);
|
||||
} // else SUCCESS
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@ -20,15 +20,40 @@
|
||||
package com.cloud.hypervisor.kvm.resource;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Scanner;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ChannelDef;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.SCSIDef;
|
||||
import org.apache.cloudstack.utils.linux.MemStat;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(value = {MemStat.class})
|
||||
public class LibvirtVMDefTest extends TestCase {
|
||||
|
||||
final String memInfo = "MemTotal: 5830236 kB\n" +
|
||||
"MemFree: 156752 kB\n" +
|
||||
"Buffers: 326836 kB\n" +
|
||||
"Cached: 2606764 kB\n" +
|
||||
"SwapCached: 0 kB\n" +
|
||||
"Active: 4260808 kB\n" +
|
||||
"Inactive: 949392 kB\n";
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
Scanner scanner = new Scanner(memInfo);
|
||||
PowerMockito.whenNew(Scanner.class).withAnyArguments().thenReturn(scanner);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterfaceEtehrnet() {
|
||||
LibvirtVMDef.InterfaceDef ifDef = new LibvirtVMDef.InterfaceDef();
|
||||
ifDef.defEthernet("targetDeviceName", "00:11:22:aa:bb:dd", LibvirtVMDef.InterfaceDef.NicModel.VIRTIO);
|
||||
@ -44,6 +69,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
assertEquals(expected, ifDef.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterfaceDirectNet() {
|
||||
LibvirtVMDef.InterfaceDef ifDef = new LibvirtVMDef.InterfaceDef();
|
||||
ifDef.defDirectNet("targetDeviceName", null, "00:11:22:aa:bb:dd", LibvirtVMDef.InterfaceDef.NicModel.VIRTIO, "private");
|
||||
@ -59,6 +85,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
assertEquals(expected, ifDef.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterfaceBridgeSlot() {
|
||||
LibvirtVMDef.InterfaceDef ifDef = new LibvirtVMDef.InterfaceDef();
|
||||
ifDef.defBridgeNet("targetDeviceName", null, "00:11:22:aa:bb:dd", LibvirtVMDef.InterfaceDef.NicModel.VIRTIO);
|
||||
@ -91,6 +118,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
assertEquals(expected, ifDef.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCpuModeDef() {
|
||||
LibvirtVMDef.CpuModeDef cpuModeDef = new LibvirtVMDef.CpuModeDef();
|
||||
cpuModeDef.setMode("custom");
|
||||
@ -111,6 +139,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiskDef() {
|
||||
String filePath = "/var/lib/libvirt/images/disk.qcow2";
|
||||
String diskLabel = "vda";
|
||||
@ -135,6 +164,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
assertEquals(xmlDef, expectedXml);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiskDefWithBurst() {
|
||||
String filePath = "/var/lib/libvirt/images/disk.qcow2";
|
||||
String diskLabel = "vda";
|
||||
@ -188,6 +218,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
assertEquals(xmlDef, expectedXml);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHypervEnlightDef() {
|
||||
LibvirtVMDef.FeaturesDef featuresDef = new LibvirtVMDef.FeaturesDef();
|
||||
LibvirtVMDef.HyperVEnlightenmentFeatureDef hyperVEnlightenmentFeatureDef = new LibvirtVMDef.HyperVEnlightenmentFeatureDef();
|
||||
@ -211,6 +242,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRngDef() {
|
||||
LibvirtVMDef.RngDef.RngBackendModel backendModel = LibvirtVMDef.RngDef.RngBackendModel.RANDOM;
|
||||
String path = "/dev/random";
|
||||
@ -225,6 +257,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
assertEquals(def.getRngRatePeriod(), period);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChannelDef() {
|
||||
ChannelDef.ChannelType type = ChannelDef.ChannelType.UNIX;
|
||||
ChannelDef.ChannelState state = ChannelDef.ChannelState.CONNECTED;
|
||||
@ -239,6 +272,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
assertEquals(path, channelDef.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWatchDogDef() {
|
||||
LibvirtVMDef.WatchDogDef.WatchDogModel model = LibvirtVMDef.WatchDogDef.WatchDogModel.I6300ESB;
|
||||
LibvirtVMDef.WatchDogDef.WatchDogAction action = LibvirtVMDef.WatchDogDef.WatchDogAction.RESET;
|
||||
@ -248,6 +282,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
assertEquals(def.getAction(), action);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSCSIDef() {
|
||||
SCSIDef def = new SCSIDef((short)0, 0, 0, 9, 0, 4);
|
||||
String str = def.toString();
|
||||
@ -258,6 +293,7 @@ public class LibvirtVMDefTest extends TestCase {
|
||||
assertEquals(str, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMetadataDef() {
|
||||
LibvirtVMDef.MetadataDef metadataDef = new LibvirtVMDef.MetadataDef();
|
||||
|
||||
|
||||
@ -28,15 +28,23 @@ import static org.mockito.Mockito.spy;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.apache.cloudstack.utils.linux.MemStat;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource.BridgeType;
|
||||
import com.cloud.network.Networks.TrafficType;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(value = {MemStat.class})
|
||||
public class LibvirtVifDriverTest {
|
||||
private LibvirtComputingResource res;
|
||||
|
||||
@ -48,8 +56,17 @@ public class LibvirtVifDriverTest {
|
||||
|
||||
private VifDriver fakeVifDriver, bridgeVifDriver, ovsVifDriver;
|
||||
|
||||
final String memInfo = "MemTotal: 5830236 kB\n" +
|
||||
"MemFree: 156752 kB\n" +
|
||||
"Buffers: 326836 kB\n" +
|
||||
"Cached: 2606764 kB\n" +
|
||||
"SwapCached: 0 kB\n" +
|
||||
"Active: 4260808 kB\n" +
|
||||
"Inactive: 949392 kB\n";
|
||||
@Before
|
||||
public void setUp() {
|
||||
public void setUp() throws Exception {
|
||||
Scanner scanner = new Scanner(memInfo);
|
||||
PowerMockito.whenNew(Scanner.class).withAnyArguments().thenReturn(scanner);
|
||||
// Use a spy because we only want to override getVifDriverClass
|
||||
LibvirtComputingResource resReal = new LibvirtComputingResource();
|
||||
res = spy(resReal);
|
||||
|
||||
@ -24,11 +24,13 @@ import static org.junit.Assert.assertTrue;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import org.apache.cloudstack.utils.linux.MemStat;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.libvirt.Connect;
|
||||
@ -55,7 +57,7 @@ import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({LibvirtConnection.class, LibvirtMigrateCommandWrapper.class})
|
||||
@PrepareForTest(value = {LibvirtConnection.class, LibvirtMigrateCommandWrapper.class, MemStat.class})
|
||||
public class LibvirtMigrateCommandWrapperTest {
|
||||
String fullfile =
|
||||
"<domain type='kvm' id='4'>\n" +
|
||||
@ -294,6 +296,20 @@ public class LibvirtMigrateCommandWrapperTest {
|
||||
|
||||
LibvirtMigrateCommandWrapper libvirtMigrateCmdWrapper = new LibvirtMigrateCommandWrapper();
|
||||
|
||||
final String memInfo = "MemTotal: 5830236 kB\n" +
|
||||
"MemFree: 156752 kB\n" +
|
||||
"Buffers: 326836 kB\n" +
|
||||
"Cached: 2606764 kB\n" +
|
||||
"SwapCached: 0 kB\n" +
|
||||
"Active: 4260808 kB\n" +
|
||||
"Inactive: 949392 kB\n";
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
Scanner scanner = new Scanner(memInfo);
|
||||
PowerMockito.whenNew(Scanner.class).withAnyArguments().thenReturn(scanner);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplaceIpForVNCInDescFile() {
|
||||
final String targetIp = "192.168.22.21";
|
||||
|
||||
@ -25,10 +25,10 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.apache.cloudstack.utils.linux.MemStat;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.libvirt.Connect;
|
||||
import org.libvirt.Domain;
|
||||
import org.libvirt.LibvirtException;
|
||||
@ -39,7 +39,14 @@ import com.cloud.agent.api.to.IpAddressTO;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.utils.ExecutionResult;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(value = {MemStat.class})
|
||||
public class LibvirtNetworkElementCommandWrapperTest {
|
||||
private static final String fullfile = "<domain type='kvm' id='143'>\n"
|
||||
+ " <name>r-3-VM</name>\n"
|
||||
@ -211,8 +218,17 @@ public class LibvirtNetworkElementCommandWrapperTest {
|
||||
private LibvirtComputingResource res;
|
||||
private final Domain _domain = mock(Domain.class);
|
||||
|
||||
final String memInfo = "MemTotal: 5830236 kB\n" +
|
||||
"MemFree: 156752 kB\n" +
|
||||
"Buffers: 326836 kB\n" +
|
||||
"Cached: 2606764 kB\n" +
|
||||
"SwapCached: 0 kB\n" +
|
||||
"Active: 4260808 kB\n" +
|
||||
"Inactive: 949392 kB\n";
|
||||
@Before
|
||||
public void setUp() throws LibvirtException, ConfigurationException {
|
||||
public void setUp() throws Exception {
|
||||
Scanner scanner = new Scanner(memInfo);
|
||||
PowerMockito.whenNew(Scanner.class).withAnyArguments().thenReturn(scanner);
|
||||
// Use a spy because we only want to override getVifDriverClass
|
||||
LibvirtComputingResource resReal = new LibvirtComputingResource() {
|
||||
{
|
||||
|
||||
@ -29,9 +29,9 @@ import static org.mockito.Mockito.when;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.apache.cloudstack.utils.linux.MemStat;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -52,8 +52,9 @@ import com.cloud.hypervisor.kvm.resource.OvsVifDriver;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.utils.script.Script;
|
||||
import com.cloud.vm.VirtualMachine;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(Script.class)
|
||||
@PrepareForTest(value = {Script.class, MemStat.class})
|
||||
public class LibvirtReplugNicCommandWrapperTest {
|
||||
private static final String part_1 =
|
||||
"<domain type='kvm' id='143'>\n"
|
||||
@ -183,8 +184,19 @@ public class LibvirtReplugNicCommandWrapperTest {
|
||||
private LibvirtComputingResource res;
|
||||
private final Domain _domain = mock(Domain.class);
|
||||
|
||||
final String memInfo = "MemTotal: 5830236 kB\n" +
|
||||
"MemFree: 156752 kB\n" +
|
||||
"Buffers: 326836 kB\n" +
|
||||
"Cached: 2606764 kB\n" +
|
||||
"SwapCached: 0 kB\n" +
|
||||
"Active: 4260808 kB\n" +
|
||||
"Inactive: 949392 kB\n";
|
||||
|
||||
@Before
|
||||
public void setUp() throws LibvirtException, ConfigurationException {
|
||||
public void setUp() throws Exception {
|
||||
Scanner scanner = new Scanner(memInfo);
|
||||
PowerMockito.whenNew(Scanner.class).withAnyArguments().thenReturn(scanner);
|
||||
|
||||
// Use a spy because we only want to override getVifDriverClass
|
||||
LibvirtComputingResource resReal = new LibvirtComputingResource();
|
||||
res = spy(resReal);
|
||||
|
||||
@ -16,11 +16,18 @@
|
||||
// under the License.
|
||||
package org.apache.cloudstack.utils.linux;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(MemStat.class)
|
||||
public class MemStatTest {
|
||||
final String memInfo = "MemTotal: 5830236 kB\n" +
|
||||
"MemFree: 156752 kB\n" +
|
||||
@ -30,21 +37,15 @@ public class MemStatTest {
|
||||
"Active: 4260808 kB\n" +
|
||||
"Inactive: 949392 kB\n";
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
Scanner scanner = new Scanner(memInfo);
|
||||
PowerMockito.whenNew(Scanner.class).withAnyArguments().thenReturn(scanner);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMemInfoParseTest() {
|
||||
MemStat memStat = null;
|
||||
try {
|
||||
memStat = new MemStat();
|
||||
} catch (RuntimeException ex) {
|
||||
// If test isn't run on linux we'll fail creation of linux-specific MemStat class due
|
||||
// to dependency on /proc/meminfo if we don't catch here.
|
||||
// We are really only interested in testing the parsing algorithm and getters.
|
||||
if (memStat == null) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
Scanner scanner = new Scanner(memInfo);
|
||||
memStat.parseFromScanner(scanner);
|
||||
MemStat memStat = new MemStat();
|
||||
|
||||
Assert.assertEquals(memStat.getTotal(), 5970161664L);
|
||||
Assert.assertEquals(memStat.getAvailable(), 2829840384L);
|
||||
@ -54,17 +55,7 @@ public class MemStatTest {
|
||||
|
||||
@Test
|
||||
public void reservedMemoryTest() {
|
||||
MemStat memStat = null;
|
||||
try {
|
||||
memStat = new MemStat(1024, 2048);
|
||||
} catch (RuntimeException ex) {
|
||||
if (memStat == null) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
Scanner scanner = new Scanner(memInfo);
|
||||
memStat.parseFromScanner(scanner);
|
||||
|
||||
MemStat memStat = new MemStat(1024, 2048);
|
||||
Assert.assertEquals(memStat.getTotal(), 5970162688L);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user