📄 pollertest.java
字号:
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2005 The OpenNMS Group, Inc. All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Original code base Copyright (C) 1999-2001 Oculan Corp. All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact:// OpenNMS Licensing <license@opennms.org>// http://www.opennms.org/// http://www.opennms.com///package org.opennms.netmgt.poller;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Timestamp;import junit.framework.TestCase;import org.opennms.netmgt.mock.EventAnticipator;import org.opennms.netmgt.mock.MockDatabase;import org.opennms.netmgt.mock.MockElement;import org.opennms.netmgt.mock.MockEventIpcManager;import org.opennms.netmgt.mock.MockInterface;import org.opennms.netmgt.mock.MockNetwork;import org.opennms.netmgt.mock.MockNode;import org.opennms.netmgt.mock.MockOutageConfig;import org.opennms.netmgt.mock.MockPollerConfig;import org.opennms.netmgt.mock.MockService;import org.opennms.netmgt.mock.MockUtil;import org.opennms.netmgt.mock.MockVisitor;import org.opennms.netmgt.mock.MockVisitorAdapter;import org.opennms.netmgt.mock.OutageAnticipator;import org.opennms.netmgt.mock.PollAnticipator;import org.opennms.netmgt.outage.OutageManager;import org.opennms.netmgt.poller.monitors.ServiceMonitor;import org.opennms.netmgt.utils.Querier;import org.opennms.netmgt.xml.event.Event;public class PollerTest extends TestCase { private Poller m_poller; private OutageManager m_outageMgr; private MockNetwork m_network; private MockDatabase m_db; private MockPollerConfig m_pollerConfig; private MockEventIpcManager m_eventMgr; private boolean m_daemonsStarted = false; private EventAnticipator m_anticipator; private OutageAnticipator m_outageAnticipator; // // SetUp and TearDown // public void setUp() { MockUtil.println("------------ Begin Test "+getName()+" --------------------------"); MockUtil.setupLogging(); MockUtil.resetLogLevel(); m_network = new MockNetwork(); m_network.setCriticalService("ICMP"); m_network.addNode(1, "Router"); m_network.addInterface("192.168.1.1"); m_network.addService("ICMP"); m_network.addService("SMTP"); m_network.addInterface("192.168.1.2"); m_network.addService("ICMP"); m_network.addService("SMTP"); m_network.addNode(2, "Server"); m_network.addInterface("192.168.1.3"); m_network.addService("ICMP"); m_network.addService("HTTP"); m_network.addNode(3, "Firewall"); m_network.addInterface("192.168.1.4"); m_network.addService("SMTP"); m_network.addService("HTTP"); m_network.addInterface("192.168.1.5"); m_network.addService("SMTP"); m_network.addService("HTTP"); m_db = new MockDatabase(); m_db.populate(m_network); m_pollerConfig = new MockPollerConfig(); m_pollerConfig.setNextOutageIdSql(m_db.getNextOutageIdStatement()); m_pollerConfig.setNodeOutageProcessingEnabled(true); m_pollerConfig.setCriticalService("ICMP"); m_pollerConfig.addPackage("TestPackage"); m_pollerConfig.addDowntime(1000L, 0L, -1L, false); m_pollerConfig.setDefaultPollInterval(1000L); m_pollerConfig.populatePackage(m_network); m_pollerConfig.addPackage("TestPkg2"); m_pollerConfig.addDowntime(1000L, 0L, -1L, false); m_pollerConfig.setDefaultPollInterval(2000L); m_pollerConfig.addService(m_network.getService(2, "192.168.1.3", "HTTP")); m_anticipator = new EventAnticipator(); m_outageAnticipator = new OutageAnticipator(m_db); m_eventMgr = new MockEventIpcManager(); m_eventMgr.setEventWriter(m_db); m_eventMgr.setEventAnticipator(m_anticipator); m_eventMgr.addEventListener(m_outageAnticipator); m_poller = new Poller(); m_poller.setEventManager(m_eventMgr); m_poller.setDbConnectionFactory(m_db); m_poller.setPollerConfig(m_pollerConfig); m_poller.setPollOutagesConfig(m_pollerConfig); MockOutageConfig config = new MockOutageConfig(); config.setGetNextOutageID(m_db.getNextOutageIdStatement()); // m_outageMgr = new OutageManager();// m_outageMgr.setEventMgr(m_eventMgr);// m_outageMgr.setOutageMgrConfig(config);// m_outageMgr.setDbConnectionFactory(m_db); } public void tearDown() { m_eventMgr.finishProcessingEvents(); stopDaemons(); sleep(200); assertTrue("Unexpected WARN or ERROR msgs in Log!", MockUtil.noWarningsOrHigherLogged()); m_db.drop(); MockUtil.println("------------ End Test "+getName()+" --------------------------"); } // // Tests // public void testBug709() { m_pollerConfig.setNodeOutageProcessingEnabled(true); MockNode node = m_network.getNode(2); MockService icmpService = m_network.getService(2, "192.168.1.3", "ICMP"); MockService httpService = m_network.getService(2, "192.168.1.3", "HTTP"); // start the poller startDaemons(); // // Bring Down the HTTP service and expect nodeLostService Event // resetAnticipated(); anticipateDown(httpService); // bring down the HTTP service httpService.bringDown(); verifyAnticipated(10000); // // Bring Down the ICMP (on the only interface on the node) now expect nodeDown // only. // resetAnticipated(); anticipateDown(node); // bring down the ICMP service icmpService.bringDown(); // make sure the down events are received //verifyAnticipated(10000); sleep(5000); // // Bring up both the node and the httpService at the same time. Expect // both a nodeUp and a nodeRegainedService // resetAnticipated(); // the order matters here anticipateUp(httpService); anticipateUp(node); // bring up all the services on the node node.bringUp(); // make sure the down events are received verifyAnticipated(10000); } private void resetAnticipated() { m_anticipator.reset(); m_outageAnticipator.reset(); } public void testCritSvcStatusPropagation() { m_pollerConfig.setNodeOutageProcessingEnabled(true); MockNode node = m_network.getNode(1); anticipateDown(node); startDaemons(); bringDownCritSvcs(node); verifyAnticipated(8000); } public void testInterfaceWithNoCriticalService() { m_pollerConfig.setNodeOutageProcessingEnabled(true); MockInterface iface = m_network.getInterface(3, "192.168.1.4"); MockService svc = iface.getService("SMTP"); MockService otherService = iface.getService("HTTP"); startDaemons(); anticipateDown(iface); iface.bringDown(); verifyAnticipated(8000); anticipateUp(iface); anticipateDown(otherService, true); svc.bringUp(); verifyAnticipated(8000); } // what about scheduled outages? public void testDontPollDuringScheduledOutages() { long start = System.currentTimeMillis(); MockInterface iface = m_network.getInterface(1, "192.168.1.2"); m_pollerConfig.addScheduledOutage(m_pollerConfig.getPackage("TestPackage"), "TestOutage", start, start + 5000, iface.getIpAddr()); MockUtil.println("Begin Outage"); startDaemons(); long now = System.currentTimeMillis(); sleep(3000 - (now - start)); MockUtil.println("End Outage"); assertEquals(0, iface.getPollCount()); sleep(5000); assertTrue(0 < iface.getPollCount()); } // Test harness that tests any type of node, interface or element. private void testElementDeleted(MockElement element, Event deleteEvent) { m_pollerConfig.setNodeOutageProcessingEnabled(false); PollAnticipator poll = new PollAnticipator(); element.addAnticipator(poll); poll.anticipateAllServices(element); startDaemons(); // wait til after the first poll of the services poll.waitForAnticipated(1000L); // now delete the node and send a nodeDeleted event m_network.resetInvalidPollCount(); m_network.removeElement(element); m_eventMgr.sendEventToListeners(deleteEvent); // now ensure that no invalid polls have occurred sleep(3000); assertEquals(0, m_network.getInvalidPollCount()); } // serviceDeleted: EventConstants.SERVICE_DELETED_EVENT_UEI public void testServiceDeleted() { MockService svc = m_network.getService(1, "192.168.1.1", "SMTP"); Event deleteEvent = MockUtil.createServiceDeletedEvent("Test", svc); testElementDeleted(svc, deleteEvent); } // interfaceDeleted: EventConstants.INTERFACE_DELETED_EVENT_UEI public void testInterfaceDeleted() { MockInterface iface = m_network.getInterface(1, "192.168.1.1"); Event deleteEvent = MockUtil.createInterfaceDeletedEvent("Test", iface); testElementDeleted(iface, deleteEvent); } // nodeDeleted: EventConstants.NODE_DELETED_EVENT_UEI public void testNodeDeleted() { MockNode node = m_network.getNode(1); Event deleteEvent = MockUtil.createNodeDeletedEvent("Test", node); testElementDeleted(node, deleteEvent); } public void testOutagesClosedOnDelete(MockElement element) { startDaemons(); Event deleteEvent = element.createDeleteEvent(); // bring down so we create an outage in the outages table anticipateDown(element); element.bringDown(); verifyAnticipated(5000); m_outageAnticipator.anticipateOutageClosed(element, deleteEvent); // now delete the service m_network.removeElement(element); m_eventMgr.sendEventToListeners(deleteEvent); verifyAnticipated(5000); } public void testServiceOutagesClosedOnDelete() { MockService element = m_network.getService(1, "192.168.1.1", "SMTP"); testOutagesClosedOnDelete(element); } public void testInterfaceOutagesClosedOnDelete() { MockInterface element = m_network.getInterface(1, "192.168.1.1"); testOutagesClosedOnDelete(element); } public void testNodeOutagesClosedOnDelete() { MockNode element = m_network.getNode(1); testOutagesClosedOnDelete(element); } // interfaceReparented: EventConstants.INTERFACE_REPARENTED_EVENT_UEI public void testInterfaceReparented() { m_pollerConfig.setNodeOutageProcessingEnabled(true); MockNode node1 = m_network.getNode(1); MockNode node2 = m_network.getNode(2); MockInterface dotOne = m_network.getInterface(1, "192.168.1.1"); MockInterface dotTwo = m_network.getInterface(1, "192.168.1.2"); MockInterface dotThree = m_network.getInterface(2, "192.168.1.3"); Event reparentEvent = MockUtil.createReparentEvent("Test", "192.168.1.2", 1, 2); // we are going to reparent to node 2 so when we bring down its only // current interface we expect an interface down not the whole node. anticipateDown(dotThree); startDaemons(); sleep(2000); // move the reparted interface and send a reparented event dotTwo.moveTo(node2); m_db.reparentInterface(dotTwo.getIpAddr(), node1.getNodeId(), node2.getNodeId()); // send the reparent event to the daemons m_eventMgr.sendEventToListeners(reparentEvent); // now bring down the other interface on the new node // System.err.println("Bring Down:"+node2Iface); dotThree.bringDown(); verifyAnticipated(2000); resetAnticipated(); anticipateDown(node2); // System.err.println("Bring Down:"+reparentedIface); dotTwo.bringDown(); //sleep(5000); verifyAnticipated(2000); } // test to see that node lost/regained service events come in public void testNodeOutageProcessingDisabled() throws Exception { m_pollerConfig.setNodeOutageProcessingEnabled(false); MockNode node = m_network.getNode(1); startDaemons(); resetAnticipated(); anticipateServicesDown(node); node.bringDown(); verifyAnticipated(10000); resetAnticipated(); anticipateServicesUp(node);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -