Example #1
0
 @Override
 public void runTest() throws Throwable {
   try {
     super.runTest();
     MockLogAppender.assertNoWarningsOrGreater();
   } finally {
     MockUtil.println("------------ End Test " + this + " --------------------------");
   }
 }
 @After
 public void tearDown() throws Exception {
   m_eventMgr.finishProcessingEvents();
   stopDaemons();
   sleep(200);
   if (m_assertLevel != null) {
     MockLogAppender.assertNotGreaterOrEqual(m_assertLevel);
   }
   m_db.drop();
   MockUtil.println("------------ End Test  --------------------------");
 }
  @Test
  public void testNodeGainedDynamicService() throws Exception {
    m_pollerConfig.setNodeOutageProcessingEnabled(true);

    startDaemons();

    TestCapsdConfigManager capsdConfig = new TestCapsdConfigManager(CAPSD_CONFIG);

    InputStream configStream =
        ConfigurationTestUtils.getInputStreamForConfigFile("opennms-server.xml");
    OpennmsServerConfigFactory onmsSvrConfig = new OpennmsServerConfigFactory(configStream);
    configStream.close();

    configStream = ConfigurationTestUtils.getInputStreamForConfigFile("database-schema.xml");
    DatabaseSchemaConfigFactory.setInstance(new DatabaseSchemaConfigFactory(configStream));
    configStream.close();

    configStream =
        ConfigurationTestUtils.getInputStreamForResource(
            this, "/org/opennms/netmgt/capsd/collectd-configuration.xml");
    CollectdConfigFactory collectdConfig =
        new CollectdConfigFactory(
            configStream, onmsSvrConfig.getServerName(), onmsSvrConfig.verifyServer());
    configStream.close();

    JdbcTemplate jdbcTemplate = new JdbcTemplate(m_db);

    JdbcCapsdDbSyncer syncer = new JdbcCapsdDbSyncer();
    syncer.setJdbcTemplate(jdbcTemplate);
    syncer.setOpennmsServerConfig(onmsSvrConfig);
    syncer.setCapsdConfig(capsdConfig);
    syncer.setPollerConfig(m_pollerConfig);
    syncer.setCollectdConfig(collectdConfig);
    syncer.setNextSvcIdSql(m_db.getNextServiceIdStatement());
    syncer.afterPropertiesSet();

    OpenNMSProvisioner provisioner = new OpenNMSProvisioner();
    provisioner.setPollerConfig(m_pollerConfig);
    provisioner.setCapsdConfig(capsdConfig);
    provisioner.setCapsdDbSyncer(syncer);

    provisioner.setEventManager(m_eventMgr);
    provisioner.addServiceDNS("MyDNS", 3, 100, 1000, 500, 3000, 53, "www.opennms.org");

    assertNotNull("The service id for MyDNS is null", m_db.getServiceID("MyDNS"));
    MockUtil.println("The service id for MyDNS is: " + m_db.getServiceID("MyDNS").toString());

    m_anticipator.reset();

    testSendNodeGainedService("MyDNS", "HTTP");
  }
  private void testElementDownUp(MockElement element) {
    startDaemons();

    resetAnticipated();
    anticipateDown(element);

    MockUtil.println("Bringing down element: " + element);
    element.bringDown();
    MockUtil.println("Finished bringing down element: " + element);

    verifyAnticipated(5000);

    sleep(2000);

    resetAnticipated();
    anticipateUp(element);

    MockUtil.println("Bringing up element: " + element);
    element.bringUp();
    MockUtil.println("Finished bringing up element: " + element);

    verifyAnticipated(8000);
  }
  @Test
  public void testNodeLostServiceIncludesReason() throws Exception {
    MockService element = m_network.getService(1, "192.168.1.1", "SMTP");
    String expectedReason = "Oh No!! An Outage!!";
    startDaemons();

    resetAnticipated();
    anticipateDown(element);

    MockUtil.println("Bringing down element: " + element);
    element.bringDown(expectedReason);
    MockUtil.println("Finished bringing down element: " + element);

    verifyAnticipated(8000);

    Collection<Event> receivedEvents = m_anticipator.getAnticipatedEventsRecieved();

    assertEquals(1, receivedEvents.size());

    Event event = receivedEvents.iterator().next();

    assertEquals(expectedReason, EventUtils.getParm(event, EventConstants.PARM_LOSTSERVICE_REASON));
  }
  // what about scheduled outages?
  @Test
  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());
  }
Example #7
0
 @Override
 public void run() {
   try {
     while (System.currentTimeMillis() < millis) {
       try {
         Thread.sleep(10);
       } catch (InterruptedException e) {
         MockUtil.println(e.getMessage());
       }
     }
     sendNodeDownEvent(reductionKey, node);
   } catch (Throwable t) {
     t.printStackTrace();
   } finally {
     signal.countDown();
   }
 }
Example #8
0
  @Test
  @JUnitTemporaryDatabase(
      tempDbClass = MockDatabase.class) // Relies on specific IDs so we need a fresh database
  public void testPersistAlarm() throws Exception {
    final MockNode node = m_mockNetwork.getNode(1);

    // there should be no alarms in the alarms table
    assertEquals(
        0, m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue());

    // this should be the first occurrence of this alarm
    // there should be 1 alarm now
    sendNodeDownEvent("%nodeid%", node);
    Thread.sleep(1000);
    assertEquals(
        1, m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue());

    // this should be the second occurrence and shouldn't create another row
    // there should still be only 1 alarm
    sendNodeDownEvent("%nodeid%", node);
    Thread.sleep(1000);
    assertEquals(
        1, m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue());

    // this should be a new alarm because of the new key
    // there should now be 2 alarms
    sendNodeDownEvent("DontReduceThis", node);
    Thread.sleep(1000);
    assertEquals(
        2, m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue());

    MockUtil.println("Going for the print of the counter column");
    m_jdbcTemplate.query(
        "select reductionKey, sum(counter) from alarms group by reductionKey",
        new RowCallbackHandler() {
          @Override
          public void processRow(ResultSet rs) throws SQLException {
            MockUtil.println(
                "count for reductionKey: " + rs.getString(1) + " is: " + rs.getObject(2));
          }
        });
  }
Example #9
0
  protected void setUp() throws Exception {
    super.setUp();
    MockUtil.println("------------ Begin Test " + this + " --------------------------");
    MockLogAppender.setupLogging();

    if (m_runSupers) {

      createMockNetwork();

      populateDatabase();

      DataSourceFactory.setInstance(m_db);

      SnmpPeerFactory.setInstance(
          new SnmpPeerFactory(new ByteArrayResource(getSnmpConfig().getBytes())));

      if (isStartEventd()) {
        m_eventdIpcMgr = new EventIpcManagerDefaultImpl();

        JdbcEventdServiceManager eventdServiceManager = new JdbcEventdServiceManager();
        eventdServiceManager.setDataSource(m_db);
        eventdServiceManager.afterPropertiesSet();

        /*
         * Make sure we specify a full resource path since "this" is
         * the unit test class, which is most likely in another package.
         */
        File configFile =
            ConfigurationTestUtils.getFileForResource(
                this, "/org/opennms/netmgt/mock/eventconf.xml");
        DefaultEventConfDao eventConfDao = new DefaultEventConfDao();
        eventConfDao.setConfigResource(new FileSystemResource(configFile));
        eventConfDao.afterPropertiesSet();
        EventconfFactory.setInstance(eventConfDao);

        EventExpander eventExpander = new EventExpander();
        eventExpander.setEventConfDao(eventConfDao);
        eventExpander.afterPropertiesSet();

        JdbcEventWriter jdbcEventWriter = new JdbcEventWriter();
        jdbcEventWriter.setEventdServiceManager(eventdServiceManager);
        jdbcEventWriter.setDataSource(m_db);
        jdbcEventWriter.setGetNextIdString(
            "select nextVal('eventsNxtId')"); // for HSQL: "SELECT max(eventId)+1 from events"
        jdbcEventWriter.afterPropertiesSet();

        EventIpcBroadcastProcessor eventIpcBroadcastProcessor = new EventIpcBroadcastProcessor();
        eventIpcBroadcastProcessor.setEventIpcBroadcaster(m_eventdIpcMgr);
        eventIpcBroadcastProcessor.afterPropertiesSet();

        List<EventProcessor> eventProcessors = new ArrayList<EventProcessor>(3);
        eventProcessors.add(eventExpander);
        eventProcessors.add(jdbcEventWriter);
        eventProcessors.add(eventIpcBroadcastProcessor);

        DefaultEventHandlerImpl eventHandler = new DefaultEventHandlerImpl();
        eventHandler.setEventProcessors(eventProcessors);
        eventHandler.afterPropertiesSet();

        m_eventdIpcMgr.setHandlerPoolSize(5);
        m_eventdIpcMgr.setEventHandler(eventHandler);
        m_eventdIpcMgr.afterPropertiesSet();

        m_eventProxy = m_eventdIpcMgr;

        EventIpcManagerFactory.setIpcManager(m_eventdIpcMgr);

        EventIpcManagerEventHandlerProxy proxy = new EventIpcManagerEventHandlerProxy();
        proxy.setEventIpcManager(m_eventdIpcMgr);
        proxy.afterPropertiesSet();
        List<EventHandler> eventHandlers = new ArrayList<EventHandler>(1);
        eventHandlers.add(proxy);

        TcpEventReceiver tcpEventReceiver = new TcpEventReceiver();
        tcpEventReceiver.setPort(5837);
        tcpEventReceiver.setEventHandlers(eventHandlers);

        UdpEventReceiver udpEventReceiver = new UdpEventReceiver();
        udpEventReceiver.setPort(5837);
        tcpEventReceiver.setEventHandlers(eventHandlers);

        List<EventReceiver> eventReceivers = new ArrayList<EventReceiver>(2);
        eventReceivers.add(tcpEventReceiver);
        eventReceivers.add(udpEventReceiver);

        m_eventd = new Eventd();
        m_eventd.setEventdServiceManager(eventdServiceManager);
        m_eventd.setEventReceivers(eventReceivers);
        m_eventd.setReceiver(new BroadcastEventProcessor(m_eventdIpcMgr, eventConfDao));

        m_eventd.init();
        m_eventd.start();
      }
    }

    m_transMgr = new DataSourceTransactionManager(DataSourceFactory.getInstance());
  }
Example #10
0
  @Before
  public void setUp() throws Exception {
    m_assertLevel = Level.WARN;

    // System.setProperty("mock.logLevel", "DEBUG");
    // System.setProperty("mock.debug", "true");
    MockUtil.println("------------ Begin Test  --------------------------");
    MockLogAppender.setupLogging();

    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.addService("SNMP");
    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.addService("SMTP");
    m_network.addService("SNMP");
    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_network.addNode(4, "DownNode");
    m_network.addInterface("192.168.1.6");
    m_network.addService("SNMP");
    //		m_network.addInterface("fe80:0000:0000:0000:0231:f982:0123:4567");
    //		m_network.addService("SNMP");

    m_db = new MockDatabase();
    m_db.populate(m_network);
    DataSourceFactory.setInstance(m_db);

    //		DemandPollDao demandPollDao = new DemandPollDaoHibernate(m_db);
    //		demandPollDao.setAllocateIdStmt(m_db
    //				.getNextSequenceValStatement("demandPollNxtId"));
    //		m_demandPollDao = demandPollDao;

    m_pollerConfig = new MockPollerConfig(m_network);
    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_eventMgr.setSynchronous(false);

    QueryManager queryManager = new DefaultQueryManager();
    queryManager.setDataSource(m_db);

    DefaultPollContext pollContext = new DefaultPollContext();
    pollContext.setEventManager(m_eventMgr);
    pollContext.setLocalHostName("localhost");
    pollContext.setName("Test.DefaultPollContext");
    pollContext.setPollerConfig(m_pollerConfig);
    pollContext.setQueryManager(queryManager);

    PollableNetwork network = new PollableNetwork(pollContext);

    m_poller = new Poller();
    m_poller.setDataSource(m_db);
    m_poller.setEventManager(m_eventMgr);
    m_poller.setNetwork(network);
    m_poller.setQueryManager(queryManager);
    m_poller.setPollerConfig(m_pollerConfig);
    m_poller.setPollOutagesConfig(m_pollerConfig);

    MockOutageConfig config = new MockOutageConfig();
    config.setGetNextOutageID(m_db.getNextOutageIdStatement());

    RrdUtils.setStrategy(new NullRrdStrategy());

    // m_outageMgr = new OutageManager();
    // m_outageMgr.setEventMgr(m_eventMgr);
    // m_outageMgr.setOutageMgrConfig(config);
    // m_outageMgr.setDbConnectionFactory(m_db);

  }
Example #11
0
  @Test
  @JUnitTemporaryDatabase(tempDbClass = MockDatabase.class)
  public void testPersistManyAlarmsAtOnce() throws InterruptedException {
    int numberOfAlarmsToReduce = 10;

    // there should be no alarms in the alarms table
    assertEquals(
        0, m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue());

    final String reductionKey = "countThese";
    final MockNode node = m_mockNetwork.getNode(1);

    final long millis = System.currentTimeMillis() + 2500;

    final CountDownLatch signal = new CountDownLatch(numberOfAlarmsToReduce);

    for (int i = 1; i <= numberOfAlarmsToReduce; i++) {
      MockUtil.println(
          "Creating Runnable: " + i + " of " + numberOfAlarmsToReduce + " events to reduce.");

      class EventRunner implements Runnable {
        @Override
        public void run() {
          try {
            while (System.currentTimeMillis() < millis) {
              try {
                Thread.sleep(10);
              } catch (InterruptedException e) {
                MockUtil.println(e.getMessage());
              }
            }
            sendNodeDownEvent(reductionKey, node);
          } catch (Throwable t) {
            t.printStackTrace();
          } finally {
            signal.countDown();
          }
        }
      }

      Runnable r = new EventRunner();
      Thread p = new Thread(r);
      p.start();
    }

    signal.await();

    // this should be the first occurrence of this alarm
    // there should be 1 alarm now
    int rowCount =
        m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue();
    Integer counterColumn =
        m_jdbcTemplate
            .queryForObject(
                "select counter from alarms where reductionKey = ?",
                new Object[] {reductionKey},
                Integer.class)
            .intValue();
    MockUtil.println("rowcCount is: " + rowCount + ", expected 1.");
    MockUtil.println("counterColumn is: " + counterColumn + ", expected " + numberOfAlarmsToReduce);
    assertEquals(1, rowCount);
    if (numberOfAlarmsToReduce != counterColumn) {
      final List<Integer> reducedEvents = new ArrayList<Integer>();
      m_jdbcTemplate.query(
          "select eventid from events where alarmID is not null",
          new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
              reducedEvents.add(rs.getInt(1));
            }
          });
      Collections.sort(reducedEvents);

      final List<Integer> nonReducedEvents = new ArrayList<Integer>();
      m_jdbcTemplate.query(
          "select eventid from events where alarmID is null",
          new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
              nonReducedEvents.add(rs.getInt(1));
            }
          });
      Collections.sort(nonReducedEvents);

      fail(
          "number of alarms to reduce ("
              + numberOfAlarmsToReduce
              + ") were not reduced into a single alarm (instead the counter column reads "
              + counterColumn
              + "); "
              + "events that were reduced: "
              + StringUtils.collectionToCommaDelimitedString(reducedEvents)
              + "; events that were not reduced: "
              + StringUtils.collectionToCommaDelimitedString(nonReducedEvents));
    }

    Integer alarmId =
        m_jdbcTemplate
            .queryForObject(
                "select alarmId from alarms where reductionKey = ?",
                new Object[] {reductionKey},
                Integer.class)
            .intValue();
    rowCount =
        m_jdbcTemplate
            .queryForObject(
                "select count(*) from events where alarmid = ?",
                new Object[] {alarmId},
                Integer.class)
            .intValue();
    MockUtil.println(String.valueOf(rowCount) + " of events with alarmid: " + alarmId);
    //      assertEquals(numberOfAlarmsToReduce, rowCount);

    rowCount =
        m_jdbcTemplate
            .queryForObject("select count(*) from events where alarmid is null", Integer.class)
            .intValue();
    MockUtil.println(String.valueOf(rowCount) + " of events with null alarmid");
    assertEquals(0, rowCount);
  }