Skip to content

Commit b64176f

Browse files
author
Gabriel Beims Bräscher
committed
Allow KVM VM live migration with ROOT volume on file
- Add JUnit tests
1 parent c6fbf56 commit b64176f

4 files changed

Lines changed: 418 additions & 17 deletions

File tree

engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/KvmNonManagedStorageDataMotionStrategy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo;
3131
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
3232
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
33-
import org.apache.commons.lang.StringUtils;
3433
import org.springframework.stereotype.Component;
3534

3635
import com.cloud.agent.api.Answer;
@@ -108,11 +107,12 @@ protected String generateDestPath(VirtualMachineTO vmTO, VolumeVO srcVolume, Hos
108107
throw new CloudRuntimeException(String.format("Unable to modify target volume on the host [host id:%s, name:%s]", destHost.getId(), destHost.getName()));
109108
}
110109

111-
String libvirtDestImgsPath = StringUtils.EMPTY;
110+
String libvirtDestImgsPath = null;
112111
if (rootImageProvisioningAnswer instanceof CreateAnswer) {
113-
libvirtDestImgsPath = ((CreateAnswer)rootImageProvisioningAnswer).getVolume().getName() + File.separator;
112+
libvirtDestImgsPath = ((CreateAnswer)rootImageProvisioningAnswer).getVolume().getName();
114113
}
115-
return libvirtDestImgsPath + destVolumeInfo.getUuid();
114+
// File.getAbsolutePath is used to keep the file separator as it should be and eliminate a verification to check if exists a file separator in the last character of libvirtDestImgsPath.
115+
return new File(libvirtDestImgsPath, destVolumeInfo.getUuid()).getAbsolutePath();
116116
}
117117

118118
/**
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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 static org.mockito.MockitoAnnotations.initMocks;
22+
23+
import java.io.File;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
28+
import org.apache.cloudstack.engine.subsystem.api.storage.StrategyPriority;
29+
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateDataFactory;
30+
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo;
31+
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
32+
import org.apache.cloudstack.storage.datastore.PrimaryDataStoreImpl;
33+
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
34+
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
35+
import org.apache.cloudstack.storage.volume.VolumeObject;
36+
import org.junit.Assert;
37+
import org.junit.Before;
38+
import org.junit.Test;
39+
import org.junit.runner.RunWith;
40+
import org.mockito.InjectMocks;
41+
import org.mockito.Mock;
42+
import org.mockito.Mockito;
43+
import org.mockito.Spy;
44+
import org.mockito.runners.MockitoJUnitRunner;
45+
46+
import com.cloud.agent.AgentManager;
47+
import com.cloud.agent.api.MigrateCommand;
48+
import com.cloud.agent.api.storage.CreateAnswer;
49+
import com.cloud.agent.api.storage.CreateCommand;
50+
import com.cloud.agent.api.to.VirtualMachineTO;
51+
import com.cloud.agent.api.to.VolumeTO;
52+
import com.cloud.host.HostVO;
53+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
54+
import com.cloud.storage.DataStoreRole;
55+
import com.cloud.storage.DiskOfferingVO;
56+
import com.cloud.storage.Storage;
57+
import com.cloud.storage.Storage.StoragePoolType;
58+
import com.cloud.storage.Volume;
59+
import com.cloud.storage.VolumeVO;
60+
import com.cloud.storage.dao.DiskOfferingDao;
61+
import com.cloud.vm.DiskProfile;
62+
63+
@RunWith(MockitoJUnitRunner.class)
64+
public class KvmNonManagedStorageSystemDataMotionTest {
65+
66+
@Mock
67+
private PrimaryDataStoreDao _storagePoolDao;
68+
69+
@Mock
70+
private TemplateDataFactory tmplFactory;
71+
72+
@Mock
73+
private AgentManager _agentMgr;
74+
75+
@Mock
76+
private DiskOfferingDao _diskOfferingDao;
77+
78+
@Spy
79+
@InjectMocks
80+
private KvmNonManagedStorageDataMotionStrategy strategy;
81+
82+
@Before
83+
public void setUp() throws Exception {
84+
initMocks(strategy);
85+
}
86+
87+
@Test
88+
public void canHandleTestExpectHypervisorStrategyForKvm() {
89+
canHandleExpectCantHandle(HypervisorType.KVM, 1, StrategyPriority.HYPERVISOR);
90+
}
91+
92+
@Test
93+
public void canHandleTestExpectCantHandle() {
94+
HypervisorType[] hypervisorTypeArray = HypervisorType.values();
95+
for (int i = 0; i < hypervisorTypeArray.length; i++) {
96+
HypervisorType ht = hypervisorTypeArray[i];
97+
if (ht.equals(HypervisorType.KVM)) {
98+
continue;
99+
}
100+
canHandleExpectCantHandle(ht, 0, StrategyPriority.CANT_HANDLE);
101+
}
102+
}
103+
104+
private void canHandleExpectCantHandle(HypervisorType hypervisorType, int times, StrategyPriority expectedStrategyPriority) {
105+
HostVO srcHost = new HostVO("sourceHostUuid");
106+
srcHost.setHypervisorType(hypervisorType);
107+
Mockito.doReturn(StrategyPriority.HYPERVISOR).when(strategy).internalCanHandle(new HashMap<>());
108+
109+
StrategyPriority strategyPriority = strategy.canHandle(new HashMap<>(), srcHost, new HostVO("destHostUuid"));
110+
111+
Mockito.verify(strategy, Mockito.times(times)).internalCanHandle(new HashMap<>());
112+
Assert.assertEquals(expectedStrategyPriority, strategyPriority);
113+
}
114+
115+
@Test
116+
public void internalCanHandleTestNonManaged() {
117+
StoragePoolType[] storagePoolTypeArray = StoragePoolType.values();
118+
for (int i = 0; i < storagePoolTypeArray.length; i++) {
119+
Map<VolumeInfo, DataStore> volumeMap = configureTestInternalCanHandle(false, storagePoolTypeArray[i]);
120+
StrategyPriority strategyPriority = strategy.internalCanHandle(volumeMap);
121+
if (storagePoolTypeArray[i] == StoragePoolType.Filesystem) {
122+
Assert.assertEquals(StrategyPriority.HYPERVISOR, strategyPriority);
123+
} else {
124+
Assert.assertEquals(StrategyPriority.CANT_HANDLE, strategyPriority);
125+
}
126+
}
127+
}
128+
129+
@Test
130+
public void internalCanHandleTestIsManaged() {
131+
StoragePoolType[] storagePoolTypeArray = StoragePoolType.values();
132+
for (int i = 0; i < storagePoolTypeArray.length; i++) {
133+
Map<VolumeInfo, DataStore> volumeMap = configureTestInternalCanHandle(true, storagePoolTypeArray[i]);
134+
StrategyPriority strategyPriority = strategy.internalCanHandle(volumeMap);
135+
Assert.assertEquals(StrategyPriority.CANT_HANDLE, strategyPriority);
136+
}
137+
}
138+
139+
private Map<VolumeInfo, DataStore> configureTestInternalCanHandle(boolean isManagedStorage, StoragePoolType storagePoolType) {
140+
VolumeObject volumeInfo = Mockito.spy(new VolumeObject());
141+
Mockito.doReturn(0l).when(volumeInfo).getPoolId();
142+
DataStore ds = Mockito.spy(new PrimaryDataStoreImpl());
143+
Mockito.doReturn(0l).when(ds).getId();
144+
145+
Map<VolumeInfo, DataStore> volumeMap = new HashMap<>();
146+
volumeMap.put(volumeInfo, ds);
147+
148+
StoragePoolVO storagePool = Mockito.spy(new StoragePoolVO());
149+
Mockito.doReturn(storagePoolType).when(storagePool).getPoolType();
150+
151+
Mockito.doReturn(storagePool).when(_storagePoolDao).findById(0l);
152+
Mockito.doReturn(isManagedStorage).when(storagePool).isManaged();
153+
return volumeMap;
154+
}
155+
156+
@Test
157+
public void getTemplateUuidTestTemplateIdNotNull() {
158+
String expectedTemplateUuid = prepareTestGetTemplateUuid();
159+
String templateUuid = strategy.getTemplateUuid(0l);
160+
Assert.assertEquals(expectedTemplateUuid, templateUuid);
161+
}
162+
163+
@Test
164+
public void getTemplateUuidTestTemplateIdNull() {
165+
prepareTestGetTemplateUuid();
166+
String templateUuid = strategy.getTemplateUuid(null);
167+
Assert.assertEquals(null, templateUuid);
168+
}
169+
170+
private String prepareTestGetTemplateUuid() {
171+
TemplateInfo templateImage = Mockito.mock(TemplateInfo.class);
172+
String expectedTemplateUuid = "template uuid";
173+
Mockito.when(templateImage.getUuid()).thenReturn(expectedTemplateUuid);
174+
Mockito.doReturn(templateImage).when(tmplFactory).getTemplate(0l, DataStoreRole.Image);
175+
return expectedTemplateUuid;
176+
}
177+
178+
@Test
179+
public void configureMigrateDiskInfoTest() {
180+
VolumeObject srcVolumeInfo = Mockito.spy(new VolumeObject());
181+
Mockito.doReturn("volume path").when(srcVolumeInfo).getPath();
182+
MigrateCommand.MigrateDiskInfo migrateDiskInfo = strategy.configureMigrateDiskInfo(srcVolumeInfo, "destPath");
183+
Assert.assertEquals(MigrateCommand.MigrateDiskInfo.DiskType.FILE, migrateDiskInfo.getDiskType());
184+
Assert.assertEquals(MigrateCommand.MigrateDiskInfo.DriverType.QCOW2, migrateDiskInfo.getDriverType());
185+
Assert.assertEquals(MigrateCommand.MigrateDiskInfo.Source.FILE, migrateDiskInfo.getSource());
186+
Assert.assertEquals("destPath", migrateDiskInfo.getSourceText());
187+
Assert.assertEquals("volume path", migrateDiskInfo.getSerialNumber());
188+
}
189+
190+
@Test
191+
public void generateDestPathTest() {
192+
String uuid = "f3d49ecc-870c-475a-89fa-fd0124420a9b";
193+
String destPath = "/var/lib/libvirt/images/";
194+
195+
VirtualMachineTO vmTO = Mockito.mock(VirtualMachineTO.class);
196+
Mockito.when(vmTO.getName()).thenReturn("vmName");
197+
198+
VolumeVO srcVolume = Mockito.spy(new VolumeVO("name", 0l, 0l, 0l, 0l, 0l, "folder", "path", Storage.ProvisioningType.THIN, 0l, Volume.Type.ROOT));
199+
StoragePoolVO destStoragePool = Mockito.spy(new StoragePoolVO());
200+
201+
VolumeInfo destVolumeInfo = Mockito.spy(new VolumeObject());
202+
Mockito.doReturn(0l).when(destVolumeInfo).getTemplateId();
203+
Mockito.doReturn(0l).when(destVolumeInfo).getId();
204+
Mockito.doReturn(Volume.Type.ROOT).when(destVolumeInfo).getVolumeType();
205+
Mockito.doReturn("name").when(destVolumeInfo).getName();
206+
Mockito.doReturn(0l).when(destVolumeInfo).getSize();
207+
Mockito.doReturn(uuid).when(destVolumeInfo).getUuid();
208+
209+
DiskOfferingVO diskOffering = Mockito.spy(new DiskOfferingVO());
210+
Mockito.doReturn(0l).when(diskOffering).getId();
211+
Mockito.doReturn(diskOffering).when(_diskOfferingDao).findById(0l);
212+
DiskProfile diskProfile = Mockito.spy(new DiskProfile(destVolumeInfo, diskOffering, HypervisorType.KVM));
213+
214+
String templateUuid = Mockito.doReturn("templateUuid").when(strategy).getTemplateUuid(0l);
215+
CreateCommand rootImageProvisioningCommand = new CreateCommand(diskProfile, templateUuid, destStoragePool, true);
216+
CreateAnswer createAnswer = Mockito.spy(new CreateAnswer(rootImageProvisioningCommand, "details"));
217+
Mockito.doReturn(true).when(createAnswer).getResult();
218+
219+
VolumeTO volumeTo = Mockito.mock(VolumeTO.class);
220+
Mockito.doReturn(destPath).when(volumeTo).getName();
221+
Mockito.doReturn(volumeTo).when(createAnswer).getVolume();
222+
223+
Mockito.doReturn(createAnswer).when(_agentMgr).easySend(0l, rootImageProvisioningCommand);
224+
225+
String generatedDestPath = strategy.generateDestPath(vmTO, srcVolume, new HostVO("sourceHostUuid"), destStoragePool, destVolumeInfo);
226+
227+
Assert.assertEquals(destPath + uuid, generatedDestPath);
228+
}
229+
}

0 commit comments

Comments
 (0)