Skip to content

Commit

Permalink
Avoid NPE on refreshlun call without plugged VMs
Browse files Browse the repository at this point in the history
Signed-off-by: Laszlo Szomor <laszomor@gmail.com>
  • Loading branch information
lszomor committed May 19, 2023
1 parent d37b4f7 commit 5d1bf92
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ovirt.engine.core.bll.storage.pool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -218,7 +219,12 @@ private boolean updateLunDisksOnGuests(Set<LUNs> lunsToUpdateInDb) {
}

private List<VM> getPluggedVms(Guid diskId) {
return vmDao.getForDisk(diskId, false).get(Boolean.TRUE);
List<VM> result = vmDao.getForDisk(diskId, false).get(Boolean.TRUE);
// Return an empty list if no plugged VMs found
if (result == null) {
result = new ArrayList<>();
}
return result;
}

private Stream<Guid> getIdsOfDirectLunsToSync() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

Expand All @@ -27,12 +28,15 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.ovirt.engine.core.bll.ValidateTestUtils;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.action.SyncDirectLunsParameters;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.businessentities.storage.DiskLunMap;
import org.ovirt.engine.core.common.businessentities.storage.LUNs;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogable;
import org.ovirt.engine.core.dao.DiskLunMapDao;
import org.ovirt.engine.core.dao.LunDao;
import org.ovirt.engine.core.dao.StoragePoolDao;
Expand Down Expand Up @@ -62,11 +66,15 @@ public class SyncDirectLunsCommandTest {
@Mock
private StorageServerConnectionDao serverConnectionDao;

@Mock
private AuditLogDirector auditLogDirector;

private LUNs lun1;
private LUNs lun2;
private LUNs lun3;
private DiskLunMap disk1Lun1Map;
private Map<Boolean, List<VM>> lunsPerHost;
private Map<Boolean, List<VM>> emptyLunsPerHost;

@Spy
@InjectMocks
Expand Down Expand Up @@ -119,6 +127,19 @@ public void testGetLunsToUpdateInDbLunIsPartOfStorageDomain() {
assertEquals(Collections.singleton(lun1), command.getLunsToUpdateInDb());
}

@Test
public void testSyncDirectLunsWithoutPluggedVms() {
command.getParameters().setDeviceList(Arrays.asList(lun1));
mockLunToDiskIdsOfDirectLunsAttachedToVmsInPool(lun1);
command.getParameters().setDirectLunIds(Collections.singleton(lun1.getDiskId()));
when(diskLunMapDao.getDiskLunMapByDiskId(lun1.getDiskId())).thenReturn(disk1Lun1Map);
when(vmDao.getForDisk(lun1.getDiskId(), false)).thenReturn(emptyLunsPerHost);
doReturn(true).when(command).validateVds();
doNothing().when(auditLogDirector).log(any(AuditLogable.class), any(AuditLogType.class));
command.executeCommand();
assertTrue(command.getReturnValue().getSucceeded(), "SyncDirectLuns without plugged VMs failed");
}

@Test
public void validateWithDirectLunIdAndInvalidVds() {
command.getParameters().setDirectLunIds(Collections.singleton(lun1.getDiskId()));
Expand Down Expand Up @@ -155,6 +176,7 @@ private void mockLunDao() {

private void mockVmDao() {
lunsPerHost = new HashMap<>();
emptyLunsPerHost = new HashMap<>();
List<VM> vms = new ArrayList<>();
VM vm = new VM();
vm.setRunOnVds(Guid.newGuid());
Expand Down

0 comments on commit 5d1bf92

Please sign in to comment.