Fix KVM unmanage disks path (#8483)

This PR fixes the volumes path on KVM import unmanaged instances

Fixes: #8479
This commit is contained in:
Nicolas Vazquez 2024-01-11 06:15:57 -03:00 committed by GitHub
parent 64f4480ef4
commit 59e78cbc45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 1 deletions

View File

@ -191,7 +191,6 @@ public final class LibvirtGetUnmanagedInstancesCommandWrapper extends CommandWra
disk.setLabel(diskDef.getDiskLabel());
disk.setController(diskDef.getBusType().toString());
Pair<String, String> sourceHostPath = getSourceHostPath(libvirtComputingResource, diskDef.getSourcePath());
if (sourceHostPath != null) {
disk.setDatastoreHost(sourceHostPath.first());
@ -210,11 +209,26 @@ public final class LibvirtGetUnmanagedInstancesCommandWrapper extends CommandWra
disk.setDatastorePort(diskDef.getSourceHostPort());
disk.setImagePath(diskDef.getSourcePath());
disk.setDatastoreName(disk.getDatastorePath());
disk.setFileBaseName(getDiskRelativePath(diskDef));
disks.add(disk);
}
return disks;
}
protected String getDiskRelativePath(LibvirtVMDef.DiskDef diskDef) {
if (diskDef == null || diskDef.getDiskType() == null || diskDef.getDiskType() == LibvirtVMDef.DiskDef.DiskType.BLOCK) {
return null;
}
String sourcePath = diskDef.getSourcePath();
if (StringUtils.isBlank(sourcePath)) {
return null;
}
if (!sourcePath.contains("/")) {
return sourcePath;
}
return sourcePath.substring(sourcePath.lastIndexOf("/") + 1);
}
private Pair<String, String> getSourceHostPath(LibvirtComputingResource libvirtComputingResource, String diskPath) {
int pathEnd = diskPath.lastIndexOf("/");
if (pathEnd >= 0) {

View File

@ -0,0 +1,73 @@
// 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.kvm.resource.wrapper;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.UUID;
@RunWith(MockitoJUnitRunner.class)
public class LibvirtGetUnmanagedInstancesCommandWrapperTest {
@Spy
private LibvirtGetUnmanagedInstancesCommandWrapper wrapper = new LibvirtGetUnmanagedInstancesCommandWrapper();
@Test
public void testGetDiskRelativePathNullDisk() {
Assert.assertNull(wrapper.getDiskRelativePath(null));
}
@Test
public void testGetDiskRelativePathBlockType() {
LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class);
Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.BLOCK);
Assert.assertNull(wrapper.getDiskRelativePath(diskDef));
}
@Test
public void testGetDiskRelativePathNullPath() {
LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class);
Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.FILE);
Mockito.when(diskDef.getSourcePath()).thenReturn(null);
Assert.assertNull(wrapper.getDiskRelativePath(diskDef));
}
@Test
public void testGetDiskRelativePathWithoutSlashes() {
LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class);
Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.FILE);
String imagePath = UUID.randomUUID().toString();
Mockito.when(diskDef.getSourcePath()).thenReturn(imagePath);
Assert.assertEquals(imagePath, wrapper.getDiskRelativePath(diskDef));
}
@Test
public void testGetDiskRelativePathFullPath() {
LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class);
Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.FILE);
String relativePath = "ea4b2296-d349-4968-ab72-c8eb523b556e";
String imagePath = String.format("/mnt/97e4c9ed-e3bc-3e26-b103-7967fc9feae1/%s", relativePath);
Mockito.when(diskDef.getSourcePath()).thenReturn(imagePath);
Assert.assertEquals(relativePath, wrapper.getDiskRelativePath(diskDef));
}
}