|
| 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 | + |
| 18 | +package com.cloud.storage; |
| 19 | + |
| 20 | +import com.cloud.agent.AgentManager; |
| 21 | +import com.cloud.agent.api.Answer; |
| 22 | +import com.cloud.exception.AgentUnavailableException; |
| 23 | +import com.cloud.exception.OperationTimedoutException; |
| 24 | +import com.cloud.host.HostVO; |
| 25 | +import com.cloud.host.Status; |
| 26 | +import com.cloud.host.dao.HostDao; |
| 27 | +import com.cloud.storage.dao.VolumeDetailsDao; |
| 28 | +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; |
| 29 | +import org.apache.cloudstack.storage.command.ClvmLockTransferCommand; |
| 30 | +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.mockito.InjectMocks; |
| 36 | +import org.mockito.Mock; |
| 37 | +import org.mockito.Mockito; |
| 38 | +import org.mockito.junit.MockitoJUnitRunner; |
| 39 | + |
| 40 | +import static org.mockito.ArgumentMatchers.any; |
| 41 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 42 | +import static org.mockito.ArgumentMatchers.eq; |
| 43 | +import static org.mockito.Mockito.never; |
| 44 | +import static org.mockito.Mockito.times; |
| 45 | +import static org.mockito.Mockito.verify; |
| 46 | +import static org.mockito.Mockito.when; |
| 47 | + |
| 48 | +@RunWith(MockitoJUnitRunner.class) |
| 49 | +public class ClvmLockManagerTest { |
| 50 | + |
| 51 | + @Mock |
| 52 | + private VolumeDetailsDao volsDetailsDao; |
| 53 | + |
| 54 | + @Mock |
| 55 | + private AgentManager agentMgr; |
| 56 | + |
| 57 | + @Mock |
| 58 | + private HostDao hostDao; |
| 59 | + |
| 60 | + @InjectMocks |
| 61 | + private ClvmLockManager clvmLockManager; |
| 62 | + |
| 63 | + private static final Long VOLUME_ID = 100L; |
| 64 | + private static final Long HOST_ID_1 = 1L; |
| 65 | + private static final Long HOST_ID_2 = 2L; |
| 66 | + private static final String VOLUME_UUID = "test-volume-uuid"; |
| 67 | + private static final String VOLUME_PATH = "test-volume-path"; |
| 68 | + private static final String VG_NAME = "acsvg"; |
| 69 | + |
| 70 | + @Before |
| 71 | + public void setUp() { |
| 72 | + // Reset mocks before each test |
| 73 | + Mockito.reset(volsDetailsDao, agentMgr, hostDao); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testGetClvmLockHostId_Success() { |
| 78 | + VolumeDetailVO detail = new VolumeDetailVO(); |
| 79 | + detail.setValue("123"); |
| 80 | + when(volsDetailsDao.findDetail(VOLUME_ID, VolumeInfo.CLVM_LOCK_HOST_ID)).thenReturn(detail); |
| 81 | + |
| 82 | + Long result = clvmLockManager.getClvmLockHostId(VOLUME_ID, VOLUME_UUID); |
| 83 | + |
| 84 | + Assert.assertEquals(Long.valueOf(123), result); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testGetClvmLockHostId_NoDetail() { |
| 89 | + when(volsDetailsDao.findDetail(VOLUME_ID, VolumeInfo.CLVM_LOCK_HOST_ID)).thenReturn(null); |
| 90 | + |
| 91 | + Long result = clvmLockManager.getClvmLockHostId(VOLUME_ID, VOLUME_UUID); |
| 92 | + |
| 93 | + Assert.assertNull(result); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testGetClvmLockHostId_InvalidNumber() { |
| 98 | + VolumeDetailVO detail = new VolumeDetailVO(); |
| 99 | + detail.setValue("invalid"); |
| 100 | + when(volsDetailsDao.findDetail(VOLUME_ID, VolumeInfo.CLVM_LOCK_HOST_ID)).thenReturn(detail); |
| 101 | + |
| 102 | + Long result = clvmLockManager.getClvmLockHostId(VOLUME_ID, VOLUME_UUID); |
| 103 | + |
| 104 | + Assert.assertNull(result); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testSetClvmLockHostId_NewDetail() { |
| 109 | + when(volsDetailsDao.findDetail(VOLUME_ID, VolumeInfo.CLVM_LOCK_HOST_ID)).thenReturn(null); |
| 110 | + |
| 111 | + clvmLockManager.setClvmLockHostId(VOLUME_ID, HOST_ID_1); |
| 112 | + |
| 113 | + verify(volsDetailsDao, times(1)).addDetail(eq(VOLUME_ID), eq(VolumeInfo.CLVM_LOCK_HOST_ID), |
| 114 | + eq(String.valueOf(HOST_ID_1)), eq(false)); |
| 115 | + verify(volsDetailsDao, never()).update(anyLong(), any()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testSetClvmLockHostId_UpdateExisting() { |
| 120 | + VolumeDetailVO existingDetail = Mockito.mock(VolumeDetailVO.class); |
| 121 | + when(existingDetail.getId()).thenReturn(50L); |
| 122 | + when(volsDetailsDao.findDetail(VOLUME_ID, VolumeInfo.CLVM_LOCK_HOST_ID)).thenReturn(existingDetail); |
| 123 | + |
| 124 | + clvmLockManager.setClvmLockHostId(VOLUME_ID, HOST_ID_2); |
| 125 | + |
| 126 | + verify(existingDetail, times(1)).setValue(String.valueOf(HOST_ID_2)); |
| 127 | + verify(volsDetailsDao, times(1)).update(eq(50L), eq(existingDetail)); |
| 128 | + verify(volsDetailsDao, never()).addDetail(anyLong(), any(), any(), Mockito.anyBoolean()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testClearClvmLockHostDetail_Success() { |
| 133 | + VolumeVO volume = Mockito.mock(VolumeVO.class); |
| 134 | + when(volume.getId()).thenReturn(VOLUME_ID); |
| 135 | + when(volume.getUuid()).thenReturn(VOLUME_UUID); |
| 136 | + |
| 137 | + VolumeDetailVO detail = Mockito.mock(VolumeDetailVO.class); |
| 138 | + when(detail.getId()).thenReturn(99L); |
| 139 | + when(volsDetailsDao.findDetail(VOLUME_ID, VolumeInfo.CLVM_LOCK_HOST_ID)).thenReturn(detail); |
| 140 | + |
| 141 | + clvmLockManager.clearClvmLockHostDetail(volume); |
| 142 | + |
| 143 | + verify(volsDetailsDao, times(1)).remove(99L); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testClearClvmLockHostDetail_NoDetail() { |
| 148 | + VolumeVO volume = Mockito.mock(VolumeVO.class); |
| 149 | + when(volume.getId()).thenReturn(VOLUME_ID); |
| 150 | + when(volsDetailsDao.findDetail(VOLUME_ID, VolumeInfo.CLVM_LOCK_HOST_ID)).thenReturn(null); |
| 151 | + |
| 152 | + clvmLockManager.clearClvmLockHostDetail(volume); |
| 153 | + |
| 154 | + verify(volsDetailsDao, never()).remove(anyLong()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testTransferClvmVolumeLock_Success() throws AgentUnavailableException, OperationTimedoutException { |
| 159 | + StoragePoolVO pool = Mockito.mock(StoragePoolVO.class); |
| 160 | + when(pool.getPath()).thenReturn("/" + VG_NAME); |
| 161 | + |
| 162 | + HostVO sourceHost = Mockito.mock(HostVO.class); |
| 163 | + when(sourceHost.getStatus()).thenReturn(Status.Up); |
| 164 | + when(hostDao.findById(HOST_ID_1)).thenReturn(sourceHost); |
| 165 | + |
| 166 | + Answer deactivateAnswer = new Answer(null, true, null); |
| 167 | + Answer activateAnswer = new Answer(null, true, null); |
| 168 | + |
| 169 | + when(agentMgr.send(eq(HOST_ID_1), any(ClvmLockTransferCommand.class))).thenReturn(deactivateAnswer); |
| 170 | + when(agentMgr.send(eq(HOST_ID_2), any(ClvmLockTransferCommand.class))).thenReturn(activateAnswer); |
| 171 | + |
| 172 | + boolean result = clvmLockManager.transferClvmVolumeLock(VOLUME_UUID, VOLUME_ID, |
| 173 | + VOLUME_PATH, pool, HOST_ID_1, HOST_ID_2); |
| 174 | + |
| 175 | + Assert.assertTrue(result); |
| 176 | + verify(agentMgr, times(2)).send(anyLong(), any(ClvmLockTransferCommand.class)); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testTransferClvmVolumeLock_NullPool() { |
| 181 | + boolean result = clvmLockManager.transferClvmVolumeLock(VOLUME_UUID, VOLUME_ID, |
| 182 | + VOLUME_PATH, null, HOST_ID_1, HOST_ID_2); |
| 183 | + |
| 184 | + Assert.assertFalse(result); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testTransferClvmVolumeLock_SameHost() throws AgentUnavailableException, OperationTimedoutException { |
| 189 | + StoragePoolVO pool = Mockito.mock(StoragePoolVO.class); |
| 190 | + when(pool.getPath()).thenReturn("/" + VG_NAME); |
| 191 | + |
| 192 | + Answer activateAnswer = new Answer(null, true, null); |
| 193 | + when(agentMgr.send(eq(HOST_ID_1), any(ClvmLockTransferCommand.class))).thenReturn(activateAnswer); |
| 194 | + |
| 195 | + boolean result = clvmLockManager.transferClvmVolumeLock(VOLUME_UUID, VOLUME_ID, |
| 196 | + VOLUME_PATH, pool, HOST_ID_1, HOST_ID_1); |
| 197 | + |
| 198 | + Assert.assertTrue(result); |
| 199 | + verify(agentMgr, times(1)).send(anyLong(), any(ClvmLockTransferCommand.class)); |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + public void testTransferClvmVolumeLock_ActivationFails() throws AgentUnavailableException, OperationTimedoutException { |
| 204 | + StoragePoolVO pool = Mockito.mock(StoragePoolVO.class); |
| 205 | + when(pool.getPath()).thenReturn(VG_NAME); |
| 206 | + |
| 207 | + Answer activateAnswer = new Answer(null, false, "Activation failed"); |
| 208 | + when(agentMgr.send(eq(HOST_ID_1), any(ClvmLockTransferCommand.class))).thenReturn(activateAnswer); |
| 209 | + |
| 210 | + boolean result = clvmLockManager.transferClvmVolumeLock(VOLUME_UUID, VOLUME_ID, |
| 211 | + VOLUME_PATH, pool, HOST_ID_1, HOST_ID_1); |
| 212 | + |
| 213 | + Assert.assertFalse(result); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void testTransferClvmVolumeLock_AgentUnavailable() throws AgentUnavailableException, OperationTimedoutException { |
| 218 | + StoragePoolVO pool = Mockito.mock(StoragePoolVO.class); |
| 219 | + when(pool.getPath()).thenReturn(VG_NAME); |
| 220 | + |
| 221 | + when(agentMgr.send(anyLong(), any(ClvmLockTransferCommand.class))) |
| 222 | + .thenThrow(new AgentUnavailableException("Agent unavailable", HOST_ID_2)); |
| 223 | + |
| 224 | + boolean result = clvmLockManager.transferClvmVolumeLock(VOLUME_UUID, VOLUME_ID, |
| 225 | + VOLUME_PATH, pool, HOST_ID_1, HOST_ID_2); |
| 226 | + |
| 227 | + Assert.assertFalse(result); |
| 228 | + } |
| 229 | +} |
0 commit comments