Skip to content

Commit e86ccc3

Browse files
committed
Adding some UT test
1 parent 363751e commit e86ccc3

File tree

2 files changed

+262
-42
lines changed

2 files changed

+262
-42
lines changed

plugins/backup/backroll/src/test/java/org/apache/cloudstack/backup/BackrollBackupProviderTest.java

Lines changed: 155 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
2122
import static org.junit.Assert.assertTrue;
2223
import static org.mockito.Mockito.mock;
2324
import static org.mockito.Mockito.times;
@@ -36,6 +37,7 @@
3637
import org.apache.cloudstack.backup.backroll.model.BackrollOffering;
3738
import org.apache.cloudstack.backup.backroll.model.response.TaskState;
3839
import org.apache.cloudstack.backup.Backup.Metric;
40+
import org.apache.cloudstack.backup.Backup.RestorePoint;
3941
import org.apache.cloudstack.backup.dao.BackupDao;
4042
import org.apache.logging.log4j.Logger;
4143
import org.junit.Before;
@@ -90,9 +92,10 @@ public void setUp() throws Exception {
9092
}
9193

9294
@Test
93-
public void listBackupOfferings_Test() throws BackrollApiException, IOException{
95+
public void listBackupOfferings_Test() throws BackrollApiException, IOException {
9496
Mockito.doReturn("dummyUrlToRequest").when(clientMock).getBackupOfferingUrl();
95-
Mockito.doReturn(Arrays.asList(new BackrollOffering("dummyName", "dummyId"))).when(clientMock).getBackupOfferings(Mockito.anyString());
97+
Mockito.doReturn(Arrays.asList(new BackrollOffering("dummyName", "dummyId"))).when(clientMock)
98+
.getBackupOfferings(Mockito.anyString());
9699
List<BackupOffering> results = backupProvider.listBackupOfferings(2L);
97100
assertTrue(results.size() == 1);
98101
}
@@ -106,7 +109,8 @@ public void takeBackup_Test() throws BackrollApiException, IOException {
106109
vmInstanceVO.setUuid(UUID.randomUUID().toString());
107110
vmInstanceVO.setBackupOfferingId(2L);
108111

109-
Mockito.doReturn("/status/f32092e4-3e8a-461b-8733-ed93e23fa782").when(clientMock).startBackupJob(Mockito.anyString());
112+
Mockito.doReturn("/status/f32092e4-3e8a-461b-8733-ed93e23fa782").when(clientMock)
113+
.startBackupJob(Mockito.anyString());
110114
Mockito.doReturn(new BackupVO()).when(backupDao).persist(Mockito.any(BackupVO.class));
111115
Mockito.doNothing().when(clientMock).triggerTaskStatus(Mockito.anyString());
112116
syncBackups_Test();
@@ -116,12 +120,13 @@ public void takeBackup_Test() throws BackrollApiException, IOException {
116120

117121
@Test
118122
public void restoreBackedUpVolume_Test() {
119-
try{
120-
backupProvider.restoreBackedUpVolume(new BackupVO(), "dummyString", "dummyString", "dummyString", new Pair<String,VirtualMachine.State>("dummyString", VirtualMachine.State.Shutdown));
123+
try {
124+
backupProvider.restoreBackedUpVolume(new BackupVO(), "dummyString", "dummyString", "dummyString",
125+
new Pair<String, VirtualMachine.State>("dummyString", VirtualMachine.State.Shutdown));
121126
} catch (Exception e) {
122127
assertEquals(CloudRuntimeException.class, e.getClass());
123128
String expected = String.format("Backroll plugin does not support this feature");
124-
assertEquals(expected , e.getMessage());
129+
assertEquals(expected, e.getMessage());
125130
}
126131
}
127132

@@ -141,7 +146,7 @@ public void getBackupMetricsEmpty_Test() {
141146
}
142147

143148
@Test
144-
public void getBackupMetrics_Test() throws BackrollApiException, IOException{
149+
public void getBackupMetrics_Test() throws BackrollApiException, IOException {
145150
VMInstanceVO vmInstanceVO = new VMInstanceVO();
146151
vmInstanceVO.setInstanceName("test");
147152
vmInstanceVO.setDataCenterId(1l);
@@ -152,20 +157,21 @@ public void getBackupMetrics_Test() throws BackrollApiException, IOException{
152157
vmInstanceVO2.setDataCenterId(2l);
153158
vmInstanceVO2.setBackupOfferingId(2l);
154159

155-
156160
VMInstanceVO vmInstanceVO3 = new VMInstanceVO();
157161
vmInstanceVO3.setInstanceName("test3");
158162
vmInstanceVO3.setDataCenterId(3l);
159163
vmInstanceVO3.setBackupOfferingId(3l);
160164

161165
List<BackrollVmBackup> backupsFromBackroll = Arrays.asList(
162-
new BackrollVmBackup("OK","OK", new Date()));
166+
new BackrollVmBackup("OK", "OK", new Date()));
163167

164168
BackrollBackupMetrics metrics = new BackrollBackupMetrics(2L, 3L);
165169

166170
Mockito.doReturn(metrics).when(clientMock).getBackupMetrics(Mockito.anyString(), Mockito.anyString());
167171
Mockito.doReturn(backupsFromBackroll).when(clientMock).getAllBackupsfromVirtualMachine(Mockito.anyString());
168-
assertEquals(backupProvider.getBackupMetrics(2L, Arrays.asList(vmInstanceVO, vmInstanceVO2, vmInstanceVO3)).size(), 1);
172+
assertEquals(
173+
backupProvider.getBackupMetrics(2L, Arrays.asList(vmInstanceVO, vmInstanceVO2, vmInstanceVO3)).size(),
174+
1);
169175

170176
Mockito.verify(clientMock, times(3)).getAllBackupsfromVirtualMachine(Mockito.anyString());
171177
Mockito.verify(clientMock, times(3)).getBackupMetrics(Mockito.anyString(), Mockito.anyString());
@@ -201,22 +207,22 @@ public void restoreVMFromBackupFalse_Test() throws BackrollApiException, IOExcep
201207
} catch (Exception e) {
202208
assertEquals(CloudRuntimeException.class, e.getClass());
203209
String expected = String.format("Failed to restore VM from Backup");
204-
assertEquals(expected , e.getMessage());
210+
assertEquals(expected, e.getMessage());
205211
}
206212
}
207213

208214
@Test
209-
public void getDescription_Test(){
215+
public void getDescription_Test() {
210216
assertEquals("Backroll Backup Plugin", backupProvider.getDescription());
211217
}
212218

213219
@Test
214-
public void isValidProviderOffering_Test(){
220+
public void isValidProviderOffering_Test() {
215221
assertTrue(backupProvider.isValidProviderOffering(2L, "dummyString"));
216222
}
217223

218224
@Test
219-
public void getName_Test(){
225+
public void getName_Test() {
220226
assertEquals("backroll", backupProvider.getName());
221227
}
222228

@@ -227,20 +233,18 @@ public void assignVMToBackupOffering_Test() {
227233
assertTrue(backupProvider.assignVMToBackupOffering(vmInstanceVO, backrollOf));
228234
}
229235

230-
231236
@Test
232-
public void removeVMFromBackupOffering_Test(){
237+
public void removeVMFromBackupOffering_Test() {
233238
assertTrue(backupProvider.removeVMFromBackupOffering(new VMInstanceVO()));
234239
}
235240

236241
@Test
237-
public void willDeleteBackupsOnOfferingRemoval_Test(){
242+
public void willDeleteBackupsOnOfferingRemoval_Test() {
238243
assertFalse(backupProvider.willDeleteBackupsOnOfferingRemoval());
239244
}
240245

241-
242246
@Test
243-
public void syncBackups_Test() throws BackrollApiException, IOException {
247+
public void syncBackups_Test() throws BackrollApiException, IOException {
244248
VMInstanceVO vmInstanceVO = new VMInstanceVO();
245249
vmInstanceVO.setInstanceName("test");
246250
vmInstanceVO.setDataCenterId(2l);
@@ -277,7 +281,7 @@ public void syncBackups_Test() throws BackrollApiException, IOException {
277281
BackrollBackupMetrics metrics = new BackrollBackupMetrics(2L, 3L);
278282

279283
List<BackrollVmBackup> backupsFromBackroll = Arrays.asList(
280-
new BackrollVmBackup("OK","OK", new Date()));
284+
new BackrollVmBackup("OK", "OK", new Date()));
281285

282286
List<Backup> backupsInDb = Arrays.asList(backupBackingUp, backupBackedUp, backupFailed);
283287
Mockito.doReturn(backupsInDb).when(backupDao).listByVmId(Mockito.anyLong(), Mockito.anyLong());
@@ -290,7 +294,6 @@ public void syncBackups_Test() throws BackrollApiException, IOException {
290294
backupProvider.syncBackups(vmInstanceVO, metric);
291295
}
292296

293-
294297
@Test
295298
public void getClient_Test() {
296299
BackrollClient client = backupProvider.getClient(2L);
@@ -326,7 +329,137 @@ public void deleteBackupBackinUp_Test() {
326329
} catch (Exception e) {
327330
assertEquals(CloudRuntimeException.class, e.getClass());
328331
String expected = String.format("You can't delete a backup while it still BackingUp");
329-
assertEquals(expected , e.getMessage());
332+
assertEquals(expected, e.getMessage());
330333
}
331334
}
335+
336+
@Test
337+
public void listRestorePoints_Test() throws BackrollApiException, IOException {
338+
List<RestorePoint> rps = Arrays.asList(new RestorePoint("rp1", new Date(), "incremental"),
339+
new RestorePoint("rp2", new Date(), "incremental"),
340+
new RestorePoint("rp3", new Date(), "incremental"),
341+
new RestorePoint("rp4", new Date(), "incremental"));
342+
343+
VMInstanceVO vmInstanceVO3 = new VMInstanceVO();
344+
vmInstanceVO3.setInstanceName("test3");
345+
vmInstanceVO3.setDataCenterId(3l);
346+
vmInstanceVO3.setBackupOfferingId(3l);
347+
348+
Mockito.doReturn(rps).when(clientMock).listRestorePoints(Mockito.anyString());
349+
350+
List<RestorePoint> rPoints = backupProvider.listRestorePoints(vmInstanceVO3);
351+
352+
assertEquals(rPoints.size(), rps.size());
353+
354+
}
355+
356+
@Test
357+
public void createNewBackupEntryForRestorePoint_Test() throws BackrollApiException, IOException {
358+
RestorePoint restorePoint = new RestorePoint("restore-123", new Date(), "INCREMENTAL");
359+
360+
VMInstanceVO vm = new VMInstanceVO();
361+
vm.setUuid("vm-uuid-456");
362+
vm.setDataCenterId(2L);
363+
vm.setBackupOfferingId(3L);
364+
365+
Backup.Metric metric = null;
366+
367+
BackrollBackupMetrics backupMetrics = new BackrollBackupMetrics(100L, 200L);
368+
Mockito.doReturn(backupMetrics).when(clientMock).getBackupMetrics(vm.getUuid(), restorePoint.getId());
369+
370+
BackupVO savedBackup = new BackupVO();
371+
Mockito.doReturn(savedBackup).when(backupDao).persist(Mockito.any(BackupVO.class));
372+
373+
Backup result = backupProvider.createNewBackupEntryForRestorePoint(restorePoint, vm, metric);
374+
375+
assertNotNull(result);
376+
assertEquals(vm.getId(), result.getVmId());
377+
assertEquals(restorePoint.getId(), result.getExternalId());
378+
assertEquals("INCREMENTAL", result.getType());
379+
assertEquals(restorePoint.getCreated(), result.getDate());
380+
assertEquals(Backup.Status.BackedUp, result.getStatus());
381+
assertEquals(vm.getBackupOfferingId(), (Long)result.getBackupOfferingId());
382+
assertEquals(vm.getAccountId(), result.getAccountId());
383+
assertEquals(vm.getDomainId(), result.getDomainId());
384+
assertEquals(vm.getDataCenterId(), result.getZoneId());
385+
assertEquals((Long)backupMetrics.getDeduplicated(), result.getSize());
386+
assertEquals((Long)backupMetrics.getSize(), result.getProtectedSize());
387+
388+
Mockito.verify(clientMock).getBackupMetrics(vm.getUuid(), restorePoint.getId());
389+
Mockito.verify(backupDao).persist(Mockito.any(BackupVO.class));
390+
}
391+
392+
@Test
393+
public void createNewBackupEntryForRestorePoint_WithMetric_Test() throws BackrollApiException, IOException {
394+
RestorePoint restorePoint = new RestorePoint("restore-789", new Date(), "INCREMENTAL");
395+
396+
VMInstanceVO vm = new VMInstanceVO();
397+
vm.setUuid("vm-uuid-789");
398+
vm.setDataCenterId(3L);
399+
vm.setBackupOfferingId(4L);
400+
401+
Backup.Metric metric = new Backup.Metric(150L, 250L);
402+
403+
404+
BackupVO savedBackup = new BackupVO();
405+
Mockito.doReturn(savedBackup).when(backupDao).persist(Mockito.any(BackupVO.class));
406+
407+
408+
Backup result = backupProvider.createNewBackupEntryForRestorePoint(restorePoint, vm, metric);
409+
410+
411+
assertNotNull(result);
412+
assertEquals(vm.getId(), result.getVmId());
413+
assertEquals(restorePoint.getId(), result.getExternalId());
414+
assertEquals("INCREMENTAL", result.getType());
415+
assertEquals(restorePoint.getCreated(), result.getDate());
416+
assertEquals(Backup.Status.BackedUp, result.getStatus());
417+
assertEquals(vm.getBackupOfferingId(), (Long)result.getBackupOfferingId());
418+
assertEquals(vm.getAccountId(), result.getAccountId());
419+
assertEquals(vm.getDomainId(), result.getDomainId());
420+
assertEquals(vm.getDataCenterId(), result.getZoneId());
421+
assertEquals(metric.getBackupSize(), result.getSize());
422+
assertEquals(metric.getDataSize(), result.getProtectedSize());
423+
424+
425+
Mockito.verify(clientMock, Mockito.never()).getBackupMetrics(Mockito.anyString(), Mockito.anyString());
426+
Mockito.verify(backupDao).persist(Mockito.any(BackupVO.class));
427+
}
428+
429+
@Test(expected = CloudRuntimeException.class)
430+
public void createNewBackupEntryForRestorePoint_BackrollApiException_Test()
431+
throws BackrollApiException, IOException {
432+
433+
RestorePoint restorePoint =new RestorePoint("restore-404", new Date(), "INCREMENTAL");
434+
435+
VMInstanceVO vm = new VMInstanceVO();
436+
vm.setUuid("vm-uuid-404");
437+
vm.setDataCenterId(4L);
438+
vm.setBackupOfferingId(5L);
439+
440+
Backup.Metric metric = null;
441+
442+
Mockito.doThrow(new BackrollApiException()).when(clientMock).getBackupMetrics(vm.getUuid(),
443+
restorePoint.getId());
444+
445+
backupProvider.createNewBackupEntryForRestorePoint(restorePoint, vm, metric);
446+
}
447+
448+
@Test(expected = CloudRuntimeException.class)
449+
public void createNewBackupEntryForRestorePoint_IOException_Test() throws BackrollApiException, IOException {
450+
451+
RestorePoint restorePoint = new RestorePoint("restore-500", new Date(), "INCREMENTAL");
452+
453+
VMInstanceVO vm = new VMInstanceVO();
454+
vm.setUuid("vm-uuid-500");
455+
vm.setDataCenterId(5L);
456+
vm.setBackupOfferingId(6L);
457+
458+
Backup.Metric metric = null;
459+
460+
Mockito.doThrow(new IOException("IO Error")).when(clientMock).getBackupMetrics(vm.getUuid(),
461+
restorePoint.getId());
462+
463+
backupProvider.createNewBackupEntryForRestorePoint(restorePoint, vm, metric);
464+
}
332465
}

0 commit comments

Comments
 (0)