|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.cloudstack.storage.lifecycle; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.mockito.InjectMocks; |
| 25 | +import org.mockito.Mock; |
| 26 | +import org.mockito.MockedStatic; |
| 27 | +import org.mockito.Mockito; |
| 28 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 29 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 30 | +import org.mockito.quality.Strictness; |
| 31 | +import org.apache.cloudstack.storage.feign.model.Volume; |
| 32 | +import com.cloud.dc.dao.ClusterDao; |
| 33 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 34 | +import com.cloud.dc.ClusterVO; |
| 35 | +import java.util.Map; |
| 36 | +import static org.mockito.ArgumentMatchers.any; |
| 37 | +import static org.mockito.Mockito.when; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 40 | +import java.util.HashMap; |
| 41 | +import org.apache.cloudstack.storage.provider.StorageProviderFactory; |
| 42 | +import org.apache.cloudstack.storage.service.StorageStrategy; |
| 43 | +import org.apache.cloudstack.storage.volume.datastore.PrimaryDataStoreHelper; |
| 44 | + |
| 45 | + |
| 46 | +@ExtendWith(MockitoExtension.class) |
| 47 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 48 | +public class OntapPrimaryDatastoreLifecycleTest { |
| 49 | + @InjectMocks |
| 50 | + private OntapPrimaryDatastoreLifecycle ontapPrimaryDatastoreLifecycle; |
| 51 | + |
| 52 | + @Mock |
| 53 | + private ClusterDao _clusterDao; |
| 54 | + |
| 55 | + @Mock |
| 56 | + private StorageStrategy storageStrategy; |
| 57 | + |
| 58 | + @Mock |
| 59 | + private PrimaryDataStoreHelper _dataStoreHelper; |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setUp() { |
| 63 | + |
| 64 | + ClusterVO clusterVO = new ClusterVO(1L, 1L, "clusterName"); |
| 65 | + clusterVO.setHypervisorType("KVM"); |
| 66 | + when(_clusterDao.findById(1L)).thenReturn(clusterVO); |
| 67 | + |
| 68 | + when(storageStrategy.connect()).thenReturn(true); |
| 69 | + when(storageStrategy.getNetworkInterface()).thenReturn("testNetworkInterface"); |
| 70 | + |
| 71 | + Volume volume = new Volume(); |
| 72 | + volume.setUuid("test-volume-uuid"); |
| 73 | + volume.setName("testVolume"); |
| 74 | + when(storageStrategy.createStorageVolume(any(), any())).thenReturn(volume); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testInitialize_positive() { |
| 80 | + |
| 81 | + Map<String, Object> dsInfos = new HashMap<>(); |
| 82 | + dsInfos.put("username", "testUser"); |
| 83 | + dsInfos.put("password", "testPassword"); |
| 84 | + dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false"); |
| 85 | + dsInfos.put("zoneId",1L); |
| 86 | + dsInfos.put("podId",1L); |
| 87 | + dsInfos.put("clusterId", 1L); |
| 88 | + dsInfos.put("name", "testStoragePool"); |
| 89 | + dsInfos.put("providerName", "testProvider"); |
| 90 | + dsInfos.put("capacityBytes",200000L); |
| 91 | + dsInfos.put("managed",true); |
| 92 | + dsInfos.put("tags", "testTag"); |
| 93 | + dsInfos.put("isTagARule", false); |
| 94 | + dsInfos.put("details", new HashMap<String, String>()); |
| 95 | + |
| 96 | + try(MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) { |
| 97 | + storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy); |
| 98 | + ontapPrimaryDatastoreLifecycle.initialize(dsInfos); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testInitialize_null_Arg() { |
| 104 | + Exception ex = assertThrows(CloudRuntimeException.class,() -> |
| 105 | + ontapPrimaryDatastoreLifecycle.initialize(null)); |
| 106 | + assertTrue(ex.getMessage().contains("Datastore info map is null, cannot create primary storage")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testInitialize_missingRequiredDetailKey() { |
| 111 | + Map<String, Object> dsInfos = new HashMap<>(); |
| 112 | + dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1"); |
| 113 | + dsInfos.put("zoneId",1L); |
| 114 | + dsInfos.put("podId",1L); |
| 115 | + dsInfos.put("clusterId", 1L); |
| 116 | + dsInfos.put("name", "testStoragePool"); |
| 117 | + dsInfos.put("providerName", "testProvider"); |
| 118 | + dsInfos.put("capacityBytes",200000L); |
| 119 | + dsInfos.put("managed",true); |
| 120 | + dsInfos.put("tags", "testTag"); |
| 121 | + dsInfos.put("isTagARule", false); |
| 122 | + dsInfos.put("details", new HashMap<String, String>()); |
| 123 | + |
| 124 | + try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) { |
| 125 | + storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy); |
| 126 | + Exception ex = assertThrows(CloudRuntimeException.class, () -> ontapPrimaryDatastoreLifecycle.initialize(dsInfos)); |
| 127 | + assertTrue(ex.getMessage().contains("missing detail")); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testInitialize_invalidCapacityBytes() { |
| 133 | + Map<String, Object> dsInfos = new HashMap<>(); |
| 134 | + dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false"); |
| 135 | + dsInfos.put("zoneId",1L); |
| 136 | + dsInfos.put("podId",1L); |
| 137 | + dsInfos.put("clusterId", 1L); |
| 138 | + dsInfos.put("name", "testStoragePool"); |
| 139 | + dsInfos.put("providerName", "testProvider"); |
| 140 | + dsInfos.put("capacityBytes",-1L); |
| 141 | + dsInfos.put("managed",true); |
| 142 | + dsInfos.put("tags", "testTag"); |
| 143 | + dsInfos.put("isTagARule", false); |
| 144 | + dsInfos.put("details", new HashMap<String, String>()); |
| 145 | + |
| 146 | + try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) { |
| 147 | + storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy); |
| 148 | + ontapPrimaryDatastoreLifecycle.initialize(dsInfos); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testInitialize_unmanagedStorage() { |
| 154 | + Map<String, Object> dsInfos = new HashMap<>(); |
| 155 | + dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false"); |
| 156 | + dsInfos.put("zoneId",1L); |
| 157 | + dsInfos.put("podId",1L); |
| 158 | + dsInfos.put("clusterId", 1L); |
| 159 | + dsInfos.put("name", "testStoragePool"); |
| 160 | + dsInfos.put("providerName", "testProvider"); |
| 161 | + dsInfos.put("capacityBytes",200000L); |
| 162 | + dsInfos.put("managed",false); |
| 163 | + dsInfos.put("tags", "testTag"); |
| 164 | + dsInfos.put("isTagARule", false); |
| 165 | + dsInfos.put("details", new HashMap<String, String>()); |
| 166 | + |
| 167 | + Exception ex = assertThrows(CloudRuntimeException.class, () -> { |
| 168 | + try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) { |
| 169 | + storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy); |
| 170 | + ontapPrimaryDatastoreLifecycle.initialize(dsInfos); |
| 171 | + } |
| 172 | + }); |
| 173 | + assertTrue(ex.getMessage().contains("must be managed")); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void testInitialize_nullStoragePoolName() { |
| 178 | + Map<String, Object> dsInfos = new HashMap<>(); |
| 179 | + dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false"); |
| 180 | + dsInfos.put("zoneId",1L); |
| 181 | + dsInfos.put("podId",1L); |
| 182 | + dsInfos.put("clusterId", 1L); |
| 183 | + dsInfos.put("name", null); |
| 184 | + dsInfos.put("providerName", "testProvider"); |
| 185 | + dsInfos.put("capacityBytes",200000L); |
| 186 | + dsInfos.put("managed",true); |
| 187 | + dsInfos.put("tags", "testTag"); |
| 188 | + dsInfos.put("isTagARule", false); |
| 189 | + dsInfos.put("details", new HashMap<String, String>()); |
| 190 | + |
| 191 | + Exception ex = assertThrows(CloudRuntimeException.class, () -> { |
| 192 | + try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) { |
| 193 | + storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy); |
| 194 | + ontapPrimaryDatastoreLifecycle.initialize(dsInfos); |
| 195 | + } |
| 196 | + }); |
| 197 | + assertTrue(ex.getMessage().contains("Storage pool name is null or empty")); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + public void testInitialize_nullProviderName() { |
| 202 | + Map<String, Object> dsInfos = new HashMap<>(); |
| 203 | + dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false"); |
| 204 | + dsInfos.put("zoneId",1L); |
| 205 | + dsInfos.put("podId",1L); |
| 206 | + dsInfos.put("clusterId", 1L); |
| 207 | + dsInfos.put("name", "testStoragePool"); |
| 208 | + dsInfos.put("providerName", null); |
| 209 | + dsInfos.put("capacityBytes",200000L); |
| 210 | + dsInfos.put("managed",true); |
| 211 | + dsInfos.put("tags", "testTag"); |
| 212 | + dsInfos.put("isTagARule", false); |
| 213 | + dsInfos.put("details", new HashMap<String, String>()); |
| 214 | + |
| 215 | + Exception ex = assertThrows(CloudRuntimeException.class, () -> { |
| 216 | + try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) { |
| 217 | + storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy); |
| 218 | + ontapPrimaryDatastoreLifecycle.initialize(dsInfos); |
| 219 | + } |
| 220 | + }); |
| 221 | + assertTrue(ex.getMessage().contains("Provider name is null or empty")); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + public void testInitialize_nullPodAndClusterAndZone() { |
| 226 | + Map<String, Object> dsInfos = new HashMap<>(); |
| 227 | + dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false"); |
| 228 | + dsInfos.put("zoneId",null); |
| 229 | + dsInfos.put("podId",null); |
| 230 | + dsInfos.put("clusterId", null); |
| 231 | + dsInfos.put("name", "testStoragePool"); |
| 232 | + dsInfos.put("providerName", "testProvider"); |
| 233 | + dsInfos.put("capacityBytes",200000L); |
| 234 | + dsInfos.put("managed",true); |
| 235 | + dsInfos.put("tags", "testTag"); |
| 236 | + dsInfos.put("isTagARule", false); |
| 237 | + dsInfos.put("details", new HashMap<String, String>()); |
| 238 | + |
| 239 | + Exception ex = assertThrows(CloudRuntimeException.class, () -> { |
| 240 | + try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) { |
| 241 | + storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy); |
| 242 | + ontapPrimaryDatastoreLifecycle.initialize(dsInfos); |
| 243 | + } |
| 244 | + }); |
| 245 | + assertTrue(ex.getMessage().contains("Pod Id, Cluster Id and Zone Id are all null")); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void testInitialize_clusterNotKVM() { |
| 250 | + ClusterVO clusterVO = new ClusterVO(2L, 1L, "clusterName"); |
| 251 | + clusterVO.setHypervisorType("XenServer"); |
| 252 | + when(_clusterDao.findById(2L)).thenReturn(clusterVO); |
| 253 | + |
| 254 | + Map<String, Object> dsInfos = new HashMap<>(); |
| 255 | + dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false"); |
| 256 | + dsInfos.put("zoneId",1L); |
| 257 | + dsInfos.put("podId",1L); |
| 258 | + dsInfos.put("clusterId", 2L); |
| 259 | + dsInfos.put("name", "testStoragePool"); |
| 260 | + dsInfos.put("providerName", "testProvider"); |
| 261 | + dsInfos.put("capacityBytes",200000L); |
| 262 | + dsInfos.put("managed",true); |
| 263 | + dsInfos.put("tags", "testTag"); |
| 264 | + dsInfos.put("isTagARule", false); |
| 265 | + dsInfos.put("details", new HashMap<String, String>()); |
| 266 | + |
| 267 | + Exception ex = assertThrows(CloudRuntimeException.class, () -> { |
| 268 | + try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) { |
| 269 | + storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy); |
| 270 | + ontapPrimaryDatastoreLifecycle.initialize(dsInfos); |
| 271 | + } |
| 272 | + }); |
| 273 | + assertTrue(ex.getMessage().contains("ONTAP primary storage is supported only for KVM hypervisor")); |
| 274 | + } |
| 275 | + |
| 276 | + @Test |
| 277 | + public void testInitialize_unexpectedDetailKey() { |
| 278 | + Map<String, Object> dsInfos = new HashMap<>(); |
| 279 | + dsInfos.put("url", "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false;unexpectedKey=unexpectedValue"); |
| 280 | + dsInfos.put("zoneId",1L); |
| 281 | + dsInfos.put("podId",1L); |
| 282 | + dsInfos.put("clusterId", 1L); |
| 283 | + dsInfos.put("name", "testStoragePool"); |
| 284 | + dsInfos.put("providerName", "testProvider"); |
| 285 | + dsInfos.put("capacityBytes",200000L); |
| 286 | + dsInfos.put("managed",true); |
| 287 | + dsInfos.put("tags", "testTag"); |
| 288 | + dsInfos.put("isTagARule", false); |
| 289 | + dsInfos.put("details", new HashMap<String, String>()); |
| 290 | + |
| 291 | + Exception ex = assertThrows(CloudRuntimeException.class, () -> { |
| 292 | + try (MockedStatic<StorageProviderFactory> storageProviderFactory = Mockito.mockStatic(StorageProviderFactory.class)) { |
| 293 | + storageProviderFactory.when(() -> StorageProviderFactory.getStrategy(any())).thenReturn(storageStrategy); |
| 294 | + ontapPrimaryDatastoreLifecycle.initialize(dsInfos); |
| 295 | + } |
| 296 | + }); |
| 297 | + assertTrue(ex.getMessage().contains("Unexpected ONTAP detail key in URL")); |
| 298 | + } |
| 299 | + |
| 300 | +} |
0 commit comments