|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.cloudstack.storage.motion; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; |
| 25 | +import org.apache.cloudstack.engine.subsystem.api.storage.StrategyPriority; |
| 26 | +import org.apache.cloudstack.engine.subsystem.api.storage.TemplateDataFactory; |
| 27 | +import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo; |
| 28 | +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; |
| 29 | +import org.apache.cloudstack.storage.datastore.PrimaryDataStoreImpl; |
| 30 | +import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
| 31 | +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
| 32 | +import org.apache.cloudstack.storage.volume.VolumeObject; |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | +import org.mockito.InjectMocks; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.Mockito; |
| 39 | +import org.mockito.Spy; |
| 40 | +import org.mockito.runners.MockitoJUnitRunner; |
| 41 | + |
| 42 | +import com.cloud.agent.AgentManager; |
| 43 | +import com.cloud.agent.api.MigrateCommand; |
| 44 | +import com.cloud.agent.api.storage.CreateAnswer; |
| 45 | +import com.cloud.agent.api.storage.CreateCommand; |
| 46 | +import com.cloud.agent.api.to.VirtualMachineTO; |
| 47 | +import com.cloud.agent.api.to.VolumeTO; |
| 48 | +import com.cloud.host.HostVO; |
| 49 | +import com.cloud.hypervisor.Hypervisor.HypervisorType; |
| 50 | +import com.cloud.storage.DataStoreRole; |
| 51 | +import com.cloud.storage.DiskOfferingVO; |
| 52 | +import com.cloud.storage.Storage; |
| 53 | +import com.cloud.storage.Storage.StoragePoolType; |
| 54 | +import com.cloud.storage.Volume; |
| 55 | +import com.cloud.storage.VolumeVO; |
| 56 | +import com.cloud.storage.dao.DiskOfferingDao; |
| 57 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 58 | +import com.cloud.vm.DiskProfile; |
| 59 | + |
| 60 | +@RunWith(MockitoJUnitRunner.class) |
| 61 | +public class KvmNonManagedStorageSystemDataMotionTest { |
| 62 | + |
| 63 | + @Mock |
| 64 | + private PrimaryDataStoreDao storagePoolDao; |
| 65 | + |
| 66 | + @Mock |
| 67 | + private TemplateDataFactory tmplFactory; |
| 68 | + |
| 69 | + @Mock |
| 70 | + private AgentManager agentMgr; |
| 71 | + |
| 72 | + @Mock |
| 73 | + private DiskOfferingDao diskOfferingDao; |
| 74 | + |
| 75 | + @Spy |
| 76 | + @InjectMocks |
| 77 | + private KvmNonManagedStorageDataMotionStrategy strategy; |
| 78 | + |
| 79 | + @Test |
| 80 | + public void canHandleTestExpectHypervisorStrategyForKvm() { |
| 81 | + canHandleExpectCanottHandle(HypervisorType.KVM, 1, StrategyPriority.HYPERVISOR); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void canHandleTestExpectCannotHandle() { |
| 86 | + HypervisorType[] hypervisorTypeArray = HypervisorType.values(); |
| 87 | + for (int i = 0; i < hypervisorTypeArray.length; i++) { |
| 88 | + HypervisorType ht = hypervisorTypeArray[i]; |
| 89 | + if (ht.equals(HypervisorType.KVM)) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + canHandleExpectCanottHandle(ht, 0, StrategyPriority.CANT_HANDLE); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private void canHandleExpectCanottHandle(HypervisorType hypervisorType, int times, StrategyPriority expectedStrategyPriority) { |
| 97 | + HostVO srcHost = new HostVO("sourceHostUuid"); |
| 98 | + srcHost.setHypervisorType(hypervisorType); |
| 99 | + Mockito.doReturn(StrategyPriority.HYPERVISOR).when(strategy).internalCanHandle(new HashMap<>()); |
| 100 | + |
| 101 | + StrategyPriority strategyPriority = strategy.canHandle(new HashMap<>(), srcHost, new HostVO("destHostUuid")); |
| 102 | + |
| 103 | + Mockito.verify(strategy, Mockito.times(times)).internalCanHandle(new HashMap<>()); |
| 104 | + Assert.assertEquals(expectedStrategyPriority, strategyPriority); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void internalCanHandleTestNonManaged() { |
| 109 | + StoragePoolType[] storagePoolTypeArray = StoragePoolType.values(); |
| 110 | + for (int i = 0; i < storagePoolTypeArray.length; i++) { |
| 111 | + Map<VolumeInfo, DataStore> volumeMap = configureTestInternalCanHandle(false, storagePoolTypeArray[i]); |
| 112 | + StrategyPriority strategyPriority = strategy.internalCanHandle(volumeMap); |
| 113 | + if (storagePoolTypeArray[i] == StoragePoolType.Filesystem) { |
| 114 | + Assert.assertEquals(StrategyPriority.HYPERVISOR, strategyPriority); |
| 115 | + } else { |
| 116 | + Assert.assertEquals(StrategyPriority.CANT_HANDLE, strategyPriority); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void internalCanHandleTestIsManaged() { |
| 123 | + StoragePoolType[] storagePoolTypeArray = StoragePoolType.values(); |
| 124 | + for (int i = 0; i < storagePoolTypeArray.length; i++) { |
| 125 | + Map<VolumeInfo, DataStore> volumeMap = configureTestInternalCanHandle(true, storagePoolTypeArray[i]); |
| 126 | + StrategyPriority strategyPriority = strategy.internalCanHandle(volumeMap); |
| 127 | + Assert.assertEquals(StrategyPriority.CANT_HANDLE, strategyPriority); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private Map<VolumeInfo, DataStore> configureTestInternalCanHandle(boolean isManagedStorage, StoragePoolType storagePoolType) { |
| 132 | + VolumeObject volumeInfo = Mockito.spy(new VolumeObject()); |
| 133 | + Mockito.doReturn(0l).when(volumeInfo).getPoolId(); |
| 134 | + DataStore ds = Mockito.spy(new PrimaryDataStoreImpl()); |
| 135 | + Mockito.doReturn(0l).when(ds).getId(); |
| 136 | + |
| 137 | + Map<VolumeInfo, DataStore> volumeMap = new HashMap<>(); |
| 138 | + volumeMap.put(volumeInfo, ds); |
| 139 | + |
| 140 | + StoragePoolVO storagePool = Mockito.spy(new StoragePoolVO()); |
| 141 | + Mockito.doReturn(storagePoolType).when(storagePool).getPoolType(); |
| 142 | + |
| 143 | + Mockito.doReturn(storagePool).when(storagePoolDao).findById(0l); |
| 144 | + Mockito.doReturn(isManagedStorage).when(storagePool).isManaged(); |
| 145 | + return volumeMap; |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void getTemplateUuidTestTemplateIdNotNull() { |
| 150 | + String expectedTemplateUuid = prepareTestGetTemplateUuid(); |
| 151 | + String templateUuid = strategy.getTemplateUuid(0l); |
| 152 | + Assert.assertEquals(expectedTemplateUuid, templateUuid); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void getTemplateUuidTestTemplateIdNull() { |
| 157 | + prepareTestGetTemplateUuid(); |
| 158 | + String templateUuid = strategy.getTemplateUuid(null); |
| 159 | + Assert.assertEquals(null, templateUuid); |
| 160 | + } |
| 161 | + |
| 162 | + private String prepareTestGetTemplateUuid() { |
| 163 | + TemplateInfo templateImage = Mockito.mock(TemplateInfo.class); |
| 164 | + String expectedTemplateUuid = "template uuid"; |
| 165 | + Mockito.when(templateImage.getUuid()).thenReturn(expectedTemplateUuid); |
| 166 | + Mockito.doReturn(templateImage).when(tmplFactory).getTemplate(0l, DataStoreRole.Image); |
| 167 | + return expectedTemplateUuid; |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void configureMigrateDiskInfoTest() { |
| 172 | + VolumeObject srcVolumeInfo = Mockito.spy(new VolumeObject()); |
| 173 | + Mockito.doReturn("volume path").when(srcVolumeInfo).getPath(); |
| 174 | + MigrateCommand.MigrateDiskInfo migrateDiskInfo = strategy.configureMigrateDiskInfo(srcVolumeInfo, "destPath"); |
| 175 | + Assert.assertEquals(MigrateCommand.MigrateDiskInfo.DiskType.FILE, migrateDiskInfo.getDiskType()); |
| 176 | + Assert.assertEquals(MigrateCommand.MigrateDiskInfo.DriverType.QCOW2, migrateDiskInfo.getDriverType()); |
| 177 | + Assert.assertEquals(MigrateCommand.MigrateDiskInfo.Source.FILE, migrateDiskInfo.getSource()); |
| 178 | + Assert.assertEquals("destPath", migrateDiskInfo.getSourceText()); |
| 179 | + Assert.assertEquals("volume path", migrateDiskInfo.getSerialNumber()); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void generateDestPathTest() { |
| 184 | + configureAndVerifygenerateDestPathTest(true, false); |
| 185 | + } |
| 186 | + |
| 187 | + @Test(expected = CloudRuntimeException.class) |
| 188 | + public void generateDestPathTestExpectCloudRuntimeException() { |
| 189 | + configureAndVerifygenerateDestPathTest(false, false); |
| 190 | + } |
| 191 | + |
| 192 | + @Test(expected = CloudRuntimeException.class) |
| 193 | + public void generateDestPathTestExpectCloudRuntimeException2() { |
| 194 | + configureAndVerifygenerateDestPathTest(false, true); |
| 195 | + } |
| 196 | + |
| 197 | + private void configureAndVerifygenerateDestPathTest(boolean answerResult, boolean answerIsNull) { |
| 198 | + String uuid = "f3d49ecc-870c-475a-89fa-fd0124420a9b"; |
| 199 | + String destPath = "/var/lib/libvirt/images/"; |
| 200 | + |
| 201 | + VirtualMachineTO vmTO = Mockito.mock(VirtualMachineTO.class); |
| 202 | + Mockito.when(vmTO.getName()).thenReturn("vmName"); |
| 203 | + |
| 204 | + VolumeVO srcVolume = Mockito.spy(new VolumeVO("name", 0l, 0l, 0l, 0l, 0l, "folder", "path", Storage.ProvisioningType.THIN, 0l, Volume.Type.ROOT)); |
| 205 | + StoragePoolVO destStoragePool = Mockito.spy(new StoragePoolVO()); |
| 206 | + |
| 207 | + VolumeInfo destVolumeInfo = Mockito.spy(new VolumeObject()); |
| 208 | + Mockito.doReturn(0l).when(destVolumeInfo).getTemplateId(); |
| 209 | + Mockito.doReturn(0l).when(destVolumeInfo).getId(); |
| 210 | + Mockito.doReturn(Volume.Type.ROOT).when(destVolumeInfo).getVolumeType(); |
| 211 | + Mockito.doReturn("name").when(destVolumeInfo).getName(); |
| 212 | + Mockito.doReturn(0l).when(destVolumeInfo).getSize(); |
| 213 | + Mockito.doReturn(uuid).when(destVolumeInfo).getUuid(); |
| 214 | + |
| 215 | + DiskOfferingVO diskOffering = Mockito.spy(new DiskOfferingVO()); |
| 216 | + Mockito.doReturn(0l).when(diskOffering).getId(); |
| 217 | + Mockito.doReturn(diskOffering).when(diskOfferingDao).findById(0l); |
| 218 | + DiskProfile diskProfile = Mockito.spy(new DiskProfile(destVolumeInfo, diskOffering, HypervisorType.KVM)); |
| 219 | + |
| 220 | + String templateUuid = Mockito.doReturn("templateUuid").when(strategy).getTemplateUuid(0l); |
| 221 | + CreateCommand rootImageProvisioningCommand = new CreateCommand(diskProfile, templateUuid, destStoragePool, true); |
| 222 | + CreateAnswer createAnswer = Mockito.spy(new CreateAnswer(rootImageProvisioningCommand, "details")); |
| 223 | + Mockito.doReturn(answerResult).when(createAnswer).getResult(); |
| 224 | + |
| 225 | + VolumeTO volumeTo = Mockito.mock(VolumeTO.class); |
| 226 | + Mockito.doReturn(destPath).when(volumeTo).getName(); |
| 227 | + Mockito.doReturn(volumeTo).when(createAnswer).getVolume(); |
| 228 | + |
| 229 | + if (answerIsNull) { |
| 230 | + Mockito.doReturn(null).when(agentMgr).easySend(0l, rootImageProvisioningCommand); |
| 231 | + } else { |
| 232 | + Mockito.doReturn(createAnswer).when(agentMgr).easySend(0l, rootImageProvisioningCommand); |
| 233 | + } |
| 234 | + |
| 235 | + String generatedDestPath = strategy.generateDestPath(vmTO, srcVolume, new HostVO("sourceHostUuid"), destStoragePool, destVolumeInfo); |
| 236 | + |
| 237 | + Assert.assertEquals(destPath + uuid, generatedDestPath); |
| 238 | + } |
| 239 | +} |
0 commit comments