|
40 | 40 | import org.junit.Test; |
41 | 41 | import org.junit.runner.RunWith; |
42 | 42 | import org.libvirt.Connect; |
| 43 | +import org.libvirt.Error; |
| 44 | +import org.libvirt.LibvirtException; |
43 | 45 | import org.libvirt.StoragePool; |
44 | 46 | import org.mockito.Mock; |
45 | 47 | import org.mockito.MockedConstruction; |
@@ -107,10 +109,96 @@ public void testCreateCLVMStoragePool_Success() throws Exception { |
107 | 109 | clvmStorageAdaptor, mockConn, uuid, host, vgName); |
108 | 110 |
|
109 | 111 | assertNotNull("Storage pool should be created", result); |
110 | | - Mockito.verify(mockStoragePool).setAutostart(1); |
| 112 | + Mockito.verify(mockStoragePool, never()).setAutostart(anyInt()); |
111 | 113 | Mockito.verify(mockConn).storagePoolDefineXML(anyString(), eq(0)); |
112 | 114 | } |
113 | 115 |
|
| 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 | + |
114 | 202 | @Test |
115 | 203 | public void testCreateCLVMStoragePool_VGNotFound() throws Exception { |
116 | 204 | String uuid = UUID.randomUUID().toString(); |
|
0 commit comments