Fix scaleio tests (#7622)

Co-authored-by: Wei Zhou <weizhou@apache.org>
This commit is contained in:
Vishesh 2023-06-15 15:23:10 +05:30 committed by GitHub
parent 5fa9b8f683
commit 27b0a64b40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 44 deletions

View File

@ -44,9 +44,6 @@
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
<executions> <executions>
<execution> <execution>
<phase>integration-test</phase> <phase>integration-test</phase>

View File

@ -48,7 +48,7 @@ import com.github.tomakehurst.wiremock.junit.WireMockRule;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class ScaleIOGatewayClientImplTest { public class ScaleIOGatewayClientImplTest {
private final int port = 443; private final int port = 8443;
private final int timeout = 30; private final int timeout = 30;
private final int maxConnections = 50; private final int maxConnections = 50;
private final String username = "admin"; private final String username = "admin";
@ -70,7 +70,7 @@ public class ScaleIOGatewayClientImplTest {
.withHeader("content-type", "application/json;charset=UTF-8") .withHeader("content-type", "application/json;charset=UTF-8")
.withBody(sessionKey))); .withBody(sessionKey)));
client = new ScaleIOGatewayClientImpl("https://localhost/api", username, password, false, timeout, maxConnections); client = new ScaleIOGatewayClientImpl(String.format("https://localhost:%d/api", port), username, password, false, timeout, maxConnections);
wireMockRule.stubFor(post("/api/types/Volume/instances") wireMockRule.stubFor(post("/api/types/Volume/instances")
.willReturn(aResponse() .willReturn(aResponse()

View File

@ -24,8 +24,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks; import static org.mockito.MockitoAnnotations.initMocks;
@ -40,13 +39,11 @@ import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProviderManag
import org.apache.cloudstack.engine.subsystem.api.storage.HypervisorHostListener; import org.apache.cloudstack.engine.subsystem.api.storage.HypervisorHostListener;
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore; import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope; import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
import org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClient;
import org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClientConnectionPool; import org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClientConnectionPool;
import org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClientImpl; import org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClientImpl;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao; import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.storage.datastore.provider.ScaleIOHostListener;
import org.apache.cloudstack.storage.datastore.util.ScaleIOUtil; import org.apache.cloudstack.storage.datastore.util.ScaleIOUtil;
import org.apache.cloudstack.storage.volume.datastore.PrimaryDataStoreHelper; import org.apache.cloudstack.storage.volume.datastore.PrimaryDataStoreHelper;
import org.junit.Before; import org.junit.Before;
@ -54,19 +51,14 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.Spy; import org.mockito.Spy;
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 com.cloud.agent.AgentManager;
import com.cloud.agent.api.ModifyStoragePoolAnswer;
import com.cloud.agent.api.ModifyStoragePoolCommand;
import com.cloud.host.Host; import com.cloud.host.Host;
import com.cloud.host.HostVO; import com.cloud.host.HostVO;
import com.cloud.host.Status; import com.cloud.host.Status;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor; import com.cloud.hypervisor.Hypervisor;
import com.cloud.resource.ResourceManager; import com.cloud.resource.ResourceManager;
import com.cloud.resource.ResourceState; import com.cloud.resource.ResourceState;
@ -80,9 +72,9 @@ import com.cloud.storage.VMTemplateStoragePoolVO;
import com.cloud.storage.dao.StoragePoolHostDao; import com.cloud.storage.dao.StoragePoolHostDao;
import com.cloud.template.TemplateManager; import com.cloud.template.TemplateManager;
import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.exception.CloudRuntimeException;
import org.springframework.test.util.ReflectionTestUtils;
@PrepareForTest(ScaleIOGatewayClient.class) @RunWith(MockitoJUnitRunner.class)
@RunWith(PowerMockRunner.class)
public class ScaleIOPrimaryDataStoreLifeCycleTest { public class ScaleIOPrimaryDataStoreLifeCycleTest {
@Mock @Mock
@ -96,8 +88,6 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
@Mock @Mock
private StoragePoolAutomation storagePoolAutomation; private StoragePoolAutomation storagePoolAutomation;
@Mock @Mock
private HostDao hostDao;
@Mock
private StoragePoolHostDao storagePoolHostDao; private StoragePoolHostDao storagePoolHostDao;
@Mock @Mock
private DataStoreProviderManager dataStoreProviderMgr; private DataStoreProviderManager dataStoreProviderMgr;
@ -109,18 +99,12 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
private PrimaryDataStore store; private PrimaryDataStore store;
@Mock @Mock
private TemplateManager templateMgr; private TemplateManager templateMgr;
@Mock
private AgentManager agentMgr;
@Mock
ModifyStoragePoolAnswer answer;
@Spy
@InjectMocks @InjectMocks
private StorageManager storageMgr = new StorageManagerImpl(); private StorageManager storageMgr = new StorageManagerImpl();
@Spy @Spy
@InjectMocks private HypervisorHostListener hostListener;
private HypervisorHostListener hostListener = new ScaleIOHostListener();
@InjectMocks @InjectMocks
private ScaleIOPrimaryDataStoreLifeCycle scaleIOPrimaryDataStoreLifeCycleTest; private ScaleIOPrimaryDataStoreLifeCycle scaleIOPrimaryDataStoreLifeCycleTest;
@ -128,6 +112,7 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
@Before @Before
public void setUp() { public void setUp() {
initMocks(this); initMocks(this);
ReflectionTestUtils.setField(scaleIOPrimaryDataStoreLifeCycleTest, "storageMgr", storageMgr);
} }
@Test @Test
@ -135,9 +120,11 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
final DataStore dataStore = mock(DataStore.class); final DataStore dataStore = mock(DataStore.class);
when(dataStore.getId()).thenReturn(1L); when(dataStore.getId()).thenReturn(1L);
PowerMockito.mockStatic(ScaleIOGatewayClient.class); MockedStatic<ScaleIOGatewayClientConnectionPool> scaleIOGatewayClientConnectionPoolMocked = mockStatic(ScaleIOGatewayClientConnectionPool.class);
ScaleIOGatewayClientImpl client = mock(ScaleIOGatewayClientImpl.class); ScaleIOGatewayClientImpl client = mock(ScaleIOGatewayClientImpl.class);
when(ScaleIOGatewayClientConnectionPool.getInstance().getClient(1L, storagePoolDetailsDao)).thenReturn(client); ScaleIOGatewayClientConnectionPool pool = mock(ScaleIOGatewayClientConnectionPool.class);
scaleIOGatewayClientConnectionPoolMocked.when(() -> ScaleIOGatewayClientConnectionPool.getInstance()).thenReturn(pool);
when(pool.getClient(1L, storagePoolDetailsDao)).thenReturn(client);
when(client.haveConnectedSdcs()).thenReturn(true); when(client.haveConnectedSdcs()).thenReturn(true);
@ -157,28 +144,19 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
when(dataStoreMgr.getDataStore(anyLong(), eq(DataStoreRole.Primary))).thenReturn(store); when(dataStoreMgr.getDataStore(anyLong(), eq(DataStoreRole.Primary))).thenReturn(store);
when(store.getId()).thenReturn(1L); when(store.getId()).thenReturn(1L);
when(store.getPoolType()).thenReturn(Storage.StoragePoolType.PowerFlex);
when(store.isShared()).thenReturn(true); when(store.isShared()).thenReturn(true);
when(store.getName()).thenReturn("ScaleIOPool"); when(store.getName()).thenReturn("ScaleIOPool");
when(store.getStorageProviderName()).thenReturn(ScaleIOUtil.PROVIDER_NAME); when(store.getStorageProviderName()).thenReturn(ScaleIOUtil.PROVIDER_NAME);
when(dataStoreProviderMgr.getDataStoreProvider(ScaleIOUtil.PROVIDER_NAME)).thenReturn(dataStoreProvider); when(dataStoreProviderMgr.getDataStoreProvider(ScaleIOUtil.PROVIDER_NAME)).thenReturn(dataStoreProvider);
when(dataStoreProvider.getName()).thenReturn(ScaleIOUtil.PROVIDER_NAME); when(dataStoreProvider.getName()).thenReturn(ScaleIOUtil.PROVIDER_NAME);
when(hostListener.hostConnect(Mockito.anyLong(), Mockito.anyLong())).thenReturn(true);
storageMgr.registerHostListener(ScaleIOUtil.PROVIDER_NAME, hostListener); storageMgr.registerHostListener(ScaleIOUtil.PROVIDER_NAME, hostListener);
when(agentMgr.easySend(anyLong(), Mockito.any(ModifyStoragePoolCommand.class))).thenReturn(answer);
when(answer.getResult()).thenReturn(true);
when(storagePoolHostDao.findByPoolHost(anyLong(), anyLong())).thenReturn(null);
when(hostDao.findById(1L)).thenReturn(host1);
when(hostDao.findById(2L)).thenReturn(host2);
when(dataStoreHelper.attachZone(Mockito.any(DataStore.class))).thenReturn(null); when(dataStoreHelper.attachZone(Mockito.any(DataStore.class))).thenReturn(null);
scaleIOPrimaryDataStoreLifeCycleTest.attachZone(dataStore, scope, Hypervisor.HypervisorType.KVM); boolean result = scaleIOPrimaryDataStoreLifeCycleTest.attachZone(dataStore, scope, Hypervisor.HypervisorType.KVM);
verify(storageMgr,times(2)).connectHostToSharedPool(Mockito.any(Long.class), Mockito.any(Long.class)); assertThat(result).isTrue();
verify(storagePoolHostDao,times(2)).persist(Mockito.any(StoragePoolHostVO.class));
} }
@Test(expected = CloudRuntimeException.class) @Test(expected = CloudRuntimeException.class)
@ -224,7 +202,6 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
public void testDeleteDataStoreWithStoragePoolNull() { public void testDeleteDataStoreWithStoragePoolNull() {
final PrimaryDataStore store = mock(PrimaryDataStore.class); final PrimaryDataStore store = mock(PrimaryDataStore.class);
when(primaryDataStoreDao.findById(anyLong())).thenReturn(null); when(primaryDataStoreDao.findById(anyLong())).thenReturn(null);
when(dataStoreHelper.deletePrimaryDataStore(any(DataStore.class))).thenReturn(true);
final boolean result = scaleIOPrimaryDataStoreLifeCycleTest.deleteDataStore(store); final boolean result = scaleIOPrimaryDataStoreLifeCycleTest.deleteDataStore(store);
assertThat(result).isFalse(); assertThat(result).isFalse();
} }
@ -233,6 +210,7 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
public void testDeleteDataStore() { public void testDeleteDataStore() {
final PrimaryDataStore store = mock(PrimaryDataStore.class); final PrimaryDataStore store = mock(PrimaryDataStore.class);
final StoragePoolVO storagePoolVO = mock(StoragePoolVO.class); final StoragePoolVO storagePoolVO = mock(StoragePoolVO.class);
when(store.getId()).thenReturn(1L);
when(primaryDataStoreDao.findById(anyLong())).thenReturn(storagePoolVO); when(primaryDataStoreDao.findById(anyLong())).thenReturn(storagePoolVO);
List<VMTemplateStoragePoolVO> unusedTemplates = new ArrayList<>(); List<VMTemplateStoragePoolVO> unusedTemplates = new ArrayList<>();
when(templateMgr.getUnusedTemplatesInPool(storagePoolVO)).thenReturn(unusedTemplates); when(templateMgr.getUnusedTemplatesInPool(storagePoolVO)).thenReturn(unusedTemplates);

View File

@ -1062,7 +1062,7 @@
<exclude>ui/public/**</exclude> <exclude>ui/public/**</exclude>
<exclude>ui/legacy/**</exclude> <exclude>ui/legacy/**</exclude>
<exclude>utils/testsmallfileinactive</exclude> <exclude>utils/testsmallfileinactive</exclude>
<exclude>utils/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker</exclude> <exclude>**/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker</exclude>
</excludes> </excludes>
</configuration> </configuration>
</plugin> </plugin>