|
| 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.ha; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.util.Arrays; |
| 21 | + |
| 22 | +import javax.inject.Inject; |
| 23 | + |
| 24 | +import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService; |
| 25 | +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; |
| 26 | +import org.apache.cloudstack.managed.context.ManagedContext; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.Mockito; |
| 32 | +import org.mockito.runners.MockitoJUnitRunner; |
| 33 | + |
| 34 | +import com.cloud.agent.AgentManager; |
| 35 | +import com.cloud.alert.AlertManager; |
| 36 | +import com.cloud.dc.ClusterDetailsDao; |
| 37 | +import com.cloud.dc.DataCenterVO; |
| 38 | +import com.cloud.dc.HostPodVO; |
| 39 | +import com.cloud.dc.dao.DataCenterDao; |
| 40 | +import com.cloud.dc.dao.HostPodDao; |
| 41 | +import com.cloud.ha.dao.HighAvailabilityDao; |
| 42 | +import com.cloud.host.Host; |
| 43 | +import com.cloud.host.HostVO; |
| 44 | +import com.cloud.host.dao.HostDao; |
| 45 | +import com.cloud.hypervisor.Hypervisor.HypervisorType; |
| 46 | +import com.cloud.resource.ResourceManager; |
| 47 | +import com.cloud.server.ManagementServer; |
| 48 | +import com.cloud.service.dao.ServiceOfferingDao; |
| 49 | +import com.cloud.storage.StorageManager; |
| 50 | +import com.cloud.storage.dao.GuestOSCategoryDao; |
| 51 | +import com.cloud.storage.dao.GuestOSDao; |
| 52 | +import com.cloud.user.AccountManager; |
| 53 | +import com.cloud.vm.VMInstanceVO; |
| 54 | +import com.cloud.vm.VirtualMachineManager; |
| 55 | +import com.cloud.vm.dao.VMInstanceDao; |
| 56 | + |
| 57 | +@RunWith(MockitoJUnitRunner.class) |
| 58 | +public class HighAvailabilityManagerImplTest { |
| 59 | + @Mock |
| 60 | + HighAvailabilityDao _haDao; |
| 61 | + @Mock |
| 62 | + VMInstanceDao _instanceDao; |
| 63 | + @Mock |
| 64 | + HostDao _hostDao; |
| 65 | + @Mock |
| 66 | + DataCenterDao _dcDao; |
| 67 | + @Mock |
| 68 | + HostPodDao _podDao; |
| 69 | + @Mock |
| 70 | + ClusterDetailsDao _clusterDetailsDao; |
| 71 | + |
| 72 | + @Mock |
| 73 | + ServiceOfferingDao _serviceOfferingDao; |
| 74 | + |
| 75 | + @Mock |
| 76 | + ManagedContext _managedContext; |
| 77 | + |
| 78 | + @Mock |
| 79 | + AgentManager _agentMgr; |
| 80 | + @Mock |
| 81 | + AlertManager _alertMgr; |
| 82 | + @Mock |
| 83 | + StorageManager _storageMgr; |
| 84 | + @Mock |
| 85 | + GuestOSDao _guestOSDao; |
| 86 | + @Mock |
| 87 | + GuestOSCategoryDao _guestOSCategoryDao; |
| 88 | + @Mock |
| 89 | + VirtualMachineManager _itMgr; |
| 90 | + @Mock |
| 91 | + AccountManager _accountMgr; |
| 92 | + @Mock |
| 93 | + ResourceManager _resourceMgr; |
| 94 | + @Mock |
| 95 | + ManagementServer _msServer; |
| 96 | + @Mock |
| 97 | + ConfigurationDao _configDao; |
| 98 | + @Mock |
| 99 | + VolumeOrchestrationService volumeMgr; |
| 100 | + |
| 101 | + @Mock |
| 102 | + HostVO hostVO; |
| 103 | + |
| 104 | + HighAvailabilityManagerImpl highAvailabilityManager; |
| 105 | + |
| 106 | + @Before |
| 107 | + public void setup() throws IllegalArgumentException, |
| 108 | + IllegalAccessException, NoSuchFieldException, SecurityException { |
| 109 | + highAvailabilityManager = new HighAvailabilityManagerImpl(); |
| 110 | + for (Field injectField : HighAvailabilityManagerImpl.class |
| 111 | + .getDeclaredFields()) { |
| 112 | + if (injectField.isAnnotationPresent(Inject.class)) { |
| 113 | + injectField.setAccessible(true); |
| 114 | + injectField.set(highAvailabilityManager, this.getClass() |
| 115 | + .getDeclaredField(injectField.getName()).get(this)); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void scheduleRestartForVmsOnHost() { |
| 122 | + Mockito.when(hostVO.getType()).thenReturn(Host.Type.Routing); |
| 123 | + Mockito.when(hostVO.getHypervisorType()).thenReturn(HypervisorType.KVM); |
| 124 | + Mockito.when(_instanceDao.listByHostId(42l)).thenReturn( |
| 125 | + Arrays.asList(Mockito.mock(VMInstanceVO.class))); |
| 126 | + Mockito.when(_podDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(HostPodVO.class)); |
| 127 | + Mockito.when(_dcDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(DataCenterVO.class)); |
| 128 | + highAvailabilityManager.scheduleRestartForVmsOnHost(hostVO, true); |
| 129 | + } |
| 130 | +} |
0 commit comments