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