-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathKVMGuruTest.java
More file actions
509 lines (388 loc) · 20.5 KB
/
KVMGuruTest.java
File metadata and controls
509 lines (388 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.hypervisor;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.utils.bytescale.ByteScaleUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import com.cloud.agent.api.to.NicTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.configuration.ConfigurationManagerImpl;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.kvm.dpdk.DpdkHelper;
import com.cloud.offering.ServiceOffering;
import com.cloud.service.ServiceOfferingDetailsVO;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.service.dao.ServiceOfferingDetailsDao;
import com.cloud.storage.GuestOSHypervisorVO;
import com.cloud.storage.GuestOSVO;
import com.cloud.storage.dao.GuestOSDao;
import com.cloud.storage.dao.GuestOSHypervisorDao;
import com.cloud.utils.Pair;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineProfile;
@RunWith(MockitoJUnitRunner.class)
public class KVMGuruTest {
@Mock
HostDao hostDao;
@Mock
ServiceOfferingDetailsDao serviceOfferingDetailsDao;
@Spy
@InjectMocks
private KVMGuru guru = new KVMGuru();
@Mock
VirtualMachineTO vmTO;
@Mock
VirtualMachineProfile vmProfile;
@Mock
VirtualMachine vm;
@Mock
HostVO host;
@Mock
ServiceOffering serviceOffering;
@Mock
ServiceOfferingDetailsVO detail1;
@Mock
ServiceOfferingDetailsVO detail2;
@Mock
ServiceOfferingVO serviceOfferingVoMock;
@Mock
VirtualMachine virtualMachineMock;
@Mock
ServiceOfferingDao serviceOfferingDaoMock;
@Mock
DpdkHelper dpdkHelperMock;
@Mock
GuestOSVO guestOsVoMock;
@Mock
GuestOSHypervisorVO guestOsMappingMock;
@Mock
GuestOSHypervisorDao guestOSHypervisorDaoMock;
@Mock
GuestOSDao guestOsDaoMock;
private static final long hostId = 1L;
private static final Long offeringId = 1L;
private static final String detail1Key = ApiConstants.EXTRA_CONFIG + "-config-1";
private static final String detail1Value = "value1";
private static final String detail2Key = "detail2";
private static final String detail2Value = "value2";
private ConfigKey<Integer> originalVmServiceOfferingMaxCpuCores;
private ConfigKey<Integer> originalVmServiceOfferingMaxRAMSize;
@Before
public void setup() throws UnsupportedEncodingException {
// Preserve the original value for restoration in tearDown
originalVmServiceOfferingMaxCpuCores = ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES;
originalVmServiceOfferingMaxRAMSize = ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE;
Mockito.when(vmTO.getLimitCpuUse()).thenReturn(true);
Mockito.when(vmProfile.getVirtualMachine()).thenReturn(vm);
Mockito.when(vm.getHostId()).thenReturn(hostId);
Mockito.when(hostDao.findById(hostId)).thenReturn(host);
Mockito.lenient().when(host.getCpus()).thenReturn(3);
Mockito.when(host.getSpeed()).thenReturn(1995L);
Mockito.when(vmTO.getMaxSpeed()).thenReturn(500);
Mockito.lenient().when(serviceOffering.getId()).thenReturn(offeringId);
Mockito.lenient().when(vmProfile.getServiceOffering()).thenReturn(serviceOffering);
Mockito.lenient().when(detail1.getName()).thenReturn(detail1Key);
Mockito.lenient().when(detail1.getValue()).thenReturn(detail1Value);
Mockito.lenient().when(detail1.getResourceId()).thenReturn(offeringId);
Mockito.lenient().when(detail2.getName()).thenReturn(detail2Key);
Mockito.lenient().when(detail2.getResourceId()).thenReturn(offeringId);
Mockito.lenient().when(detail2.getValue()).thenReturn(detail2Value);
Mockito.lenient().when(serviceOfferingDetailsDao.listDetails(offeringId)).thenReturn(
Arrays.asList(detail1, detail2));
}
@After
public void tearDown() {
// Restore the original value
ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES = originalVmServiceOfferingMaxCpuCores;
ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE = originalVmServiceOfferingMaxRAMSize;
}
@Test
public void testSetVmQuotaPercentage() {
guru.setVmQuotaPercentage(vmTO, vmProfile);
Mockito.verify(vmTO).setCpuQuotaPercentage(Mockito.anyDouble());
}
@Test
public void testSetVmQuotaPercentageNullHost() {
Mockito.when(hostDao.findById(hostId)).thenReturn(null);
guru.setVmQuotaPercentage(vmTO, vmProfile);
Mockito.verify(vmTO, Mockito.never()).setCpuQuotaPercentage(Mockito.anyDouble());
}
@Test
public void testSetVmQuotaPercentageZeroDivision() {
Mockito.when(host.getSpeed()).thenReturn(0l);
guru.setVmQuotaPercentage(vmTO, vmProfile);
Mockito.verify(vmTO, Mockito.never()).setCpuQuotaPercentage(Mockito.anyDouble());
}
@Test
public void testSetVmQuotaPercentageNotCPULimit() {
Mockito.when(vmTO.getLimitCpuUse()).thenReturn(false);
guru.setVmQuotaPercentage(vmTO, vmProfile);
Mockito.verify(vmProfile, Mockito.never()).getVirtualMachine();
Mockito.verify(vmTO, Mockito.never()).setCpuQuotaPercentage(Mockito.anyDouble());
}
@Test
public void testSetVmQuotaPercentageOverProvision() {
Mockito.when(vmTO.getMaxSpeed()).thenReturn(3000);
guru.setVmQuotaPercentage(vmTO, vmProfile);
Mockito.verify(vmTO).setCpuQuotaPercentage(1d);
}
@Test
public void validateGetVmMaxMemoryReturnCustomOfferingMaxMemory(){
int maxCustomOfferingMemory = 64;
Mockito.when(serviceOfferingVoMock.getDetail(ApiConstants.MAX_MEMORY)).thenReturn(String.valueOf(maxCustomOfferingMemory));
long result = guru.getVmMaxMemory(serviceOfferingVoMock, "Vm description", 1l);
Assert.assertEquals(ByteScaleUtils.mebibytesToBytes(maxCustomOfferingMemory), result);
}
@Test
public void validateGetVmMaxMemoryReturnVmServiceOfferingMaxRAMSize(){
int maxMemoryConfig = 64;
Mockito.when(serviceOfferingVoMock.getDetail(ApiConstants.MAX_MEMORY)).thenReturn(null);
ConfigKey<Integer> vmServiceOfferingMaxRAMSize = Mockito.mock(ConfigKey.class);
ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE = vmServiceOfferingMaxRAMSize;
Mockito.when(vmServiceOfferingMaxRAMSize.value()).thenReturn(maxMemoryConfig);
long result = guru.getVmMaxMemory(serviceOfferingVoMock, "Vm description", 1l);
Assert.assertEquals(ByteScaleUtils.mebibytesToBytes(maxMemoryConfig), result);
}
@Test
public void validateGetVmMaxMemoryReturnMaxHostMemory(){
long maxHostMemory = ByteScaleUtils.mebibytesToBytes(2000);
Mockito.when(serviceOfferingVoMock.getDetail(ApiConstants.MAX_MEMORY)).thenReturn(null);
ConfigKey<Integer> vmServiceOfferingMaxRAMSize = Mockito.mock(ConfigKey.class);
ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE = vmServiceOfferingMaxRAMSize;
Mockito.when(vmServiceOfferingMaxRAMSize.value()).thenReturn(0);
long result = guru.getVmMaxMemory(serviceOfferingVoMock, "Vm description", maxHostMemory);
Assert.assertEquals(maxHostMemory, result);
}
@Test
public void validateGetVmMaxCpuCoresReturnCustomOfferingMaxCpuCores(){
int maxCustomOfferingCpuCores = 16;
Mockito.when(serviceOfferingVoMock.getDetail(ApiConstants.MAX_CPU_NUMBER)).thenReturn(String.valueOf(maxCustomOfferingCpuCores));
long result = guru.getVmMaxCpuCores(serviceOfferingVoMock, "Vm description", 1);
Assert.assertEquals(maxCustomOfferingCpuCores, result);
}
@Test
public void validateGetVmMaxCpuCoresVmServiceOfferingMaxCPUCores(){
int maxCpuCoresConfig = 16;
Mockito.when(serviceOfferingVoMock.getDetail(ApiConstants.MAX_CPU_NUMBER)).thenReturn(null);
ConfigKey<Integer> vmServiceOfferingMaxCPUCores = Mockito.mock(ConfigKey.class);
ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES = vmServiceOfferingMaxCPUCores;
Mockito.when(vmServiceOfferingMaxCPUCores.value()).thenReturn(maxCpuCoresConfig);
long result = guru.getVmMaxCpuCores(serviceOfferingVoMock, "Vm description", 1);
Assert.assertEquals(maxCpuCoresConfig, result);
}
@Test
public void validateGetVmMaxCpuCoresReturnMaxHostMemory(){
int maxHostCpuCores = 64;
Mockito.when(serviceOfferingVoMock.getDetail(ApiConstants.MAX_CPU_NUMBER)).thenReturn(null);
ConfigKey<Integer> vmServiceOfferingMaxCPUCores = Mockito.mock(ConfigKey.class);
ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES = vmServiceOfferingMaxCPUCores;
Mockito.when(vmServiceOfferingMaxCPUCores.value()).thenReturn(0);
long result = guru.getVmMaxCpuCores(serviceOfferingVoMock, "Vm description", maxHostCpuCores);
Assert.assertEquals(maxHostCpuCores, result);
}
@Test
public void validateGetHostMaxMemoryAndCpuCoresHostNotNull(){
Long maxMemory = 2048l;
Integer maxCpuCores = 16;
Mockito.when(host.getTotalMemory()).thenReturn(maxMemory);
Mockito.when(host.getCpus()).thenReturn(maxCpuCores);
Pair<Long, Integer> result = guru.getHostMaxMemoryAndCpuCores(host, virtualMachineMock, "Vm description");
Assert.assertEquals(new Pair<>(maxMemory, maxCpuCores), result);
}
@Test
public void validateGetHostMaxMemoryAndCpuCoresHostNullAndLastHostIdNull(){
Long maxMemory = Long.MAX_VALUE;
Integer maxCpuCores = Integer.MAX_VALUE;
Pair<Long, Integer> result = guru.getHostMaxMemoryAndCpuCores(null, virtualMachineMock, "Vm description");
Assert.assertEquals(new Pair<>(maxMemory, maxCpuCores), result);
}
@Test
public void validateGetHostMaxMemoryAndCpuCoresHostNullAndLastHostIdNotNullAndLastHostNull(){
Long maxMemory = Long.MAX_VALUE;
Integer maxCpuCores = Integer.MAX_VALUE;
guru.hostDao = hostDao;
Mockito.when(virtualMachineMock.getLastHostId()).thenReturn(1l);
Mockito.doReturn(null).when(hostDao).findById(Mockito.any());
Pair<Long, Integer> result = guru.getHostMaxMemoryAndCpuCores(null, virtualMachineMock, "Vm description");
Assert.assertEquals(new Pair<>(maxMemory, maxCpuCores), result);
}
@Test
public void validateGetHostMaxMemoryAndCpuCoresHostNullAndLastHostIdNotNullAndLastHostNotNull(){
Long maxMemory = 2048l;
Integer maxCpuCores = 16;
guru.hostDao = hostDao;
Mockito.when(virtualMachineMock.getLastHostId()).thenReturn(1l);
Mockito.doReturn(host).when(hostDao).findById(Mockito.any());
Mockito.when(host.getTotalMemory()).thenReturn(maxMemory);
Mockito.when(host.getCpus()).thenReturn(maxCpuCores);
Pair<Long, Integer> result = guru.getHostMaxMemoryAndCpuCores(null, virtualMachineMock, "Vm description");
Assert.assertEquals(new Pair<>(maxMemory, maxCpuCores), result);
}
@Test
public void validateConfigureVmMemoryAndCpuCoresServiceOfferingIsDynamicAndVmIsDynamicCallGetMethods(){
guru.serviceOfferingDao = serviceOfferingDaoMock;
Mockito.doReturn(serviceOfferingVoMock).when(serviceOfferingDaoMock).findById(Mockito.anyLong(), Mockito.anyLong());
Mockito.doReturn(true).when(guru).isVmDynamicScalable(Mockito.any(), Mockito.any(), Mockito.any());
guru.configureVmMemoryAndCpuCores(vmTO, host, virtualMachineMock, vmProfile);
Mockito.verify(guru).getVmMaxMemory(Mockito.any(ServiceOfferingVO.class), Mockito.anyString(), Mockito.anyLong());
Mockito.verify(guru).getVmMaxCpuCores(Mockito.any(ServiceOfferingVO.class), Mockito.anyString(), Mockito.anyInt());
}
@Test
public void validateConfigureVmMemoryAndCpuCoresServiceOfferingIsNotDynamicAndVmIsDynamicDoNotCallGetMethods(){
guru.serviceOfferingDao = serviceOfferingDaoMock;
Mockito.doReturn(serviceOfferingVoMock).when(serviceOfferingDaoMock).findById(Mockito.anyLong(), Mockito.anyLong());
Mockito.doReturn(false).when(guru).isVmDynamicScalable(Mockito.any(), Mockito.any(), Mockito.any());
guru.configureVmMemoryAndCpuCores(vmTO, host, virtualMachineMock, vmProfile);
Mockito.verify(guru, Mockito.never()).getVmMaxMemory(Mockito.any(ServiceOfferingVO.class), Mockito.anyString(), Mockito.anyLong());
Mockito.verify(guru, Mockito.never()).getVmMaxCpuCores(Mockito.any(ServiceOfferingVO.class), Mockito.anyString(), Mockito.anyInt());
}
@Test
public void validateConfigureVmMemoryAndCpuCoresServiceOfferingIsDynamicAndVmIsNotDynamicDoNotCallGetMethods(){
guru.serviceOfferingDao = serviceOfferingDaoMock;
Mockito.doReturn(serviceOfferingVoMock).when(serviceOfferingDaoMock).findById(Mockito.anyLong(), Mockito.anyLong());
Mockito.doReturn(true).when(serviceOfferingVoMock).isDynamic();
Mockito.doReturn(false).when(vmTO).isEnableDynamicallyScaleVm();
guru.configureVmMemoryAndCpuCores(vmTO, host, virtualMachineMock, vmProfile);
Mockito.verify(guru, Mockito.never()).getVmMaxMemory(Mockito.any(ServiceOfferingVO.class), Mockito.anyString(), Mockito.anyLong());
Mockito.verify(guru, Mockito.never()).getVmMaxCpuCores(Mockito.any(ServiceOfferingVO.class), Mockito.anyString(), Mockito.anyInt());
}
@Test
public void validateEnableDpdkIfNeededCallDpdkHelperSetDpdkVhostUserMode() {
Mockito.when(dpdkHelperMock.isDpdkvHostUserModeSettingOnServiceOffering(vmProfile)).thenReturn(Boolean.TRUE);
guru.enableDpdkIfNeeded(vmProfile, vmTO);
Mockito.verify(dpdkHelperMock).setDpdkVhostUserMode(vmTO, vmProfile);
}
@Test
public void validateEnableDpdkIfNeededDoNotCallDpdkHelperSetDpdkVhostUserMode() {
Mockito.when(dpdkHelperMock.isDpdkvHostUserModeSettingOnServiceOffering(vmProfile)).thenReturn(Boolean.FALSE);
guru.enableDpdkIfNeeded(vmProfile, vmTO);
Mockito.verify(dpdkHelperMock, Mockito.times(0)).setDpdkVhostUserMode(vmTO, vmProfile);
}
@Test
public void validateEnableDpdkIfNeededNicSetDpdkEnabledTrue() {
Map<String, String> map = new HashMap<>();
map.put(DpdkHelper.DPDK_NUMA, "test1");
map.put(DpdkHelper.DPDK_HUGE_PAGES, "test2");
NicTO nicTo1 = Mockito.mock(NicTO.class);
NicTO nicTo2 = Mockito.mock(NicTO.class);
NicTO nicTo3 = Mockito.mock(NicTO.class);
NicTO[] nics = {nicTo1, nicTo2, nicTo3};
Mockito.when(vmTO.getType()).thenReturn(VirtualMachine.Type.User);
Mockito.when(vmTO.getExtraConfig()).thenReturn(map);
Mockito.when(vmTO.getNics()).thenReturn(nics);
guru.enableDpdkIfNeeded(vmProfile, vmTO);
for (NicTO nic : nics) {
Mockito.verify(nic).setDpdkEnabled(true);
}
}
@Test
public void validateConfigureVmOsDescriptionHostNotNullAndGuestOsMappingNotNullAndGuestOsDisplayNameNotNull(){
guru._guestOsDao = guestOsDaoMock;
guru._guestOsHypervisorDao = guestOSHypervisorDaoMock;
VirtualMachineTO virtualMachineTo = new VirtualMachineTO() {};
String platformEmulator = "Ubuntu";
Mockito.doReturn(guestOsVoMock).when(guestOsDaoMock).findByIdIncludingRemoved(Mockito.any());
Mockito.doReturn(guestOsMappingMock).when(guestOSHypervisorDaoMock).findByOsIdAndHypervisor(Mockito.anyLong(), Mockito.anyString(), Mockito.any());
Mockito.doReturn(platformEmulator).when(guestOsMappingMock).getGuestOsName();
guru.configureVmOsDescription(virtualMachineMock, virtualMachineTo, host);
Assert.assertEquals(platformEmulator, virtualMachineTo.getPlatformEmulator());
}
@Test
public void validateConfigureVmOsDescriptionHostNotNullAndGuestOsMappingNullAndGuestOsDisplayNameNull(){
guru._guestOsDao = guestOsDaoMock;
guru._guestOsHypervisorDao = guestOSHypervisorDaoMock;
VirtualMachineTO virtualMachineTo = new VirtualMachineTO() {};
Mockito.doReturn(guestOsVoMock).when(guestOsDaoMock).findByIdIncludingRemoved(Mockito.any());
Mockito.doReturn(null).when(guestOSHypervisorDaoMock).findByOsIdAndHypervisor(Mockito.anyLong(), Mockito.anyString(), Mockito.any());
guru.configureVmOsDescription(virtualMachineMock, virtualMachineTo, host);
Assert.assertEquals("Other", virtualMachineTo.getPlatformEmulator());
}
@Test
public void validateConfigureVmOsDescriptionHostNotNullAndGuestOsMappingNullAndGuestOsDisplayNameNotNull(){
guru._guestOsDao = guestOsDaoMock;
guru._guestOsHypervisorDao = guestOSHypervisorDaoMock;
VirtualMachineTO virtualMachineTo = new VirtualMachineTO() {};
String platformEmulator = "Ubuntu";
Mockito.doReturn(guestOsVoMock).when(guestOsDaoMock).findByIdIncludingRemoved(Mockito.any());
Mockito.doReturn(null).when(guestOSHypervisorDaoMock).findByOsIdAndHypervisor(Mockito.anyLong(), Mockito.anyString(), Mockito.any());
Mockito.doReturn(platformEmulator).when(guestOsVoMock).getDisplayName();
guru.configureVmOsDescription(virtualMachineMock, virtualMachineTo, host);
Assert.assertEquals(platformEmulator, virtualMachineTo.getPlatformEmulator());
}
@Test
public void validateConfigureVmOsDescriptionHostNullAndGuestOsMappingNullAndGuestOsDisplayNameNull(){
guru._guestOsDao = guestOsDaoMock;
VirtualMachineTO virtualMachineTo = new VirtualMachineTO() {};
Mockito.doReturn(guestOsVoMock).when(guestOsDaoMock).findByIdIncludingRemoved(Mockito.any());
guru.configureVmOsDescription(virtualMachineMock, virtualMachineTo, host);
Assert.assertEquals("Other", virtualMachineTo.getPlatformEmulator());
}
@Test
public void validateConfigureVmOsDescriptionHostNullAndGuestOsMappingNullAndGuestOsDisplayNameNotNull(){
guru._guestOsDao = guestOsDaoMock;
VirtualMachineTO virtualMachineTo = new VirtualMachineTO() {};
String platformEmulator = "Ubuntu";
Mockito.doReturn(guestOsVoMock).when(guestOsDaoMock).findByIdIncludingRemoved(Mockito.any());
Mockito.doReturn(platformEmulator).when(guestOsVoMock).getDisplayName();
guru.configureVmOsDescription(virtualMachineMock, virtualMachineTo, host);
Assert.assertEquals(platformEmulator, virtualMachineTo.getPlatformEmulator());
}
@Test
public void testGetClusterIdFromVMHost() {
Mockito.when(vm.getHostId()).thenReturn(123l);
HostVO vo = new HostVO("");
Long expected = 5l;
vo.setClusterId(expected);
Mockito.when(hostDao.findById(123l)).thenReturn(vo);
Long clusterId = guru.findClusterOfVm(vm);
Assert.assertEquals(expected, clusterId);
}
@Test
public void testGetClusterIdFromLastVMHost() {
Mockito.when(vm.getHostId()).thenReturn(null);
Mockito.when(vm.getLastHostId()).thenReturn(321l);
HostVO vo = new HostVO("");
Long expected = 7l;
vo.setClusterId(expected);
Mockito.when(hostDao.findById(321l)).thenReturn(vo);
Long clusterId = guru.findClusterOfVm(vm);
Assert.assertEquals(expected, clusterId);
}
@Test
public void testGetNullWhenVMThereIsNoInformationOfUsedHosts() {
Mockito.when(vm.getHostId()).thenReturn(null);
Mockito.when(vm.getLastHostId()).thenReturn(null);
Long clusterId = guru.findClusterOfVm(vm);
Assert.assertNull(clusterId);
}
}