2424import static org .mockito .ArgumentMatchers .anyLong ;
2525import static org .mockito .ArgumentMatchers .eq ;
2626import static org .mockito .Mockito .mock ;
27- import static org .mockito .Mockito .times ;
28- import static org .mockito .Mockito .verify ;
2927import static org .mockito .Mockito .when ;
3028import static org .mockito .MockitoAnnotations .initMocks ;
3129
4038import org .apache .cloudstack .engine .subsystem .api .storage .HypervisorHostListener ;
4139import org .apache .cloudstack .engine .subsystem .api .storage .PrimaryDataStore ;
4240import org .apache .cloudstack .engine .subsystem .api .storage .ZoneScope ;
43- import org .apache .cloudstack .storage .datastore .client .ScaleIOGatewayClient ;
4441import org .apache .cloudstack .storage .datastore .client .ScaleIOGatewayClientConnectionPool ;
4542import org .apache .cloudstack .storage .datastore .client .ScaleIOGatewayClientImpl ;
4643import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
4744import org .apache .cloudstack .storage .datastore .db .StoragePoolDetailsDao ;
4845import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
49- import org .apache .cloudstack .storage .datastore .provider .ScaleIOHostListener ;
5046import org .apache .cloudstack .storage .datastore .util .ScaleIOUtil ;
5147import org .apache .cloudstack .storage .volume .datastore .PrimaryDataStoreHelper ;
5248import org .junit .Before ;
5551import org .mockito .InjectMocks ;
5652import org .mockito .Mock ;
5753import org .mockito .Mockito ;
58- import org .mockito .Spy ;
54+ import org .mockito .stubbing . Answer ;
5955import org .powermock .api .mockito .PowerMockito ;
6056import org .powermock .core .classloader .annotations .PrepareForTest ;
6157import org .powermock .modules .junit4 .PowerMockRunner ;
58+ import org .springframework .test .util .ReflectionTestUtils ;
6259
6360import com .cloud .agent .AgentManager ;
6461import com .cloud .agent .api .ModifyStoragePoolAnswer ;
65- import com .cloud .agent .api .ModifyStoragePoolCommand ;
6662import com .cloud .host .Host ;
6763import com .cloud .host .HostVO ;
6864import com .cloud .host .Status ;
8177import com .cloud .template .TemplateManager ;
8278import com .cloud .utils .exception .CloudRuntimeException ;
8379
84- @ PrepareForTest (ScaleIOGatewayClient .class )
80+ @ PrepareForTest (ScaleIOGatewayClientConnectionPool .class )
8581@ RunWith (PowerMockRunner .class )
8682public class ScaleIOPrimaryDataStoreLifeCycleTest {
8783
@@ -114,30 +110,28 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
114110 @ Mock
115111 ModifyStoragePoolAnswer answer ;
116112
117- @ Spy
118113 @ InjectMocks
119114 private StorageManager storageMgr = new StorageManagerImpl ();
120115
121- @ Spy
122- @ InjectMocks
123- private HypervisorHostListener hostListener = new ScaleIOHostListener ();
124-
125116 @ InjectMocks
126117 private ScaleIOPrimaryDataStoreLifeCycle scaleIOPrimaryDataStoreLifeCycleTest ;
127118
128119 @ Before
129120 public void setUp () {
130121 initMocks (this );
122+ ReflectionTestUtils .setField (scaleIOPrimaryDataStoreLifeCycleTest , "storageMgr" , storageMgr );
131123 }
132124
133125 @ Test
134126 public void testAttachZone () throws Exception {
135127 final DataStore dataStore = mock (DataStore .class );
136128 when (dataStore .getId ()).thenReturn (1L );
137129
138- PowerMockito .mockStatic (ScaleIOGatewayClient .class );
130+ PowerMockito .mockStatic (ScaleIOGatewayClientConnectionPool .class );
139131 ScaleIOGatewayClientImpl client = mock (ScaleIOGatewayClientImpl .class );
140- when (ScaleIOGatewayClientConnectionPool .getInstance ().getClient (1L , storagePoolDetailsDao )).thenReturn (client );
132+ ScaleIOGatewayClientConnectionPool pool = mock (ScaleIOGatewayClientConnectionPool .class );
133+ when (pool .getClient (1L , storagePoolDetailsDao )).thenReturn (client );
134+ when (ScaleIOGatewayClientConnectionPool .getInstance ()).thenAnswer ((Answer <ScaleIOGatewayClientConnectionPool >) invocation -> pool );
141135
142136 when (client .haveConnectedSdcs ()).thenReturn (true );
143137
@@ -157,28 +151,20 @@ public void testAttachZone() throws Exception {
157151
158152 when (dataStoreMgr .getDataStore (anyLong (), eq (DataStoreRole .Primary ))).thenReturn (store );
159153 when (store .getId ()).thenReturn (1L );
160- when (store .getPoolType ()).thenReturn (Storage .StoragePoolType .PowerFlex );
161154 when (store .isShared ()).thenReturn (true );
162155 when (store .getName ()).thenReturn ("ScaleIOPool" );
163156 when (store .getStorageProviderName ()).thenReturn (ScaleIOUtil .PROVIDER_NAME );
164157
165158 when (dataStoreProviderMgr .getDataStoreProvider (ScaleIOUtil .PROVIDER_NAME )).thenReturn (dataStoreProvider );
166159 when (dataStoreProvider .getName ()).thenReturn (ScaleIOUtil .PROVIDER_NAME );
160+ HypervisorHostListener hostListener = Mockito .mock (HypervisorHostListener .class );
161+ when (hostListener .hostConnect (Mockito .anyLong (), Mockito .anyLong ())).thenReturn (true );
167162 storageMgr .registerHostListener (ScaleIOUtil .PROVIDER_NAME , hostListener );
168163
169- when (agentMgr .easySend (anyLong (), Mockito .any (ModifyStoragePoolCommand .class ))).thenReturn (answer );
170- when (answer .getResult ()).thenReturn (true );
171-
172- when (storagePoolHostDao .findByPoolHost (anyLong (), anyLong ())).thenReturn (null );
173-
174- when (hostDao .findById (1L )).thenReturn (host1 );
175- when (hostDao .findById (2L )).thenReturn (host2 );
176-
177164 when (dataStoreHelper .attachZone (Mockito .any (DataStore .class ))).thenReturn (null );
178165
179- scaleIOPrimaryDataStoreLifeCycleTest .attachZone (dataStore , scope , Hypervisor .HypervisorType .KVM );
180- verify (storageMgr ,times (2 )).connectHostToSharedPool (Mockito .any (Long .class ), Mockito .any (Long .class ));
181- verify (storagePoolHostDao ,times (2 )).persist (Mockito .any (StoragePoolHostVO .class ));
166+ boolean result = scaleIOPrimaryDataStoreLifeCycleTest .attachZone (dataStore , scope , Hypervisor .HypervisorType .KVM );
167+ assertThat (result ).isTrue ();
182168 }
183169
184170 @ Test (expected = CloudRuntimeException .class )
@@ -239,6 +225,9 @@ public void testDeleteDataStore() {
239225 List <StoragePoolHostVO > poolHostVOs = new ArrayList <>();
240226 when (storagePoolHostDao .listByPoolId (anyLong ())).thenReturn (poolHostVOs );
241227 when (dataStoreHelper .deletePrimaryDataStore (any (DataStore .class ))).thenReturn (true );
228+ PowerMockito .mockStatic (ScaleIOGatewayClientConnectionPool .class );
229+ ScaleIOGatewayClientConnectionPool pool = mock (ScaleIOGatewayClientConnectionPool .class );
230+ when (ScaleIOGatewayClientConnectionPool .getInstance ()).thenAnswer ((Answer <ScaleIOGatewayClientConnectionPool >) invocation -> pool );
242231 final boolean result = scaleIOPrimaryDataStoreLifeCycleTest .deleteDataStore (store );
243232 assertThat (result ).isTrue ();
244233 }
0 commit comments