|
| 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 | + |
| 18 | +package com.cloud.hypervisor.kvm.resource.wrapper; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertFalse; |
| 22 | +import static org.junit.Assert.assertNotNull; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | +import static org.mockito.ArgumentMatchers.anyMap; |
| 25 | +import static org.mockito.ArgumentMatchers.eq; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | + |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.mockito.ArgumentCaptor; |
| 36 | +import org.mockito.Mock; |
| 37 | +import org.mockito.junit.MockitoJUnitRunner; |
| 38 | + |
| 39 | +import com.cloud.agent.api.Answer; |
| 40 | +import com.cloud.agent.api.ModifyStoragePoolAnswer; |
| 41 | +import com.cloud.agent.api.ModifyStoragePoolCommand; |
| 42 | +import com.cloud.agent.api.to.StorageFilerTO; |
| 43 | +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; |
| 44 | +import com.cloud.hypervisor.kvm.storage.KVMStoragePool; |
| 45 | +import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; |
| 46 | +import com.cloud.storage.Storage.StoragePoolType; |
| 47 | + |
| 48 | +@RunWith(MockitoJUnitRunner.class) |
| 49 | +public class LibvirtModifyStoragePoolCommandWrapperTest { |
| 50 | + |
| 51 | + @Mock |
| 52 | + private LibvirtComputingResource libvirtComputingResource; |
| 53 | + |
| 54 | + @Mock |
| 55 | + private KVMStoragePoolManager storagePoolManager; |
| 56 | + |
| 57 | + @Mock |
| 58 | + private ModifyStoragePoolCommand command; |
| 59 | + |
| 60 | + @Mock |
| 61 | + private StorageFilerTO storageFilerTO; |
| 62 | + |
| 63 | + @Mock |
| 64 | + private KVMStoragePool storagePool; |
| 65 | + |
| 66 | + private LibvirtModifyStoragePoolCommandWrapper wrapper; |
| 67 | + |
| 68 | + @Before |
| 69 | + public void setUp() { |
| 70 | + wrapper = new LibvirtModifyStoragePoolCommandWrapper(); |
| 71 | + when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolManager); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testAddClvmStoragePoolWithoutDetails() { |
| 76 | + when(command.getAdd()).thenReturn(true); |
| 77 | + when(command.getPool()).thenReturn(storageFilerTO); |
| 78 | + when(command.getDetails()).thenReturn(null); |
| 79 | + |
| 80 | + when(storageFilerTO.getUuid()).thenReturn("pool-uuid"); |
| 81 | + when(storageFilerTO.getHost()).thenReturn("192.168.1.100"); |
| 82 | + when(storageFilerTO.getPort()).thenReturn(0); |
| 83 | + when(storageFilerTO.getPath()).thenReturn("/vg0"); |
| 84 | + when(storageFilerTO.getUserInfo()).thenReturn(null); |
| 85 | + when(storageFilerTO.getType()).thenReturn(StoragePoolType.CLVM); |
| 86 | + |
| 87 | + when(storagePool.getCapacity()).thenReturn(1000000L); |
| 88 | + when(storagePool.getAvailable()).thenReturn(500000L); |
| 89 | + when(storagePool.getDetails()).thenReturn(new HashMap<>()); |
| 90 | + |
| 91 | + when(storagePoolManager.createStoragePool(eq("pool-uuid"), eq("192.168.1.100"), eq(0), |
| 92 | + eq("/vg0"), eq(null), eq(StoragePoolType.CLVM), anyMap())) |
| 93 | + .thenReturn(storagePool); |
| 94 | + |
| 95 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 96 | + |
| 97 | + assertTrue("Answer should indicate success. Details: " + answer.getDetails(), answer.getResult()); |
| 98 | + assertTrue("Answer should be ModifyStoragePoolAnswer", answer instanceof ModifyStoragePoolAnswer); |
| 99 | + |
| 100 | + // Verify the details were passed correctly |
| 101 | + ArgumentCaptor<Map<String, String>> detailsCaptor = ArgumentCaptor.forClass(Map.class); |
| 102 | + verify(storagePoolManager).createStoragePool(eq("pool-uuid"), eq("192.168.1.100"), eq(0), |
| 103 | + eq("/vg0"), eq(null), eq(StoragePoolType.CLVM), detailsCaptor.capture()); |
| 104 | + |
| 105 | + Map<String, String> capturedDetails = detailsCaptor.getValue(); |
| 106 | + assertNotNull("Details should not be null", capturedDetails); |
| 107 | + assertEquals("CLVM_SECURE_ZERO_FILL should default to false", |
| 108 | + "false", capturedDetails.get(KVMStoragePool.CLVM_SECURE_ZERO_FILL)); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testAddClvmNgStoragePoolWithEmptyDetails() { |
| 113 | + when(command.getAdd()).thenReturn(true); |
| 114 | + when(command.getPool()).thenReturn(storageFilerTO); |
| 115 | + when(command.getDetails()).thenReturn(new HashMap<>()); |
| 116 | + |
| 117 | + when(storageFilerTO.getUuid()).thenReturn("pool-uuid"); |
| 118 | + when(storageFilerTO.getHost()).thenReturn("192.168.1.100"); |
| 119 | + when(storageFilerTO.getPort()).thenReturn(0); |
| 120 | + when(storageFilerTO.getPath()).thenReturn("/vg0"); |
| 121 | + when(storageFilerTO.getUserInfo()).thenReturn(null); |
| 122 | + when(storageFilerTO.getType()).thenReturn(StoragePoolType.CLVM_NG); |
| 123 | + |
| 124 | + when(storagePool.getCapacity()).thenReturn(2000000L); |
| 125 | + when(storagePool.getAvailable()).thenReturn(1000000L); |
| 126 | + when(storagePool.getDetails()).thenReturn(new HashMap<>()); |
| 127 | + |
| 128 | + when(storagePoolManager.createStoragePool(eq("pool-uuid"), eq("192.168.1.100"), eq(0), |
| 129 | + eq("/vg0"), eq(null), eq(StoragePoolType.CLVM_NG), anyMap())) |
| 130 | + .thenReturn(storagePool); |
| 131 | + |
| 132 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 133 | + |
| 134 | + assertTrue("Answer should indicate success", answer.getResult()); |
| 135 | + |
| 136 | + ArgumentCaptor<Map<String, String>> detailsCaptor = ArgumentCaptor.forClass(Map.class); |
| 137 | + verify(storagePoolManager).createStoragePool(eq("pool-uuid"), eq("192.168.1.100"), eq(0), |
| 138 | + eq("/vg0"), eq(null), eq(StoragePoolType.CLVM_NG), detailsCaptor.capture()); |
| 139 | + |
| 140 | + Map<String, String> capturedDetails = detailsCaptor.getValue(); |
| 141 | + assertNotNull("Details should not be null", capturedDetails); |
| 142 | + assertEquals("CLVM_SECURE_ZERO_FILL should default to false", |
| 143 | + "false", capturedDetails.get(KVMStoragePool.CLVM_SECURE_ZERO_FILL)); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testAddClvmStoragePoolWithExistingSecureZeroFillSetting() { |
| 148 | + Map<String, String> details = new HashMap<>(); |
| 149 | + details.put(KVMStoragePool.CLVM_SECURE_ZERO_FILL, "true"); |
| 150 | + details.put("someOtherKey", "someValue"); |
| 151 | + |
| 152 | + when(command.getAdd()).thenReturn(true); |
| 153 | + when(command.getPool()).thenReturn(storageFilerTO); |
| 154 | + when(command.getDetails()).thenReturn(details); |
| 155 | + |
| 156 | + when(storageFilerTO.getUuid()).thenReturn("pool-uuid"); |
| 157 | + when(storageFilerTO.getHost()).thenReturn("192.168.1.100"); |
| 158 | + when(storageFilerTO.getPort()).thenReturn(0); |
| 159 | + when(storageFilerTO.getPath()).thenReturn("/vg0"); |
| 160 | + when(storageFilerTO.getUserInfo()).thenReturn(null); |
| 161 | + when(storageFilerTO.getType()).thenReturn(StoragePoolType.CLVM); |
| 162 | + |
| 163 | + when(storagePool.getCapacity()).thenReturn(1000000L); |
| 164 | + when(storagePool.getAvailable()).thenReturn(500000L); |
| 165 | + when(storagePool.getDetails()).thenReturn(new HashMap<>()); |
| 166 | + |
| 167 | + when(storagePoolManager.createStoragePool(eq("pool-uuid"), eq("192.168.1.100"), eq(0), |
| 168 | + eq("/vg0"), eq(null), eq(StoragePoolType.CLVM), anyMap())) |
| 169 | + .thenReturn(storagePool); |
| 170 | + |
| 171 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 172 | + |
| 173 | + assertTrue("Answer should indicate success", answer.getResult()); |
| 174 | + |
| 175 | + ArgumentCaptor<Map<String, String>> detailsCaptor = ArgumentCaptor.forClass(Map.class); |
| 176 | + verify(storagePoolManager).createStoragePool(eq("pool-uuid"), eq("192.168.1.100"), eq(0), |
| 177 | + eq("/vg0"), eq(null), eq(StoragePoolType.CLVM), detailsCaptor.capture()); |
| 178 | + |
| 179 | + Map<String, String> capturedDetails = detailsCaptor.getValue(); |
| 180 | + assertNotNull("Details should not be null", capturedDetails); |
| 181 | + assertEquals("CLVM_SECURE_ZERO_FILL should preserve existing value", |
| 182 | + "true", capturedDetails.get(KVMStoragePool.CLVM_SECURE_ZERO_FILL)); |
| 183 | + assertEquals("Other details should be preserved", |
| 184 | + "someValue", capturedDetails.get("someOtherKey")); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testAddClvmStoragePoolPreservesOtherDetailsWhenAddingDefault() { |
| 189 | + Map<String, String> details = new HashMap<>(); |
| 190 | + details.put("customKey1", "value1"); |
| 191 | + details.put("customKey2", "value2"); |
| 192 | + |
| 193 | + when(command.getAdd()).thenReturn(true); |
| 194 | + when(command.getPool()).thenReturn(storageFilerTO); |
| 195 | + when(command.getDetails()).thenReturn(details); |
| 196 | + |
| 197 | + when(storageFilerTO.getUuid()).thenReturn("pool-uuid"); |
| 198 | + when(storageFilerTO.getHost()).thenReturn("192.168.1.100"); |
| 199 | + when(storageFilerTO.getPort()).thenReturn(0); |
| 200 | + when(storageFilerTO.getPath()).thenReturn("/vg0"); |
| 201 | + when(storageFilerTO.getUserInfo()).thenReturn(null); |
| 202 | + when(storageFilerTO.getType()).thenReturn(StoragePoolType.CLVM_NG); |
| 203 | + |
| 204 | + when(storagePool.getCapacity()).thenReturn(1000000L); |
| 205 | + when(storagePool.getAvailable()).thenReturn(500000L); |
| 206 | + when(storagePool.getDetails()).thenReturn(new HashMap<>()); |
| 207 | + |
| 208 | + when(storagePoolManager.createStoragePool(eq("pool-uuid"), eq("192.168.1.100"), eq(0), |
| 209 | + eq("/vg0"), eq(null), eq(StoragePoolType.CLVM_NG), anyMap())) |
| 210 | + .thenReturn(storagePool); |
| 211 | + |
| 212 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 213 | + |
| 214 | + assertTrue("Answer should indicate success", answer.getResult()); |
| 215 | + |
| 216 | + ArgumentCaptor<Map<String, String>> detailsCaptor = ArgumentCaptor.forClass(Map.class); |
| 217 | + verify(storagePoolManager).createStoragePool(eq("pool-uuid"), eq("192.168.1.100"), eq(0), |
| 218 | + eq("/vg0"), eq(null), eq(StoragePoolType.CLVM_NG), detailsCaptor.capture()); |
| 219 | + |
| 220 | + Map<String, String> capturedDetails = detailsCaptor.getValue(); |
| 221 | + assertNotNull("Details should not be null", capturedDetails); |
| 222 | + assertEquals("CLVM_SECURE_ZERO_FILL should default to false", |
| 223 | + "false", capturedDetails.get(KVMStoragePool.CLVM_SECURE_ZERO_FILL)); |
| 224 | + assertEquals("Custom details should be preserved", |
| 225 | + "value1", capturedDetails.get("customKey1")); |
| 226 | + assertEquals("Custom details should be preserved", |
| 227 | + "value2", capturedDetails.get("customKey2")); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void testDeleteClvmStoragePoolSuccess() { |
| 232 | + when(command.getAdd()).thenReturn(false); |
| 233 | + when(command.getPool()).thenReturn(storageFilerTO); |
| 234 | + when(command.getDetails()).thenReturn(null); |
| 235 | + |
| 236 | + when(storageFilerTO.getUuid()).thenReturn("pool-uuid"); |
| 237 | + when(storageFilerTO.getType()).thenReturn(StoragePoolType.CLVM); |
| 238 | + |
| 239 | + when(storagePoolManager.deleteStoragePool(StoragePoolType.CLVM, "pool-uuid", null)) |
| 240 | + .thenReturn(true); |
| 241 | + |
| 242 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 243 | + |
| 244 | + assertTrue("Answer should indicate success", answer.getResult()); |
| 245 | + verify(storagePoolManager).deleteStoragePool(StoragePoolType.CLVM, "pool-uuid", null); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void testDeleteClvmNgStoragePoolSuccess() { |
| 250 | + when(command.getAdd()).thenReturn(false); |
| 251 | + when(command.getPool()).thenReturn(storageFilerTO); |
| 252 | + when(command.getDetails()).thenReturn(null); |
| 253 | + |
| 254 | + when(storageFilerTO.getUuid()).thenReturn("pool-uuid"); |
| 255 | + when(storageFilerTO.getType()).thenReturn(StoragePoolType.CLVM_NG); |
| 256 | + |
| 257 | + when(storagePoolManager.deleteStoragePool(StoragePoolType.CLVM_NG, "pool-uuid", null)) |
| 258 | + .thenReturn(true); |
| 259 | + |
| 260 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 261 | + |
| 262 | + assertTrue("Answer should indicate success", answer.getResult()); |
| 263 | + verify(storagePoolManager).deleteStoragePool(StoragePoolType.CLVM_NG, "pool-uuid", null); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void testDeleteClvmStoragePoolFailure() { |
| 268 | + when(command.getAdd()).thenReturn(false); |
| 269 | + when(command.getPool()).thenReturn(storageFilerTO); |
| 270 | + when(command.getDetails()).thenReturn(null); |
| 271 | + |
| 272 | + when(storageFilerTO.getUuid()).thenReturn("pool-uuid"); |
| 273 | + when(storageFilerTO.getType()).thenReturn(StoragePoolType.CLVM); |
| 274 | + |
| 275 | + when(storagePoolManager.deleteStoragePool(StoragePoolType.CLVM, "pool-uuid", null)) |
| 276 | + .thenReturn(false); |
| 277 | + |
| 278 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 279 | + |
| 280 | + assertFalse("Answer should indicate failure", answer.getResult()); |
| 281 | + assertEquals("Failed to delete storage pool", answer.getDetails()); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + public void testAddClvmStoragePoolCreationFailure() { |
| 286 | + when(command.getAdd()).thenReturn(true); |
| 287 | + when(command.getPool()).thenReturn(storageFilerTO); |
| 288 | + when(command.getDetails()).thenReturn(null); |
| 289 | + |
| 290 | + when(storageFilerTO.getUuid()).thenReturn("pool-uuid"); |
| 291 | + when(storageFilerTO.getHost()).thenReturn("192.168.1.100"); |
| 292 | + when(storageFilerTO.getPort()).thenReturn(0); |
| 293 | + when(storageFilerTO.getPath()).thenReturn("/vg0"); |
| 294 | + when(storageFilerTO.getUserInfo()).thenReturn(null); |
| 295 | + when(storageFilerTO.getType()).thenReturn(StoragePoolType.CLVM); |
| 296 | + |
| 297 | + when(storagePoolManager.createStoragePool(eq("pool-uuid"), eq("192.168.1.100"), eq(0), |
| 298 | + eq("/vg0"), eq(null), eq(StoragePoolType.CLVM), anyMap())) |
| 299 | + .thenReturn(null); |
| 300 | + |
| 301 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 302 | + |
| 303 | + assertFalse("Answer should indicate failure", answer.getResult()); |
| 304 | + assertEquals(" Failed to create storage pool", answer.getDetails()); |
| 305 | + } |
| 306 | + |
| 307 | + @Test |
| 308 | + public void testAddNfsStoragePoolDoesNotSetClvmSecureZeroFill() { |
| 309 | + when(command.getAdd()).thenReturn(true); |
| 310 | + when(command.getPool()).thenReturn(storageFilerTO); |
| 311 | + when(command.getDetails()).thenReturn(null); |
| 312 | + |
| 313 | + when(storageFilerTO.getUuid()).thenReturn("pool-uuid"); |
| 314 | + when(storageFilerTO.getHost()).thenReturn("nfs.server.com"); |
| 315 | + when(storageFilerTO.getPort()).thenReturn(0); |
| 316 | + when(storageFilerTO.getPath()).thenReturn("/export/nfs"); |
| 317 | + when(storageFilerTO.getUserInfo()).thenReturn(null); |
| 318 | + when(storageFilerTO.getType()).thenReturn(StoragePoolType.NetworkFilesystem); |
| 319 | + |
| 320 | + when(storagePool.getCapacity()).thenReturn(1000000L); |
| 321 | + when(storagePool.getAvailable()).thenReturn(500000L); |
| 322 | + when(storagePool.getDetails()).thenReturn(new HashMap<>()); |
| 323 | + |
| 324 | + when(storagePoolManager.createStoragePool(eq("pool-uuid"), eq("nfs.server.com"), eq(0), |
| 325 | + eq("/export/nfs"), eq(null), eq(StoragePoolType.NetworkFilesystem), anyMap())) |
| 326 | + .thenReturn(storagePool); |
| 327 | + |
| 328 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 329 | + |
| 330 | + assertTrue("Answer should indicate success", answer.getResult()); |
| 331 | + |
| 332 | + ArgumentCaptor<Map<String, String>> detailsCaptor = ArgumentCaptor.forClass(Map.class); |
| 333 | + verify(storagePoolManager).createStoragePool(eq("pool-uuid"), eq("nfs.server.com"), eq(0), |
| 334 | + eq("/export/nfs"), eq(null), eq(StoragePoolType.NetworkFilesystem), detailsCaptor.capture()); |
| 335 | + |
| 336 | + Map<String, String> capturedDetails = detailsCaptor.getValue(); |
| 337 | + assertNotNull("Details should not be null", capturedDetails); |
| 338 | + assertEquals("CLVM_SECURE_ZERO_FILL gets added for all pools", |
| 339 | + "false", capturedDetails.get(KVMStoragePool.CLVM_SECURE_ZERO_FILL)); |
| 340 | + } |
| 341 | +} |
0 commit comments