|
| 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.kvm.resource.wrapper; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | +import com.cloud.agent.api.Answer; |
| 22 | +import com.cloud.agent.api.GetVolumesOnStorageAnswer; |
| 23 | +import com.cloud.agent.api.GetVolumesOnStorageCommand; |
| 24 | +import com.cloud.agent.api.to.StorageFilerTO; |
| 25 | +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; |
| 26 | +import com.cloud.hypervisor.kvm.storage.KVMStoragePool; |
| 27 | +import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; |
| 28 | +import com.cloud.storage.Storage; |
| 29 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 30 | +import org.apache.cloudstack.storage.volume.VolumeOnStorageTO; |
| 31 | +import org.apache.cloudstack.utils.qemu.QemuImg; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.mockito.MockedConstruction; |
| 35 | + |
| 36 | +import static org.junit.Assert.assertEquals; |
| 37 | +import static org.junit.Assert.assertFalse; |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | +import static org.mockito.ArgumentMatchers.any; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | +import static org.mockito.Mockito.mockConstruction; |
| 42 | +import static org.mockito.Mockito.never; |
| 43 | +import static org.mockito.Mockito.verify; |
| 44 | +import static org.mockito.Mockito.when; |
| 45 | + |
| 46 | +public class LibvirtLinstorImportListingTest { |
| 47 | + private KVMStoragePool pool; |
| 48 | + private KVMStoragePoolManager manager; |
| 49 | + private LibvirtComputingResource resource; |
| 50 | + private StorageFilerTO filer; |
| 51 | + private final LibvirtGetVolumesOnStorageCommandWrapper wrapper = new LibvirtGetVolumesOnStorageCommandWrapper(); |
| 52 | + |
| 53 | + @Before |
| 54 | + public void setup() { |
| 55 | + pool = mock(KVMStoragePool.class); |
| 56 | + manager = mock(KVMStoragePoolManager.class); |
| 57 | + resource = mock(LibvirtComputingResource.class); |
| 58 | + filer = mock(StorageFilerTO.class); |
| 59 | + when(filer.getType()).thenReturn(Storage.StoragePoolType.Linstor); |
| 60 | + when(filer.getUuid()).thenReturn("pool"); |
| 61 | + when(resource.getStoragePoolMgr()).thenReturn(manager); |
| 62 | + when(manager.getStoragePool(Storage.StoragePoolType.Linstor, "pool", true, true)).thenReturn(pool); |
| 63 | + } |
| 64 | + |
| 65 | + private VolumeOnStorageTO volume(String name, boolean locked) { |
| 66 | + VolumeOnStorageTO volume = new VolumeOnStorageTO(null, name, name, "/dev/drbd/by-res/cs-" + name + "/0", "RAW", 1024, 1024); |
| 67 | + volume.addDetail(VolumeOnStorageTO.Detail.IS_LOCKED, Boolean.toString(locked)); |
| 68 | + return volume; |
| 69 | + } |
| 70 | + |
| 71 | + private void verifyNoStorageMutation() { |
| 72 | + verify(manager, never()).connectPhysicalDisk(any(), any(), any(), any()); |
| 73 | + verify(manager, never()).disconnectPhysicalDisk(any(), any(), any()); |
| 74 | + verify(pool, never()).getPhysicalDisk(any()); |
| 75 | + verify(pool, never()).listPhysicalDisks(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void listingUsesMetadataWithoutQemuOrConnectingAndKeepsKeyword() { |
| 80 | + when(pool.getVolumesForImport(null)).thenReturn(Arrays.asList(volume("alpha", false), volume("beta", true))); |
| 81 | + try (MockedConstruction<QemuImg> qemu = mockConstruction(QemuImg.class)) { |
| 82 | + GetVolumesOnStorageAnswer answer = (GetVolumesOnStorageAnswer) wrapper.execute( |
| 83 | + new GetVolumesOnStorageCommand(filer, null, "beta"), resource); |
| 84 | + assertTrue(answer.getResult()); |
| 85 | + assertEquals(1, answer.getVolumes().size()); |
| 86 | + assertEquals("beta", answer.getVolumes().get(0).getName()); |
| 87 | + assertEquals("true", answer.getVolumes().get(0).getDetails().get(VolumeOnStorageTO.Detail.IS_LOCKED)); |
| 88 | + assertTrue(qemu.constructed().isEmpty()); |
| 89 | + } |
| 90 | + verifyNoStorageMutation(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void singlePathAlsoUsesReadOnlyMetadata() { |
| 95 | + when(pool.getVolumesForImport("alpha")).thenReturn(Collections.singletonList(volume("alpha", false))); |
| 96 | + try (MockedConstruction<QemuImg> qemu = mockConstruction(QemuImg.class)) { |
| 97 | + GetVolumesOnStorageAnswer answer = (GetVolumesOnStorageAnswer) wrapper.execute( |
| 98 | + new GetVolumesOnStorageCommand(filer, "alpha", null), resource); |
| 99 | + assertTrue(answer.getResult()); |
| 100 | + assertEquals("alpha", answer.getVolumes().get(0).getPath()); |
| 101 | + assertTrue(qemu.constructed().isEmpty()); |
| 102 | + } |
| 103 | + verifyNoStorageMutation(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void wrongGroupDoesNotConnectOrDisconnect() { |
| 108 | + when(pool.getVolumesForImport("alpha")).thenThrow(new CloudRuntimeException("resource group mismatch")); |
| 109 | + Answer answer = wrapper.execute(new GetVolumesOnStorageCommand(filer, "alpha", null), resource); |
| 110 | + assertFalse(answer.getResult()); |
| 111 | + assertEquals("resource group mismatch", answer.getDetails()); |
| 112 | + verifyNoStorageMutation(); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void controllerFailureDoesNotReturnSuccessfulEmptyList() { |
| 117 | + when(pool.getVolumesForImport(null)).thenThrow(new CloudRuntimeException("controller unavailable")); |
| 118 | + Answer answer = wrapper.execute(new GetVolumesOnStorageCommand(filer, null, null), resource); |
| 119 | + assertFalse(answer.getResult()); |
| 120 | + assertEquals("controller unavailable", answer.getDetails()); |
| 121 | + verifyNoStorageMutation(); |
| 122 | + } |
| 123 | +} |
0 commit comments