|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.hypervisor.guru; |
| 18 | + |
| 19 | +import com.cloud.agent.api.Command; |
| 20 | +import com.cloud.agent.api.MigrateVmToPoolCommand; |
| 21 | +import com.cloud.dc.ClusterDetailsDao; |
| 22 | +import com.cloud.host.HostVO; |
| 23 | +import com.cloud.host.dao.HostDao; |
| 24 | +import com.cloud.storage.StoragePool; |
| 25 | +import com.cloud.storage.StoragePoolHostVO; |
| 26 | +import com.cloud.storage.Volume; |
| 27 | +import com.cloud.storage.dao.StoragePoolHostDao; |
| 28 | +import com.cloud.utils.Pair; |
| 29 | +import com.cloud.vm.VirtualMachine; |
| 30 | +import com.cloud.vm.VirtualMachineManager; |
| 31 | +import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
| 32 | +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.mockito.InjectMocks; |
| 38 | +import org.mockito.Mock; |
| 39 | +import org.mockito.Mockito; |
| 40 | +import org.mockito.MockitoAnnotations; |
| 41 | +import org.mockito.Spy; |
| 42 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 43 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 44 | +import org.springframework.test.context.ContextConfiguration; |
| 45 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 46 | + |
| 47 | +import java.util.ArrayList; |
| 48 | +import java.util.HashMap; |
| 49 | +import java.util.List; |
| 50 | +import java.util.Map; |
| 51 | + |
| 52 | +@RunWith(PowerMockRunner.class) |
| 53 | +@PrepareForTest({VMwareGuru.class}) |
| 54 | +@ContextConfiguration(loader = AnnotationConfigContextLoader.class) |
| 55 | +public class VMwareGuruTest { |
| 56 | + |
| 57 | + @Spy |
| 58 | + @InjectMocks |
| 59 | + private VMwareGuru vMwareGuru = new VMwareGuru(); |
| 60 | + |
| 61 | + @Mock |
| 62 | + PrimaryDataStoreDao _storagePoolDao; |
| 63 | + |
| 64 | + @Mock |
| 65 | + StoragePoolHostDao storagePoolHostDao; |
| 66 | + |
| 67 | + @Mock |
| 68 | + HostDao _hostDao; |
| 69 | + |
| 70 | + @Mock |
| 71 | + VirtualMachineManager vmManager; |
| 72 | + |
| 73 | + @Mock |
| 74 | + ClusterDetailsDao _clusterDetailsDao; |
| 75 | + |
| 76 | + @Before |
| 77 | + public void testSetUp() throws Exception { |
| 78 | + MockitoAnnotations.initMocks(this); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void finalizeMigrateForLocalStorageToHaveTargetHostGuid(){ |
| 83 | + VirtualMachine vm = Mockito.mock(VirtualMachine.class); |
| 84 | + Map<Volume, StoragePool> volumeToPool = new HashMap<>(); |
| 85 | + Volume rootVolume = Mockito.mock(Volume.class); |
| 86 | + Volume dataVolume = Mockito.mock(Volume.class); |
| 87 | + StoragePool localStorage = Mockito.mock(StoragePool.class); |
| 88 | + volumeToPool.put(rootVolume, localStorage); |
| 89 | + volumeToPool.put(dataVolume, localStorage); |
| 90 | + |
| 91 | + // prepare localstorage host guid |
| 92 | + StoragePoolVO storagePoolVO = Mockito.mock(StoragePoolVO.class); |
| 93 | + StoragePoolHostVO storagePoolHostVO = Mockito.mock(StoragePoolHostVO.class); |
| 94 | + HostVO hostVO = Mockito.mock(HostVO.class); |
| 95 | + |
| 96 | + Mockito.when(localStorage.getId()).thenReturn(1L); |
| 97 | + Mockito.when(vm.getId()).thenReturn(1L); |
| 98 | + Mockito.when(_storagePoolDao.findById(1L)).thenReturn(storagePoolVO); |
| 99 | + Mockito.when(rootVolume.getVolumeType()).thenReturn(Volume.Type.ROOT); |
| 100 | + Mockito.when(dataVolume.getVolumeType()).thenReturn(Volume.Type.DATADISK); |
| 101 | + Mockito.when(localStorage.isLocal()).thenReturn(true); |
| 102 | + Pair<Long, Long> clusterAndHost = new Pair<>(1L, 1L); |
| 103 | + |
| 104 | + Mockito.when(vmManager.findClusterAndHostIdForVm(1L)).thenReturn(clusterAndHost); |
| 105 | + |
| 106 | + List<StoragePoolHostVO> storagePoolHostVOS = new ArrayList<>(); |
| 107 | + storagePoolHostVOS.add(storagePoolHostVO); |
| 108 | + Mockito.when(storagePoolHostDao.listByPoolId(1L)).thenReturn(storagePoolHostVOS); |
| 109 | + Mockito.when(storagePoolHostVO.getHostId()).thenReturn(2L); |
| 110 | + Mockito.when(_hostDao.findById(2L)).thenReturn(hostVO); |
| 111 | + Mockito. when( hostVO. getGuid()). thenReturn( "HostSystem:[email protected]"); |
| 112 | + |
| 113 | + List<Command> commandsList = vMwareGuru.finalizeMigrate(vm, volumeToPool); |
| 114 | + |
| 115 | + MigrateVmToPoolCommand migrateVmToPoolCommand = (MigrateVmToPoolCommand) commandsList.get(0); |
| 116 | + Assert. assertEquals( "HostSystem:[email protected]", migrateVmToPoolCommand. getHostGuidInTargetCluster()); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments