Skip to content

Commit dae4f3b

Browse files
committed
Added UT for coverage
1 parent a58f6d4 commit dae4f3b

File tree

5 files changed

+881
-18
lines changed

5 files changed

+881
-18
lines changed

plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ public class NASBackupProvider extends AdapterBase implements BackupProvider, Co
112112
@Inject
113113
private VMSnapshotDao vmSnapshotDao;
114114

115-
@Inject
116-
private VolumeDao _volsDao;
117-
118-
@Inject
119-
protected PrimaryDataStoreDao _storagePoolDao = null;
120-
121115
@Inject
122116
private VMSnapshotDetailsDao vmSnapshotDetailsDao;
123117

@@ -130,13 +124,16 @@ public class NASBackupProvider extends AdapterBase implements BackupProvider, Co
130124
@Inject
131125
private DiskOfferingDao diskOfferingDao;
132126

133-
protected Long getClusterIdFromRootVolume(VirtualMachine vm) {
134-
VolumeVO rootVolume = _volsDao.getInstanceRootVolume(vm.getId());
135-
StoragePoolVO rootDiskPool = _storagePoolDao.findById(rootVolume.getPoolId());
127+
private Long getClusterIdFromRootVolume(VirtualMachine vm) {
128+
VolumeVO rootVolume = volumeDao.getInstanceRootVolume(vm.getId());
129+
StoragePoolVO rootDiskPool = primaryDataStoreDao.findById(rootVolume.getPoolId());
130+
if (rootDiskPool == null) {
131+
return null;
132+
}
136133
return rootDiskPool.getClusterId();
137134
}
138135

139-
protected Host getLastVMHypervisorHost(VirtualMachine vm) {
136+
protected Host getVMHypervisorHost(VirtualMachine vm) {
140137
Long hostId = vm.getLastHostId();
141138
Long clusterId = null;
142139

@@ -145,13 +142,14 @@ protected Host getLastVMHypervisorHost(VirtualMachine vm) {
145142
if (host.getStatus() == Status.Up) {
146143
return host;
147144
}
145+
// Try to find any Up host in the same cluster
148146
clusterId = host.getClusterId();
149147
} else {
148+
// Try to find any Up host in the same cluster as the root volume
150149
clusterId = getClusterIdFromRootVolume(vm);
151150
}
152151

153152
if (clusterId != null) {
154-
// Try to find any Up host in the same cluster
155153
for (final Host hostInCluster : hostDao.findHypervisorHostInCluster(clusterId)) {
156154
if (hostInCluster.getStatus() == Status.Up) {
157155
LOG.debug("Found Host {} in cluster {}", hostInCluster, clusterId);
@@ -164,7 +162,7 @@ protected Host getLastVMHypervisorHost(VirtualMachine vm) {
164162
return resourceManager.findOneRandomRunningHostByHypervisor(Hypervisor.HypervisorType.KVM, vm.getDataCenterId());
165163
}
166164

167-
protected Host getVMHypervisorHost(VirtualMachine vm) {
165+
protected Host getVMHypervisorHostForBackup(VirtualMachine vm) {
168166
Long hostId = vm.getHostId();
169167
if (hostId == null && VirtualMachine.State.Running.equals(vm.getState())) {
170168
throw new CloudRuntimeException(String.format("Unable to find the hypervisor host for %s. Make sure the virtual machine is running", vm.getName()));
@@ -184,7 +182,7 @@ protected Host getVMHypervisorHost(VirtualMachine vm) {
184182

185183
@Override
186184
public Pair<Boolean, Backup> takeBackup(final VirtualMachine vm, Boolean quiesceVM) {
187-
final Host host = getVMHypervisorHost(vm);
185+
final Host host = getVMHypervisorHostForBackup(vm);
188186

189187
final BackupRepository backupRepository = backupRepositoryDao.findByBackupOfferingId(vm.getBackupOfferingId());
190188
if (backupRepository == null) {
@@ -297,7 +295,7 @@ private Pair<Boolean, String> restoreVMBackup(VirtualMachine vm, Backup backup)
297295
LOG.debug("Restoring vm {} from backup {} on the NAS Backup Provider", vm, backup);
298296
BackupRepository backupRepository = getBackupRepository(backup);
299297

300-
final Host host = getLastVMHypervisorHost(vm);
298+
final Host host = getVMHypervisorHost(vm);
301299
RestoreBackupCommand restoreCommand = new RestoreBackupCommand();
302300
restoreCommand.setBackupPath(backup.getExternalId());
303301
restoreCommand.setBackupRepoType(backupRepository.getType());
@@ -425,7 +423,7 @@ public boolean deleteBackup(Backup backup, boolean forced) {
425423
final Host host;
426424
final VirtualMachine vm = vmInstanceDao.findByIdIncludingRemoved(backup.getVmId());
427425
if (vm != null) {
428-
host = getLastVMHypervisorHost(vm);
426+
host = getVMHypervisorHost(vm);
429427
} else {
430428
host = resourceManager.findOneRandomRunningHostByHypervisor(Hypervisor.HypervisorType.KVM, backup.getZoneId());
431429
}

plugins/backup/nas/src/test/java/org/apache/cloudstack/backup/NASBackupProviderTest.java

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121

2222
import java.util.Collections;
2323
import java.util.List;
24+
import java.util.Objects;
2425
import java.util.Optional;
2526

26-
import org.apache.cloudstack.backup.dao.BackupDao;
27-
import org.apache.cloudstack.backup.dao.BackupRepositoryDao;
28-
import org.apache.cloudstack.backup.dao.BackupOfferingDao;
2927
import org.junit.Assert;
3028
import org.junit.Test;
3129
import org.junit.runner.RunWith;
@@ -39,6 +37,7 @@
3937
import com.cloud.agent.AgentManager;
4038
import com.cloud.exception.AgentUnavailableException;
4139
import com.cloud.exception.OperationTimedoutException;
40+
import com.cloud.host.Host;
4241
import com.cloud.host.HostVO;
4342
import com.cloud.host.Status;
4443
import com.cloud.host.dao.HostDao;
@@ -51,6 +50,12 @@
5150
import com.cloud.vm.VMInstanceVO;
5251
import com.cloud.vm.dao.VMInstanceDao;
5352

53+
import org.apache.cloudstack.backup.dao.BackupDao;
54+
import org.apache.cloudstack.backup.dao.BackupRepositoryDao;
55+
import org.apache.cloudstack.backup.dao.BackupOfferingDao;
56+
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
57+
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
58+
5459
@RunWith(MockitoJUnitRunner.class)
5560
public class NASBackupProviderTest {
5661
@Spy
@@ -84,6 +89,9 @@ public class NASBackupProviderTest {
8489
@Mock
8590
private ResourceManager resourceManager;
8691

92+
@Mock
93+
private PrimaryDataStoreDao storagePoolDao;
94+
8795
@Test
8896
public void testDeleteBackup() throws OperationTimedoutException, AgentUnavailableException {
8997
Long hostId = 1L;
@@ -227,4 +235,118 @@ public void takeBackupSuccessfully() throws AgentUnavailableException, Operation
227235
Mockito.verify(backupDao).update(Mockito.anyLong(), Mockito.any(BackupVO.class));
228236
Mockito.verify(agentManager).send(anyLong(), Mockito.any(TakeBackupCommand.class));
229237
}
238+
239+
@Test
240+
public void testGetVMHypervisorHost() {
241+
Long hostId = 1L;
242+
Long vmId = 1L;
243+
Long zoneId = 1L;
244+
245+
VMInstanceVO vm = mock(VMInstanceVO.class);
246+
Mockito.when(vm.getLastHostId()).thenReturn(hostId);
247+
248+
HostVO host = mock(HostVO.class);
249+
Mockito.when(host.getId()).thenReturn(hostId);
250+
Mockito.when(host.getStatus()).thenReturn(Status.Up);
251+
Mockito.when(hostDao.findById(hostId)).thenReturn(host);
252+
253+
Host result = nasBackupProvider.getVMHypervisorHost(vm);
254+
255+
Assert.assertNotNull(result);
256+
Assert.assertTrue(Objects.equals(hostId, result.getId()));
257+
Mockito.verify(hostDao).findById(hostId);
258+
}
259+
260+
@Test
261+
public void testGetVMHypervisorHostWithHostDown() {
262+
Long hostId = 1L;
263+
Long clusterId = 2L;
264+
Long vmId = 1L;
265+
Long zoneId = 1L;
266+
267+
VMInstanceVO vm = mock(VMInstanceVO.class);
268+
Mockito.when(vm.getLastHostId()).thenReturn(hostId);
269+
270+
HostVO downHost = mock(HostVO.class);
271+
Mockito.when(downHost.getStatus()).thenReturn(Status.Down);
272+
Mockito.when(downHost.getClusterId()).thenReturn(clusterId);
273+
Mockito.when(hostDao.findById(hostId)).thenReturn(downHost);
274+
275+
HostVO upHostInCluster = mock(HostVO.class);
276+
Mockito.when(upHostInCluster.getId()).thenReturn(3L);
277+
Mockito.when(upHostInCluster.getStatus()).thenReturn(Status.Up);
278+
Mockito.when(hostDao.findHypervisorHostInCluster(clusterId)).thenReturn(List.of(upHostInCluster));
279+
280+
Host result = nasBackupProvider.getVMHypervisorHost(vm);
281+
282+
Assert.assertNotNull(result);
283+
Assert.assertTrue(Objects.equals(Long.valueOf(3L), result.getId()));
284+
Mockito.verify(hostDao).findById(hostId);
285+
Mockito.verify(hostDao).findHypervisorHostInCluster(clusterId);
286+
}
287+
288+
@Test
289+
public void testGetVMHypervisorHostWithUpHostViaRootVolumeCluster() {
290+
Long vmId = 1L;
291+
Long zoneId = 1L;
292+
Long clusterId = 2L;
293+
Long poolId = 3L;
294+
295+
VMInstanceVO vm = mock(VMInstanceVO.class);
296+
Mockito.when(vm.getLastHostId()).thenReturn(null);
297+
Mockito.when(vm.getId()).thenReturn(vmId);
298+
299+
VolumeVO rootVolume = mock(VolumeVO.class);
300+
Mockito.when(rootVolume.getPoolId()).thenReturn(poolId);
301+
Mockito.when(volumeDao.getInstanceRootVolume(vmId)).thenReturn(rootVolume);
302+
303+
StoragePoolVO storagePool = mock(StoragePoolVO.class);
304+
Mockito.when(storagePool.getClusterId()).thenReturn(clusterId);
305+
Mockito.when(storagePoolDao.findById(poolId)).thenReturn(storagePool);
306+
307+
HostVO upHostInCluster = mock(HostVO.class);
308+
Mockito.when(upHostInCluster.getId()).thenReturn(4L);
309+
Mockito.when(upHostInCluster.getStatus()).thenReturn(Status.Up);
310+
Mockito.when(hostDao.findHypervisorHostInCluster(clusterId)).thenReturn(List.of(upHostInCluster));
311+
312+
Host result = nasBackupProvider.getVMHypervisorHost(vm);
313+
314+
Assert.assertNotNull(result);
315+
Assert.assertTrue(Objects.equals(Long.valueOf(4L), result.getId()));
316+
Mockito.verify(volumeDao).getInstanceRootVolume(vmId);
317+
Mockito.verify(storagePoolDao).findById(poolId);
318+
Mockito.verify(hostDao).findHypervisorHostInCluster(clusterId);
319+
}
320+
321+
@Test
322+
public void testGetVMHypervisorHostFallbackToZoneWideKVMHost() {
323+
Long hostId = 1L;
324+
Long clusterId = 2L;
325+
Long vmId = 1L;
326+
Long zoneId = 1L;
327+
328+
VMInstanceVO vm = mock(VMInstanceVO.class);
329+
Mockito.when(vm.getLastHostId()).thenReturn(hostId);
330+
Mockito.when(vm.getDataCenterId()).thenReturn(zoneId);
331+
332+
HostVO downHost = mock(HostVO.class);
333+
Mockito.when(downHost.getStatus()).thenReturn(Status.Down);
334+
Mockito.when(downHost.getClusterId()).thenReturn(clusterId);
335+
Mockito.when(hostDao.findById(hostId)).thenReturn(downHost);
336+
337+
Mockito.when(hostDao.findHypervisorHostInCluster(clusterId)).thenReturn(Collections.emptyList());
338+
339+
HostVO fallbackHost = mock(HostVO.class);
340+
Mockito.when(fallbackHost.getId()).thenReturn(5L);
341+
Mockito.when(resourceManager.findOneRandomRunningHostByHypervisor(Hypervisor.HypervisorType.KVM, zoneId))
342+
.thenReturn(fallbackHost);
343+
344+
Host result = nasBackupProvider.getVMHypervisorHost(vm);
345+
346+
Assert.assertNotNull(result);
347+
Assert.assertTrue(Objects.equals(Long.valueOf(5L), result.getId()));
348+
Mockito.verify(hostDao).findById(hostId);
349+
Mockito.verify(hostDao).findHypervisorHostInCluster(clusterId);
350+
Mockito.verify(resourceManager).findOneRandomRunningHostByHypervisor(Hypervisor.HypervisorType.KVM, zoneId);
351+
}
230352
}

0 commit comments

Comments
 (0)