|
| 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 | + |
| 20 | +package com.cloud.hypervisor.kvm.resource.wrapper; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.cloudstack.utils.qemu.QemuImg; |
| 25 | +import org.apache.cloudstack.utils.qemu.QemuImgFile; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.MockedConstruction; |
| 32 | +import org.mockito.MockedStatic; |
| 33 | +import org.mockito.Mockito; |
| 34 | +import org.mockito.junit.MockitoJUnitRunner; |
| 35 | + |
| 36 | +import com.cloud.agent.api.Answer; |
| 37 | +import com.cloud.agent.api.CheckVolumeAnswer; |
| 38 | +import com.cloud.agent.api.CheckVolumeCommand; |
| 39 | +import com.cloud.agent.api.to.StorageFilerTO; |
| 40 | +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; |
| 41 | +import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; |
| 42 | +import com.cloud.hypervisor.kvm.storage.KVMStoragePool; |
| 43 | +import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; |
| 44 | +import com.cloud.storage.Storage; |
| 45 | + |
| 46 | +@RunWith(MockitoJUnitRunner.class) |
| 47 | +public class LibvirtCheckVolumeCommandWrapperTest { |
| 48 | + |
| 49 | + private static final String SOURCE_FILE = "disk.qcow2"; |
| 50 | + private static final String DISK_PATH = "/mnt/primary/disk.qcow2"; |
| 51 | + private static final String POOL_UUID = "pool-uuid"; |
| 52 | + private static final long DISK_SIZE = 1024L; |
| 53 | + |
| 54 | + @Mock |
| 55 | + private LibvirtComputingResource libvirtComputingResource; |
| 56 | + |
| 57 | + @Mock |
| 58 | + private KVMStoragePoolManager storagePoolMgr; |
| 59 | + |
| 60 | + @Mock |
| 61 | + private KVMStoragePool storagePool; |
| 62 | + |
| 63 | + @Mock |
| 64 | + private StorageFilerTO storageFilerTO; |
| 65 | + |
| 66 | + @Mock |
| 67 | + private KVMPhysicalDisk physicalDisk; |
| 68 | + |
| 69 | + @Mock |
| 70 | + private CheckVolumeCommand command; |
| 71 | + |
| 72 | + private final LibvirtCheckVolumeCommandWrapper wrapper = new LibvirtCheckVolumeCommandWrapper(); |
| 73 | + |
| 74 | + @Before |
| 75 | + public void setUp() { |
| 76 | + Mockito.when(command.getSrcFile()).thenReturn(SOURCE_FILE); |
| 77 | + Mockito.when(command.getStorageFilerTO()).thenReturn(storageFilerTO); |
| 78 | + Mockito.when(storageFilerTO.getUuid()).thenReturn(POOL_UUID); |
| 79 | + Mockito.when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testExecuteSupportsSharedMountPointStorage() throws Exception { |
| 84 | + Mockito.when(storageFilerTO.getType()).thenReturn(Storage.StoragePoolType.SharedMountPoint); |
| 85 | + Mockito.when(storagePoolMgr.getStoragePool(Storage.StoragePoolType.SharedMountPoint, POOL_UUID)).thenReturn(storagePool); |
| 86 | + Mockito.when(storagePool.getPhysicalDisk(SOURCE_FILE)).thenReturn(physicalDisk); |
| 87 | + Mockito.when(storagePool.getType()).thenReturn(Storage.StoragePoolType.SharedMountPoint); |
| 88 | + Mockito.when(physicalDisk.getPath()).thenReturn(DISK_PATH); |
| 89 | + Mockito.when(physicalDisk.getFormat()).thenReturn(QemuImg.PhysicalDiskFormat.QCOW2); |
| 90 | + |
| 91 | + try (MockedStatic<KVMPhysicalDisk> diskMock = Mockito.mockStatic(KVMPhysicalDisk.class); |
| 92 | + MockedConstruction<QemuImg> ignored = Mockito.mockConstruction(QemuImg.class, (mock, context) -> |
| 93 | + Mockito.when(mock.info(Mockito.any(QemuImgFile.class), Mockito.anyBoolean())).thenReturn(Map.of()))) { |
| 94 | + diskMock.when(() -> KVMPhysicalDisk.checkQcow2File(DISK_PATH)).thenAnswer(invocation -> null); |
| 95 | + diskMock.when(() -> KVMPhysicalDisk.getVirtualSizeFromFile(DISK_PATH)).thenReturn(DISK_SIZE); |
| 96 | + |
| 97 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 98 | + |
| 99 | + Assert.assertTrue(answer instanceof CheckVolumeAnswer); |
| 100 | + Assert.assertTrue(answer.getResult()); |
| 101 | + Assert.assertEquals(DISK_SIZE, ((CheckVolumeAnswer) answer).getSize()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testExecuteRejectsUnsupportedStoragePoolType() { |
| 107 | + Mockito.when(storageFilerTO.getType()).thenReturn(Storage.StoragePoolType.RBD); |
| 108 | + Mockito.when(storagePoolMgr.getStoragePool(Storage.StoragePoolType.RBD, POOL_UUID)).thenReturn(storagePool); |
| 109 | + |
| 110 | + Answer answer = wrapper.execute(command, libvirtComputingResource); |
| 111 | + |
| 112 | + Assert.assertFalse(answer.getResult()); |
| 113 | + Assert.assertEquals("Unsupported Storage Pool", answer.getDetails()); |
| 114 | + Mockito.verify(storagePool, Mockito.never()).getPhysicalDisk(Mockito.anyString()); |
| 115 | + } |
| 116 | +} |
0 commit comments