Skip to content

Commit

Permalink
Fix KVM unmanage disks path (apache#8483)
Browse files Browse the repository at this point in the history
This PR fixes the volumes path on KVM import unmanaged instances

Fixes: apache#8479
  • Loading branch information
nvazquez authored Jan 11, 2024
1 parent 64f4480 commit 59e78cb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ private List<UnmanagedInstanceTO.Disk> getUnmanagedInstanceDisks(List<LibvirtVMD
disk.setLabel(diskDef.getDiskLabel());
disk.setController(diskDef.getBusType().toString());


Pair<String, String> sourceHostPath = getSourceHostPath(libvirtComputingResource, diskDef.getSourcePath());
if (sourceHostPath != null) {
disk.setDatastoreHost(sourceHostPath.first());
Expand All @@ -210,11 +209,26 @@ private List<UnmanagedInstanceTO.Disk> getUnmanagedInstanceDisks(List<LibvirtVMD
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 59e78cb

Please sign in to comment.