2727import com .cloud .utils .db .GlobalLock ;
2828import com .cloud .utils .exception .CloudRuntimeException ;
2929import com .cloud .vm .VirtualMachine ;
30+ import com .cloud .vm .VmDetailConstants ;
3031import com .cloud .vm .dao .UserVmDetailsDao ;
3132import org .apache .cloudstack .api .command .user .vm .DestroyVMCmd ;
3233import org .apache .cloudstack .api .command .user .vm .StopVMCmd ;
34+ import org .apache .cloudstack .framework .config .ConfigKey ;
3335import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
3436import org .apache .cloudstack .framework .jobs .AsyncJobDispatcher ;
3537import org .apache .cloudstack .framework .jobs .AsyncJobManager ;
4749import org .springframework .context .ApplicationContext ;
4850
4951import javax .naming .ConfigurationException ;
52+ import java .lang .reflect .Field ;
5053import java .util .ArrayList ;
5154import java .util .Arrays ;
5255import java .util .HashMap ;
5659import static org .junit .Assert .assertEquals ;
5760import static org .junit .Assert .assertNotNull ;
5861import static org .junit .Assert .assertNull ;
62+ import static org .junit .Assert .assertTrue ;
5963import static org .mockito .ArgumentMatchers .any ;
6064import static org .mockito .ArgumentMatchers .anyBoolean ;
6165import static org .mockito .ArgumentMatchers .anyLong ;
@@ -246,6 +250,57 @@ public void testGetLeaseExpiryInvalidAction() {
246250 assertNull (vmLeaseManager .getLeaseExpiryAction (vm ));
247251 }
248252
253+ @ Test
254+ public void testGetComponentName () {
255+ assertEquals (vmLeaseManager .getConfigComponentName (), "VMLeaseManager" );
256+ }
257+
258+ @ Test
259+ public void testConfigKeys () {
260+ assertEquals (vmLeaseManager .getConfigKeys ().length , 4 );
261+ }
262+
263+ @ Test
264+ public void testConfigure () throws Exception {
265+ overrideDefaultConfigValue (VMLeaseManager .InstanceLeaseEnabled , "true" );
266+ vmLeaseManager .configure ("VMLeaseManagerImpl" , new HashMap <>());
267+ }
268+
269+ @ Test
270+ public void testDefaultExpiryAction () {
271+ UserVmJoinVO vm = createMockVm (1L , VM_UUID , VM_NAME , VirtualMachine .State .Running , false );
272+ assertNull (vmLeaseManager .executeExpiryAction (vm , VMLeaseManager .ExpiryAction .UNKNOWN , 123L ));
273+ }
274+
275+ @ Test
276+ public void testStopShouldShutdownExecutors () {
277+ assertTrue (vmLeaseManager .stop ());
278+ }
279+
280+ @ Test
281+ public void testCancelLeaseOnExistingInstances () {
282+ UserVmJoinVO vm = createMockVm (1L , VM_UUID , VM_NAME , VirtualMachine .State .Running , true );
283+ when (userVmJoinDao .listLeaseInstancesExpiringInDays (-1 )).thenReturn (List .of (vm ));
284+ try (MockedStatic <ActionEventUtils > utilities = Mockito .mockStatic (ActionEventUtils .class )) {
285+ utilities .when (() -> ActionEventUtils .onStartedActionEvent (Mockito .anyLong (), Mockito .anyLong (), Mockito .anyString (),
286+ Mockito .anyString (), Mockito .anyLong (), Mockito .anyString (), Mockito .anyBoolean (), Mockito .anyLong ())).thenReturn (1L );
287+ vmLeaseManager .cancelLeaseOnExistingInstances ();
288+ verify (userVmDetailsDao ).addDetail (1L , VmDetailConstants .INSTANCE_LEASE_EXECUTION , VMLeaseManager .LeaseActionExecution .CANCELLED .name (), false );
289+ }
290+ }
291+
292+ @ Test
293+ public void testOnLeaseFeatureToggleEnabled () throws Exception {
294+ overrideDefaultConfigValue (VMLeaseManager .InstanceLeaseEnabled , "true" );
295+ vmLeaseManager .onLeaseFeatureToggle ();
296+ }
297+
298+ @ Test
299+ public void testOnLeaseFeatureToggleDisabled () throws Exception {
300+ overrideDefaultConfigValue (VMLeaseManager .InstanceLeaseEnabled , "false" );
301+ vmLeaseManager .onLeaseFeatureToggle ();
302+ }
303+
249304 private UserVmJoinVO createMockVm (Long id , String uuid , String name , VirtualMachine .State state , boolean deleteProtection ) {
250305 return createMockVm (id , uuid , name , state , deleteProtection , "STOP" );
251306 }
@@ -260,4 +315,10 @@ private UserVmJoinVO createMockVm(Long id, String uuid, String name, VirtualMach
260315 when (vm .getLeaseExpiryAction ()).thenReturn (expiryAction );
261316 return vm ;
262317 }
318+
319+ private void overrideDefaultConfigValue (final ConfigKey configKey , final String value ) throws IllegalAccessException , NoSuchFieldException {
320+ final Field f = ConfigKey .class .getDeclaredField ("_defaultValue" );
321+ f .setAccessible (true );
322+ f .set (configKey , value );
323+ }
263324}
0 commit comments