From 27b0a64b40098beb30f59b341d56d9a77759eb33 Mon Sep 17 00:00:00 2001 From: Vishesh Date: Thu, 15 Jun 2023 15:23:10 +0530 Subject: [PATCH] Fix scaleio tests (#7622) Co-authored-by: Wei Zhou --- plugins/storage/volume/scaleio/pom.xml | 3 -- .../client/ScaleIOGatewayClientImplTest.java | 4 +- .../ScaleIOPrimaryDataStoreLifeCycleTest.java | 52 ++++++------------- .../org.mockito.plugins.MockMaker | 1 + pom.xml | 2 +- .../org.mockito.plugins.MockMaker | 2 +- 6 files changed, 20 insertions(+), 44 deletions(-) create mode 100644 plugins/storage/volume/scaleio/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/plugins/storage/volume/scaleio/pom.xml b/plugins/storage/volume/scaleio/pom.xml index 17740b54fd1..390d11b0637 100644 --- a/plugins/storage/volume/scaleio/pom.xml +++ b/plugins/storage/volume/scaleio/pom.xml @@ -44,9 +44,6 @@ maven-surefire-plugin - - true - integration-test diff --git a/plugins/storage/volume/scaleio/src/test/java/org/apache/cloudstack/storage/datastore/client/ScaleIOGatewayClientImplTest.java b/plugins/storage/volume/scaleio/src/test/java/org/apache/cloudstack/storage/datastore/client/ScaleIOGatewayClientImplTest.java index 577b918a4d8..cf624c26680 100644 --- a/plugins/storage/volume/scaleio/src/test/java/org/apache/cloudstack/storage/datastore/client/ScaleIOGatewayClientImplTest.java +++ b/plugins/storage/volume/scaleio/src/test/java/org/apache/cloudstack/storage/datastore/client/ScaleIOGatewayClientImplTest.java @@ -48,7 +48,7 @@ import com.github.tomakehurst.wiremock.junit.WireMockRule; @RunWith(MockitoJUnitRunner.class) public class ScaleIOGatewayClientImplTest { - private final int port = 443; + private final int port = 8443; private final int timeout = 30; private final int maxConnections = 50; private final String username = "admin"; @@ -70,7 +70,7 @@ public class ScaleIOGatewayClientImplTest { .withHeader("content-type", "application/json;charset=UTF-8") .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") .willReturn(aResponse() diff --git a/plugins/storage/volume/scaleio/src/test/java/org/apache/cloudstack/storage/datastore/lifecycle/ScaleIOPrimaryDataStoreLifeCycleTest.java b/plugins/storage/volume/scaleio/src/test/java/org/apache/cloudstack/storage/datastore/lifecycle/ScaleIOPrimaryDataStoreLifeCycleTest.java index 6cc7b874557..17d0c9b9edf 100644 --- a/plugins/storage/volume/scaleio/src/test/java/org/apache/cloudstack/storage/datastore/lifecycle/ScaleIOPrimaryDataStoreLifeCycleTest.java +++ b/plugins/storage/volume/scaleio/src/test/java/org/apache/cloudstack/storage/datastore/lifecycle/ScaleIOPrimaryDataStoreLifeCycleTest.java @@ -24,8 +24,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; 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.PrimaryDataStore; 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.ScaleIOGatewayClientImpl; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao; 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.volume.datastore.PrimaryDataStoreHelper; import org.junit.Before; @@ -54,19 +51,14 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.Spy; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; -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.HostVO; import com.cloud.host.Status; -import com.cloud.host.dao.HostDao; import com.cloud.hypervisor.Hypervisor; import com.cloud.resource.ResourceManager; import com.cloud.resource.ResourceState; @@ -80,9 +72,9 @@ import com.cloud.storage.VMTemplateStoragePoolVO; import com.cloud.storage.dao.StoragePoolHostDao; import com.cloud.template.TemplateManager; import com.cloud.utils.exception.CloudRuntimeException; +import org.springframework.test.util.ReflectionTestUtils; -@PrepareForTest(ScaleIOGatewayClient.class) -@RunWith(PowerMockRunner.class) +@RunWith(MockitoJUnitRunner.class) public class ScaleIOPrimaryDataStoreLifeCycleTest { @Mock @@ -96,8 +88,6 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest { @Mock private StoragePoolAutomation storagePoolAutomation; @Mock - private HostDao hostDao; - @Mock private StoragePoolHostDao storagePoolHostDao; @Mock private DataStoreProviderManager dataStoreProviderMgr; @@ -109,18 +99,12 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest { private PrimaryDataStore store; @Mock private TemplateManager templateMgr; - @Mock - private AgentManager agentMgr; - @Mock - ModifyStoragePoolAnswer answer; - @Spy @InjectMocks private StorageManager storageMgr = new StorageManagerImpl(); @Spy - @InjectMocks - private HypervisorHostListener hostListener = new ScaleIOHostListener(); + private HypervisorHostListener hostListener; @InjectMocks private ScaleIOPrimaryDataStoreLifeCycle scaleIOPrimaryDataStoreLifeCycleTest; @@ -128,6 +112,7 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest { @Before public void setUp() { initMocks(this); + ReflectionTestUtils.setField(scaleIOPrimaryDataStoreLifeCycleTest, "storageMgr", storageMgr); } @Test @@ -135,9 +120,11 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest { final DataStore dataStore = mock(DataStore.class); when(dataStore.getId()).thenReturn(1L); - PowerMockito.mockStatic(ScaleIOGatewayClient.class); + MockedStatic scaleIOGatewayClientConnectionPoolMocked = mockStatic(ScaleIOGatewayClientConnectionPool.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); @@ -157,28 +144,19 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest { when(dataStoreMgr.getDataStore(anyLong(), eq(DataStoreRole.Primary))).thenReturn(store); when(store.getId()).thenReturn(1L); - when(store.getPoolType()).thenReturn(Storage.StoragePoolType.PowerFlex); when(store.isShared()).thenReturn(true); when(store.getName()).thenReturn("ScaleIOPool"); when(store.getStorageProviderName()).thenReturn(ScaleIOUtil.PROVIDER_NAME); when(dataStoreProviderMgr.getDataStoreProvider(ScaleIOUtil.PROVIDER_NAME)).thenReturn(dataStoreProvider); when(dataStoreProvider.getName()).thenReturn(ScaleIOUtil.PROVIDER_NAME); + when(hostListener.hostConnect(Mockito.anyLong(), Mockito.anyLong())).thenReturn(true); 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); - scaleIOPrimaryDataStoreLifeCycleTest.attachZone(dataStore, scope, Hypervisor.HypervisorType.KVM); - verify(storageMgr,times(2)).connectHostToSharedPool(Mockito.any(Long.class), Mockito.any(Long.class)); - verify(storagePoolHostDao,times(2)).persist(Mockito.any(StoragePoolHostVO.class)); + boolean result = scaleIOPrimaryDataStoreLifeCycleTest.attachZone(dataStore, scope, Hypervisor.HypervisorType.KVM); + assertThat(result).isTrue(); } @Test(expected = CloudRuntimeException.class) @@ -224,7 +202,6 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest { public void testDeleteDataStoreWithStoragePoolNull() { final PrimaryDataStore store = mock(PrimaryDataStore.class); when(primaryDataStoreDao.findById(anyLong())).thenReturn(null); - when(dataStoreHelper.deletePrimaryDataStore(any(DataStore.class))).thenReturn(true); final boolean result = scaleIOPrimaryDataStoreLifeCycleTest.deleteDataStore(store); assertThat(result).isFalse(); } @@ -233,6 +210,7 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest { public void testDeleteDataStore() { final PrimaryDataStore store = mock(PrimaryDataStore.class); final StoragePoolVO storagePoolVO = mock(StoragePoolVO.class); + when(store.getId()).thenReturn(1L); when(primaryDataStoreDao.findById(anyLong())).thenReturn(storagePoolVO); List unusedTemplates = new ArrayList<>(); when(templateMgr.getUnusedTemplatesInPool(storagePoolVO)).thenReturn(unusedTemplates); diff --git a/plugins/storage/volume/scaleio/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/plugins/storage/volume/scaleio/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 00000000000..1f0955d450f --- /dev/null +++ b/plugins/storage/volume/scaleio/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline diff --git a/pom.xml b/pom.xml index 2497dee7e0a..7aa0724000f 100644 --- a/pom.xml +++ b/pom.xml @@ -1062,7 +1062,7 @@ ui/public/** ui/legacy/** utils/testsmallfileinactive - utils/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker + **/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/utils/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/utils/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker index ca6ee9cea8e..1f0955d450f 100644 --- a/utils/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker +++ b/utils/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -1 +1 @@ -mock-maker-inline \ No newline at end of file +mock-maker-inline