KVMFencer extends AdapterBase implements FenceBuilder { @Override public Boolean fenceOff(VirtualMachine vm, Host host) { if (host.getHypervisorType() != HypervisorType.KVM) { s_logger.warn("Don't know how to fence non kvm hosts " + host.getHypervisorType()); return null; } List<HostVO> hosts = _resourceMgr.listAllHostsInCluster(host.getClusterId()); FenceCommand fence = new FenceCommand(vm, host); int i = 0; for (HostVO h : hosts) { if (h.getHypervisorType() == HypervisorType.KVM) { if (h.getStatus() != Status.Up) { continue; } i++; if (h.getId() == host.getId()) { continue; } FenceAnswer answer; try { answer = (FenceAnswer)_agentMgr.send(h.getId(), fence); } catch (AgentUnavailableException e) { s_logger.info("Moving on to the next host because " + h.toString() + " is unavailable"); continue; } catch (OperationTimedoutException e) { s_logger.info("Moving on to the next host because " + h.toString() + " is unavailable"); continue; } if (answer != null && answer.getResult()) { return true; } } } _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, host.getDataCenterId(), host.getPodId(), "Unable to fence off host: " + host.getId(), "Fencing off host " + host.getId() + " did not succeed after asking " + i + " hosts. " + "Check Agent logs for more information."); s_logger.error("Unable to fence off " + vm.toString() + " on " + host.toString()); return false; } KVMFencer(); @Override boolean configure(String name, Map<String, Object> params); @Override boolean start(); @Override boolean stop(); @Override Boolean fenceOff(VirtualMachine vm, Host host);  }
@Test public void testWithSingleHost() { HostVO host = Mockito.mock(HostVO.class); Mockito.when(host.getClusterId()).thenReturn(1l); Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); Mockito.when(host.getDataCenterId()).thenReturn(1l); Mockito.when(host.getPodId()).thenReturn(1l); Mockito.when(host.getStatus()).thenReturn(Status.Up); Mockito.when(host.getId()).thenReturn(1l); VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(Collections.singletonList(host)); Assert.assertFalse(fencer.fenceOff(virtualMachine, host)); }
@Test public void testWithSingleHostDown() { HostVO host = Mockito.mock(HostVO.class); Mockito.when(host.getClusterId()).thenReturn(1l); Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); Mockito.when(host.getDataCenterId()).thenReturn(1l); Mockito.when(host.getPodId()).thenReturn(1l); Mockito.when(host.getStatus()).thenReturn(Status.Down); Mockito.when(host.getId()).thenReturn(1l); VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(Collections.singletonList(host)); Assert.assertFalse(fencer.fenceOff(virtualMachine, host)); }
@Test public void testWithHosts() throws AgentUnavailableException, OperationTimedoutException { HostVO host = Mockito.mock(HostVO.class); Mockito.when(host.getClusterId()).thenReturn(1l); Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); Mockito.when(host.getStatus()).thenReturn(Status.Up); Mockito.when(host.getDataCenterId()).thenReturn(1l); Mockito.when(host.getPodId()).thenReturn(1l); Mockito.when(host.getId()).thenReturn(1l); HostVO secondHost = Mockito.mock(HostVO.class); Mockito.when(secondHost.getClusterId()).thenReturn(1l); Mockito.when(secondHost.getHypervisorType()).thenReturn(HypervisorType.KVM); Mockito.when(secondHost.getStatus()).thenReturn(Status.Up); Mockito.when(secondHost.getDataCenterId()).thenReturn(1l); Mockito.when(secondHost.getPodId()).thenReturn(1l); Mockito.when(host.getId()).thenReturn(2l); VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(Arrays.asList(host, secondHost)); FenceAnswer answer = new FenceAnswer(null, true, "ok"); Mockito.when(agentManager.send(Matchers.anyLong(), Matchers.any(FenceCommand.class))).thenReturn(answer); Assert.assertTrue(fencer.fenceOff(virtualMachine, host)); }
@Test public void testWithFailingFence() throws AgentUnavailableException, OperationTimedoutException { HostVO host = Mockito.mock(HostVO.class); Mockito.when(host.getClusterId()).thenReturn(1l); Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); Mockito.when(host.getStatus()).thenReturn(Status.Up); Mockito.when(host.getDataCenterId()).thenReturn(1l); Mockito.when(host.getPodId()).thenReturn(1l); Mockito.when(host.getId()).thenReturn(1l); HostVO secondHost = Mockito.mock(HostVO.class); Mockito.when(secondHost.getClusterId()).thenReturn(1l); Mockito.when(secondHost.getHypervisorType()).thenReturn(HypervisorType.KVM); Mockito.when(secondHost.getStatus()).thenReturn(Status.Up); Mockito.when(secondHost.getDataCenterId()).thenReturn(1l); Mockito.when(secondHost.getPodId()).thenReturn(1l); Mockito.when(host.getId()).thenReturn(2l); VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(Arrays.asList(host, secondHost)); Mockito.when(agentManager.send(Matchers.anyLong(), Matchers.any(FenceCommand.class))).thenThrow(new AgentUnavailableException(2l)); Assert.assertFalse(fencer.fenceOff(virtualMachine, host)); }
@Test public void testWithTimeoutingFence() throws AgentUnavailableException, OperationTimedoutException { HostVO host = Mockito.mock(HostVO.class); Mockito.when(host.getClusterId()).thenReturn(1l); Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM); Mockito.when(host.getStatus()).thenReturn(Status.Up); Mockito.when(host.getDataCenterId()).thenReturn(1l); Mockito.when(host.getPodId()).thenReturn(1l); Mockito.when(host.getId()).thenReturn(1l); HostVO secondHost = Mockito.mock(HostVO.class); Mockito.when(secondHost.getClusterId()).thenReturn(1l); Mockito.when(secondHost.getHypervisorType()).thenReturn(HypervisorType.KVM); Mockito.when(secondHost.getStatus()).thenReturn(Status.Up); Mockito.when(secondHost.getDataCenterId()).thenReturn(1l); Mockito.when(secondHost.getPodId()).thenReturn(1l); Mockito.when(host.getId()).thenReturn(2l); VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(Arrays.asList(host, secondHost)); Mockito.when(agentManager.send(Matchers.anyLong(), Matchers.any(FenceCommand.class))).thenThrow(new OperationTimedoutException(null, 2l, 0l, 0, false)); Assert.assertFalse(fencer.fenceOff(virtualMachine, host)); }
@Test public void testWithSingleNotKVM() { HostVO host = Mockito.mock(HostVO.class); Mockito.when(host.getClusterId()).thenReturn(1l); Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.Any); Mockito.when(host.getStatus()).thenReturn(Status.Down); Mockito.when(host.getId()).thenReturn(1l); Mockito.when(host.getDataCenterId()).thenReturn(1l); Mockito.when(host.getPodId()).thenReturn(1l); VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(Collections.singletonList(host)); Assert.assertNull(fencer.fenceOff(virtualMachine, host)); }