Skip to content

Commit 7e89a31

Browse files
committed
Fix config validation for value range and code coverage improvement
1 parent 41dd137 commit 7e89a31

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

server/src/main/java/org/apache/cloudstack/vm/lease/VMLeaseManager.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.cloudstack.vm.lease;
2020

2121
import com.cloud.utils.component.Manager;
22+
import com.google.common.annotations.VisibleForTesting;
2223
import org.apache.cloudstack.framework.config.ConfigKey;
2324

2425
import java.util.List;
@@ -29,7 +30,9 @@ public interface VMLeaseManager extends Manager {
2930

3031
enum ExpiryAction {
3132
STOP,
32-
DESTROY
33+
DESTROY,
34+
@VisibleForTesting
35+
UNKNOWN
3336
}
3437

3538
enum LeaseActionExecution {
@@ -45,15 +48,15 @@ enum LeaseActionExecution {
4548
"Re-enabling feature will not cause lease expiry actions on grandfathered instances",
4649
true, List.of(ConfigKey.Scope.Global));
4750

48-
ConfigKey<Long> InstanceLeaseSchedulerInterval = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Long.class,
51+
ConfigKey<Integer> InstanceLeaseSchedulerInterval = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Integer.class,
4952
"instance.lease.scheduler.interval", "3600", "VM Lease Scheduler interval in seconds",
5053
false, List.of(ConfigKey.Scope.Global));
5154

52-
ConfigKey<Long> InstanceLeaseExpiryEventSchedulerInterval = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Long.class,
55+
ConfigKey<Integer> InstanceLeaseExpiryEventSchedulerInterval = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Integer.class,
5356
"instance.lease.eventscheduler.interval", "86400", "Lease expiry event Scheduler interval in seconds",
5457
false, List.of(ConfigKey.Scope.Global));
5558

56-
ConfigKey<Long> InstanceLeaseExpiryEventDaysBefore = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Long.class,
59+
ConfigKey<Integer> InstanceLeaseExpiryEventDaysBefore = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Integer.class,
5760
"instance.lease.expiryevent.daysbefore", "7", "Indicates how many days in advance, expiry events will be created before expiry.",
5861
true, List.of(ConfigKey.Scope.Global));
5962

server/src/main/java/org/apache/cloudstack/vm/lease/VMLeaseManagerImpl.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ protected void runInContext() {
203203
try {
204204
if (scanLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION)) {
205205
try {
206-
List<UserVmJoinVO> leaseExpiringForInstances = userVmJoinDao.listLeaseInstancesExpiringInDays(InstanceLeaseExpiryEventDaysBefore.value().intValue());
206+
List<UserVmJoinVO> leaseExpiringForInstances = userVmJoinDao.listLeaseInstancesExpiringInDays(InstanceLeaseExpiryEventDaysBefore.value());
207207
for (UserVmJoinVO instance : leaseExpiringForInstances) {
208208
String leaseExpiryEventMsg = String.format("Lease expiring for for instance: %s (id: %s) with action: %s",
209209
instance.getName(), instance.getUuid(), instance.getLeaseExpiryAction());
@@ -220,11 +220,6 @@ protected void runInContext() {
220220
}
221221
}
222222

223-
@Override
224-
public Map<String, Object> getConfigParams() {
225-
return super.getConfigParams();
226-
}
227-
228223
protected void reallyRun() {
229224
// fetch user_instances having leaseDuration configured and has expired
230225
List<UserVmJoinVO> leaseExpiredInstances = userVmJoinDao.listEligibleInstancesWithExpiredLease();

server/src/test/java/org/apache/cloudstack/vm/lease/VMLeaseManagerImplTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
import com.cloud.utils.db.GlobalLock;
2828
import com.cloud.utils.exception.CloudRuntimeException;
2929
import com.cloud.vm.VirtualMachine;
30+
import com.cloud.vm.VmDetailConstants;
3031
import com.cloud.vm.dao.UserVmDetailsDao;
3132
import org.apache.cloudstack.api.command.user.vm.DestroyVMCmd;
3233
import org.apache.cloudstack.api.command.user.vm.StopVMCmd;
34+
import org.apache.cloudstack.framework.config.ConfigKey;
3335
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
3436
import org.apache.cloudstack.framework.jobs.AsyncJobDispatcher;
3537
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
@@ -47,6 +49,7 @@
4749
import org.springframework.context.ApplicationContext;
4850

4951
import javax.naming.ConfigurationException;
52+
import java.lang.reflect.Field;
5053
import java.util.ArrayList;
5154
import java.util.Arrays;
5255
import java.util.HashMap;
@@ -56,6 +59,7 @@
5659
import static org.junit.Assert.assertEquals;
5760
import static org.junit.Assert.assertNotNull;
5861
import static org.junit.Assert.assertNull;
62+
import static org.junit.Assert.assertTrue;
5963
import static org.mockito.ArgumentMatchers.any;
6064
import static org.mockito.ArgumentMatchers.anyBoolean;
6165
import 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

Comments
 (0)