mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Remove powermock from core (#7978)
This commit is contained in:
parent
0375714ded
commit
481d2bd9c3
@ -23,10 +23,9 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Captor;
|
import org.mockito.Captor;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.powermock.api.mockito.PowerMockito;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
||||||
import org.powermock.modules.junit4.PowerMockRunner;
|
|
||||||
|
|
||||||
import javax.naming.ConfigurationException;
|
import javax.naming.ConfigurationException;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
@ -40,7 +39,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class ServerResourceBaseTest {
|
public class ServerResourceBaseTest {
|
||||||
|
|
||||||
private static final String[] NIC_NAME_STARTS_TO_AVOID = {"vnif", "vnbr", "peth", "vif", "virbr"};
|
private static final String[] NIC_NAME_STARTS_TO_AVOID = {"vnif", "vnbr", "peth", "vif", "virbr"};
|
||||||
@ -54,165 +53,162 @@ public class ServerResourceBaseTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
networkInterfaceMock1 = PowerMockito.mock(NetworkInterface.class);
|
networkInterfaceMock1 = Mockito.mock(NetworkInterface.class);
|
||||||
networkInterfaceMock2 = PowerMockito.mock(NetworkInterface.class);
|
networkInterfaceMock2 = Mockito.mock(NetworkInterface.class);
|
||||||
networkInterfaceMock3 = PowerMockito.mock(NetworkInterface.class);
|
networkInterfaceMock3 = Mockito.mock(NetworkInterface.class);
|
||||||
networkInterfaceMock4 = PowerMockito.mock(NetworkInterface.class);
|
networkInterfaceMock4 = Mockito.mock(NetworkInterface.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest(ServerResourceBase.class)
|
|
||||||
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNicIsVirtual() {
|
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNicIsVirtual() {
|
||||||
NetworkInterface networkInterfaceMock = PowerMockito.mock(NetworkInterface.class);
|
NetworkInterface networkInterfaceMock = Mockito.mock(NetworkInterface.class);
|
||||||
PowerMockito.when(networkInterfaceMock.isVirtual()).thenReturn(true);
|
Mockito.when(networkInterfaceMock.isVirtual()).thenReturn(true);
|
||||||
|
|
||||||
Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest(ServerResourceBase.class)
|
|
||||||
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNicNameStartsWithOneOfTheAvoidList() {
|
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNicNameStartsWithOneOfTheAvoidList() {
|
||||||
NetworkInterface networkInterfaceMock = PowerMockito.mock(NetworkInterface.class);
|
NetworkInterface networkInterfaceMock = Mockito.mock(NetworkInterface.class);
|
||||||
PowerMockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
Mockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
||||||
PowerMockito.when(networkInterfaceMock.getName()).thenReturn("vniftest", "vnbrtest", "pethtest", "viftest", "virbrtest");
|
Mockito.when(networkInterfaceMock.getName()).thenReturn("vniftest", "vnbrtest", "pethtest", "viftest", "virbrtest");
|
||||||
|
|
||||||
Arrays.asList(NIC_NAME_STARTS_TO_AVOID).forEach(type -> Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock)));
|
Arrays.asList(NIC_NAME_STARTS_TO_AVOID).forEach(type -> Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest(ServerResourceBase.class)
|
|
||||||
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNicNameContainsColon() {
|
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNicNameContainsColon() {
|
||||||
NetworkInterface networkInterfaceMock = PowerMockito.mock(NetworkInterface.class);
|
NetworkInterface networkInterfaceMock = Mockito.mock(NetworkInterface.class);
|
||||||
PowerMockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
Mockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
||||||
PowerMockito.when(networkInterfaceMock.getName()).thenReturn("te:st");
|
Mockito.when(networkInterfaceMock.getName()).thenReturn("te:st");
|
||||||
|
|
||||||
Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest({ServerResourceBase.class, NetUtils.class})
|
|
||||||
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNetUtilsGetNicParamsReturnsNull() {
|
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNetUtilsGetNicParamsReturnsNull() {
|
||||||
NetworkInterface networkInterfaceMock = PowerMockito.mock(NetworkInterface.class);
|
NetworkInterface networkInterfaceMock = Mockito.mock(NetworkInterface.class);
|
||||||
PowerMockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
Mockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
||||||
PowerMockito.when(networkInterfaceMock.getName()).thenReturn("testvnif", "testvnbr", "testpeth", "testvif", "testvirbr");
|
Mockito.when(networkInterfaceMock.getName()).thenReturn("testvnif", "testvnbr", "testpeth", "testvif", "testvirbr");
|
||||||
|
|
||||||
PowerMockito.mockStatic(NetUtils.class);
|
try (MockedStatic<NetUtils> ignored = Mockito.mockStatic(NetUtils.class)) {
|
||||||
PowerMockito.when(NetUtils.getNicParams(Mockito.anyString())).thenReturn(null);
|
Mockito.when(NetUtils.getNicParams(Mockito.anyString())).thenReturn(null);
|
||||||
|
|
||||||
Arrays.asList(NIC_NAME_STARTS_TO_AVOID).forEach(type -> {
|
Arrays.asList(NIC_NAME_STARTS_TO_AVOID).forEach(type -> {
|
||||||
Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest({ServerResourceBase.class, NetUtils.class})
|
|
||||||
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNetUtilsGetNicParamsReturnsFirstElementNull() {
|
public void isValidNicToUseAsPrivateNicTestReturnFalseWhenNetUtilsGetNicParamsReturnsFirstElementNull() {
|
||||||
NetworkInterface networkInterfaceMock = PowerMockito.mock(NetworkInterface.class);
|
NetworkInterface networkInterfaceMock = Mockito.mock(NetworkInterface.class);
|
||||||
PowerMockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
Mockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
||||||
PowerMockito.when(networkInterfaceMock.getName()).thenReturn("testvnif", "testvnbr", "testpeth", "testvif", "testvirbr");
|
Mockito.when(networkInterfaceMock.getName()).thenReturn("testvnif", "testvnbr", "testpeth", "testvif", "testvirbr");
|
||||||
|
|
||||||
PowerMockito.mockStatic(NetUtils.class);
|
try (MockedStatic<NetUtils> ignored = Mockito.mockStatic(NetUtils.class)) {
|
||||||
PowerMockito.when(NetUtils.getNicParams(Mockito.anyString())).thenReturn(new String[]{null});
|
Mockito.when(NetUtils.getNicParams(Mockito.anyString())).thenReturn(new String[]{null});
|
||||||
|
|
||||||
Arrays.asList(NIC_NAME_STARTS_TO_AVOID).forEach(type -> {
|
Arrays.asList(NIC_NAME_STARTS_TO_AVOID).forEach(type -> {
|
||||||
Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
Assert.assertFalse(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest({ServerResourceBase.class, NetUtils.class})
|
|
||||||
public void isValidNicToUseAsPrivateNicTestReturnTrueWhenNetUtilsGetNicParamsReturnsAValidFirstElement() {
|
public void isValidNicToUseAsPrivateNicTestReturnTrueWhenNetUtilsGetNicParamsReturnsAValidFirstElement() {
|
||||||
NetworkInterface networkInterfaceMock = PowerMockito.mock(NetworkInterface.class);
|
NetworkInterface networkInterfaceMock = Mockito.mock(NetworkInterface.class);
|
||||||
PowerMockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
Mockito.when(networkInterfaceMock.isVirtual()).thenReturn(false);
|
||||||
PowerMockito.when(networkInterfaceMock.getName()).thenReturn("testvnif", "testvnbr", "testpeth", "testvif", "testvirbr");
|
Mockito.when(networkInterfaceMock.getName()).thenReturn("testvnif", "testvnbr", "testpeth", "testvif", "testvirbr");
|
||||||
|
|
||||||
PowerMockito.mockStatic(NetUtils.class);
|
try (MockedStatic<NetUtils> ignored = Mockito.mockStatic(NetUtils.class)) {
|
||||||
PowerMockito.when(NetUtils.getNicParams(Mockito.anyString())).thenReturn(new String[]{"test"});
|
Mockito.when(NetUtils.getNicParams(Mockito.anyString())).thenReturn(new String[]{"test"});
|
||||||
|
|
||||||
Arrays.asList(NIC_NAME_STARTS_TO_AVOID).forEach(type -> {
|
Arrays.asList(NIC_NAME_STARTS_TO_AVOID).forEach(type -> {
|
||||||
Assert.assertTrue(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
Assert.assertTrue(serverResourceBaseSpy.isValidNicToUseAsPrivateNic(networkInterfaceMock));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ConfigurationException.class)
|
@Test(expected = ConfigurationException.class)
|
||||||
@PrepareForTest(ServerResourceBase.class)
|
|
||||||
public void tryToAutoDiscoverResourcePrivateNetworkInterfaceTestThrowConfigurationExceptionWhenNicsDoesNotHaveMoreElements() throws SocketException, ConfigurationException {
|
public void tryToAutoDiscoverResourcePrivateNetworkInterfaceTestThrowConfigurationExceptionWhenNicsDoesNotHaveMoreElements() throws SocketException, ConfigurationException {
|
||||||
PowerMockito.mockStatic(NetworkInterface.class);
|
try (MockedStatic<NetworkInterface> ignored = Mockito.mockStatic(NetworkInterface.class)) {
|
||||||
PowerMockito.when(NetworkInterface.getNetworkInterfaces()).thenReturn(Collections.enumeration(new ArrayList<>()));
|
Mockito.when(NetworkInterface.getNetworkInterfaces()).thenReturn(Collections.enumeration(new ArrayList<>()));
|
||||||
|
|
||||||
serverResourceBaseSpy.tryToAutoDiscoverResourcePrivateNetworkInterface();
|
serverResourceBaseSpy.tryToAutoDiscoverResourcePrivateNetworkInterface();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ConfigurationException.class)
|
@Test(expected = ConfigurationException.class)
|
||||||
@PrepareForTest(ServerResourceBase.class)
|
|
||||||
public void tryToAutoDiscoverResourcePrivateNetworkInterfaceTestThrowConfigurationExceptionWhenNicsGetNetworkInterfacesThrowsSocketException() throws SocketException, ConfigurationException {
|
public void tryToAutoDiscoverResourcePrivateNetworkInterfaceTestThrowConfigurationExceptionWhenNicsGetNetworkInterfacesThrowsSocketException() throws SocketException, ConfigurationException {
|
||||||
PowerMockito.mockStatic(NetworkInterface.class);
|
try (MockedStatic<NetworkInterface> ignored = Mockito.mockStatic(NetworkInterface.class)) {
|
||||||
PowerMockito.when(NetworkInterface.getNetworkInterfaces()).thenThrow(SocketException.class);
|
Mockito.when(NetworkInterface.getNetworkInterfaces()).thenThrow(SocketException.class);
|
||||||
|
|
||||||
serverResourceBaseSpy.tryToAutoDiscoverResourcePrivateNetworkInterface();
|
serverResourceBaseSpy.tryToAutoDiscoverResourcePrivateNetworkInterface();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ConfigurationException.class)
|
@Test(expected = ConfigurationException.class)
|
||||||
@PrepareForTest(ServerResourceBase.class)
|
|
||||||
public void tryToAutoDiscoverResourcePrivateNetworkInterfaceTestThrowConfigurationExceptionWhenThereIsNoValidNics() throws SocketException, ConfigurationException {
|
public void tryToAutoDiscoverResourcePrivateNetworkInterfaceTestThrowConfigurationExceptionWhenThereIsNoValidNics() throws SocketException, ConfigurationException {
|
||||||
PowerMockito.mockStatic(NetworkInterface.class);
|
try (MockedStatic<NetworkInterface> ignored = Mockito.mockStatic(NetworkInterface.class)) {
|
||||||
PowerMockito.when(NetworkInterface.getNetworkInterfaces()).thenReturn(Collections.enumeration(Arrays.asList(networkInterfaceMock1, networkInterfaceMock2)));
|
Mockito.when(NetworkInterface.getNetworkInterfaces()).thenReturn(Collections.enumeration(Arrays.asList(networkInterfaceMock1, networkInterfaceMock2)));
|
||||||
Mockito.doReturn(false).when(serverResourceBaseSpy).isValidNicToUseAsPrivateNic(Mockito.any());
|
Mockito.doReturn(false).when(serverResourceBaseSpy).isValidNicToUseAsPrivateNic(Mockito.any());
|
||||||
|
|
||||||
serverResourceBaseSpy.tryToAutoDiscoverResourcePrivateNetworkInterface();
|
serverResourceBaseSpy.tryToAutoDiscoverResourcePrivateNetworkInterface();
|
||||||
|
|
||||||
Mockito.verify(serverResourceBaseSpy, Mockito.times(2)).isValidNicToUseAsPrivateNic(Mockito.any());
|
Mockito.verify(serverResourceBaseSpy, Mockito.times(2)).isValidNicToUseAsPrivateNic(Mockito.any());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest(ServerResourceBase.class)
|
|
||||||
public void tryToAutoDiscoverResourcePrivateNetworkInterfaceTestReturnNic() throws SocketException, ConfigurationException {
|
public void tryToAutoDiscoverResourcePrivateNetworkInterfaceTestReturnNic() throws SocketException, ConfigurationException {
|
||||||
Enumeration<NetworkInterface> interfaces = Collections.enumeration(Arrays.asList(networkInterfaceMock1, networkInterfaceMock2));
|
Enumeration<NetworkInterface> interfaces = Collections.enumeration(Arrays.asList(networkInterfaceMock1, networkInterfaceMock2));
|
||||||
|
|
||||||
PowerMockito.mockStatic(NetworkInterface.class);
|
try (MockedStatic<NetworkInterface> ignored = Mockito.mockStatic(NetworkInterface.class)) {
|
||||||
PowerMockito.when(NetworkInterface.getNetworkInterfaces()).thenReturn(interfaces);
|
Mockito.when(NetworkInterface.getNetworkInterfaces()).thenReturn(interfaces);
|
||||||
Mockito.doReturn(false, true).when(serverResourceBaseSpy).isValidNicToUseAsPrivateNic(Mockito.any());
|
Mockito.doReturn(false, true).when(serverResourceBaseSpy).isValidNicToUseAsPrivateNic(Mockito.any());
|
||||||
|
|
||||||
serverResourceBaseSpy.tryToAutoDiscoverResourcePrivateNetworkInterface();
|
serverResourceBaseSpy.tryToAutoDiscoverResourcePrivateNetworkInterface();
|
||||||
|
|
||||||
Assert.assertEquals(networkInterfaceMock2, serverResourceBaseSpy._privateNic);
|
Assert.assertEquals(networkInterfaceMock2, serverResourceBaseSpy._privateNic);
|
||||||
Mockito.verify(serverResourceBaseSpy, Mockito.times(2)).isValidNicToUseAsPrivateNic(Mockito.any());
|
Mockito.verify(serverResourceBaseSpy, Mockito.times(2)).isValidNicToUseAsPrivateNic(Mockito.any());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest(NetUtils.class)
|
|
||||||
public void defineResourceNetworkInterfacesTestUseXenbr0WhenPrivateNetworkInterfaceNotConfigured() {
|
public void defineResourceNetworkInterfacesTestUseXenbr0WhenPrivateNetworkInterfaceNotConfigured() {
|
||||||
Map<String, Object> params = createParamsMap(null, "cloudbr1", "cloudbr2", "cloudbr3");
|
Map<String, Object> params = createParamsMap(null, "cloudbr1", "cloudbr2", "cloudbr3");
|
||||||
PowerMockito.mockStatic(NetUtils.class);
|
try (MockedStatic<NetUtils> ignored = Mockito.mockStatic(NetUtils.class)) {
|
||||||
PowerMockito.when(NetUtils.getNetworkInterface(Mockito.anyString())).thenReturn(networkInterfaceMock1, networkInterfaceMock2, networkInterfaceMock3, networkInterfaceMock4);
|
Mockito.when(NetUtils.getNetworkInterface(Mockito.anyString())).thenReturn(networkInterfaceMock1, networkInterfaceMock2, networkInterfaceMock3, networkInterfaceMock4);
|
||||||
|
|
||||||
serverResourceBaseSpy.defineResourceNetworkInterfaces(params);
|
serverResourceBaseSpy.defineResourceNetworkInterfaces(params);
|
||||||
|
|
||||||
verifyAndAssertNetworkInterfaces("xenbr0", "cloudbr1", "cloudbr2", "cloudbr3");
|
verifyAndAssertNetworkInterfaces("xenbr0", "cloudbr1", "cloudbr2", "cloudbr3");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest(NetUtils.class)
|
|
||||||
public void defineResourceNetworkInterfacesTestUseXenbr1WhenPublicNetworkInterfaceNotConfigured() {
|
public void defineResourceNetworkInterfacesTestUseXenbr1WhenPublicNetworkInterfaceNotConfigured() {
|
||||||
Map<String, Object> params = createParamsMap("cloudbr0", null, "cloudbr2", "cloudbr3");
|
Map<String, Object> params = createParamsMap("cloudbr0", null, "cloudbr2", "cloudbr3");
|
||||||
PowerMockito.mockStatic(NetUtils.class);
|
try (MockedStatic<NetUtils> ignored = Mockito.mockStatic(NetUtils.class)) {
|
||||||
PowerMockito.when(NetUtils.getNetworkInterface(Mockito.anyString())).thenReturn(networkInterfaceMock1, networkInterfaceMock2, networkInterfaceMock3, networkInterfaceMock4);
|
Mockito.when(NetUtils.getNetworkInterface(Mockito.anyString())).thenReturn(networkInterfaceMock1, networkInterfaceMock2, networkInterfaceMock3, networkInterfaceMock4);
|
||||||
|
|
||||||
serverResourceBaseSpy.defineResourceNetworkInterfaces(params);
|
serverResourceBaseSpy.defineResourceNetworkInterfaces(params);
|
||||||
|
|
||||||
verifyAndAssertNetworkInterfaces("cloudbr0", "xenbr1", "cloudbr2", "cloudbr3");
|
verifyAndAssertNetworkInterfaces("cloudbr0", "xenbr1", "cloudbr2", "cloudbr3");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PrepareForTest(NetUtils.class)
|
|
||||||
public void defineResourceNetworkInterfacesTestUseConfiguredNetworkInterfaces() {
|
public void defineResourceNetworkInterfacesTestUseConfiguredNetworkInterfaces() {
|
||||||
Map<String, Object> params = createParamsMap("cloudbr0", "cloudbr1", "cloudbr2", "cloudbr3");
|
Map<String, Object> params = createParamsMap("cloudbr0", "cloudbr1", "cloudbr2", "cloudbr3");
|
||||||
PowerMockito.mockStatic(NetUtils.class);
|
try (MockedStatic<NetUtils> ignored = Mockito.mockStatic(NetUtils.class)) {
|
||||||
PowerMockito.when(NetUtils.getNetworkInterface(Mockito.anyString())).thenReturn(networkInterfaceMock1, networkInterfaceMock2, networkInterfaceMock3, networkInterfaceMock4);
|
Mockito.when(NetUtils.getNetworkInterface(Mockito.anyString())).thenReturn(networkInterfaceMock1, networkInterfaceMock2, networkInterfaceMock3, networkInterfaceMock4);
|
||||||
|
|
||||||
serverResourceBaseSpy.defineResourceNetworkInterfaces(params);
|
serverResourceBaseSpy.defineResourceNetworkInterfaces(params);
|
||||||
|
|
||||||
verifyAndAssertNetworkInterfaces("cloudbr0", "cloudbr1", "cloudbr2", "cloudbr3");
|
verifyAndAssertNetworkInterfaces("cloudbr0", "cloudbr1", "cloudbr2", "cloudbr3");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> createParamsMap(String... params) {
|
private Map<String, Object> createParamsMap(String... params) {
|
||||||
@ -225,7 +221,7 @@ public class ServerResourceBaseTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void verifyAndAssertNetworkInterfaces(String... expectedResults) {
|
private void verifyAndAssertNetworkInterfaces(String... expectedResults) {
|
||||||
PowerMockito.verifyStatic(NetUtils.class, Mockito.times(4));
|
Mockito.verify(NetUtils.class, Mockito.times(4));
|
||||||
NetUtils.getNetworkInterface(keyCaptor.capture());
|
NetUtils.getNetworkInterface(keyCaptor.capture());
|
||||||
List<String> keys = keyCaptor.getAllValues();
|
List<String> keys = keyCaptor.getAllValues();
|
||||||
|
|
||||||
|
|||||||
@ -28,19 +28,15 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockedConstruction;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.powermock.api.mockito.PowerMockito;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
||||||
import org.powermock.modules.junit4.PowerMockRunner;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
@PowerMockIgnore({"javax.xml.*", "java.xml.*", "javax.management.*", "org.apache.xerces.*"})
|
|
||||||
@PrepareForTest(OVAProcessor.class)
|
|
||||||
public class OVAProcessorTest {
|
public class OVAProcessorTest {
|
||||||
OVAProcessor processor;
|
OVAProcessor processor;
|
||||||
|
|
||||||
@ -49,7 +45,7 @@ public class OVAProcessorTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
processor = PowerMockito.spy(new OVAProcessor());
|
processor = Mockito.spy(new OVAProcessor());
|
||||||
Map<String, Object> params = new HashMap<String, Object>();
|
Map<String, Object> params = new HashMap<String, Object>();
|
||||||
params.put(StorageLayer.InstanceConfigKey, mockStorageLayer);
|
params.put(StorageLayer.InstanceConfigKey, mockStorageLayer);
|
||||||
processor.configure("OVA Processor", params);
|
processor.configure("OVA Processor", params);
|
||||||
@ -62,11 +58,11 @@ public class OVAProcessorTest {
|
|||||||
|
|
||||||
Mockito.when(mockStorageLayer.exists(Mockito.anyString())).thenReturn(true);
|
Mockito.when(mockStorageLayer.exists(Mockito.anyString())).thenReturn(true);
|
||||||
|
|
||||||
Script mockScript = Mockito.mock(Script.class);
|
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class, (mock, context) -> {
|
||||||
PowerMockito.whenNew(Script.class).withAnyArguments().thenReturn(mockScript);
|
Mockito.when(mock.execute()).thenReturn("error while untaring the file");
|
||||||
PowerMockito.when(mockScript.execute()).thenReturn("error while untaring the file");
|
})) {
|
||||||
|
processor.process(templatePath, null, templateName);
|
||||||
processor.process(templatePath, null, templateName);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = InternalErrorException.class)
|
@Test(expected = InternalErrorException.class)
|
||||||
@ -78,11 +74,11 @@ public class OVAProcessorTest {
|
|||||||
Mockito.when(mockStorageLayer.getSize(Mockito.anyString())).thenReturn(1000l);
|
Mockito.when(mockStorageLayer.getSize(Mockito.anyString())).thenReturn(1000l);
|
||||||
Mockito.doThrow(new InternalErrorException("virtual size calculation failed")).when(processor).getTemplateVirtualSize(Mockito.anyString(), Mockito.anyString());
|
Mockito.doThrow(new InternalErrorException("virtual size calculation failed")).when(processor).getTemplateVirtualSize(Mockito.anyString(), Mockito.anyString());
|
||||||
|
|
||||||
Script mockScript = Mockito.mock(Script.class);
|
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class, (mock, context) -> {
|
||||||
PowerMockito.whenNew(Script.class).withAnyArguments().thenReturn(mockScript);
|
Mockito.when(mock.execute()).thenReturn(null);
|
||||||
PowerMockito.when(mockScript.execute()).thenReturn(null);
|
})) {
|
||||||
|
processor.process(templatePath, null, templateName);
|
||||||
processor.process(templatePath, null, templateName);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -96,15 +92,15 @@ public class OVAProcessorTest {
|
|||||||
Mockito.when(mockStorageLayer.getSize(Mockito.anyString())).thenReturn(actualSize);
|
Mockito.when(mockStorageLayer.getSize(Mockito.anyString())).thenReturn(actualSize);
|
||||||
Mockito.doReturn(virtualSize).when(processor).getTemplateVirtualSize(Mockito.anyString(), Mockito.anyString());
|
Mockito.doReturn(virtualSize).when(processor).getTemplateVirtualSize(Mockito.anyString(), Mockito.anyString());
|
||||||
|
|
||||||
Script mockScript = Mockito.mock(Script.class);
|
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class, (mock, context) -> {
|
||||||
PowerMockito.whenNew(Script.class).withAnyArguments().thenReturn(mockScript);
|
Mockito.when(mock.execute()).thenReturn(null);
|
||||||
PowerMockito.when(mockScript.execute()).thenReturn(null);
|
})) {
|
||||||
|
Processor.FormatInfo info = processor.process(templatePath, null, templateName);
|
||||||
Processor.FormatInfo info = processor.process(templatePath, null, templateName);
|
Assert.assertEquals(Storage.ImageFormat.OVA, info.format);
|
||||||
Assert.assertEquals(Storage.ImageFormat.OVA, info.format);
|
Assert.assertEquals("actual size:", actualSize, info.size);
|
||||||
Assert.assertEquals("actual size:", actualSize, info.size);
|
Assert.assertEquals("virtual size:", virtualSize, info.virtualSize);
|
||||||
Assert.assertEquals("virtual size:", virtualSize, info.virtualSize);
|
Assert.assertEquals("template name:", templateName + ".ova", info.filename);
|
||||||
Assert.assertEquals("template name:", templateName + ".ova", info.filename);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -129,7 +125,6 @@ public class OVAProcessorTest {
|
|||||||
String templatePath = "/tmp";
|
String templatePath = "/tmp";
|
||||||
String templateName = "template";
|
String templateName = "template";
|
||||||
File mockFile = Mockito.mock(File.class);
|
File mockFile = Mockito.mock(File.class);
|
||||||
Mockito.when(mockFile.length()).thenReturn(actualSize);
|
|
||||||
Mockito.when(mockFile.getParent()).thenReturn(templatePath);
|
Mockito.when(mockFile.getParent()).thenReturn(templatePath);
|
||||||
Mockito.when(mockFile.getName()).thenReturn(templateName);
|
Mockito.when(mockFile.getName()).thenReturn(templateName);
|
||||||
Mockito.doReturn(virtualSize).when(processor).getTemplateVirtualSize(templatePath, templateName);
|
Mockito.doReturn(virtualSize).when(processor).getTemplateVirtualSize(templatePath, templateName);
|
||||||
|
|||||||
@ -29,15 +29,13 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
||||||
import org.powermock.modules.junit4.PowerMockRunner;
|
|
||||||
|
|
||||||
import com.cloud.exception.InternalErrorException;
|
import com.cloud.exception.InternalErrorException;
|
||||||
import com.cloud.storage.Storage;
|
import com.cloud.storage.Storage;
|
||||||
import com.cloud.storage.StorageLayer;
|
import com.cloud.storage.StorageLayer;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
@PrepareForTest(QCOW2Processor.class)
|
|
||||||
public class QCOW2ProcessorTest {
|
public class QCOW2ProcessorTest {
|
||||||
QCOW2Processor processor;
|
QCOW2Processor processor;
|
||||||
|
|
||||||
@ -102,9 +100,7 @@ public class QCOW2ProcessorTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetVirtualSize() throws Exception {
|
public void testGetVirtualSize() throws Exception {
|
||||||
long virtualSize = 2000;
|
long virtualSize = 2000;
|
||||||
long actualSize = 1000;
|
|
||||||
File mockFile = Mockito.mock(File.class);
|
File mockFile = Mockito.mock(File.class);
|
||||||
Mockito.when(mockFile.length()).thenReturn(actualSize);
|
|
||||||
Mockito.doReturn(virtualSize).when(processor).getTemplateVirtualSize((File)Mockito.any());
|
Mockito.doReturn(virtualSize).when(processor).getTemplateVirtualSize((File)Mockito.any());
|
||||||
Assert.assertEquals(virtualSize, processor.getVirtualSize(mockFile));
|
Assert.assertEquals(virtualSize, processor.getVirtualSize(mockFile));
|
||||||
Mockito.verify(mockFile, Mockito.times(0)).length();
|
Mockito.verify(mockFile, Mockito.times(0)).length();
|
||||||
|
|||||||
@ -32,15 +32,14 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
||||||
import org.powermock.modules.junit4.PowerMockRunner;
|
|
||||||
|
|
||||||
import com.cloud.exception.InternalErrorException;
|
import com.cloud.exception.InternalErrorException;
|
||||||
import com.cloud.storage.Storage;
|
import com.cloud.storage.Storage;
|
||||||
import com.cloud.storage.StorageLayer;
|
import com.cloud.storage.StorageLayer;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
@PrepareForTest(VhdProcessor.class)
|
|
||||||
public class VhdProcessorTest {
|
public class VhdProcessorTest {
|
||||||
VhdProcessor processor;
|
VhdProcessor processor;
|
||||||
|
|
||||||
@ -107,7 +106,6 @@ public class VhdProcessorTest {
|
|||||||
long virtualSize = 2000;
|
long virtualSize = 2000;
|
||||||
long actualSize = 1000;
|
long actualSize = 1000;
|
||||||
File mockFile = Mockito.mock(File.class);
|
File mockFile = Mockito.mock(File.class);
|
||||||
Mockito.when(mockFile.length()).thenReturn(actualSize);
|
|
||||||
Mockito.doReturn(virtualSize).when(processor).getTemplateVirtualSize((File) Mockito.any());
|
Mockito.doReturn(virtualSize).when(processor).getTemplateVirtualSize((File) Mockito.any());
|
||||||
Assert.assertEquals(virtualSize, processor.getVirtualSize(mockFile));
|
Assert.assertEquals(virtualSize, processor.getVirtualSize(mockFile));
|
||||||
Mockito.verify(mockFile,Mockito.times(0)).length();
|
Mockito.verify(mockFile,Mockito.times(0)).length();
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
mock-maker-inline
|
||||||
Loading…
x
Reference in New Issue
Block a user