|
54 | 54 | import static org.mockito.Mockito.verify; |
55 | 55 | import static org.mockito.Mockito.times; |
56 | 56 | import static org.mockito.Mockito.withSettings; |
| 57 | +import static org.mockito.Mockito.mock; |
| 58 | +import static org.mockito.Mockito.never; |
| 59 | +import static org.mockito.ArgumentMatchers.contains; |
57 | 60 | import static org.junit.jupiter.api.Assertions.assertThrows; |
58 | 61 | import static org.junit.jupiter.api.Assertions.assertTrue; |
59 | 62 | import static org.junit.jupiter.api.Assertions.assertFalse; |
60 | 63 | import java.util.HashMap; |
| 64 | +import com.cloud.storage.StoragePool; |
| 65 | +import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle; |
61 | 66 | import org.apache.cloudstack.storage.provider.StorageProviderFactory; |
62 | 67 | import org.apache.cloudstack.storage.service.StorageStrategy; |
63 | 68 | import org.apache.cloudstack.storage.volume.datastore.PrimaryDataStoreHelper; |
@@ -854,4 +859,104 @@ public void testAttachZone_kvmHypervisorSetsAndUpdatesPool() throws Exception { |
854 | 859 | } |
855 | 860 | } |
856 | 861 |
|
| 862 | + // ========== updateStoragePool() Tests ========== |
| 863 | + |
| 864 | + @Test |
| 865 | + public void testUpdateStoragePool_positive_resizesFlexVolume() { |
| 866 | + // Setup |
| 867 | + StoragePool storagePool = mock(StoragePool.class); |
| 868 | + when(storagePool.getName()).thenReturn("test-pool"); |
| 869 | + when(storagePool.getCapacityBytes()).thenReturn(2147483648L); // 2 GB current |
| 870 | + |
| 871 | + Map<String, String> details = new HashMap<>(); |
| 872 | + details.put(PrimaryDataStoreLifeCycle.CAPACITY_BYTES, String.valueOf(5368709120L)); // 5 GB new |
| 873 | + details.put(OntapStorageConstants.VOLUME_UUID, "flex-vol-uuid-123"); |
| 874 | + details.put(OntapStorageConstants.VOLUME_NAME, "flexvol-name"); |
| 875 | + details.put("protocol", "NFS3"); |
| 876 | + |
| 877 | + Volume updatedVolume = new Volume(); |
| 878 | + updatedVolume.setUuid("flex-vol-uuid-123"); |
| 879 | + updatedVolume.setSize(5368709120L); |
| 880 | + when(storageStrategy.updateStorageVolume(any(Volume.class))).thenReturn(updatedVolume); |
| 881 | + |
| 882 | + try (MockedStatic<OntapStorageUtils> utilityMock = Mockito.mockStatic(OntapStorageUtils.class)) { |
| 883 | + utilityMock.when(() -> OntapStorageUtils.getStrategyByStoragePoolDetails(any())) |
| 884 | + .thenReturn(storageStrategy); |
| 885 | + |
| 886 | + // Execute |
| 887 | + ontapPrimaryDatastoreLifecycle.updateStoragePool(storagePool, details); |
| 888 | + |
| 889 | + // Verify |
| 890 | + verify(storageStrategy, times(1)).updateStorageVolume(any(Volume.class)); |
| 891 | + } |
| 892 | + } |
| 893 | + |
| 894 | + @Test |
| 895 | + public void testUpdateStoragePool_noCapacityBytesInDetails_skipsResize() { |
| 896 | + // Setup |
| 897 | + StoragePool storagePool = mock(StoragePool.class); |
| 898 | + when(storagePool.getName()).thenReturn("test-pool"); |
| 899 | + |
| 900 | + Map<String, String> details = new HashMap<>(); |
| 901 | + details.put(OntapStorageConstants.VOLUME_UUID, "flex-vol-uuid-123"); |
| 902 | + details.put("protocol", "NFS3"); |
| 903 | + // No CAPACITY_BYTES key — resize should be skipped |
| 904 | + |
| 905 | + // Execute |
| 906 | + ontapPrimaryDatastoreLifecycle.updateStoragePool(storagePool, details); |
| 907 | + |
| 908 | + // Verify — storageStrategy should never be called |
| 909 | + verify(storageStrategy, never()).updateStorageVolume(any()); |
| 910 | + } |
| 911 | + |
| 912 | + @Test |
| 913 | + public void testUpdateStoragePool_missingVolumeUuid_throwsCloudRuntimeException() { |
| 914 | + // Setup |
| 915 | + StoragePool storagePool = mock(StoragePool.class); |
| 916 | + when(storagePool.getName()).thenReturn("test-pool"); |
| 917 | + when(storagePool.getCapacityBytes()).thenReturn(1073741824L); |
| 918 | + |
| 919 | + Map<String, String> details = new HashMap<>(); |
| 920 | + details.put(PrimaryDataStoreLifeCycle.CAPACITY_BYTES, String.valueOf(3221225472L)); |
| 921 | + details.put("protocol", "NFS3"); |
| 922 | + // No VOLUME_UUID — cannot resize without it |
| 923 | + |
| 924 | + try (MockedStatic<OntapStorageUtils> utilityMock = Mockito.mockStatic(OntapStorageUtils.class)) { |
| 925 | + utilityMock.when(() -> OntapStorageUtils.getStrategyByStoragePoolDetails(any())) |
| 926 | + .thenReturn(storageStrategy); |
| 927 | + |
| 928 | + // Execute & Verify |
| 929 | + assertThrows(CloudRuntimeException.class, |
| 930 | + () -> ontapPrimaryDatastoreLifecycle.updateStoragePool(storagePool, details)); |
| 931 | + verify(storageStrategy, never()).updateStorageVolume(any()); |
| 932 | + } |
| 933 | + } |
| 934 | + |
| 935 | + @Test |
| 936 | + public void testUpdateStoragePool_updateStorageVolumeThrows_propagatesCloudRuntimeException() { |
| 937 | + // Setup |
| 938 | + StoragePool storagePool = mock(StoragePool.class); |
| 939 | + when(storagePool.getName()).thenReturn("test-pool"); |
| 940 | + when(storagePool.getCapacityBytes()).thenReturn(1073741824L); |
| 941 | + |
| 942 | + Map<String, String> details = new HashMap<>(); |
| 943 | + details.put(PrimaryDataStoreLifeCycle.CAPACITY_BYTES, String.valueOf(3221225472L)); |
| 944 | + details.put(OntapStorageConstants.VOLUME_UUID, "flex-vol-uuid-err"); |
| 945 | + details.put(OntapStorageConstants.VOLUME_NAME, "flexvol-err"); |
| 946 | + details.put("protocol", "NFS3"); |
| 947 | + |
| 948 | + when(storageStrategy.updateStorageVolume(any(Volume.class))) |
| 949 | + .thenThrow(new CloudRuntimeException("ONTAP resize failed")); |
| 950 | + |
| 951 | + try (MockedStatic<OntapStorageUtils> utilityMock = Mockito.mockStatic(OntapStorageUtils.class)) { |
| 952 | + utilityMock.when(() -> OntapStorageUtils.getStrategyByStoragePoolDetails(any())) |
| 953 | + .thenReturn(storageStrategy); |
| 954 | + |
| 955 | + // Execute & Verify |
| 956 | + assertThrows(CloudRuntimeException.class, |
| 957 | + () -> ontapPrimaryDatastoreLifecycle.updateStoragePool(storagePool, details)); |
| 958 | + verify(storageStrategy, times(1)).updateStorageVolume(any(Volume.class)); |
| 959 | + } |
| 960 | + } |
| 961 | + |
857 | 962 | } |
0 commit comments