|
| 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 | +package com.cloud.hypervisor.vmware.manager; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.UUID; |
| 22 | + |
| 23 | +import org.apache.cloudstack.storage.to.PrimaryDataStoreTO; |
| 24 | +import org.apache.cloudstack.storage.to.VolumeObjectTO; |
| 25 | +import org.junit.Assert; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | +import org.mockito.InjectMocks; |
| 29 | +import org.mockito.Mockito; |
| 30 | + |
| 31 | +import com.cloud.hypervisor.vmware.mo.VirtualMachineMO; |
| 32 | +import com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost; |
| 33 | +import com.cloud.hypervisor.vmware.util.VmwareClient; |
| 34 | +import com.cloud.hypervisor.vmware.util.VmwareContext; |
| 35 | +import com.cloud.storage.Storage; |
| 36 | +import com.vmware.vim25.HostDatastoreBrowserSearchResults; |
| 37 | +import com.vmware.vim25.ManagedObjectReference; |
| 38 | +import com.vmware.vim25.VimPortType; |
| 39 | +import com.vmware.vim25.VirtualDisk; |
| 40 | + |
| 41 | +public class VmwareStorageManagerImplTest { |
| 42 | + |
| 43 | + @InjectMocks |
| 44 | + private VmwareStorageManagerImpl storageManager; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void init() { |
| 48 | + VmwareStorageMount mountService = Mockito.mock(VmwareStorageMount.class); |
| 49 | + |
| 50 | + storageManager = new VmwareStorageManagerImpl(mountService); |
| 51 | + } |
| 52 | + |
| 53 | + private void testCommon(Storage.StoragePoolType poolType, Storage.StoragePoolType parentPoolType, boolean dsChange) { |
| 54 | + VirtualMachineMO vmMo = Mockito.mock(VirtualMachineMO.class); |
| 55 | + VmwareContext context = Mockito.mock(VmwareContext.class); |
| 56 | + VmwareHypervisorHost hyperHost = Mockito.mock(VmwareHypervisorHost.class); |
| 57 | + final String volumePath = "somepath"; |
| 58 | + final String uuid1 = UUID.randomUUID().toString(); |
| 59 | + final String uuid2 = UUID.randomUUID().toString(); |
| 60 | + VolumeObjectTO volumeTO = new VolumeObjectTO(); |
| 61 | + volumeTO.setPath(volumePath); |
| 62 | + PrimaryDataStoreTO primaryDataStoreTO = Mockito.mock(PrimaryDataStoreTO.class); |
| 63 | + Mockito.when(primaryDataStoreTO.getPoolType()).thenReturn(poolType); |
| 64 | + Mockito.when(primaryDataStoreTO.getParentPoolType()).thenReturn(parentPoolType); |
| 65 | + Mockito.when(primaryDataStoreTO.getUuid()).thenReturn(uuid1); |
| 66 | + volumeTO.setDataStore(primaryDataStoreTO); |
| 67 | + boolean isVolumeOnDatastoreCluster = Storage.StoragePoolType.DatastoreCluster.equals(poolType) || Storage.StoragePoolType.DatastoreCluster.equals(parentPoolType); |
| 68 | + if (isVolumeOnDatastoreCluster && dsChange) { |
| 69 | + volumeTO.setDataStoreUuid(uuid2); |
| 70 | + } |
| 71 | + VmwareClient vmwareClient = Mockito.mock(VmwareClient.class); |
| 72 | + VimPortType service = Mockito.mock(VimPortType.class); |
| 73 | + ManagedObjectReference mor = Mockito.mock(ManagedObjectReference.class); |
| 74 | + ArrayList<HostDatastoreBrowserSearchResults> arr = new ArrayList<>(); |
| 75 | + Mockito.when(context.getVimClient()).thenReturn(vmwareClient); |
| 76 | + Mockito.when(context.getService()).thenReturn(service); |
| 77 | + List<VolumeObjectTO> volumes = List.of(volumeTO); |
| 78 | + try { |
| 79 | + Mockito.when(vmMo.getVmName()).thenReturn("dummy-vm"); |
| 80 | + String key = "browser"; |
| 81 | + Mockito.when(vmwareClient.getDynamicProperty(null, key)).thenReturn(null); |
| 82 | + Mockito.when(vmwareClient.getDynamicProperty(mor, key)).thenReturn(mor); |
| 83 | + Mockito.when(vmwareClient.waitForTask(Mockito.any())).thenReturn(true); |
| 84 | + Mockito.doNothing().when(context).waitForTaskProgressDone(Mockito.any(ManagedObjectReference.class)); |
| 85 | + key = "info.result"; |
| 86 | + Mockito.when(vmwareClient.getDynamicProperty(null, key)).thenReturn(arr); |
| 87 | + Mockito.doThrow(RuntimeException.class).when(service).searchDatastoreSubFoldersTask(Mockito.eq(null), Mockito.anyString(), Mockito.any()); |
| 88 | + Mockito.when(vmMo.getAllDiskDevice()).thenReturn(new VirtualDisk[0]); |
| 89 | + if (isVolumeOnDatastoreCluster) { |
| 90 | + if (dsChange) { |
| 91 | + Mockito.when(hyperHost.findDatastore(uuid2)).thenReturn(mor); |
| 92 | + } else { |
| 93 | + Mockito.when(hyperHost.findDatastore(uuid1)).thenReturn(mor); |
| 94 | + } |
| 95 | + } else { |
| 96 | + Mockito.when(hyperHost.findDatastoreByName(volumePath)).thenReturn(mor); |
| 97 | + } |
| 98 | + storageManager.setVolumeToPathAndSize(volumes, vmMo, Mockito.mock(VmwareHostService.class), context, hyperHost); |
| 99 | + } catch (Exception e) { |
| 100 | + e.printStackTrace(); |
| 101 | + } |
| 102 | + Assert.assertEquals(0L, (long) volumes.get(0).getSize()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testSetVolumeToPathAndSizeNotDatastoreCluster() { |
| 107 | + testCommon(Storage.StoragePoolType.VMFS, null, false); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testSetVolumeToPathAndSizeDatastoreClusterSameChildStore() { |
| 112 | + testCommon(Storage.StoragePoolType.PreSetup, Storage.StoragePoolType.DatastoreCluster, false); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testSetVolumeToPathAndSizeDatastoreClusterDifferentChildStore() { |
| 117 | + testCommon(Storage.StoragePoolType.PreSetup, Storage.StoragePoolType.DatastoreCluster, true); |
| 118 | + } |
| 119 | +} |
0 commit comments