|
| 1 | +package org.apache.cloudstack.storage.datastore.util; |
| 2 | + |
| 3 | +import com.linbit.linstor.api.ApiException; |
| 4 | +import com.linbit.linstor.api.DevelopersApi; |
| 5 | +import com.linbit.linstor.api.model.AutoSelectFilter; |
| 6 | +import com.linbit.linstor.api.model.Node; |
| 7 | +import com.linbit.linstor.api.model.Properties; |
| 8 | +import com.linbit.linstor.api.model.ProviderKind; |
| 9 | +import com.linbit.linstor.api.model.ResourceGroup; |
| 10 | +import com.linbit.linstor.api.model.StoragePool; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +import org.junit.Assert; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.mockito.junit.MockitoJUnitRunner; |
| 22 | + |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +@RunWith(MockitoJUnitRunner.class) |
| 27 | +public class LinstorUtilTest { |
| 28 | + |
| 29 | + private static final String LINSTOR_URL_TEST = "devnull.com:3370"; |
| 30 | + private DevelopersApi api; |
| 31 | + |
| 32 | + private Node mockNode(String name) { |
| 33 | + Node nodeMock = new Node(); |
| 34 | + nodeMock.setName(name); |
| 35 | + |
| 36 | + return nodeMock; |
| 37 | + } |
| 38 | + |
| 39 | + private StoragePool mockStoragePool(String name, String node, ProviderKind kind) { |
| 40 | + StoragePool sp = new StoragePool(); |
| 41 | + sp.setStoragePoolName(name); |
| 42 | + sp.setNodeName(node); |
| 43 | + sp.setProviderKind(kind); |
| 44 | + return sp; |
| 45 | + } |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setUp() throws ApiException { |
| 49 | + api = mock(DevelopersApi.class); |
| 50 | + |
| 51 | + when(api.nodeList(Collections.emptyList(), Collections.emptyList(), null, null)) |
| 52 | + .thenReturn(Arrays.asList(mockNode("nodeA"), mockNode("nodeB"), mockNode("nodeC"))); |
| 53 | + |
| 54 | + ResourceGroup csGroup = new ResourceGroup(); |
| 55 | + csGroup.setName("cloudstack"); |
| 56 | + AutoSelectFilter asf = new AutoSelectFilter(); |
| 57 | + asf.setPlaceCount(2); |
| 58 | + csGroup.setSelectFilter(asf); |
| 59 | + when(api.resourceGroupList(Collections.singletonList("cloudstack"), null, null, null)) |
| 60 | + .thenReturn(Collections.singletonList(csGroup)); |
| 61 | + |
| 62 | + when(api.viewStoragePools(Collections.emptyList(), null, null, null, null, true)) |
| 63 | + .thenReturn(Arrays.asList( |
| 64 | + mockStoragePool("thinpool", "nodeA", ProviderKind.LVM_THIN), |
| 65 | + mockStoragePool("thinpool", "nodeB", ProviderKind.LVM_THIN), |
| 66 | + mockStoragePool("thinpool", "nodeC", ProviderKind.LVM_THIN) |
| 67 | + )); |
| 68 | + |
| 69 | +// when(LinstorUtil.getLinstorAPI(LINSTOR_URL_TEST)).thenReturn(api); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testGetLinstorNodeNames() throws ApiException { |
| 74 | + List<String> linstorNodes = LinstorUtil.getLinstorNodeNames(api); |
| 75 | + Assert.assertEquals(Arrays.asList("nodeA", "nodeB", "nodeC"), linstorNodes); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testGetSnapshotPath() { |
| 80 | + { |
| 81 | + StoragePool spLVMThin = new StoragePool(); |
| 82 | + Properties lvmThinProps = new Properties(); |
| 83 | + lvmThinProps.put("StorDriver/StorPoolName", "storage/storage-thin"); |
| 84 | + spLVMThin.setProps(lvmThinProps); |
| 85 | + spLVMThin.setProviderKind(ProviderKind.LVM_THIN); |
| 86 | + String snapPath = LinstorUtil.getSnapshotPath(spLVMThin, "cs-cb32532a-dd8f-47e0-a81c-8a75573d3545", "snap3"); |
| 87 | + Assert.assertEquals("/dev/mapper/storage-cs--cb32532a--dd8f--47e0--a81c--8a75573d3545_00000_snap3", snapPath); |
| 88 | + } |
| 89 | + |
| 90 | + { |
| 91 | + StoragePool spZFS = new StoragePool(); |
| 92 | + Properties zfsProps = new Properties(); |
| 93 | + zfsProps.put("StorDriver/StorPoolName", "linstorPool"); |
| 94 | + spZFS.setProps(zfsProps); |
| 95 | + spZFS.setProviderKind(ProviderKind.ZFS); |
| 96 | + |
| 97 | + String snapPath = LinstorUtil.getSnapshotPath(spZFS, "cs-cb32532a-dd8f-47e0-a81c-8a75573d3545", "snap2"); |
| 98 | + Assert.assertEquals("zfs://linstorPool/cs-cb32532a-dd8f-47e0-a81c-8a75573d3545_00000@snap2", snapPath); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testGetRscGroupStoragePools() throws ApiException { |
| 104 | + List<StoragePool> storagePools = LinstorUtil.getRscGroupStoragePools(api, "cloudstack"); |
| 105 | + |
| 106 | + List<String> names = storagePools.stream() |
| 107 | + .map(sp -> String.format("%s::%s", sp.getNodeName(), sp.getStoragePoolName())) |
| 108 | + .collect(Collectors.toList()); |
| 109 | + Assert.assertEquals(names, Arrays.asList("nodeA::thinpool", "nodeB::thinpool", "nodeC::thinpool")); |
| 110 | + } |
| 111 | +} |
0 commit comments