Skip to content

Commit 0b4cc5d

Browse files
committed
Merge pull request apache#986 from wido/kvm-libvirt-unittests
Add Unit Tests for Libvirt/KVM storage codeThese classes were not covered by Unit Tests and this commit adds some tests for their basic functionality. * pr/986: Add Unit Tests for Libvirt/KVM storage code Signed-off-by: Remi Bergsma <github@remi.nl>
2 parents 0c52f70 + 7568f21 commit 0b4cc5d

3 files changed

Lines changed: 143 additions & 5 deletions

File tree

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/LibvirtStoragePool.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
public class LibvirtStoragePool implements KVMStoragePool {
3333
private static final Logger s_logger = Logger.getLogger(LibvirtStoragePool.class);
3434
protected String uuid;
35-
protected String uri;
3635
protected long capacity;
3736
protected long used;
3837
protected long available;
@@ -100,10 +99,6 @@ public String getUuid() {
10099
return this.uuid;
101100
}
102101

103-
public String uri() {
104-
return this.uri;
105-
}
106-
107102
@Override
108103
public PhysicalDiskFormat getDefaultFormat() {
109104
if (getStoragePoolType() == StoragePoolType.CLVM || getStoragePoolType() == StoragePoolType.RBD) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.storage;
18+
19+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
20+
import org.mockito.Mockito;
21+
22+
import junit.framework.TestCase;
23+
24+
public class KVMPhysicalDiskTest extends TestCase {
25+
26+
public void testRBDStringBuilder() {
27+
assertEquals(KVMPhysicalDisk.RBDStringBuilder("ceph-monitor", 8000, "admin", "supersecret", "volume1"),
28+
"rbd:volume1:mon_host=ceph-monitor\\\\:8000:auth_supported=cephx:id=admin:key=supersecret:rbd_default_format=2:client_mount_timeout=30");
29+
}
30+
31+
public void testAttributes() {
32+
String name = "3bc186e0-6c29-45bf-b2b0-ddef6f91f5ef";
33+
String path = "/" + name;
34+
35+
LibvirtStoragePool pool = Mockito.mock(LibvirtStoragePool.class);
36+
37+
KVMPhysicalDisk disk = new KVMPhysicalDisk(path, name, pool);
38+
assertEquals(disk.getName(), name);
39+
assertEquals(disk.getPath(), path);
40+
assertEquals(disk.getPool(), pool);
41+
assertEquals(disk.getSize(), 0);
42+
assertEquals(disk.getVirtualSize(), 0);
43+
44+
disk.setSize(1024);
45+
disk.setVirtualSize(2048);
46+
assertEquals(disk.getSize(), 1024);
47+
assertEquals(disk.getVirtualSize(), 2048);
48+
49+
disk.setFormat(PhysicalDiskFormat.RAW);
50+
assertEquals(disk.getFormat(), PhysicalDiskFormat.RAW);
51+
}
52+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.storage;
18+
19+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
20+
import org.libvirt.StoragePool;
21+
import org.mockito.Mockito;
22+
23+
import com.cloud.storage.Storage.StoragePoolType;
24+
25+
import junit.framework.TestCase;
26+
27+
public class LibvirtStoragePoolTest extends TestCase {
28+
29+
public void testAttributes() {
30+
String uuid = "4c4fb08b-373e-4f30-a120-3aa3a43f31da";
31+
String name = "myfirstpool";
32+
33+
StoragePoolType type = StoragePoolType.NetworkFilesystem;
34+
35+
StorageAdaptor adapter = Mockito.mock(LibvirtStorageAdaptor.class);
36+
StoragePool storage = Mockito.mock(StoragePool.class);
37+
38+
LibvirtStoragePool pool = new LibvirtStoragePool(uuid, name, type, adapter, storage);
39+
assertEquals(pool.getCapacity(), 0);
40+
assertEquals(pool.getUsed(), 0);
41+
assertEquals(pool.getName(), name);
42+
assertEquals(pool.getUuid(), uuid);
43+
assertEquals(pool.getAvailable(), 0);
44+
assertEquals(pool.getStoragePoolType(), type);
45+
46+
pool.setCapacity(2048);
47+
pool.setUsed(1024);
48+
pool.setAvailable(1023);
49+
50+
assertEquals(pool.getCapacity(), 2048);
51+
assertEquals(pool.getUsed(), 1024);
52+
assertEquals(pool.getAvailable(), 1023);
53+
}
54+
55+
public void testDefaultFormats() {
56+
String uuid = "f40cbf53-1f37-4c62-8912-801edf398f47";
57+
String name = "myfirstpool";
58+
59+
StorageAdaptor adapter = Mockito.mock(LibvirtStorageAdaptor.class);
60+
StoragePool storage = Mockito.mock(StoragePool.class);
61+
62+
LibvirtStoragePool nfsPool = new LibvirtStoragePool(uuid, name, StoragePoolType.NetworkFilesystem, adapter, storage);
63+
assertEquals(nfsPool.getDefaultFormat(), PhysicalDiskFormat.QCOW2);
64+
assertEquals(nfsPool.getStoragePoolType(), StoragePoolType.NetworkFilesystem);
65+
66+
LibvirtStoragePool rbdPool = new LibvirtStoragePool(uuid, name, StoragePoolType.RBD, adapter, storage);
67+
assertEquals(rbdPool.getDefaultFormat(), PhysicalDiskFormat.RAW);
68+
assertEquals(rbdPool.getStoragePoolType(), StoragePoolType.RBD);
69+
70+
LibvirtStoragePool clvmPool = new LibvirtStoragePool(uuid, name, StoragePoolType.CLVM, adapter, storage);
71+
assertEquals(clvmPool.getDefaultFormat(), PhysicalDiskFormat.RAW);
72+
assertEquals(clvmPool.getStoragePoolType(), StoragePoolType.CLVM);
73+
}
74+
75+
public void testExternalSnapshot() {
76+
String uuid = "60b46738-c5d0-40a9-a79e-9a4fe6295db7";
77+
String name = "myfirstpool";
78+
79+
StorageAdaptor adapter = Mockito.mock(LibvirtStorageAdaptor.class);
80+
StoragePool storage = Mockito.mock(StoragePool.class);
81+
82+
LibvirtStoragePool nfsPool = new LibvirtStoragePool(uuid, name, StoragePoolType.NetworkFilesystem, adapter, storage);
83+
assertFalse(nfsPool.isExternalSnapshot());
84+
85+
LibvirtStoragePool rbdPool = new LibvirtStoragePool(uuid, name, StoragePoolType.RBD, adapter, storage);
86+
assertTrue(rbdPool.isExternalSnapshot());
87+
88+
LibvirtStoragePool clvmPool = new LibvirtStoragePool(uuid, name, StoragePoolType.CLVM, adapter, storage);
89+
assertTrue(clvmPool.isExternalSnapshot());
90+
}
91+
}

0 commit comments

Comments
 (0)