|
| 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.resource; |
| 19 | + |
| 20 | +import com.cloud.agent.AgentManager; |
| 21 | +import com.cloud.agent.api.GetVncPortAnswer; |
| 22 | +import com.cloud.agent.api.GetVncPortCommand; |
| 23 | +import com.cloud.capacity.dao.CapacityDao; |
| 24 | +import com.cloud.event.ActionEventUtils; |
| 25 | +import com.cloud.ha.HighAvailabilityManager; |
| 26 | +import com.cloud.host.Host; |
| 27 | +import com.cloud.host.HostVO; |
| 28 | +import com.cloud.host.dao.HostDao; |
| 29 | +import com.cloud.hypervisor.Hypervisor; |
| 30 | +import com.cloud.storage.StorageManager; |
| 31 | +import com.cloud.utils.fsm.NoTransitionException; |
| 32 | +import com.cloud.vm.VMInstanceVO; |
| 33 | +import com.cloud.vm.dao.UserVmDetailsDao; |
| 34 | +import com.cloud.vm.dao.VMInstanceDao; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | +import org.junit.runner.RunWith; |
| 39 | +import org.mockito.BDDMockito; |
| 40 | +import org.mockito.InjectMocks; |
| 41 | +import org.mockito.Mock; |
| 42 | +import org.mockito.MockitoAnnotations; |
| 43 | +import org.mockito.Spy; |
| 44 | +import org.powermock.api.mockito.PowerMockito; |
| 45 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 46 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 47 | + |
| 48 | +import java.util.ArrayList; |
| 49 | +import java.util.Arrays; |
| 50 | +import java.util.List; |
| 51 | + |
| 52 | +import static com.cloud.resource.ResourceState.Event.InternalEnterMaintenance; |
| 53 | +import static com.cloud.resource.ResourceState.Event.UnableToMigrate; |
| 54 | +import static org.mockito.Matchers.any; |
| 55 | +import static org.mockito.Matchers.anyBoolean; |
| 56 | +import static org.mockito.Matchers.anyLong; |
| 57 | +import static org.mockito.Matchers.anyString; |
| 58 | +import static org.mockito.Matchers.eq; |
| 59 | +import static org.mockito.Mockito.times; |
| 60 | +import static org.mockito.Mockito.verify; |
| 61 | +import static org.mockito.Mockito.when; |
| 62 | + |
| 63 | +@RunWith(PowerMockRunner.class) |
| 64 | +@PrepareForTest({ActionEventUtils.class, ResourceManagerImpl.class}) |
| 65 | +public class ResourceManagerImplTest { |
| 66 | + |
| 67 | + @Mock |
| 68 | + private CapacityDao capacityDao; |
| 69 | + @Mock |
| 70 | + private StorageManager storageManager; |
| 71 | + @Mock |
| 72 | + private HighAvailabilityManager haManager; |
| 73 | + @Mock |
| 74 | + private UserVmDetailsDao userVmDetailsDao; |
| 75 | + @Mock |
| 76 | + private AgentManager agentManager; |
| 77 | + @Mock |
| 78 | + private HostDao hostDao; |
| 79 | + @Mock |
| 80 | + private VMInstanceDao vmInstanceDao; |
| 81 | + |
| 82 | + @Spy |
| 83 | + @InjectMocks |
| 84 | + private ResourceManagerImpl resourceManager = new ResourceManagerImpl(); |
| 85 | + |
| 86 | + @Mock |
| 87 | + private HostVO host; |
| 88 | + @Mock |
| 89 | + private VMInstanceVO vm1; |
| 90 | + @Mock |
| 91 | + private VMInstanceVO vm2; |
| 92 | + |
| 93 | + @Mock |
| 94 | + private GetVncPortAnswer getVncPortAnswerVm1; |
| 95 | + @Mock |
| 96 | + private GetVncPortAnswer getVncPortAnswerVm2; |
| 97 | + @Mock |
| 98 | + private GetVncPortCommand getVncPortCommandVm1; |
| 99 | + @Mock |
| 100 | + private GetVncPortCommand getVncPortCommandVm2; |
| 101 | + |
| 102 | + private static long hostId = 1L; |
| 103 | + |
| 104 | + private static long vm1Id = 1L; |
| 105 | + private static String vm1InstanceName = "i-1-VM"; |
| 106 | + private static long vm2Id = 2L; |
| 107 | + private static String vm2InstanceName = "i-2-VM"; |
| 108 | + |
| 109 | + private static String vm1VncAddress = "10.2.2.2"; |
| 110 | + private static int vm1VncPort = 5900; |
| 111 | + private static String vm2VncAddress = "10.2.2.2"; |
| 112 | + private static int vm2VncPort = 5901; |
| 113 | + |
| 114 | + @Before |
| 115 | + public void setup() throws Exception { |
| 116 | + MockitoAnnotations.initMocks(this); |
| 117 | + when(host.getType()).thenReturn(Host.Type.Routing); |
| 118 | + when(host.getId()).thenReturn(hostId); |
| 119 | + when(host.getResourceState()).thenReturn(ResourceState.Enabled); |
| 120 | + when(host.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.VMware); |
| 121 | + when(hostDao.findById(hostId)).thenReturn(host); |
| 122 | + when(vm1.getId()).thenReturn(vm1Id); |
| 123 | + when(vm2.getId()).thenReturn(vm2Id); |
| 124 | + when(vm1.getInstanceName()).thenReturn(vm1InstanceName); |
| 125 | + when(vm2.getInstanceName()).thenReturn(vm2InstanceName); |
| 126 | + when(vmInstanceDao.listByHostId(hostId)).thenReturn(new ArrayList<>()); |
| 127 | + when(vmInstanceDao.listVmsMigratingFromHost(hostId)).thenReturn(new ArrayList<>()); |
| 128 | + when(vmInstanceDao.listNonMigratingVmsByHostEqualsLastHost(hostId)).thenReturn(new ArrayList<>()); |
| 129 | + PowerMockito.mockStatic(ActionEventUtils.class); |
| 130 | + BDDMockito.given(ActionEventUtils.onCompletedActionEvent(anyLong(), anyLong(), anyString(), anyString(), anyString(), anyLong())) |
| 131 | + .willReturn(1L); |
| 132 | + when(getVncPortAnswerVm1.getAddress()).thenReturn(vm1VncAddress); |
| 133 | + when(getVncPortAnswerVm1.getPort()).thenReturn(vm1VncPort); |
| 134 | + when(getVncPortAnswerVm2.getAddress()).thenReturn(vm2VncAddress); |
| 135 | + when(getVncPortAnswerVm2.getPort()).thenReturn(vm2VncPort); |
| 136 | + PowerMockito.whenNew(GetVncPortCommand.class).withArguments(vm1Id, vm1InstanceName).thenReturn(getVncPortCommandVm1); |
| 137 | + PowerMockito.whenNew(GetVncPortCommand.class).withArguments(vm2Id, vm2InstanceName).thenReturn(getVncPortCommandVm2); |
| 138 | + when(agentManager.easySend(eq(hostId), eq(getVncPortCommandVm1))).thenReturn(getVncPortAnswerVm1); |
| 139 | + when(agentManager.easySend(eq(hostId), eq(getVncPortCommandVm2))).thenReturn(getVncPortAnswerVm2); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testCheckAndMaintainEnterMaintenanceMode() throws NoTransitionException { |
| 144 | + boolean enterMaintenanceMode = resourceManager.checkAndMaintain(hostId); |
| 145 | + verify(resourceManager).isHostInMaintenance(host, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); |
| 146 | + verify(resourceManager).setHostIntoMaintenance(host); |
| 147 | + verify(resourceManager).resourceStateTransitTo(eq(host), eq(InternalEnterMaintenance), anyLong()); |
| 148 | + Assert.assertTrue(enterMaintenanceMode); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testCheckAndMaintainErrorInMaintenanceRunningVms() throws NoTransitionException { |
| 153 | + when(vmInstanceDao.listByHostId(hostId)).thenReturn(Arrays.asList(vm1, vm2)); |
| 154 | + boolean enterMaintenanceMode = resourceManager.checkAndMaintain(hostId); |
| 155 | + verify(resourceManager).isHostInMaintenance(host, Arrays.asList(vm1, vm2), new ArrayList<>(), new ArrayList<>()); |
| 156 | + Assert.assertFalse(enterMaintenanceMode); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testCheckAndMaintainErrorInMaintenanceMigratingVms() throws NoTransitionException { |
| 161 | + when(vmInstanceDao.listVmsMigratingFromHost(hostId)).thenReturn(Arrays.asList(vm1, vm2)); |
| 162 | + boolean enterMaintenanceMode = resourceManager.checkAndMaintain(hostId); |
| 163 | + verify(resourceManager).isHostInMaintenance(host, new ArrayList<>(), Arrays.asList(vm1, vm2), new ArrayList<>()); |
| 164 | + Assert.assertFalse(enterMaintenanceMode); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testCheckAndMaintainErrorInMaintenanceFailedMigrations() throws NoTransitionException { |
| 169 | + when(vmInstanceDao.listNonMigratingVmsByHostEqualsLastHost(hostId)).thenReturn(Arrays.asList(vm1, vm2)); |
| 170 | + boolean enterMaintenanceMode = resourceManager.checkAndMaintain(hostId); |
| 171 | + verify(resourceManager).isHostInMaintenance(host, new ArrayList<>(), new ArrayList<>(), Arrays.asList(vm1, vm2)); |
| 172 | + verify(resourceManager).setHostIntoErrorInMaintenance(host, Arrays.asList(vm1, vm2)); |
| 173 | + verify(resourceManager).resourceStateTransitTo(eq(host), eq(UnableToMigrate), anyLong()); |
| 174 | + Assert.assertFalse(enterMaintenanceMode); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testConfigureVncAccessForKVMHostFailedMigrations() { |
| 179 | + when(host.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); |
| 180 | + List<VMInstanceVO> vms = Arrays.asList(vm1, vm2); |
| 181 | + resourceManager.configureVncAccessForKVMHostFailedMigrations(host, vms); |
| 182 | + verify(agentManager).pullAgentOutMaintenance(hostId); |
| 183 | + verify(resourceManager).setKVMVncAccess(hostId, vms); |
| 184 | + verify(agentManager, times(vms.size())).easySend(eq(hostId), any(GetVncPortCommand.class)); |
| 185 | + verify(userVmDetailsDao).addDetail(eq(vm1Id), eq("kvm.vnc.address"), eq(vm1VncAddress), anyBoolean()); |
| 186 | + verify(userVmDetailsDao).addDetail(eq(vm1Id), eq("kvm.vnc.port"), eq(String.valueOf(vm1VncPort)), anyBoolean()); |
| 187 | + verify(userVmDetailsDao).addDetail(eq(vm2Id), eq("kvm.vnc.address"), eq(vm2VncAddress), anyBoolean()); |
| 188 | + verify(userVmDetailsDao).addDetail(eq(vm2Id), eq("kvm.vnc.port"), eq(String.valueOf(vm2VncPort)), anyBoolean()); |
| 189 | + verify(agentManager).pullAgentToMaintenance(hostId); |
| 190 | + } |
| 191 | +} |
0 commit comments