kvm: Add UnitTests for LibvirtUtilitiesHelper

These were lacking, but this helper is used in various places
inside the KVM code.

Some simple tests to verify the helper is doing what we expect it
to do.
This commit is contained in:
Wido den Hollander 2015-10-29 12:52:42 +01:00
parent eabf11c99d
commit 602231132d
6 changed files with 72 additions and 12 deletions

View File

@ -66,7 +66,7 @@ public final class LibvirtCopyVolumeCommandWrapper extends CommandWrapper<CopyVo
}
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final String volumeName = libvirtUtilitiesHelper.generatereUUIDName();
final String volumeName = libvirtUtilitiesHelper.generateUUIDName();
if (copyToSecondary) {
final String destVolumeName = volumeName + ".qcow2";

View File

@ -54,7 +54,7 @@ public final class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper extend
final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
final String templateInstallFolder = "template/tmpl/" + templateFolder;
final String tmplName = libvirtUtilitiesHelper.generatereUUIDName();
final String tmplName = libvirtUtilitiesHelper.generateUUIDName();
final String tmplFileName = tmplName + ".qcow2";
KVMStoragePool secondaryPool = null;

View File

@ -64,7 +64,7 @@ public class LibvirtUtilitiesHelper {
return qcow2Processor;
}
public String generatereUUIDName() {
public String generateUUIDName() {
return UUID.randomUUID().toString();
}

View File

@ -3743,7 +3743,7 @@ public class LibvirtComputingResourceTest {
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
try {
when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenReturn(qcow2Processor);
@ -3817,7 +3817,7 @@ public class LibvirtComputingResourceTest {
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
try {
when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenThrow(ConfigurationException.class);
@ -3890,7 +3890,7 @@ public class LibvirtComputingResourceTest {
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
try {
when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenReturn(qcow2Processor);
@ -3964,7 +3964,7 @@ public class LibvirtComputingResourceTest {
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
try {
when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenReturn(qcow2Processor);
@ -4022,7 +4022,7 @@ public class LibvirtComputingResourceTest {
snapshotPath = snapshotPath.substring(0, index);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool);
when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl())).thenReturn(secondaryPool);
@ -4067,7 +4067,7 @@ public class LibvirtComputingResourceTest {
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(destVolumeName);
when(primary.getPhysicalDisk(command.getVolumePath())).thenReturn(disk);
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL)).thenReturn(secondary);
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
@ -4113,7 +4113,7 @@ public class LibvirtComputingResourceTest {
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(destVolumeName);
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
@ -4156,7 +4156,7 @@ public class LibvirtComputingResourceTest {
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(destVolumeName);
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
@ -4230,7 +4230,7 @@ public class LibvirtComputingResourceTest {
pool.getUserInfo(), pool.getType())).thenReturn(primary);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(destVolumeName);
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);

View File

@ -0,0 +1,56 @@
// 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 java.util.UUID;
import com.cloud.utils.script.Script;
import junit.framework.TestCase;
public class LibvirtUtilitiesHelperTest extends TestCase {
public void testGenerateUUID() {
LibvirtUtilitiesHelper helper = new LibvirtUtilitiesHelper();
UUID uuid = UUID.fromString(helper.generateUUIDName());
assertEquals(4, uuid.version());
}
public void testSSHKeyPaths() {
LibvirtUtilitiesHelper helper = new LibvirtUtilitiesHelper();
/* These paths are hardcoded in LibvirtComputingResource and we should
* verify that they do not change.
* Hardcoded paths are not what we want in the longer run
*/
assertEquals("/root/.ssh", helper.retrieveSshKeysPath());
assertEquals("/root/.ssh/id_rsa.pub.cloud", helper.retrieveSshPubKeyPath());
assertEquals("/root/.ssh/id_rsa.cloud", helper.retrieveSshPrvKeyPath());
}
public void testBashScriptPath() {
LibvirtUtilitiesHelper helper = new LibvirtUtilitiesHelper();
assertEquals("/bin/bash", helper.retrieveBashScriptPath());
}
public void testBuildScript() {
LibvirtUtilitiesHelper helper = new LibvirtUtilitiesHelper();
String path = "/path/to/my/script";
Script script = helper.buildScript(path);
assertEquals(path + " ", script.toString());
assertEquals(LibvirtUtilitiesHelper.TIMEOUT, script.getTimeout());
}
}

View File

@ -158,6 +158,10 @@ public class Script implements Callable<String> {
return builder.toString();
}
public long getTimeout() {
return _timeout;
}
public String execute() {
return execute(new OutputInterpreter.OutputLogger(_logger));
}