Skip to content

Commit 4ecc286

Browse files
committed
Address review comments
1 parent 150d800 commit 4ecc286

2 files changed

Lines changed: 112 additions & 11 deletions

File tree

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
3939
import org.joda.time.Duration;
4040
import org.libvirt.Connect;
41+
import org.libvirt.Error;
4142
import org.libvirt.LibvirtException;
4243
import org.libvirt.StoragePool;
4344
import org.libvirt.StorageVol;
@@ -106,25 +107,37 @@ public boolean deleteStoragePool(String uuid) {
106107
}
107108

108109
private boolean undefineInactiveClvmPool(Connect conn, String uuid) throws LibvirtException {
109-
StoragePool sp;
110-
try {
111-
sp = conn.storagePoolLookupByUUIDString(uuid);
112-
} catch (LibvirtException e) {
110+
StoragePool sp = lookupClvmPool(conn, uuid);
111+
if (sp == null) {
113112
logger.warn("CLVM/CLVM_NG storage pool {} doesn't exist in libvirt. Assuming it is already removed", uuid);
114113
return true;
115114
}
116115

117-
if (sp.isActive() == 1) {
118-
sp.destroy();
119-
}
120-
if (sp.isPersistent() == 1) {
121-
sp.undefine();
116+
try {
117+
if (sp.isActive() == 1) {
118+
sp.destroy();
119+
}
120+
if (sp.isPersistent() == 1) {
121+
sp.undefine();
122+
}
123+
} finally {
124+
sp.free();
122125
}
123-
sp.free();
124126
logger.info("CLVM/CLVM_NG storage pool {} was successfully removed from libvirt", uuid);
125127
return true;
126128
}
127129

130+
private StoragePool lookupClvmPool(Connect conn, String uuid) throws LibvirtException {
131+
try {
132+
return conn.storagePoolLookupByUUIDString(uuid);
133+
} catch (LibvirtException e) {
134+
if (e.getError() != null && e.getError().getCode() == Error.ErrorNumber.VIR_ERR_NO_STORAGE_POOL) {
135+
return null;
136+
}
137+
throw e;
138+
}
139+
}
140+
128141
@Override
129142
public KVMStoragePool getStoragePool(String uuid, boolean refreshInfo) {
130143
logger.info("Fetching CLVM/CLVM_NG storage pool {} ", uuid);

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.junit.Test;
4141
import org.junit.runner.RunWith;
4242
import org.libvirt.Connect;
43+
import org.libvirt.Error;
44+
import org.libvirt.LibvirtException;
4345
import org.libvirt.StoragePool;
4446
import org.mockito.Mock;
4547
import org.mockito.MockedConstruction;
@@ -107,10 +109,96 @@ public void testCreateCLVMStoragePool_Success() throws Exception {
107109
clvmStorageAdaptor, mockConn, uuid, host, vgName);
108110

109111
assertNotNull("Storage pool should be created", result);
110-
Mockito.verify(mockStoragePool).setAutostart(1);
112+
Mockito.verify(mockStoragePool, never()).setAutostart(anyInt());
111113
Mockito.verify(mockConn).storagePoolDefineXML(anyString(), eq(0));
112114
}
113115

116+
private Connect mockLibvirtConnection() throws Exception {
117+
Connect mockConn = Mockito.mock(Connect.class);
118+
libvirtConnectionMockedStatic.when(LibvirtConnection::getConnection).thenReturn(mockConn);
119+
return mockConn;
120+
}
121+
122+
private LibvirtException mockLibvirtException(Error.ErrorNumber errorNumber) {
123+
Error error = Mockito.mock(Error.class);
124+
when(error.getCode()).thenReturn(errorNumber);
125+
LibvirtException exception = Mockito.mock(LibvirtException.class);
126+
when(exception.getError()).thenReturn(error);
127+
return exception;
128+
}
129+
130+
@Test
131+
public void testDeleteStoragePool_InactivePersistentPoolIsUndefinedWithoutDestroy() throws Exception {
132+
String uuid = UUID.randomUUID().toString();
133+
Connect mockConn = mockLibvirtConnection();
134+
StoragePool mockStoragePool = Mockito.mock(StoragePool.class);
135+
when(mockConn.storagePoolLookupByUUIDString(uuid)).thenReturn(mockStoragePool);
136+
when(mockStoragePool.isActive()).thenReturn(0);
137+
when(mockStoragePool.isPersistent()).thenReturn(1);
138+
139+
assertTrue(clvmStorageAdaptor.deleteStoragePool(uuid));
140+
141+
Mockito.verify(mockStoragePool, never()).destroy();
142+
Mockito.verify(mockStoragePool).undefine();
143+
Mockito.verify(mockStoragePool).free();
144+
}
145+
146+
@Test
147+
public void testDeleteStoragePool_ActivePoolIsDestroyedThenUndefined() throws Exception {
148+
String uuid = UUID.randomUUID().toString();
149+
Connect mockConn = mockLibvirtConnection();
150+
StoragePool mockStoragePool = Mockito.mock(StoragePool.class);
151+
when(mockConn.storagePoolLookupByUUIDString(uuid)).thenReturn(mockStoragePool);
152+
when(mockStoragePool.isActive()).thenReturn(1);
153+
when(mockStoragePool.isPersistent()).thenReturn(1);
154+
155+
assertTrue(clvmStorageAdaptor.deleteStoragePool(uuid));
156+
157+
org.mockito.InOrder inOrder = Mockito.inOrder(mockStoragePool);
158+
inOrder.verify(mockStoragePool).destroy();
159+
inOrder.verify(mockStoragePool).undefine();
160+
inOrder.verify(mockStoragePool).free();
161+
}
162+
163+
@Test
164+
public void testDeleteStoragePool_MissingPoolIsTreatedAsRemoved() throws Exception {
165+
String uuid = UUID.randomUUID().toString();
166+
Connect mockConn = mockLibvirtConnection();
167+
LibvirtException lookupError = mockLibvirtException(Error.ErrorNumber.VIR_ERR_NO_STORAGE_POOL);
168+
when(mockConn.storagePoolLookupByUUIDString(uuid)).thenThrow(lookupError);
169+
170+
assertTrue(clvmStorageAdaptor.deleteStoragePool(uuid));
171+
}
172+
173+
@Test(expected = CloudRuntimeException.class)
174+
public void testDeleteStoragePool_OtherLookupErrorIsNotSwallowed() throws Exception {
175+
String uuid = UUID.randomUUID().toString();
176+
Connect mockConn = mockLibvirtConnection();
177+
LibvirtException lookupError = mockLibvirtException(Error.ErrorNumber.VIR_ERR_INTERNAL_ERROR);
178+
when(mockConn.storagePoolLookupByUUIDString(uuid)).thenThrow(lookupError);
179+
180+
clvmStorageAdaptor.deleteStoragePool(uuid);
181+
}
182+
183+
@Test
184+
public void testDeleteStoragePool_PoolIsFreedWhenUndefineFails() throws Exception {
185+
String uuid = UUID.randomUUID().toString();
186+
Connect mockConn = mockLibvirtConnection();
187+
StoragePool mockStoragePool = Mockito.mock(StoragePool.class);
188+
when(mockConn.storagePoolLookupByUUIDString(uuid)).thenReturn(mockStoragePool);
189+
when(mockStoragePool.isActive()).thenReturn(0);
190+
when(mockStoragePool.isPersistent()).thenReturn(1);
191+
LibvirtException undefineError = mockLibvirtException(Error.ErrorNumber.VIR_ERR_INTERNAL_ERROR);
192+
Mockito.doThrow(undefineError).when(mockStoragePool).undefine();
193+
194+
try {
195+
clvmStorageAdaptor.deleteStoragePool(uuid);
196+
org.junit.Assert.fail("Expected CloudRuntimeException");
197+
} catch (CloudRuntimeException expected) {
198+
Mockito.verify(mockStoragePool).free();
199+
}
200+
}
201+
114202
@Test
115203
public void testCreateCLVMStoragePool_VGNotFound() throws Exception {
116204
String uuid = UUID.randomUUID().toString();

0 commit comments

Comments
 (0)