Skip to content

Commit 30a8528

Browse files
committed
Merge pull request apache#1005 from wido/libvirt-unittests
kvm: Add UnitTests for LibvirtUtilitiesHelperThese 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. * pr/1005: kvm: Add UnitTests for LibvirtUtilitiesHelper Signed-off-by: Remi Bergsma <github@remi.nl>
2 parents a981d34 + 6022311 commit 30a8528

6 files changed

Lines changed: 72 additions & 12 deletions

File tree

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCopyVolumeCommandWrapper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Answer execute(final CopyVolumeCommand command, final LibvirtComputingRes
6666
}
6767

6868
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
69-
final String volumeName = libvirtUtilitiesHelper.generatereUUIDName();
69+
final String volumeName = libvirtUtilitiesHelper.generateUUIDName();
7070

7171
if (copyToSecondary) {
7272
final String destVolumeName = volumeName + ".qcow2";

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, fi
5454

5555
final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
5656
final String templateInstallFolder = "template/tmpl/" + templateFolder;
57-
final String tmplName = libvirtUtilitiesHelper.generatereUUIDName();
57+
final String tmplName = libvirtUtilitiesHelper.generateUUIDName();
5858
final String tmplFileName = tmplName + ".qcow2";
5959

6060
KVMStoragePool secondaryPool = null;

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Processor buildQCOW2Processor(final StorageLayer storage) throws Configur
6464
return qcow2Processor;
6565
}
6666

67-
public String generatereUUIDName() {
67+
public String generateUUIDName() {
6868
return UUID.randomUUID().toString();
6969
}
7070

‎plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3743,7 +3743,7 @@ public void testCreatePrivateTemplateFromSnapshotCommand() {
37433743

37443744
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
37453745
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
3746-
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
3746+
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
37473747

37483748
try {
37493749
when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenReturn(qcow2Processor);
@@ -3817,7 +3817,7 @@ public void testCreatePrivateTemplateFromSnapshotCommandConfigurationException()
38173817

38183818
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
38193819
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
3820-
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
3820+
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
38213821

38223822
try {
38233823
when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenThrow(ConfigurationException.class);
@@ -3890,7 +3890,7 @@ public void testCreatePrivateTemplateFromSnapshotCommandInternalErrorException()
38903890

38913891
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
38923892
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
3893-
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
3893+
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
38943894

38953895
try {
38963896
when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenReturn(qcow2Processor);
@@ -3964,7 +3964,7 @@ public void testCreatePrivateTemplateFromSnapshotCommandIOException() {
39643964

39653965
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
39663966
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
3967-
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
3967+
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
39683968

39693969
try {
39703970
when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenReturn(qcow2Processor);
@@ -4022,7 +4022,7 @@ public void testCreatePrivateTemplateFromSnapshotCommandCloudRuntime() {
40224022
snapshotPath = snapshotPath.substring(0, index);
40234023

40244024
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
4025-
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(tmplName);
4025+
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName);
40264026

40274027
when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool);
40284028
when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl())).thenReturn(secondaryPool);
@@ -4067,7 +4067,7 @@ public void testCopyVolumeCommand() {
40674067
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
40684068

40694069
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
4070-
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
4070+
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(destVolumeName);
40714071
when(primary.getPhysicalDisk(command.getVolumePath())).thenReturn(disk);
40724072
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL)).thenReturn(secondary);
40734073
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
@@ -4113,7 +4113,7 @@ public void testCopyVolumeCommandToSecFalse() {
41134113
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
41144114
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
41154115
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
4116-
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
4116+
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(destVolumeName);
41174117
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
41184118
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
41194119
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
@@ -4156,7 +4156,7 @@ public void testCopyVolumeCommandCloudRuntime() {
41564156
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
41574157
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
41584158
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
4159-
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
4159+
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(destVolumeName);
41604160
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
41614161
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
41624162
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
@@ -4230,7 +4230,7 @@ public void testCopyVolumeCommandPrimaryNotFound() {
42304230
pool.getUserInfo(), pool.getType())).thenReturn(primary);
42314231

42324232
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
4233-
when(libvirtUtilitiesHelper.generatereUUIDName()).thenReturn(destVolumeName);
4233+
when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(destVolumeName);
42344234
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
42354235
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
42364236
when(storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.hypervisor.kvm.resource.wrapper;
18+
19+
import java.util.UUID;
20+
21+
import com.cloud.utils.script.Script;
22+
23+
import junit.framework.TestCase;
24+
25+
public class LibvirtUtilitiesHelperTest extends TestCase {
26+
27+
public void testGenerateUUID() {
28+
LibvirtUtilitiesHelper helper = new LibvirtUtilitiesHelper();
29+
UUID uuid = UUID.fromString(helper.generateUUIDName());
30+
assertEquals(4, uuid.version());
31+
}
32+
33+
public void testSSHKeyPaths() {
34+
LibvirtUtilitiesHelper helper = new LibvirtUtilitiesHelper();
35+
/* These paths are hardcoded in LibvirtComputingResource and we should
36+
* verify that they do not change.
37+
* Hardcoded paths are not what we want in the longer run
38+
*/
39+
assertEquals("/root/.ssh", helper.retrieveSshKeysPath());
40+
assertEquals("/root/.ssh/id_rsa.pub.cloud", helper.retrieveSshPubKeyPath());
41+
assertEquals("/root/.ssh/id_rsa.cloud", helper.retrieveSshPrvKeyPath());
42+
}
43+
44+
public void testBashScriptPath() {
45+
LibvirtUtilitiesHelper helper = new LibvirtUtilitiesHelper();
46+
assertEquals("/bin/bash", helper.retrieveBashScriptPath());
47+
}
48+
49+
public void testBuildScript() {
50+
LibvirtUtilitiesHelper helper = new LibvirtUtilitiesHelper();
51+
String path = "/path/to/my/script";
52+
Script script = helper.buildScript(path);
53+
assertEquals(path + " ", script.toString());
54+
assertEquals(LibvirtUtilitiesHelper.TIMEOUT, script.getTimeout());
55+
}
56+
}

‎utils/src/main/java/com/cloud/utils/script/Script.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ protected String buildCommandLine(List<String> command) {
158158
return builder.toString();
159159
}
160160

161+
public long getTimeout() {
162+
return _timeout;
163+
}
164+
161165
public String execute() {
162166
return execute(new OutputInterpreter.OutputLogger(_logger));
163167
}

0 commit comments

Comments
 (0)