예제 #1
0
 def testDBtruncate(self):
     ''' Test that the DB tables correctly truncate '''
     # Truncate the tables 
     self.prepare_db()
     teal = Teal('data/aaaa_assumptions_test/minimal.conf', 'stderr', msgLevel=self.msglevel, data_only=True, commit_alerts=False, commit_checkpoints=False)
     self.dbi = registry.get_service(SERVICE_DB_INTERFACE)
     self.cnxn = self.dbi.get_connection()
     self.cursor = self.cnxn.cursor()
     # Check the tables
     self._check_rows(db_interface.TABLE_EVENT_LOG, 0)
     self._check_rows(db_interface.TABLE_ALERT_LOG, 0)
     self._check_rows(db_interface.TABLE_ALERT2ALERT, 0)
     self._check_rows(db_interface.TABLE_CHECKPOINT, 0)
     self.cnxn.close()
     teal.shutdown()
     
     teal = Teal('data/aaaa_assumptions_test/minimal.conf', 'stderr', msgLevel=self.msglevel)
     self.dbi = registry.get_service(SERVICE_DB_INTERFACE)
     self.cnxn = self.dbi.get_connection()
     self.cursor = self.cnxn.cursor()
     # Check the tables
     self._check_rows(db_interface.TABLE_EVENT_LOG, 0)
     self._check_rows(db_interface.TABLE_ALERT_LOG, 0)
     self._check_rows(db_interface.TABLE_ALERT2ALERT, 0)
     self._check_rows(db_interface.TABLE_CHECKPOINT, 1)
     self.cnxn.close()
     teal.shutdown()
예제 #2
0
파일: teal_test.py 프로젝트: ppjsand/pyteal
class TealTestDemoEventQ(TealTestCase):
    '''Test the demo configuration ''' 
        
    def testDemo1EventQ(self):
        '''Test that the first demo flow works -- Inject Event Q'''
        self.teal = Teal('data/teal_test/configurationtest_05_auto.conf', 'stderr', msgLevel=self.msglevel, 
                         commit_alerts=False, commit_checkpoints=False, run_mode=TEAL_RUN_MODE_HISTORIC)
        j_in = Journal('j_in', file='data/demo/data_sample_demo_NEW_001.json')
        j_out_aaq = Journal('j_out_aaq')
        j_out_dq = Journal('j_out_dq')
        j_out_lis = Journal('j_out_lis')
        q_in = registry.get_service(SERVICE_EVENT_Q)
        q_out_aaq = registry.get_service(SERVICE_ALERT_ANALYZER_Q)
        q_out_dq = registry.get_service(SERVICE_ALERT_DELIVERY_Q)
        q_out_dq.register_listener(j_out_dq)
        q_out_aaq.register_listener(j_out_aaq)
        listeners = get_service(SERVICE_ALERT_DELIVERY).listeners
        for listener in listeners:
            if listener.get_name() == 'outputJournal':
                j_out_lis = listener.journal
        j_in.inject_queue(q_in)
        self.assertTrue(j_out_lis.wait_for_entries(3))
        j_exp_aaq = Journal('j_exp_aaq', 'data/teal_test/data_sample_demo_NEW_001_AAQ_Result.json')
        self.assertTrue(j_out_aaq.deep_match(j_exp_aaq, ignore_delay=True, ignore_times=True))
        j_exp_dq = Journal('j_exp_dq', 'data/teal_test/data_sample_demo_NEW_001_DQ_Result.json')
        self.assertTrue(j_out_dq.deep_match(j_exp_dq, ignore_delay=True, ignore_times=True))
        j_exp_lis = Journal('j_exp_lis', 'data/teal_test/data_sample_demo_NEW_001_LIS_Result.json')
        self.assertTrue(j_out_lis.deep_match(j_exp_lis, ignore_delay=True, ignore_times=True))
        
        q_out_aaq.unregister_listener(j_out_aaq)        
        q_out_dq.unregister_listener(j_out_dq)
        self.teal.shutdown()
예제 #3
0
class JournalTest(TealTestCase):


    def setUp(self):        
        self.teal = Teal('data/common/configurationtest.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=False, commit_checkpoints=False)

    def tearDown(self):
        self.teal.shutdown()

    def testJournalLoadSave(self):
        ''' Test loading and saving a json file ''' 
        Journal('test journal', file='data/journal_test/data_sample_001_NEW.json')
        # TODO: Clean up the file after it gets created
        #j.save('data/journal_test/data_sample_001_NEW_AUTO_OUT.json')
        return
    
    def testJournalQueue(self):
        ''' Test injecting the Journal through a queue'''
        lq = ListenableQueue('test journal queue')
        j = Journal('test journal -- input', file='data/journal_test/data_sample_001_NEW.json')
        j_rec = Journal('j_rec -- output')
        lq.register_listener(j_rec)
        j.inject_queue(lq)
        while len(j) != len(j_rec):
            # p rint ('waiting for queue to process %s of %s' % (str(len(j_rec)), str(len(j))))
            sleep(1.0)
        #p rint j
        #p rint j_rec
        self.assertTrue(j.deep_match(j))
        self.assertTrue(j.deep_match(j_rec, ignore_delay=True))
        return
예제 #4
0
class AlertTestDuplicateSupport(TealTestCase):
    
    def testDisableDup(self):
        ''' Test That disable dup works'''
        self.teal = Teal('data/common/configurationtest.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=False, commit_checkpoints=False)
        am = get_service(SERVICE_ALERT_MGR)
        self.assertEqual(len(am.in_mem_alerts), 0)
        self.assertEqual(len(am.in_mem_alerts_duplicate), 0)
        self.assertEqual(len(am.active_alerts_open), 0)
        create_teal_alert('XXXXXXXX', 'no reason at all', 'medium well', loc_instance="YYY")
        self.assertEqual(len(am.in_mem_alerts), 1)
        self.assertEqual(len(am.in_mem_alerts_duplicate), 0)
        self.assertEqual(len(am.active_alerts_open), 1)
        create_teal_alert('XXXXXXXX', 'no reason at all', 'medium well', loc_instance="YYY")
        self.assertEqual(len(am.in_mem_alerts), 2)
        self.assertEqual(len(am.in_mem_alerts_duplicate), 1)
        self.assertEqual(len(am.active_alerts_open), 1)
        create_teal_alert('XXXXXXXX', 'no reason at all', 'medium well', loc_instance="YYY", disable_dup=True)
        self.assertEqual(len(am.in_mem_alerts), 3)
        self.assertEqual(len(am.in_mem_alerts_duplicate), 1)
        self.assertEqual(len(am.active_alerts_open), 2)
        create_teal_alert('XXXXXXXX', 'no reason at all', 'medium well', loc_instance="YYY", disable_dup=True)
        self.assertEqual(len(am.in_mem_alerts), 4)
        self.assertEqual(len(am.in_mem_alerts_duplicate), 1)
        self.assertEqual(len(am.active_alerts_open), 3)
        create_teal_alert('XXXXXXXX', 'no reason at all', 'medium well', loc_instance="YYY")
        self.assertEqual(len(am.in_mem_alerts), 5)
        self.assertEqual(len(am.in_mem_alerts_duplicate), 2)
        self.assertEqual(len(am.active_alerts_open), 3)
        self.teal.shutdown()
        return
예제 #5
0
class JournalTestWithOptionalData(TealTestCase):


    def setUp(self):
        self.teal = Teal('data/common/configurationtest.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=False, commit_checkpoints=False)

    def tearDown(self):
        self.teal.shutdown()

#    def testJournalLoadSave(self):
#        ''' Test loading and saving a json file with optional fields. ''' 
#        j = Journal('test journal', file='data/journal_test/data_sample_002_NEW.json')
#        j.save('data/journal_test/data_sample_002_NEW_AUTO_OUT.json')
#        return
    
    def testJournalQueue(self):
        ''' Test injecting a Journal containing optional fields through a queue'''
        lq = ListenableQueue('test journal queue')
        j = Journal('test journal', file='data/journal_test/data_sample_002_NEW.json')
        j_rec = Journal('j_rec')
        lq.register_listener(j_rec)
        j.inject_queue(lq)
        while len(j) != len(j_rec):
            # p rint ('waiting for queue to process %s of %s' % (str(len(j_rec)), str(len(j))))
            sleep(1.0)
        #p rint j
        #p rint j_rec
        self.assertTrue(j.deep_match(j))
        self.assertTrue(j.deep_match(j_rec, ignore_delay=True))
        return
예제 #6
0
파일: teal_test.py 프로젝트: ppjsand/pyteal
 def testHistoricOnlyHistoric(self):
     ''' Verify that the historic monitor can only be run in historic mode '''
     self.assertRaisesTealError(TealError, "Historic monitor can only enabled for historic use.  Unsupported value specified: realtime", Teal, 'data/teal_test/bad_hist_mon_01.conf', 'stderr', self.msglevel, 'now', 'realtime')
     self.assertRaisesTealError(TealError, "Historic monitor can only enabled for historic use.  Unsupported value specified: all", Teal, 'data/teal_test/bad_hist_mon_02.conf', 'stderr', self.msglevel, 'now', 'historic')
     # Should run ... clear the DB and run it with no events, so nothing will happen
     self.prepare_db()
     teal = Teal('data/teal_test/bad_hist_mon_03.conf', 'stderr', self.msglevel, 'now', 'historic')
     teal.shutdown()
예제 #7
0
파일: teal_test.py 프로젝트: ppjsand/pyteal
 def testEnabledHistoric(self):
     ''' Verify that historic indicator on enabled plug-in control works '''
     teal = Teal('data/teal_test/good_01.conf', 'stderr', msgLevel=self.msglevel, run_mode='historic', commit_alerts=False, commit_checkpoints=False)
     event_q_lsrs = registry.get_service(SERVICE_EVENT_Q).listener_methods
     self.assertEquals(len(event_q_lsrs), 1)
     names = []
     for lsr_m in event_q_lsrs:
         names.append(lsr_m.__self__.get_name())
     self.assertTrue('AnalyzerTest055a' in names)
     teal.shutdown()
예제 #8
0
 def setUp(self):
     global VFYRULE
     t = Teal('data/tlcommands_test/test.conf',data_only=True)
     teal_path = registry.get_service(registry.TEAL_ROOT_DIR)
     VFYRULE = os.path.join(teal_path,'bin/tlvfyrule')
     t.shutdown()
     
     tmp_data_dir = os.path.join(os.environ.get('TEAL_ROOT_DIR','/opt/teal'),'data')
     self.teal_data_dir = self.force_env('TEAL_DATA_DIR', tmp_data_dir)
     return
예제 #9
0
파일: teal_test.py 프로젝트: ppjsand/pyteal
class TealTestDemoDB(TealTestCase):
    '''Test the demo configuration '''
    
    def testDemo1DB(self):
        '''Test demo flow by injecting into DB'''
        self.prepare_db()
        keep_var = self.force_env('TEAL_TEST_POOL_TIMERS_OFF', 'YES')
        self.teal = Teal('data/teal_test/configurationtest_05_semaphore_auto.conf', 'stderr', 
                         msgLevel=self.msglevel)

        j_in = Journal('j_in', file='data/demo/data_sample_demo_NEW_001.json')
        j_out_eq = Journal('j_out_eq')
        j_out_aaq = Journal('j_out_aaq')
        j_out_dq = Journal('j_out_dq')
        j_out_lis = Journal('j_out_lis')
        q_out_eq = registry.get_service(SERVICE_EVENT_Q)
        q_out_aaq = registry.get_service(SERVICE_ALERT_ANALYZER_Q)
        q_out_dq = registry.get_service(SERVICE_ALERT_DELIVERY_Q)
        q_out_eq.register_listener(j_out_eq)
        q_out_dq.register_listener(j_out_dq)
        q_out_aaq.register_listener(j_out_aaq)
        listeners = get_service(SERVICE_ALERT_DELIVERY).listeners
        for listener in listeners:
            if listener.get_name() == 'outputJournal':
                j_out_lis = listener.journal
        try:
            j_in.insert_in_db(progress_cb=None, truncate=False, use_rec_ids=True, no_delay=False, post=True)
        except:
            print 'INSERTION FAILED'
            q_out_eq.unregister_listener(j_out_eq)
            q_out_dq.unregister_listener(j_out_dq)
            q_out_aaq.unregister_listener(j_out_aaq)
            raise
       
        # Yes, only 2: Flush can't be injected to connector, so pool does not get closed, so last event
        # Does not get turned into an alert!
        self.assertTrue(j_out_lis.wait_for_entries(2))

        # Note these connector ('C') versions have one less alert
        #     The analyzer is being run in historic mode (see configuration) if that was 
        #     changed to runtime then the pool would time out and the last alert would be journaled
        j_exp_aaq = Journal('j_exp_aaq', 'data/teal_test/data_sample_demo_NEW_001_AAQ_Result_C.json')
        self.assertTrue(j_out_aaq.deep_match(j_exp_aaq, ignore_delay=True, ignore_times=True))
        j_exp_dq = Journal('j_exp_dq', 'data/teal_test/data_sample_demo_NEW_001_DQ_Result_C.json')
        self.assertTrue(j_out_dq.deep_match(j_exp_dq, ignore_delay=True, ignore_times=True))
        j_exp_lis = Journal('j_exp_lis', 'data/teal_test/data_sample_demo_NEW_001_LIS_Result_C.json')
        self.assertTrue(j_out_lis.deep_match(j_exp_lis, ignore_delay=True, ignore_times=True))

        q_out_eq.unregister_listener(j_out_eq)
        q_out_dq.unregister_listener(j_out_dq)
        q_out_aaq.unregister_listener(j_out_aaq)
        
        self.teal.shutdown()
        self.restore_env('TEAL_TEST_POOL_TIMERS_OFF', keep_var)
예제 #10
0
class TEALShutdownModeTest3b(TealTestCase):
    ''' Test setting of shutdown mode works '''
    
    def setUp(self):
        self.t = Teal('data/checkpoint_test/shutdown_immediate.conf', 'stderr', msgLevel=self.msglevel, data_only=True)  
          
    def tearDown(self):
        self.t.shutdown()
    
    def testSettingImmediateShutdownMode(self):
        ''' Test that the shutdown mode to immediate works config '''  
        self.assertEqual(SHUTDOWN_MODE_IMMEDIATE, registry.get_service(registry.SERVICE_SHUTDOWN_MODE))
예제 #11
0
class TEALShutdownModeTest1(TealTestCase):
    ''' Test setting of shutdown mode works '''
    
    def setUp(self):
        self.t = Teal('data/tlcommands_test/test.conf', 'stderr', msgLevel=self.msglevel, data_only=True)  
          
    def tearDown(self):
        self.t.shutdown()
    
    def testDefaultShutdownMode(self):
        ''' Test that the shutdown mode correctly defaults '''  
        self.assertEqual(SHUTDOWN_MODE_DEFERRED, registry.get_service(registry.SERVICE_SHUTDOWN_MODE))
예제 #12
0
class AlertListenerFailureTest(TealTestCase):
    """Test the alert delivery (filters and listeners)"""

    def setUp(self):
        """Setup Teal"""
        # Not testing duplicates, so OK to turn off
        self.keep_ADC = self.force_env("TEAL_ALERT_DUPLICATE_CHECK", "No")
        self.teal = Teal(
            "data/alert_delivery_test/listener_failure/test.conf",
            "stderr",
            msgLevel=self.msglevel,
            commit_alerts=False,
            commit_checkpoints=False,
        )
        return

    def tearDown(self):
        """Teardown teal"""
        self.teal.shutdown()
        self.restore_env("TEAL_ALERT_DUPLICATE_CHECK", self.keep_ADC)
        return

    def testGeneralFilters(self):
        """test alert delivery with global and local filtering"""
        j_in_dq = Journal("j_in_DQ", "data/alert_delivery_test/listener_failure/inject_DQ_alerts.json")
        dq_q = get_service(SERVICE_ALERT_DELIVERY_Q)
        # Get the AlertListenerJournal journals
        listeners = get_service(SERVICE_ALERT_DELIVERY).listeners
        for listener in listeners:
            name = listener.get_name()
            if name == "AllAlerts":
                j_out_all = listener.journal
            if name == "OnlyAnalyzer1":
                j_out_analyzer1 = listener.journal
        # inject
        j_in_dq.inject_queue(dq_q)
        # Create a TEAL alert
        create_teal_alert("XXXXXXXX", "no reason at all", "medium well", loc_instance="YYY")

        # Get expected values
        j_out_all_exp = Journal("all_exp", "data/alert_delivery_test/analyzer_filter/alerts_out_all.json")
        j_out_analyzer1_exp = Journal("analyzer1", "data/alert_delivery_test/analyzer_filter/alerts_out_analyzer1.json")
        # wait for stuff to come out
        self.assertTrue(j_out_all.wait_for_entries(len(j_out_all_exp) + 3))
        self.assertTrue(j_out_analyzer1.wait_for_entries(len(j_out_analyzer1_exp)))
        # Check that it was what was expected

        # Can't really check this because the location is unique for each machine and run
        #  Make sure only 3 extra
        self.assertEqual(len(j_out_all) - len(j_out_all_exp), 3)
        # self.assertTrue(j_out_all.deep_match(j_out_all_exp, ignore_delay=True, ignore_times=True))
        self.assertTrue(j_out_analyzer1.deep_match(j_out_analyzer1_exp, ignore_delay=True, ignore_times=True))
        return
예제 #13
0
class AlertDeliveryTest(TealTestCase):
    """Test the alert delivery (filters and listeners)"""

    def setUp(self):
        """Setup Teal"""
        self.keep_ADC = self.force_env("TEAL_ALERT_DUPLICATE_CHECK", "No")
        self.teal = Teal(
            "data/alert_delivery_test/test_local_filters.conf",
            "stderr",
            msgLevel=self.msglevel,
            commit_alerts=False,
            commit_checkpoints=False,
        )
        return

    def tearDown(self):
        """Teardown teal"""
        self.teal.shutdown()
        self.restore_env("TEAL_ALERT_DUPLICATE_CHECK", self.keep_ADC)
        return

    def testGeneralFilters(self):
        """test alert delivery with global and local filtering"""
        j_in_dq = Journal("j_in_DQ", "data/alert_delivery_test/data_sample_inject_DQ.json")
        # p rint str(j_in_dq)
        dq_q = get_service(SERVICE_ALERT_DELIVERY_Q)
        # Get the AlertListenerJournal journals
        listeners = get_service(SERVICE_ALERT_DELIVERY).listeners
        for listener in listeners:
            name = listener.get_name()
            # p rint name
            if name == "AllAlerts":
                j_out_all = listener.journal
            if name == "OnlyAlertId":
                j_out_alert_id = listener.journal
            if name == "OnlyAlertIdUrgent":
                j_out_ai_urgent = listener.journal
        # inject
        j_in_dq.inject_queue(dq_q)
        # wait for stuff to come out
        self.assertTrue(j_out_all.wait_for_entries(5))
        self.assertTrue(j_out_alert_id.wait_for_entries(3))
        self.assertTrue(j_out_ai_urgent.wait_for_entries(2))
        #
        j_out_all_exp = Journal("j_out_all_exp", "data/alert_delivery_test/data_sample_out_all_alerts.json")
        self.assertTrue(j_out_all.deep_match(j_out_all_exp, ignore_delay=True, ignore_times=True))
        j_out_alert_id_exp = Journal("j_out_alert_id_exp", "data/alert_delivery_test/data_sample_out_alert_id.json")
        self.assertTrue(j_out_alert_id.deep_match(j_out_alert_id_exp, ignore_delay=True, ignore_times=True))
        j_out_ai_urgent_exp = Journal("j_out_ai_urgent_exp", "data/alert_delivery_test/data_sample_out_ai_urgent.json")
        self.assertTrue(j_out_ai_urgent.deep_match(j_out_ai_urgent_exp, ignore_delay=True, ignore_times=True))
        return
예제 #14
0
class TEALShutdownModeTest3a(TealTestCase):
    ''' Test setting of shutdown mode works '''
    
    def setUp(self):
        self.keep_mode = self.force_env(TEAL_SHUTDOWN_MODE, SHUTDOWN_MODE_IMMEDIATE)
        self.t = Teal('data/tlcommands_test/test.conf', 'stderr', msgLevel=self.msglevel, data_only=True)  
          
    def tearDown(self):
        self.t.shutdown()
        self.restore_env(TEAL_SHUTDOWN_MODE, self.keep_mode)
    
    def testSettingImmediateShutdownMode(self):
        ''' Test that the shutdown mode to immediate works ENV '''  
        self.assertEqual(SHUTDOWN_MODE_IMMEDIATE, registry.get_service(registry.SERVICE_SHUTDOWN_MODE))
예제 #15
0
class TEALShutdownModeTest2(TealTestCase):
    ''' Test setting of shutdown mode works '''
    
    def setUp(self):
        self.keep_mode = self.force_env(TEAL_SHUTDOWN_MODE, SHUTDOWN_MODE_DEFERRED)
        self.t = Teal('data/tlcommands_test/test.conf', 'stderr', msgLevel=self.msglevel, data_only=True)  
          
    def tearDown(self):
        self.t.shutdown()
        self.restore_env(TEAL_SHUTDOWN_MODE, self.keep_mode)
    
    def testSettingDeferredShutdownMode(self):
        ''' Test that the shutdown mode being set to deferred works '''  
        self.assertEqual(SHUTDOWN_MODE_DEFERRED, registry.get_service(registry.SERVICE_SHUTDOWN_MODE))
예제 #16
0
class TEALShutdownModeTest4(TealTestCase):
    ''' Test setting of shutdown mode ignored '''
    
    def setUp(self):
        self.keep_mode = self.force_env(TEAL_SHUTDOWN_MODE, SHUTDOWN_MODE_IMMEDIATE)
        self.t = Teal('data/tlcommands_test/test.conf', 'stderr', msgLevel=self.msglevel, data_only=True, run_mode=TEAL_RUN_MODE_HISTORIC, commit_alerts=False)  
          
    def tearDown(self):
        self.t.shutdown()
        self.restore_env(TEAL_SHUTDOWN_MODE, self.keep_mode)
    
    def testSettingImmediateShutdownMode(self):
        ''' Test that the shutdown mode to immediate ignored if HISTORIC '''  
        self.assertEqual(SHUTDOWN_MODE_DEFERRED, registry.get_service(registry.SERVICE_SHUTDOWN_MODE))
예제 #17
0
 def setUp(self): 
     # Setup to use the tllsckpt command
     t = Teal('data/tlcommands_test/test.conf', 'stderr', msgLevel=self.msglevel, data_only=True)
     teal_path = registry.get_service(registry.TEAL_ROOT_DIR)
     self.tllsckpt = os.path.join(teal_path,'bin/tllsckpt')
     t.shutdown()
     
     self.prepare_db()
     
     # Need to have checkpointing to DB off so this doesn't interfer with the other instance of TEAL's checkpoints       
     self.teal = Teal('data/checkpoint_test/noop_monitor.conf','stderr',msgLevel=self.msglevel, commit_checkpoints=False)
     self.db = registry.get_service(registry.SERVICE_DB_INTERFACE)
     self.query = self.db.gen_select(
                  [EVENT_CPF_CHKPT_ID, EVENT_CPF_NAME, EVENT_CPF_STATUS, EVENT_CPF_EVENT_RECID, EVENT_CPF_DATA],
                  db_interface.TABLE_CHECKPOINT)
     self.query_mon_data = self.db.gen_select([EVENT_CPF_DATA], db_interface.TABLE_CHECKPOINT, 
                                              where="${0} = 'monitor_event_queue'".format(EVENT_CPF_NAME), where_fields=[EVENT_CPF_NAME])
     self.truncate = self.db.gen_truncate(db_interface.TABLE_CHECKPOINT)
예제 #18
0
class AlertAnalyzerSubclassTest3(TealTestCase):
    '''Test the alert analyzer can be subclassed '''

    def setUp(self):
        '''Setup Teal'''
        self.keep_ADC = self.force_env('TEAL_ALERT_DUPLICATE_CHECK', 'Yes') 
        self.remove_env('TEAL_ALERT_DUPLICATE_CHECK')   # Get rid of environment variable so configuration will be used
        self.teal = Teal('data/alert_analyzer_test/test02.conf', "stderr", msgLevel=self.msglevel, commit_alerts=False, commit_checkpoints=False)
    
    def tearDown(self):
        '''Teardown teal'''
        self.teal.shutdown()
        self.restore_env('TEAL_ALERT_DUPLICATE_CHECK', self.keep_ADC)

    def testAlertAnalyzerSubclassing01_3(self):
        ''' Test that the alert analyzer subclass works properly and that environment section in configuration works correctly  '''
        # Get the alert listener
        for listener in get_service(SERVICE_ALERT_DELIVERY).listeners:
            if listener.get_name() == 'AllAlerts':
                j_out_all = listener.journal
                
        # Pump in some events
        j_in_event = Journal('j_in_events', 'data/alert_analyzer_test/inject_events01.json')
        j_in_event.inject_queue(get_service(SERVICE_EVENT_Q))
        
        # Check that the analyzer got them -- only len because can't compare locations
        self.assertTrue(j_out_all.wait_for_entries(27))

        # Pump in some alerts
        j_in_alert = Journal('j_in_events', 'data/alert_analyzer_test/inject_alerts01.json')
        j_in_alert.inject_queue(get_service(SERVICE_ALERT_ANALYZER_Q))
        
        # Check that the analyzer got them -- only len because can't compare locations
        self.assertTrue(j_out_all.wait_for_entries(34))

        alertmgr = get_service(SERVICE_ALERT_MGR)
        self.assertEqual(len(alertmgr.in_mem_alerts_duplicate), 0)
예제 #19
0
class TealTestSemaphoreInit(TealTestCase):
    '''Test the situation where TEAL starts and then shuts down immediately without
    processing events. Testing to ensure the monitor semaphore which is touched during
    shutdown is initialized correctly.'''

    
    def setUp(self):
        '''Setup Teal'''
        self.teal = Teal('data/checkpoint_test/configurationtest_05_semaphore.conf', 'stderr', msgLevel=self.msglevel)
        return
    
    def tearDown(self):
        '''Teardown teal'''

        if self.teal is not None:
            self.teal.shutdown()
        return    
    

    def testTealSemaphoreInit(self):
        '''Test checkpoint value after inserting into an empty db.'''
        # If there is a problem, the shutdown process would throw
        # an exception.
        return
예제 #20
0
class EventTest(TealTestCase):
    def setUp(self):
        self.teal = Teal(
            "data/common/configurationtest.conf",
            "stderr",
            msgLevel=self.msglevel,
            commit_alerts=False,
            commit_checkpoints=False,
        )

    def tearDown(self):
        """Nothing to do ... yet
        """
        self.teal.shutdown()

    def testCreateRecIdOnly(self):
        """Test basic event creation
        """
        te1 = Event.fromDict({EVENT_ATTR_REC_ID: 1})
        self.assertEquals(te1.get_rec_id(), 1)
        # Can't use any other get methods, since they will try to
        #  load the Event from the DB is they are not set
        self.assertEquals(te1.event_id, None)
        self.assertEquals(te1.time_occurred, None)
        self.assertEquals(te1.time_logged, None)
        self.assertEquals(te1.src_comp, None)
        self.assertEquals(te1.src_loc, None)
        self.assertEquals(te1.rpt_comp, None)
        self.assertEquals(te1.rpt_loc, None)
        self.assertEquals(te1.event_cnt, None)
        self.assertEquals(te1.elapsed_time, None)
        self.assertEquals(te1.raw_data, None)
        # No associations
        # Not valid because missing required fields
        self.assertFalse(te1.is_valid())
        return

    def testCreateDict(self):
        """Create from a dictionary"""
        right_now = datetime.now()
        crt_dict = {}
        crt_dict[EVENT_ATTR_REC_ID] = 2
        crt_dict[EVENT_ATTR_EVENT_ID] = "Event2"
        crt_dict[EVENT_ATTR_TIME_OCCURRED] = right_now
        crt_dict[EVENT_ATTR_TIME_LOGGED] = right_now + timedelta(seconds=1)
        crt_dict[EVENT_ATTR_SRC_COMP] = "SC"
        crt_dict[EVENT_ATTR_SRC_LOC] = "SCL"
        crt_dict[EVENT_ATTR_SRC_LOC_TYPE] = "S"
        crt_dict[EVENT_ATTR_RPT_COMP] = "RC"
        crt_dict[EVENT_ATTR_RPT_LOC] = "RL"
        crt_dict[EVENT_ATTR_RPT_LOC_TYPE] = "S"
        crt_dict[EVENT_ATTR_EVENT_CNT] = 12
        crt_dict[EVENT_ATTR_ELAPSED_TIME] = 4
        crt_dict[EVENT_ATTR_RAW_DATA_FMT] = long("0x5445535400000001", 16)
        crt_dict[EVENT_ATTR_RAW_DATA] = "When in the course"
        te1 = Event.fromDict(crt_dict)
        self.assertEquals(te1.get_rec_id(), 2)
        # Can't use any other get methods, since they will try to
        #  load the Event from the DB is they are not set
        self.assertEquals(te1.event_id, crt_dict[EVENT_ATTR_EVENT_ID])
        self.assertEquals(te1.time_occurred, crt_dict[EVENT_ATTR_TIME_OCCURRED])
        self.assertEquals(te1.time_logged, crt_dict[EVENT_ATTR_TIME_LOGGED])
        self.assertEquals(te1.src_comp, crt_dict[EVENT_ATTR_SRC_COMP])
        self.assertEquals(te1.src_loc.get_location(), crt_dict[EVENT_ATTR_SRC_LOC])
        self.assertEquals(te1.src_loc.get_id(), crt_dict[EVENT_ATTR_SRC_LOC_TYPE])
        self.assertEquals(te1.rpt_comp, crt_dict[EVENT_ATTR_RPT_COMP])
        self.assertEquals(te1.rpt_loc.get_location(), crt_dict[EVENT_ATTR_RPT_LOC])
        self.assertEquals(te1.rpt_loc.get_id(), crt_dict[EVENT_ATTR_RPT_LOC_TYPE])
        self.assertEquals(te1.event_cnt, crt_dict[EVENT_ATTR_EVENT_CNT])
        self.assertEquals(te1.elapsed_time, crt_dict[EVENT_ATTR_ELAPSED_TIME])
        self.assertEquals(te1.raw_data[EXT_DATA_RAW_DATA], crt_dict[EVENT_ATTR_RAW_DATA])
        # No associations
        # Valid because has all required fields
        self.assertTrue(te1.is_valid())
        # Reuse local
        crt_dict[EVENT_ATTR_REC_ID] = 5
        te1 = Event.fromDict(crt_dict)
        self.assertEquals(te1.get_rec_id(), 5)
        # Can't use any other get methods, since they will try to
        #  load the Event from the DB is they are not set
        self.assertEquals(te1.event_id, crt_dict[EVENT_ATTR_EVENT_ID])
        self.assertEquals(te1.time_occurred, crt_dict[EVENT_ATTR_TIME_OCCURRED])
        self.assertEquals(te1.time_logged, crt_dict[EVENT_ATTR_TIME_LOGGED])
        self.assertEquals(te1.src_comp, crt_dict[EVENT_ATTR_SRC_COMP])
        self.assertEquals(te1.src_loc.get_location(), crt_dict[EVENT_ATTR_SRC_LOC])
        self.assertEquals(te1.src_loc.get_id(), crt_dict[EVENT_ATTR_SRC_LOC_TYPE])
        self.assertEquals(te1.rpt_comp, crt_dict[EVENT_ATTR_RPT_COMP])
        self.assertEquals(te1.rpt_loc.get_location(), crt_dict[EVENT_ATTR_RPT_LOC])
        self.assertEquals(te1.rpt_loc.get_id(), crt_dict[EVENT_ATTR_RPT_LOC_TYPE])
        self.assertEquals(te1.event_cnt, crt_dict[EVENT_ATTR_EVENT_CNT])
        self.assertEquals(te1.elapsed_time, crt_dict[EVENT_ATTR_ELAPSED_TIME])
        self.assertEquals(te1.raw_data[EXT_DATA_RAW_DATA], crt_dict[EVENT_ATTR_RAW_DATA])
        # No associations
        # Not valid because has all required fields
        self.assertTrue(te1.is_valid())
        return

    def testCreateTuple1(self):
        """Create from a tuple"""
        right_now = datetime.now()
        crt_dict = {}
        crt_dict[EVENT_ATTR_REC_ID] = 2
        crt_dict[EVENT_ATTR_EVENT_ID] = "Event2"
        crt_dict[EVENT_ATTR_TIME_OCCURRED] = right_now
        crt_dict[EVENT_ATTR_TIME_LOGGED] = right_now + timedelta(seconds=1)
        crt_dict[EVENT_ATTR_SRC_COMP] = "SC"
        crt_dict[EVENT_ATTR_SRC_LOC] = "SCL"
        crt_dict[EVENT_ATTR_SRC_LOC_TYPE] = "S"
        crt_dict[EVENT_ATTR_RPT_COMP] = "RC"
        crt_dict[EVENT_ATTR_RPT_LOC] = "RL"
        crt_dict[EVENT_ATTR_RPT_LOC_TYPE] = "S"
        crt_dict[EVENT_ATTR_EVENT_CNT] = None
        crt_dict[EVENT_ATTR_ELAPSED_TIME] = None
        crt_dict[EVENT_ATTR_RAW_DATA_FMT] = long("0x5445535400000001", 16)
        crt_dict[EVENT_ATTR_RAW_DATA] = "When in the course"
        in_tuple = (
            crt_dict[EVENT_ATTR_REC_ID],
            crt_dict[EVENT_ATTR_EVENT_ID],
            crt_dict[EVENT_ATTR_TIME_OCCURRED],
            crt_dict[EVENT_ATTR_TIME_LOGGED],
            crt_dict[EVENT_ATTR_SRC_COMP],
            crt_dict[EVENT_ATTR_SRC_LOC],
            crt_dict[EVENT_ATTR_SRC_LOC_TYPE],
            crt_dict[EVENT_ATTR_RPT_COMP],
            crt_dict[EVENT_ATTR_RPT_LOC],
            crt_dict[EVENT_ATTR_RPT_LOC_TYPE],
            crt_dict[EVENT_ATTR_EVENT_CNT],
            crt_dict[EVENT_ATTR_ELAPSED_TIME],
            crt_dict[EVENT_ATTR_RAW_DATA_FMT],
            crt_dict[EVENT_ATTR_RAW_DATA],
        )

        te1 = Event.fromDB(in_tuple)
        self.assertEquals(te1.get_rec_id(), 2)
        # Can't use any other get methods, since they will try to
        #  load the Event from the DB is they are not set
        self.assertEquals(te1.event_id, crt_dict[EVENT_ATTR_EVENT_ID])
        self.assertEquals(te1.time_occurred, crt_dict[EVENT_ATTR_TIME_OCCURRED])
        self.assertEquals(te1.time_logged, crt_dict[EVENT_ATTR_TIME_LOGGED])
        self.assertEquals(te1.src_comp, crt_dict[EVENT_ATTR_SRC_COMP])
        self.assertEquals(te1.src_loc.get_location(), crt_dict[EVENT_ATTR_SRC_LOC])
        self.assertEquals(te1.src_loc.get_id(), crt_dict[EVENT_ATTR_SRC_LOC_TYPE])
        self.assertEquals(te1.rpt_comp, crt_dict[EVENT_ATTR_RPT_COMP])
        self.assertEquals(te1.rpt_loc.get_location(), crt_dict[EVENT_ATTR_RPT_LOC])
        self.assertEquals(te1.rpt_loc.get_id(), crt_dict[EVENT_ATTR_RPT_LOC_TYPE])
        self.assertEquals(te1.event_cnt, crt_dict[EVENT_ATTR_EVENT_CNT])
        self.assertEquals(te1.elapsed_time, crt_dict[EVENT_ATTR_ELAPSED_TIME])
        self.assertEquals(te1.raw_data[EXT_DATA_RAW_DATA], crt_dict[EVENT_ATTR_RAW_DATA])
        # No associations
        # Valid because has all required fields
        self.assertTrue(te1.is_valid())
        # Reuse local
        crt_dict[EVENT_ATTR_REC_ID] = 5
        te1 = Event.fromDict(crt_dict)
        self.assertEquals(te1.get_rec_id(), 5)
        # Can't use any other get methods, since they will try to
        #  load the Event from the DB is they are not set
        self.assertEquals(te1.event_id, crt_dict[EVENT_ATTR_EVENT_ID])
        self.assertEquals(te1.time_occurred, crt_dict[EVENT_ATTR_TIME_OCCURRED])
        self.assertEquals(te1.time_logged, crt_dict[EVENT_ATTR_TIME_LOGGED])
        self.assertEquals(te1.src_comp, crt_dict[EVENT_ATTR_SRC_COMP])
        self.assertEquals(te1.src_loc.get_location(), crt_dict[EVENT_ATTR_SRC_LOC])
        self.assertEquals(te1.src_loc.get_id(), crt_dict[EVENT_ATTR_SRC_LOC_TYPE])
        self.assertEquals(te1.rpt_comp, crt_dict[EVENT_ATTR_RPT_COMP])
        self.assertEquals(te1.rpt_loc.get_location(), crt_dict[EVENT_ATTR_RPT_LOC])
        self.assertEquals(te1.rpt_loc.get_id(), crt_dict[EVENT_ATTR_RPT_LOC_TYPE])
        self.assertEquals(te1.event_cnt, crt_dict[EVENT_ATTR_EVENT_CNT])
        self.assertEquals(te1.elapsed_time, crt_dict[EVENT_ATTR_ELAPSED_TIME])
        self.assertEquals(te1.raw_data[EXT_DATA_RAW_DATA], crt_dict[EVENT_ATTR_RAW_DATA])
        # No associations
        # Not valid because has all required fields
        self.assertTrue(te1.is_valid())
        return

    def testCreateTuple2(self):
        """Create from a tuple"""
        right_now = datetime.now()
        crt_dict = {}
        crt_dict[EVENT_ATTR_REC_ID] = 2
        crt_dict[EVENT_ATTR_EVENT_ID] = "Event2"
        crt_dict[EVENT_ATTR_TIME_OCCURRED] = right_now
        crt_dict[EVENT_ATTR_TIME_LOGGED] = right_now + timedelta(seconds=1)
        crt_dict[EVENT_ATTR_SRC_COMP] = "SC"
        crt_dict[EVENT_ATTR_SRC_LOC] = "SCL"
        crt_dict[EVENT_ATTR_SRC_LOC_TYPE] = "S"
        crt_dict[EVENT_ATTR_RPT_COMP] = "RC"
        crt_dict[EVENT_ATTR_RPT_LOC] = None
        crt_dict[EVENT_ATTR_RPT_LOC_TYPE] = "S"
        crt_dict[EVENT_ATTR_EVENT_CNT] = None
        crt_dict[EVENT_ATTR_ELAPSED_TIME] = None
        crt_dict[EVENT_ATTR_RAW_DATA_FMT] = long("0x5445535400000001", 16)
        crt_dict[EVENT_ATTR_RAW_DATA] = "When in the course"
        in_tuple = (
            crt_dict[EVENT_ATTR_REC_ID],
            crt_dict[EVENT_ATTR_EVENT_ID],
            crt_dict[EVENT_ATTR_TIME_OCCURRED],
            crt_dict[EVENT_ATTR_TIME_LOGGED],
            crt_dict[EVENT_ATTR_SRC_COMP],
            crt_dict[EVENT_ATTR_SRC_LOC],
            crt_dict[EVENT_ATTR_SRC_LOC_TYPE],
            crt_dict[EVENT_ATTR_RPT_COMP],
            crt_dict[EVENT_ATTR_RPT_LOC],
            crt_dict[EVENT_ATTR_RPT_LOC_TYPE],
            crt_dict[EVENT_ATTR_EVENT_CNT],
            crt_dict[EVENT_ATTR_ELAPSED_TIME],
            crt_dict[EVENT_ATTR_RAW_DATA_FMT],
            crt_dict[EVENT_ATTR_RAW_DATA],
        )

        te1 = Event.fromDB(in_tuple)
        self.assertEquals(te1.get_rec_id(), 2)
        # Can't use any other get methods, since they will try to
        #  load the Event from the DB is they are not set
        self.assertEquals(te1.event_id, crt_dict[EVENT_ATTR_EVENT_ID])
        self.assertEquals(te1.time_occurred, crt_dict[EVENT_ATTR_TIME_OCCURRED])
        self.assertEquals(te1.time_logged, crt_dict[EVENT_ATTR_TIME_LOGGED])
        self.assertEquals(te1.src_comp, crt_dict[EVENT_ATTR_SRC_COMP])
        self.assertEquals(te1.src_loc.get_location(), crt_dict[EVENT_ATTR_SRC_LOC])
        self.assertEquals(te1.src_loc.get_id(), crt_dict[EVENT_ATTR_SRC_LOC_TYPE])
        self.assertEquals(te1.rpt_comp, crt_dict[EVENT_ATTR_RPT_COMP])
        self.assertEquals(te1.rpt_loc, None)
        self.assertEquals(te1.event_cnt, crt_dict[EVENT_ATTR_EVENT_CNT])
        self.assertEquals(te1.elapsed_time, crt_dict[EVENT_ATTR_ELAPSED_TIME])
        self.assertEquals(te1.raw_data[EXT_DATA_RAW_DATA], crt_dict[EVENT_ATTR_RAW_DATA])
        # No associations
        # Valid because has all required fields
        self.assertFalse(te1.is_valid())
        return

    def testCreateTuple3(self):
        """Create from a tuple"""
        right_now = datetime.now()
        crt_dict = {}
        crt_dict[EVENT_ATTR_REC_ID] = 2
        crt_dict[EVENT_ATTR_EVENT_ID] = "Event2"
        crt_dict[EVENT_ATTR_TIME_OCCURRED] = right_now
        crt_dict[EVENT_ATTR_TIME_LOGGED] = right_now + timedelta(seconds=1)
        crt_dict[EVENT_ATTR_SRC_COMP] = "SC"
        crt_dict[EVENT_ATTR_SRC_LOC] = "SCL"
        crt_dict[EVENT_ATTR_SRC_LOC_TYPE] = "S"
        crt_dict[EVENT_ATTR_RPT_COMP] = "RC"
        crt_dict[EVENT_ATTR_RPT_LOC] = "RL"
        crt_dict[EVENT_ATTR_RPT_LOC_TYPE] = None
        crt_dict[EVENT_ATTR_EVENT_CNT] = None
        crt_dict[EVENT_ATTR_ELAPSED_TIME] = None
        crt_dict[EVENT_ATTR_RAW_DATA_FMT] = long("0x5445535400000001", 16)
        crt_dict[EVENT_ATTR_RAW_DATA] = "When in the course"
        in_tuple = (
            crt_dict[EVENT_ATTR_REC_ID],
            crt_dict[EVENT_ATTR_EVENT_ID],
            crt_dict[EVENT_ATTR_TIME_OCCURRED],
            crt_dict[EVENT_ATTR_TIME_LOGGED],
            crt_dict[EVENT_ATTR_SRC_COMP],
            crt_dict[EVENT_ATTR_SRC_LOC],
            crt_dict[EVENT_ATTR_SRC_LOC_TYPE],
            crt_dict[EVENT_ATTR_RPT_COMP],
            crt_dict[EVENT_ATTR_RPT_LOC],
            crt_dict[EVENT_ATTR_RPT_LOC_TYPE],
            crt_dict[EVENT_ATTR_EVENT_CNT],
            crt_dict[EVENT_ATTR_ELAPSED_TIME],
            crt_dict[EVENT_ATTR_RAW_DATA_FMT],
            crt_dict[EVENT_ATTR_RAW_DATA],
        )

        te1 = Event.fromDB(in_tuple)
        self.assertEquals(te1.get_rec_id(), 2)
        # Can't use any other get methods, since they will try to
        #  load the Event from the DB is they are not set
        self.assertEquals(te1.event_id, crt_dict[EVENT_ATTR_EVENT_ID])
        self.assertEquals(te1.time_occurred, crt_dict[EVENT_ATTR_TIME_OCCURRED])
        self.assertEquals(te1.time_logged, crt_dict[EVENT_ATTR_TIME_LOGGED])
        self.assertEquals(te1.src_comp, crt_dict[EVENT_ATTR_SRC_COMP])
        self.assertEquals(te1.src_loc.get_location(), crt_dict[EVENT_ATTR_SRC_LOC])
        self.assertEquals(te1.src_loc.get_id(), crt_dict[EVENT_ATTR_SRC_LOC_TYPE])
        self.assertEquals(te1.rpt_comp, crt_dict[EVENT_ATTR_RPT_COMP])
        self.assertEquals(te1.rpt_loc, None)  # Couldn't create
        self.assertEquals(te1.event_cnt, crt_dict[EVENT_ATTR_EVENT_CNT])
        self.assertEquals(te1.elapsed_time, crt_dict[EVENT_ATTR_ELAPSED_TIME])
        self.assertEquals(te1.raw_data[EXT_DATA_RAW_DATA], crt_dict[EVENT_ATTR_RAW_DATA])
        # No associations
        # Valid because has all required fields
        self.assertFalse(te1.is_valid())
        return

    def testEventMatch(self):
        """Test the match method"""
        # Create an event to work with
        right_now = datetime.now()
        crt_dict = {}
        crt_dict[EVENT_ATTR_REC_ID] = 2
        crt_dict[EVENT_ATTR_EVENT_ID] = "Event2"
        crt_dict[EVENT_ATTR_TIME_OCCURRED] = right_now
        crt_dict[EVENT_ATTR_TIME_LOGGED] = right_now + timedelta(seconds=1)
        crt_dict[EVENT_ATTR_SRC_COMP] = "SC"
        crt_dict[EVENT_ATTR_SRC_LOC] = "node##app##pid"
        crt_dict[EVENT_ATTR_SRC_LOC_TYPE] = "S"
        crt_dict[EVENT_ATTR_RPT_COMP] = "RC"
        crt_dict[EVENT_ATTR_RPT_LOC] = "node2##app2##pid"
        crt_dict[EVENT_ATTR_RPT_LOC_TYPE] = "S"
        crt_dict[EVENT_ATTR_EVENT_CNT] = 12
        crt_dict[EVENT_ATTR_ELAPSED_TIME] = 4
        crt_dict[EVENT_ATTR_RAW_DATA_FMT] = long("0x5445535400000001", 16)
        crt_dict[EVENT_ATTR_RAW_DATA] = "When in the course"
        te1 = teal.Event.fromDict(crt_dict)
        # match(event_id, src_comp, src_loc, rpt_loc, scope)
        # Always matches
        self.assertTrue(te1.match(None, None, None, None, None))
        # Check event id
        self.assertTrue(te1.match("Event2", None, None, None, None))
        self.assertFalse(te1.match("Event3", None, None, None, None))
        # Check src comp
        self.assertTrue(te1.match(None, "SC", None, None, None))
        self.assertFalse(te1.match(None, "RC", None, None, None))
        self.assertTrue(te1.match("Event2", "SC", None, None, None))
        self.assertFalse(te1.match("Event2", "RC", None, None, None))
        self.assertFalse(te1.match("Event3", "SC", None, None, None))
        # Check scr loc
        chk_loc1 = Location("S", "node##app##pid")
        self.assertTrue(te1.match(None, None, chk_loc1, None, None))
        self.assertTrue(te1.match(None, None, chk_loc1, None, "application"))
        self.assertTrue(te1.match(None, None, chk_loc1, None, "node"))
        self.assertTrue(te1.match(None, None, chk_loc1, None, "pid"))
        self.assertTrue(te1.match("Event2", "SC", chk_loc1, None, "node"))
        chk_loc2 = Location("S", "node##app##pid2")
        self.assertFalse(te1.match(None, None, chk_loc2, None, None))
        self.assertTrue(te1.match(None, None, chk_loc2, None, "application"))
        self.assertTrue(te1.match(None, None, chk_loc2, None, "node"))
        self.assertFalse(te1.match(None, None, chk_loc2, None, "pid"))
        self.assertTrue(te1.match("Event2", "SC", chk_loc2, None, "node"))
        self.assertFalse(te1.match("Event2", "SC", chk_loc2, None, "pid"))
        chk_loc3 = Location("C", "MB")
        self.assertFalse(te1.match(None, None, chk_loc3, None, None))
        self.assertFalse(te1.match(None, None, chk_loc3, None, "application"))
        # Check rpt_loc
        chk_loc1r = Location("S", "node2##app2##pid")
        self.assertTrue(te1.match(None, None, None, chk_loc1r, None))
        self.assertTrue(te1.match(None, None, None, chk_loc1r, "application"))
        self.assertTrue(te1.match(None, None, None, chk_loc1r, "node"))
        self.assertTrue(te1.match(None, None, None, chk_loc1r, "pid"))
        self.assertTrue(te1.match(None, None, chk_loc1, chk_loc1r, None))
        self.assertTrue(te1.match(None, None, chk_loc1, chk_loc1r, "application"))
        self.assertTrue(te1.match(None, None, chk_loc1, chk_loc1r, "node"))
        self.assertTrue(te1.match(None, None, chk_loc1, chk_loc1r, "pid"))
        self.assertFalse(te1.match(None, None, chk_loc2, chk_loc1r, "pid"))
        chk_loc2r = Location("S", "node2##app2##pidzzz")
        self.assertFalse(te1.match(None, None, chk_loc1, chk_loc2r, "pid"))
        self.assertTrue(te1.match("Event2", "SC", chk_loc1, chk_loc1r, "node"))
        # Test with no rpt loc
        del crt_dict[EVENT_ATTR_RPT_COMP]
        del crt_dict[EVENT_ATTR_RPT_LOC]
        te2 = Event.fromDict(crt_dict)
        self.assertFalse(te2.match("Event2", "SC", chk_loc1, chk_loc1r, "node"))
        self.assertTrue(te2.match("Event2", "SC", chk_loc1, None, "node"))
        return

    def testCreateBadlocations(self):
        """Create from a dictionary with bad locations"""
        keep_env = self.force_env("TEAL_LOCATION_VALIDATION", "IMMEDIATE")
        right_now = datetime.now()
        crt_dict = {}
        crt_dict[EVENT_ATTR_REC_ID] = 72
        crt_dict[EVENT_ATTR_EVENT_ID] = "Event 7"
        crt_dict[EVENT_ATTR_TIME_OCCURRED] = right_now
        crt_dict[EVENT_ATTR_TIME_LOGGED] = right_now + timedelta(seconds=1)
        crt_dict[EVENT_ATTR_SRC_COMP] = "SC"
        crt_dict[EVENT_ATTR_SRC_LOC] = "Sbad"
        crt_dict[EVENT_ATTR_SRC_LOC_TYPE] = "DABs"
        crt_dict[EVENT_ATTR_RPT_COMP] = "RC"
        crt_dict[EVENT_ATTR_RPT_LOC] = "Rbad"
        crt_dict[EVENT_ATTR_RPT_LOC_TYPE] = "DABr"
        crt_dict[EVENT_ATTR_EVENT_CNT] = 12
        crt_dict[EVENT_ATTR_ELAPSED_TIME] = 4
        crt_dict[EVENT_ATTR_RAW_DATA_FMT] = long("0x5445535400000001", 16)
        crt_dict[EVENT_ATTR_RAW_DATA] = "When in the course"
        te1 = Event.fromDict(crt_dict)
        self.assertEquals(te1.get_rec_id(), 72)
        # Can't use any other get methods, since they will try to
        #  load the Event from the DB is they are not set
        self.assertEquals(te1.event_id, crt_dict[EVENT_ATTR_EVENT_ID])
        self.assertEquals(te1.time_occurred, crt_dict[EVENT_ATTR_TIME_OCCURRED])
        self.assertEquals(te1.time_logged, crt_dict[EVENT_ATTR_TIME_LOGGED])
        self.assertEquals(te1.src_comp, crt_dict[EVENT_ATTR_SRC_COMP])
        self.assertEquals(te1.src_loc, None)
        self.assertEquals(te1.rpt_comp, crt_dict[EVENT_ATTR_RPT_COMP])
        self.assertEquals(te1.rpt_loc, None)
        self.assertEquals(te1.event_cnt, crt_dict[EVENT_ATTR_EVENT_CNT])
        self.assertEquals(te1.elapsed_time, crt_dict[EVENT_ATTR_ELAPSED_TIME])
        self.assertEquals(te1.raw_data[EXT_DATA_RAW_DATA], crt_dict[EVENT_ATTR_RAW_DATA])
        self.restore_env("TEAL_LOCATION_VALIDATION", keep_env)
        return
예제 #21
0
class RestartTest(TealTestCase):

    def setUp(self):
        self.prepare_db()
        
    def tearDown(self):
        self.stop_teal()

    def testRestartBeginEmptyDb(self):  
        ''' Verify we can start up in begin mode with an empty DB
        '''      
        self.restart_with_empty_db('begin')
        
    def testRestartRecoveryEmptyDb(self):
        ''' Verify we can start up in recovery with an empty DB
        '''      
        self.restart_with_empty_db('recovery')

    def testRestartLastProcEmptyDb(self):
        ''' Verify we can start up in lastproc mode with an empty DB
        '''      
        self.restart_with_empty_db('lastproc')

    def testRestartNowEmptyDb(self):
        ''' Verify we can start up in now mode with an empty DB
        '''      
        self.restart_with_empty_db('now')
    
    def testRestartLastProc(self):
        ''' Verify lastproc mode by reading events that have been added after shutdown
        '''
        self.add_entries_before_restart()
        # Add a few more events so we process things
        self.start_teal_no_monitor()
        j_inj = Journal('Pre-populate','data/restart_test/three_events_one.json')
        j_inj.insert_in_db(use_rec_ids=False, no_delay=True)
        self.stop_teal()
        
        # Now start up in lastproc mode and see that we don't get entries ... not checkpoints so acts like Now
        self.start_teal('lastproc')
        j_act = self.find_analyzer().journal
        self.assertFalse(j_act.wait_for_entries(1,seconds=3, msg_mode='quiet'))
        #self.assertTrue(j_act.wait_for_entries(3))
        #j_exp = Journal('After restart','data/restart_test/three_events_one_fromq.json')        
        #self.assertTrue(j_act.deep_match(j_exp,ignore_delay=True))
        j_act.clear()
        
        # Make sure we start getting new entries
        self.inject_new_entries()
        
    def testRestartNow(self):
        ''' Verify now mode by making sure no previous events are read
        '''
        self.add_entries_before_restart()
        
        # Now start up in now mode and make sure we don't get any events
        self.start_teal('now')
        j_act = self.find_analyzer().journal
        self.assertFalse(j_act.wait_for_entries(1,seconds=3, msg_mode='quiet'))
        j_empty = Journal('Empty')        
        self.assertTrue(j_act.deep_match(j_empty,ignore_delay=True))

        # Make sure we start getting new entries
        self.inject_new_entries()
        
    def testRestartBegin(self):
        ''' Verify begin mode by reading all events in the DB
        '''
        self.add_entries_before_restart()

        # Now start up in begin mode and make sure we get ALL the events
        self.start_teal('begin')
        j_act = self.find_analyzer().journal
        j_exp = Journal('After begin','data/restart_test/three_events_one_fromq.json')
        self.assertTrue(j_act.wait_for_entries(3))
        self.assertTrue(j_act.deep_match(j_exp,ignore_delay=True))
        j_act.clear()
        
        # Make sure we start getting new entries
        self.inject_new_entries()

    def testRestartRecovery(self):
        ''' Verify recovery mode by reading events that have been added after shutdown
        '''
        self.add_entries_before_restart()
                
        # Add a few more events so we process things
        self.start_teal_no_monitor()
        j_inj = Journal('Pre-populate','data/restart_test/three_events_one.json')
        j_inj.insert_in_db(use_rec_ids=False, no_delay=True)
        self.stop_teal()
        
        # Now start up in recovery mode and see that we get 3 events
        self.start_teal('recovery')

        j_act = self.find_analyzer().journal
        self.assertFalse(j_act.wait_for_entries(1,seconds=3, msg_mode='quiet'))
        #self.assertTrue(j_act.wait_for_entries(3))
        #j_exp = Journal('After restart','data/restart_test/three_events_one_fromq.json')        
        #self.assertTrue(j_act.deep_match(j_exp,ignore_delay=True))
        j_act.clear()
        
        # Make sure we start getting new entries
        self.inject_new_entries()
        
    def restart_with_empty_db(self,restart):
        ''' Restart TEAL in the specified mode. This assumes that since the 
        database is empty, there will be no events being processed       
        '''
        self.start_teal(restart)
        
        # Make sure that our analyzer doesn't get any events        
        j_act = self.find_analyzer().journal
        j_empty = Journal('Empty')        
        self.assertFalse(j_act.wait_for_entries(1, seconds=5, msg_mode='quiet'))
        self.assertTrue(j_act.deep_match(j_empty,ignore_delay=True))
        
        # Make sure we start getting new events
        self.inject_new_entries()

    def add_entries_before_restart(self, stop_teal=True):
        ''' Add events to TEAL and make sure they are processed
        '''
        self.start_teal('now')     
        # Insert a set of events and process them
        j_act = self.find_analyzer().journal
        j_inj = Journal('Pre-populate','data/restart_test/three_events_one.json')
        j_inj.insert_in_db(no_delay=True, truncate=True)   # Truncate is testing that we handle the ckpt table being destroyed
        registry.get_service(registry.SERVICE_NOTIFIER).post()
        self.assertTrue(j_act.wait_for_entries(3))
        j_exp = Journal('Expected', 'data/restart_test/three_events_one_fromq.json')
        self.assertTrue(j_act.deep_match(j_exp, ignore_delay=True))        
        # Stop this instance of TEAL if requested otherwise it is up
        # to the caller to stop it
        if stop_teal:
            self.stop_teal()

    def inject_new_entries(self,exp_json='data/restart_test/three_events_one_fromq.json',exp_num=3):
        ''' Verify that events still flow through TEAL after startup
        '''
        # Now make sure we start getting new events
        j_inj = Journal('After restart','data/restart_test/three_events_one.json')
        j_inj.insert_in_db(use_rec_ids=False, no_delay=True)
        registry.get_service(registry.SERVICE_NOTIFIER).post()
        j_exp = Journal('Inject New Entries', exp_json)
        j_act = self.find_analyzer().journal
        self.assertTrue(j_act.wait_for_entries(exp_num))
        self.assertTrue(j_act.deep_match(j_exp, ignore_delay=True))   

    def find_analyzer(self):
        ''' Find the Test analyzer that holds onto the journal for processing
        '''
        o = None
        event_q = registry.get_service(registry.SERVICE_EVENT_Q)
        for lm in event_q.listener_methods:
            o = lm.__self__
            if o.get_name() == "RestartAnalyzer":
                break;
        return o
                
    def start_teal(self,restart):
        ''' Start up teal with Journal Analyzer
        '''
        self.t = Teal('data/restart_test/teal_nodelta_noanalyze.conf', logFile='stderr', msgLevel=self.msglevel, restart=restart)

    def start_teal_no_monitor(self):
        ''' start Teal without a monitor so events can be added
        '''
        self.t = Teal('data/common/dbinterfaceonly.conf', logFile='stderr', msgLevel=self.msglevel, commit_checkpoints=False, data_only=True, commit_alerts=False)

    def stop_teal(self):
        ''' Shutdown the currently active TEAL instance
        '''
        try:
            self.t.shutdown()
        except:
            pass
예제 #22
0
class AlertTestStates(TealTestCase):
    
    def testStateIncompleteIM(self):
        ''' Test alert gets created in memory in incomplete state'''
        self.teal = Teal('data/common/configurationtest.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=False, commit_checkpoints=False)
        am = get_service(SERVICE_ALERT_MGR)
        t_alert = am.allocate('TestAlert', {})
        self.assertEquals(t_alert.state, ALERT_STATE_INCOMPLETE)
        # No metadata
        self.assertRaises(KeyError, am.commit, t_alert)
        self.teal.shutdown()
        
    def testStateIncompleteDB(self):
        ''' Test alert gets created in memory in incomplete state'''
        self.teal = Teal('data/alert_test/test.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=True, commit_checkpoints=False)
        am = get_service(SERVICE_ALERT_MGR)
        t_alert = am.allocate('TestAlert', {})
        self.assertEquals(t_alert.state, ALERT_STATE_INCOMPLETE)
        # No metadata
        self.assertRaises(KeyError, am.commit, t_alert)
        self.teal.shutdown()

    def testStateOtherStatesIM(self):
        '''test alert state NEW in memory'''
        self.teal = Teal('data/alert_test/test.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=False, commit_checkpoints=False)

        j_in_dq = Journal('j_in_DQ', 'data/alert_test/inject_DQ_alerts.json')
        tq = ListenableQueue('test LQ')
        ql = CheckAlertStateListener(7)
        tq.register_listener(ql)
        j_in_dq.inject_queue(tq, progress_cb=None, fail_on_invalid=False, no_delay=True)
        ql.my_event.wait()
        self.assertEquals(ql.count, 7)
        ta1 = ql.alerts[0]
        am = get_service(SERVICE_ALERT_MGR)
        # TODO: Should not be hardcoded rec ids after this 
        self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.close, 5)
        self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.close, 6)
        self.assertRaisesTealError(AlertMgrError, 'Current alert state does not allow this operation', am.reopen, ta1.rec_id)
        am.close(ta1.rec_id)
        self.assertEquals(ta1.state, ALERT_STATE_CLOSED)
        self.assertRaisesTealError(AlertMgrError, 'Current alert state does not allow this operation', am.close, ta1.rec_id)
        self.assertRaisesTealError(AlertMgrError, 'Alert with specified record id not found', am.close, 23456)
        self.assertEquals(ql.alerts[1].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[2].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[3].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[4].state, ALERT_STATE_CLOSED)
        self.assertEquals(ql.alerts[5].state, ALERT_STATE_CLOSED)
        self.assertEquals(ql.alerts[6].state, ALERT_STATE_OPEN)
        # reopen it 
        self.assertRaisesTealError(AlertMgrError, 'Alert with specified record id not found', am.reopen, 23456)
        self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.reopen, 5)
        self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.reopen, 6)
        am.reopen(ta1.rec_id)
        self.assertEquals(ta1.state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[1].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[2].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[3].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[4].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[5].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[6].state, ALERT_STATE_OPEN)
        am.close(3)
        self.assertEquals(ta1.state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[1].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[2].state, ALERT_STATE_CLOSED)
        self.assertEquals(ql.alerts[3].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[4].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[5].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[6].state, ALERT_STATE_OPEN)
        am.reopen(3)
        self.assertEquals(ta1.state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[1].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[2].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[3].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[4].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[5].state, ALERT_STATE_OPEN)
        self.assertEquals(ql.alerts[6].state, ALERT_STATE_OPEN)
        self.teal.shutdown()
        return
    
    def testStateOtherStatesDB(self):
        '''test alert state NEW in memory'''
        self.prepare_db()
        self.teal = Teal('data/alert_test/test.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=True, commit_checkpoints=False)

        j_in_dq = Journal('j_in_DQ', 'data/alert_test/inject_DQ_alerts.json')
        tq = ListenableQueue('test LQ')
        ql = CheckAlertStateListener(7)
        tq.register_listener(ql)
        j_in_dq.inject_queue(tq, progress_cb=None, fail_on_invalid=False, no_delay=True)
        ql.my_event.wait()
        self.assertEquals(ql.count, 7)
        ta1 = ql.alerts[0]
        am = get_service(SERVICE_ALERT_MGR)
        self.assertEquals(ta1.state, ALERT_STATE_OPEN)
        # TODO: Really should query to get the recid to use for hardcoded ones in rest of this test case
        self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.close, 5)
        self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.close, 6)
        self.assertRaisesTealError(AlertMgrError, 'Current alert state does not allow this operation', am.reopen, ta1.rec_id)
        am.close(ta1.rec_id)
        # Get duplicates of this one        
        self.assertRaisesTealError(AlertMgrError, 'Current alert state does not allow this operation', am.close, ta1.rec_id)
        self.assertRaisesTealError(AlertMgrError, 'Alert with specified record id not found', am.close, 23456)
        # Note that in memory won't be updated ... only in DB
        # so lets get it from the DB
        dbi = get_service(SERVICE_DB_INTERFACE)
        event_cnxn, cursor =_get_connection(dbi)
        self.assert_alert_closed(dbi, cursor, ta1.rec_id)
        self.assert_alert_open(dbi, cursor, 2)
        self.assert_alert_open(dbi, cursor, 3)
        self.assert_alert_open(dbi, cursor, 4)
        self.assert_alert_closed(dbi, cursor, 5)
        self.assert_alert_closed(dbi, cursor, 6)
        self.assert_alert_open(dbi, cursor, 7)
        self.assertRaisesTealError(AlertMgrError, 'Alert with specified record id not found', am.reopen, 23456)
        self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.reopen, 5)
        self.assertRaisesTealError(AlertMgrError, 'Operation not allowed on duplicate alert', am.reopen, 6)
        # reopen it 
        am.reopen(ta1.rec_id)
        event_cnxn, cursor =_get_connection(dbi, event_cnxn)
        self.assert_alert_open(dbi, cursor, ta1.rec_id)
        self.assert_alert_open(dbi, cursor, 2)
        self.assert_alert_open(dbi, cursor, 3)
        self.assert_alert_open(dbi, cursor, 4)
        self.assert_alert_open(dbi, cursor, 5)
        self.assert_alert_open(dbi, cursor, 6)
        self.assert_alert_open(dbi, cursor, 7)
        am.close(3)
        event_cnxn, cursor =_get_connection(dbi, event_cnxn)
        self.assert_alert_open(dbi, cursor, ta1.rec_id)
        self.assert_alert_open(dbi, cursor, 2)
        self.assert_alert_closed(dbi, cursor, 3)
        self.assert_alert_open(dbi, cursor, 4)
        self.assert_alert_open(dbi, cursor, 5)
        self.assert_alert_open(dbi, cursor, 6)
        self.assert_alert_open(dbi, cursor, 7)
        am.reopen(3)
        event_cnxn, cursor =_get_connection(dbi, event_cnxn)
        self.assert_alert_open(dbi, cursor, ta1.rec_id)
        self.assert_alert_open(dbi, cursor, 2)
        self.assert_alert_open(dbi, cursor, 3)
        self.assert_alert_open(dbi, cursor, 4)
        self.assert_alert_open(dbi, cursor, 5)
        self.assert_alert_open(dbi, cursor, 6)
        self.assert_alert_open(dbi, cursor, 7)
        event_cnxn.close()
        self.teal.shutdown()
        return
    
    def assert_alert_closed(self, dbi, cursor, rec_id):
        ''' Check that the alert for the specified rec_id is closed '''
        dbi.select(cursor, ['state'], db_interface.TABLE_ALERT_LOG, where='$rec_id = ?', where_fields=['rec_id'], parms=(rec_id,))
        row = cursor.fetchone()
        self.assertTrue(row != None)
        self.assertTrue(row[0] != None)
        self.assertEquals(row[0],ALERT_STATE_CLOSED)
        return 
    
    def assert_alert_open(self, dbi, cursor, rec_id):
        ''' Check that the alert for the specified rec_id is closed '''
        dbi.select(cursor, ['state'], db_interface.TABLE_ALERT_LOG, where='$rec_id = ?', where_fields=['rec_id'], parms=(rec_id,))
        row = cursor.fetchone()
        self.assertTrue(row != None)
        self.assertTrue(row[0] != None)
        self.assertEquals(row[0],ALERT_STATE_OPEN)
        return 
예제 #23
0
파일: teal_test.py 프로젝트: ppjsand/pyteal
class TealTest(TealTestCase):

    def testTooManyMonitors(self):
        ''' Verify that Teal fails if more than one monitor is specified'''
        self.assertRaisesTealError(TealError, "There can only be one section called 'event_monitor'", Teal, 'data/teal_test/tealtest_01.conf', 'stderr', self.msglevel,'now','realtime',False, False, None, False, '', False)

    def testTooFewMonitors(self):
        ''' Verify that Teal fails if no monitor is specified'''
        self.assertRaisesTealError(TealError, 
                                   'No monitor configured - must have one monitor configured and enabled',
                                   Teal, 
                                   'data/teal_test/tealtest_03.conf', 'stderr', self.msglevel,'now','realtime',False, False, None, False, '', False)

    def testTooManyDatabases(self):
        ''' Verify that Teal fails if more than one database is specified'''
        #TODO: Need to create a dummy database connection or full db connection usport
        #self.assertRaises(TealError, Teal, 'data/teal_test/tealtest_02.conf', 'stderr')
        pass
    
    def testBadEnabled(self):
        '''Verify failure when plugin section has bad value for enabled keyword'''
        self.assertRaisesTealError(TealError, "Configuration section 'event_monitor.EventMonitorNoop' has an unrecognized value for enabled keyword: 'bad_value'", Teal, 'data/teal_test/bad_02.conf','stderr', self.msglevel,'now','realtime',False, False, None, False, '', False)
    
    def testBadEnvHasConfig(self):
        ''' Verify failure when environment section specifies a config dir '''
        self.assertRaisesTealError(TealError, "Option 'TEAL_CONF_DIR' is not allowed in the 'environment' stanza", Teal, 'data/teal_test/bad_03.conf','stderr', self.msglevel,'now','realtime',False, False, None, False, '', False)
        
    def testBadRunModeValue(self):
        ''' Verify failure if bad runmode is passed '''
        self.assertRaisesTealError(TealError, "Unrecognized run mode specified: bad_mode", Teal, 'data/teal_test/bad_02.conf', 'stderr', self.msglevel, 'now', 'bad_mode')
       
    def testEnabledRealtime(self):
        ''' Verify that realtime indicator on enabled plug-in control works '''
        teal = Teal('data/teal_test/good_01.conf', 'stderr', msgLevel=self.msglevel, run_mode='realtime', commit_alerts=False, commit_checkpoints=False)
        event_q_lsrs = registry.get_service(SERVICE_EVENT_Q).listener_methods
        self.assertEquals(len(event_q_lsrs), 2)
        names = []
        for lsr_m in event_q_lsrs:
            names.append(lsr_m.__self__.get_name())
        self.assertTrue('AnalyzerTest055a' in names)
        self.assertTrue('AnalyzerTest055b' in names)
        teal.shutdown()
    
    def testEnabledHistoric(self):
        ''' Verify that historic indicator on enabled plug-in control works '''
        teal = Teal('data/teal_test/good_01.conf', 'stderr', msgLevel=self.msglevel, run_mode='historic', commit_alerts=False, commit_checkpoints=False)
        event_q_lsrs = registry.get_service(SERVICE_EVENT_Q).listener_methods
        self.assertEquals(len(event_q_lsrs), 1)
        names = []
        for lsr_m in event_q_lsrs:
            names.append(lsr_m.__self__.get_name())
        self.assertTrue('AnalyzerTest055a' in names)
        teal.shutdown()
    
    def testHistoricOnlyHistoric(self):
        ''' Verify that the historic monitor can only be run in historic mode '''
        self.assertRaisesTealError(TealError, "Historic monitor can only enabled for historic use.  Unsupported value specified: realtime", Teal, 'data/teal_test/bad_hist_mon_01.conf', 'stderr', self.msglevel, 'now', 'realtime')
        self.assertRaisesTealError(TealError, "Historic monitor can only enabled for historic use.  Unsupported value specified: all", Teal, 'data/teal_test/bad_hist_mon_02.conf', 'stderr', self.msglevel, 'now', 'historic')
        # Should run ... clear the DB and run it with no events, so nothing will happen
        self.prepare_db()
        teal = Teal('data/teal_test/bad_hist_mon_03.conf', 'stderr', self.msglevel, 'now', 'historic')
        teal.shutdown()
    
    def testRealtimeOnlyRealtime(self):
        ''' Verify that the realtime monitor can only be run in realtime mode '''
        self.assertRaisesTealError(TealError, "Realtime monitor can only enabled for realtime use.  Unsupported value specified: historic", Teal, 'data/teal_test/bad_real_mon_01.conf', 'stderr', self.msglevel, 'now', 'historic')
        self.assertRaisesTealError(TealError, "Realtime monitor can only enabled for realtime use.  Unsupported value specified: all", Teal, 'data/teal_test/bad_real_mon_02.conf', 'stderr', self.msglevel, 'now', 'realtime')
        # Notifier is checked after the enabled check ... so this makes sure enabled check passed
        keep_ev = self.remove_env('TEAL_TEST_NOTIFIER_CONFIG')
        self.assertRaisesTealError(TealError, "RealtimeMonitor requires notifier be specified in the configuration file or as an environment variable", Teal, 'data/teal_test/bad_real_mon_03.conf', 'stderr', self.msglevel, 'now', 'realtime')
        self.restore_env('TEAL_TEST_NOTIFIER_CONFIG', keep_ev)
        
    def testEnvInConfig(self):
        ''' Test that having env in the config works '''
        keep_root_dir = self.remove_env(TEAL_ROOT_DIR)
        keep_log_dir = self.remove_env(TEAL_LOG_DIR)
        keep_data_dir = self.remove_env(TEAL_DATA_DIR)
        self.teal = Teal('data/teal_test/env_test.conf', 'stderr', msgLevel=self.msglevel, data_only=True, commit_alerts=False, commit_checkpoints=False)
        
        root_dir = registry.get_service(registry.TEAL_ROOT_DIR)
        data_dir = registry.get_service(registry.TEAL_DATA_DIR)
        log_dir = registry.get_service(registry.TEAL_LOG_DIR)
        
        tealp = Process(target=check_environment, args=(root_dir, data_dir, log_dir))
        
        tealp.start()
        tealp.join()
        
        self.teal.shutdown()
        self.restore_env(TEAL_ROOT_DIR, keep_root_dir)
        self.restore_env(TEAL_LOG_DIR, keep_log_dir)
        self.restore_env(TEAL_DATA_DIR, keep_data_dir)
        
        # After everything is restored, make sure that the subprocess completed successfully
        self.assertEquals(tealp.exitcode, 0)
예제 #24
0
class AlertTestBasic(TealTestCase):
    
    def setUp(self):
        self.teal = Teal('data/common/configurationtest.conf','stderr',msgLevel=self.msglevel, commit_alerts=False, commit_checkpoints=False)
          
    def tearDown(self):
        '''Nothing to do ... yet
        '''
        self.teal.shutdown()
 
    def testAlertRawDataDict(self):
        ''' test the raw data dictionary support '''
        t_alert = get_service(SERVICE_ALERT_MGR).allocate('TestAlert', {})
        # Test with no raw data
        t_dictO = t_alert.get_raw_data_as_dict()
        self.assertEquals(t_alert.raw_data, None)
        self.assertEquals(len(t_dictO), 1)
        self.assertTrue('non_dict_raw_data' in t_dictO)
        self.assertEquals(t_dictO['non_dict_raw_data'], None)
        t_dictO = None
        # Set raw data to a value 
        t_alert.raw_data = 'stuff'
        t_dict0 = t_alert.get_raw_data_as_dict()
        self.assertEquals(t_alert.raw_data, 'stuff')
        self.assertEquals(len(t_dict0), 1)
        self.assertTrue('non_dict_raw_data' in t_dict0)
        self.assertEquals(t_dict0['non_dict_raw_data'], 'stuff')
        t_dict0['k1'] = 'v1'
        t_alert.set_raw_data_from_dict(t_dict0)
        self.assertEquals(t_alert.raw_data, '{"k1":"v1"}stuff')
        t_dict2 = t_alert.get_raw_data_as_dict()
        self.assertEquals(len(t_dict2), 2)
        self.assertTrue('non_dict_raw_data' in t_dict2)
        self.assertEquals(t_dict2['non_dict_raw_data'], 'stuff')
        self.assertTrue('k1' in t_dict2)
        self.assertEquals(t_dict2['k1'], 'v1')
        t_dict2 = None
        t_dict0['k2'] = 'v2'
        t_alert.set_raw_data_from_dict(t_dict0)
        self.assertEquals(t_alert.raw_data, '{"k2":"v2","k1":"v1"}stuff')
        t_dict2 = t_alert.get_raw_data_as_dict()
        self.assertEquals(len(t_dict2), 3)
        self.assertTrue('non_dict_raw_data' in t_dict2)
        self.assertEquals(t_dict2['non_dict_raw_data'], 'stuff')
        self.assertTrue('k1' in t_dict2)
        self.assertEquals(t_dict2['k1'], 'v1')
        self.assertTrue('k2' in t_dict2)
        self.assertEquals(t_dict2['k2'], 'v2')
        t_dict0 = {'key1':'value1','key3':'value3'}
        t_alert.raw_data = None
        t_dictO = t_alert.get_raw_data_as_dict()
        self.assertEquals(t_alert.raw_data, None)
        self.assertEquals(len(t_dictO), 1)
        self.assertTrue('non_dict_raw_data' in t_dictO)
        self.assertEquals(t_dictO['non_dict_raw_data'], None)
        t_dictO = None
        t_alert.set_raw_data_from_dict(t_dict0)
        self.assertEquals(t_alert.raw_data, '{"key3":"value3","key1":"value1"}')
        t_dict2 = t_alert.get_raw_data_as_dict()
        self.assertEquals(len(t_dict2), 2)
        self.assertTrue('non_dict_raw_data' not in t_dict2)
        self.assertTrue('key1' in t_dict2)
        self.assertEquals(t_dict2['key1'], 'value1')
        self.assertTrue('key3' in t_dict2)
        self.assertEquals(t_dict2['key3'], 'value3') 
        t_dict0 = {'non_dict_raw_data':'when in the course'} 
        t_alert.set_raw_data_from_dict(t_dict0) 
        self.assertEquals(t_alert.raw_data, 'when in the course')
        # Bug test
        t_alert.raw_data = '{"fru_list":"{HFI_DDG,Isolation Procedure,,,},{HFI_CAB,Symbolic Procedure,Uf.i.s-P1-T11-T3,,},{CABCONT,Symbolic Procedure,Uf.i.s-P1-T11-T3,,},{47K1234,FRU,Ug.h.t-P1-R2, 47K1234,YL55555,ABC123,TRMD},{47K1234,FRU,Uf.i.s-P1-R3,47K1234,YL12345,ABC123,TRMD},"}'
        n_dict = t_alert.get_raw_data_as_dict()
        
        return
예제 #25
0
    def generate_event_log(self, event_lines):
        ''' Generate the event data required to log an AMM event
        
        This will return two tuples, the first one is the data for the common event structure and
        the second is for the extended data
        '''
        t = Teal(None, data_only=True, msgLevel='warn', logFile='$TEAL_LOG_DIR/tlammtraphandler.log')        
        db = registry.get_service(registry.SERVICE_DB_INTERFACE)
                
        event_valid = self.is_valid() 
        
        # Generate the event id
        src_comp = 'AMM'
        if event_valid:
            event_id = hex(self.evt_name).upper()[2:].rjust(8,'0')
            
            # Check if this event should be reported or not
            if event_filtered(event_id):
                return
            
            time_occurred = self.date_time
            
            src_loc_type = 'D'
            try:
                src_node_address = socket.gethostbyaddr(self.sys_ip_address)[0]
            except socket.herror:
                src_node_address =  self.sys_ip_address
                  
            if (self.source_id.startswith('BLADE_')):
                tmp_loc = find_blade_name(db, self.sys_ip_address, self.source_id)
                
                # If a node was found, use that instead of the AMM address
                if (tmp_loc != self.source_id):
                    src_loc =  tmp_loc
                else:
                    # Unknown blade - report it as a subcomponent of the AMM
                    src_loc = '{0}##{1}'.format(src_node_address, self.source_id)
            else:
                src_loc = '{0}##{1}'.format(src_node_address, self.source_id)
                
            
            rpt_comp = 'AMM'
            rpt_loc_type = 'A'
            rpt_loc = src_node_address
        else:        
            event_id = AMM_INVALID_EVENT
            time_occurred = datetime.now()
            src_loc_type = 'A'
            src_loc = socket.gethostname()

            rpt_comp = None
            rpt_loc_type = None
            rpt_loc = None
        
        # Generate the extended data
        if event_valid:
            raw_data_fmt = AMM_EXTDATA_FMT
            raw_data = None
            exd = (self.app_id,
                   self.sp_txt_id,
                   self.sys_uuid,
                   self.sys_sern,
                   self.app_type,
                   self.priority,
                   self.msg_text,
                   self.host_contact,
                   self.host_location,
                   self.blade_name,
                   self.blade_sern,
                   self.blade_uuid,
                   self.evt_name,
                   self.source_id,
                   self.call_home_flag,
                   self.sys_ip_address,
                   self.sys_machine_model,
                   self.blade_machine_model)
        else:
            raw_data_fmt = 0
            raw_data = event_lines[0:2048]
            exd = None

        # Generate the common base event
        conn = db.get_connection()
        cursor = conn.cursor()
        
        cbe = (event_id, time_occurred, src_comp, src_loc_type, src_loc, rpt_comp, rpt_loc_type, rpt_loc, raw_data_fmt, raw_data)
        db.insert(cursor,                  
                  [event.EVENT_ATTR_EVENT_ID, 
                   event.EVENT_ATTR_TIME_OCCURRED,
                   event.EVENT_ATTR_SRC_COMP,
                   event.EVENT_ATTR_SRC_LOC_TYPE,
                   event.EVENT_ATTR_SRC_LOC,
                   event.EVENT_ATTR_RPT_COMP,
                   event.EVENT_ATTR_RPT_LOC_TYPE,
                   event.EVENT_ATTR_RPT_LOC,
                   event.EVENT_ATTR_RAW_DATA_FMT,
                   event.EVENT_ATTR_RAW_DATA],
                   db_interface.TABLE_EVENT_LOG,
                   parms=cbe)

        if exd:
            db.insert_dependent(cursor,
                                event.EVENT_ATTR_REC_ID,
                                [AMM_APP_ID,
                                 AMM_SP_TXT_ID,
                                 AMM_SYS_UUID,
                                 AMM_SYS_SERN,
                                 AMM_APP_TYPE,
                                 AMM_PRIORITY,
                                 AMM_MSG_TEXT,
                                 AMM_HOST_CONTACT,
                                 AMM_HOST_LOCATION,
                                 AMM_BLADE_NAME,
                                 AMM_BLADE_SERN,
                                 AMM_BLADE_UUID,
                                 AMM_EVT_NAME,
                                 AMM_SOURCE_ID,
                                 AMM_CALL_HOME_FLAG,
                                 AMM_SYS_IP_ADDRESS,
                                 AMM_SYS_MACHINE_MODEL,
                                 AMM_BLADE_MACHINE_MODEL],
                                 extdata.extdata_fmt2table_name(AMM_EXTDATA_FMT),
                                 parms=exd)
                       
        conn.commit()
        cursor.close()
        conn.close()
        t.shutdown()
        
        # Tell TEAL that a new event has been added
        teal_semaphore.Semaphore().post()        
예제 #26
0
class TealCheckpointShutdownTest(TealTestCase):
    def setUp(self): 
        # Setup to use the tllsckpt command
        t = Teal('data/tlcommands_test/test.conf', 'stderr', msgLevel=self.msglevel, data_only=True)
        teal_path = registry.get_service(registry.TEAL_ROOT_DIR)
        self.tllsckpt = os.path.join(teal_path,'bin/tllsckpt')
        t.shutdown()
        
        self.prepare_db()
        
        # Need to have checkpointing to DB off so this doesn't interfer with the other instance of TEAL's checkpoints       
        self.teal = Teal('data/checkpoint_test/noop_monitor.conf','stderr',msgLevel=self.msglevel, commit_checkpoints=False)
        self.db = registry.get_service(registry.SERVICE_DB_INTERFACE)
        self.query = self.db.gen_select(
                     [EVENT_CPF_CHKPT_ID, EVENT_CPF_NAME, EVENT_CPF_STATUS, EVENT_CPF_EVENT_RECID, EVENT_CPF_DATA],
                     db_interface.TABLE_CHECKPOINT)
        self.query_mon_data = self.db.gen_select([EVENT_CPF_DATA], db_interface.TABLE_CHECKPOINT, 
                                                 where="${0} = 'monitor_event_queue'".format(EVENT_CPF_NAME), where_fields=[EVENT_CPF_NAME])
        self.truncate = self.db.gen_truncate(db_interface.TABLE_CHECKPOINT)

    def tearDown(self):
        self.teal.shutdown()
        
    def testContolledShutdownClean(self):
        # Make sure the checkpoint table has no values
        cnxn = self.db.get_connection()
        cnxn.cursor().execute(self.truncate)
        cnxn.commit()
        
        # Start teal and let it shutdown gracefully
        tealp = Process(target=run_teal, args=(self.msglevel,1.0))
        tealp.start()
        tealp.join()
        
        # Now check the table to be sure we shut down cleanly
        cnxn = self.db.get_connection()
        cursor = cnxn.cursor()
        cursor.execute(self.query)
        
        row = cursor.fetchone()
        self.assertTrue(row)
        self.assertEqual(row[1],'Demo1Analyzer')
        self.assertEqual(row[2],'S')
        self.assertEqual(row[3], None)
        self.assertEqual(row[4], None)
        row = cursor.fetchone()
        self.assertTrue(row)
        self.assertEqual(row[1],'monitor_event_queue')
        self.assertEqual(row[2],'S')
        self.assertEqual(row[3], None)
        self.assertEqual(row[4], 'recovery None')
        row = cursor.fetchone()
        self.assertEqual(row, None)
        self.assertCmdWorks([self.tllsckpt], exp_good_msg='Demo1Analyzer        S  Nonemonitor_event_queue  S  NoneMAX_event_rec_id        None', exp_err_msg='', print_out=False)
        self.assertCmdWorks([self.tllsckpt, '-f', 'brief'], exp_good_msg='Demo1Analyzer        S  Nonemonitor_event_queue  S  NoneMAX_event_rec_id        None', exp_err_msg='', print_out=False)
        self.assertCmdWorks([self.tllsckpt, '-f', 'text'], 
                            exp_good_msg='==================================================='
                                       + 'name : Demo1Analyzer'
                                       + 'status : S'
                                       + 'event_recid : None'
                                       + 'data : None'
                                       + '==================================================='
                                       + 'name : monitor_event_queue'
                                       + 'status : S'
                                       + 'event_recid : None'
                                       + 'data : recovery None'
                                       + '==================================================='
                                       + 'name : MAX_event_rec_id'
                                       + 'status :  '
                                       + 'event_recid : None'
                                       + 'data :', exp_err_msg='', print_out=False)
        self.assertCmdWorks([self.tllsckpt, '-f', 'json'], 
                            exp_good_msg='{"status": "S", "data": null, "name": "Demo1Analyzer", "event_recid": null}'
                                       + '{"status": "S", "data": "recovery None", "name": "monitor_event_queue", "event_recid": null}'
                                       + '{"status": " ", "data": "", "name": "MAX_event_rec_id", "event_recid": null}', exp_err_msg='', print_out=False)
        self.assertCmdWorks([self.tllsckpt, '-f', 'csv'], exp_good_msg='Demo1Analyzer,S,,monitor_event_queue,S,,recovery NoneMAX_event_rec_id, ,,', exp_err_msg='', print_out=False)
        cnxn.close()
        return 
    
    def testControlledShutdownDirty(self):
        # Start TEAL and let it shutdown badly
        tealp = Process(target=run_teal, args=(self.msglevel,1000.0))
        tealp.start()
        try_times = 11
        time.sleep(10)
        while try_times > 0:
            time.sleep(1)
            cnxn = self.db.get_connection()
            cursor = cnxn.cursor()
            cursor.execute(self.query_mon_data)
            row = cursor.fetchone()
            if row is not None:
                try_times = 0
            else:
                try_times -= 1
            #cnxn.close()  <<<< uncommenting changing the timing enough that it stops working!
        tealp.terminate()
        tealp.join()
        
        # Now check the table to be sure we shut down cleanly
        cnxn = self.db.get_connection()
        cursor = cnxn.cursor()
        cursor.execute(self.query)
        
        row = cursor.fetchone()
        self.assertEqual(row[1],'Demo1Analyzer')
        self.assertEqual(row[2],'R')
        self.assertEqual(row[3], None)
        self.assertEqual(row[4], None)
        row = cursor.fetchone()
        self.assertEqual(row[1],'monitor_event_queue')
        self.assertEqual(row[2],'R')
        self.assertEqual(row[3], None)
        self.assertEqual(row[4], 'recovery None')  
        row = cursor.fetchone()
        self.assertEqual(row, None)
        self.assertCmdWorks([self.tllsckpt], exp_good_msg='Demo1Analyzer        R  Nonemonitor_event_queue  R  NoneMAX_event_rec_id        None', exp_err_msg='', print_out=False)
        self.assertCmdWorks([self.tllsckpt, '-f', 'brief'], exp_good_msg='Demo1Analyzer        R  Nonemonitor_event_queue  R  NoneMAX_event_rec_id        None', exp_err_msg='', print_out=False)
        self.assertCmdWorks([self.tllsckpt, '-f', 'text'], 
                            exp_good_msg='==================================================='
                                       + 'name : Demo1Analyzer'
                                       + 'status : R'
                                       + 'event_recid : None'
                                       + 'data : None'
                                       + '==================================================='
                                       + 'name : monitor_event_queue'
                                       + 'status : R'
                                       + 'event_recid : None'
                                       + 'data : recovery None'
                                       + '==================================================='
                                       + 'name : MAX_event_rec_id'
                                       + 'status :  '
                                       + 'event_recid : None'
                                       + 'data :', exp_err_msg='', print_out=False)
        self.assertCmdWorks([self.tllsckpt, '-f', 'json'], 
                            exp_good_msg='{"status": "R", "data": null, "name": "Demo1Analyzer", "event_recid": null}'
                                       + '{"status": "R", "data": "recovery None", "name": "monitor_event_queue", "event_recid": null}'
                                       + '{"status": " ", "data": "", "name": "MAX_event_rec_id", "event_recid": null}', exp_err_msg='', print_out=False)
        self.assertCmdWorks([self.tllsckpt, '-f', 'csv'], exp_good_msg='Demo1Analyzer,R,,monitor_event_queue,R,,recovery NoneMAX_event_rec_id, ,,', exp_err_msg='', print_out=False)
        cnxn.close()
        return 
예제 #27
0
def run_teal(msglevel, sleep_amount):
    teal = Teal('data/checkpoint_test/configurationtest_05_semaphore.conf', 'stderr', msgLevel=msglevel)
    time.sleep(sleep_amount)
    teal.shutdown()
    return
예제 #28
0
class AlertAnalyzerFilterTest(TealTestCase):
    """Test the alert delivery (filters and listeners)"""

    def setUp(self):
        """Setup Teal"""
        # Not testing duplicates, so OK to turn off
        self.keep_ADC = self.force_env("TEAL_ALERT_DUPLICATE_CHECK", "No")
        self.teal = Teal(
            "data/alert_delivery_test/analyzer_filter/test.conf",
            "stderr",
            msgLevel=self.msglevel,
            commit_alerts=False,
            commit_checkpoints=False,
        )
        return

    def tearDown(self):
        """Teardown teal"""
        self.teal.shutdown()
        self.restore_env("TEAL_ALERT_DUPLICATE_CHECK", self.keep_ADC)
        return

    def testGeneralFilters(self):
        """test alert delivery with global and local filtering"""
        j_in_dq = Journal("j_in_DQ", "data/alert_delivery_test/analyzer_filter/inject_DQ_alerts.json")
        dq_q = get_service(SERVICE_ALERT_DELIVERY_Q)
        # Get the AlertListenerJournal journals
        listeners = get_service(SERVICE_ALERT_DELIVERY).listeners
        for listener in listeners:
            name = listener.get_name()
            if name == "AllAlerts":
                j_out_all = listener.journal
            if name == "OnlyAnalyzer1":
                j_out_analyzer1 = listener.journal
            if name == "AnyButAnalyzer1":
                j_out_not_analyzer1 = listener.journal
            if name == "OnlyAnalyzer2and3":
                j_out_analyzer2and3 = listener.journal
            if name == "AnyButAnalyzer2and3":
                j_out_not_analyzer2and3 = listener.journal
            if name == "AnyButAnalyzer1and2and3":
                j_out_not_analyzer1and2and3 = listener.journal
        # inject
        j_in_dq.inject_queue(dq_q)
        # Get expected values
        j_out_all_exp = Journal("all_exp", "data/alert_delivery_test/analyzer_filter/alerts_out_all.json")
        j_out_analyzer1_exp = Journal("analyzer1", "data/alert_delivery_test/analyzer_filter/alerts_out_analyzer1.json")
        j_out_not_analyzer1_exp = Journal(
            "not_analyzer1", "data/alert_delivery_test/analyzer_filter/alerts_out_not_analyzer1.json"
        )
        j_out_analyzer2and3_exp = Journal(
            "analyzer2and3", "data/alert_delivery_test/analyzer_filter/alerts_out_analyzer2and3.json"
        )
        j_out_not_analyzer2and3_exp = Journal(
            "not_analyzer2and3", "data/alert_delivery_test/analyzer_filter/alerts_out_not_analyzer2and3.json"
        )
        j_out_not_analyzer1and2and3_exp = Journal(
            "not_analyzer1and2and3", "data/alert_delivery_test/analyzer_filter/alerts_out_not_analyzer1and2and3.json"
        )
        # wait for stuff to come out
        self.assertTrue(j_out_all.wait_for_entries(len(j_out_all_exp)))
        self.assertTrue(j_out_analyzer1.wait_for_entries(len(j_out_analyzer1_exp)))
        self.assertTrue(j_out_not_analyzer1.wait_for_entries(len(j_out_not_analyzer1_exp)))
        self.assertTrue(j_out_analyzer2and3.wait_for_entries(len(j_out_analyzer2and3_exp)))
        self.assertTrue(j_out_not_analyzer2and3.wait_for_entries(len(j_out_not_analyzer2and3_exp)))
        self.assertTrue(j_out_not_analyzer1and2and3.wait_for_entries(len(j_out_not_analyzer1and2and3_exp)))
        # Check that it was what was expected
        self.assertTrue(j_out_all.deep_match(j_out_all_exp, ignore_delay=True, ignore_times=True))
        self.assertTrue(j_out_analyzer1.deep_match(j_out_analyzer1_exp, ignore_delay=True, ignore_times=True))
        self.assertTrue(j_out_not_analyzer1.deep_match(j_out_not_analyzer1_exp, ignore_delay=True, ignore_times=True))
        self.assertTrue(j_out_analyzer2and3.deep_match(j_out_analyzer2and3_exp, ignore_delay=True, ignore_times=True))
        self.assertTrue(
            j_out_not_analyzer2and3.deep_match(j_out_not_analyzer2and3_exp, ignore_delay=True, ignore_times=True)
        )
        self.assertTrue(
            j_out_not_analyzer1and2and3.deep_match(
                j_out_not_analyzer1and2and3_exp, ignore_delay=True, ignore_times=True
            )
        )
        return
예제 #29
0
class JournalTestWithDB(TealTestCase):

    def testJournalWriteEventDB1(self):
        ''' Test writing to Event log DB basic
        '''
        self.teal = Teal('data/journal_test/events_001.conf','stderr',msgLevel=self.msglevel)
        j = Journal('DB test journal to write', file='data/journal_test/events_001.json')
        j.insert_in_db(truncate=True, no_delay=True)
        jdb = Journal('DB test journal to read')
        jdb.select_from_db('event')
        self.assertTrue(j.deep_match(jdb, ignore_delay=True, ignore_times=False, ignore_rec_id=False))
        #p rint j
        #p rint jdb
        self.teal.shutdown()
        return
    
    def testJournalWriteEventDB2(self):
        ''' Test writing to Event log DB without recids
        '''
        self.teal = Teal('data/journal_test/events_001.conf','stderr',msgLevel=self.msglevel)
        j = Journal('DB test journal to write', file='data/journal_test/events_001.json')
        j.insert_in_db(truncate=True, use_rec_ids=False, no_delay=True)
        jdb = Journal('DB test journal to read')
        jdb.select_from_db('event')
        self.assertTrue(j.deep_match(jdb, ignore_delay=True, ignore_times=False, ignore_rec_id=True))
        #p rint j
        #p rint jdb
        self.teal.shutdown()
        return
    
    def testJournalWriteEventDB3(self):
        ''' Test reading from event DB with a subset of fields 
        '''
        self.teal = Teal('data/journal_test/events_001.conf','stderr',msgLevel=self.msglevel)
        j = Journal('DB test journal to write', file='data/journal_test/events_001.json')
        j.insert_in_db(truncate=True, use_rec_ids=False, no_delay=True)
        jdb = Journal('DB test journal to read')
        jdb.select_from_db('event', event_fields=[EVENT_ATTR_REC_ID, EVENT_ATTR_EVENT_ID])
        jexp = Journal('DB expected', 'data/journal_test/events_004.json')
        self.assertTrue(jexp.deep_match(jdb, ignore_delay=True, ignore_times=False, ignore_rec_id=True))
        #p rint j
        #p rint jdb
        self.teal.shutdown()
        return

    def testJournalWriteAlertDB1(self):
        ''' Test writing of Alert log DB basic '''
        self.teal = Teal('data/journal_test/events_001.conf','stderr',msgLevel=self.msglevel)
        # Events
        je = Journal('DB test input EVENTS', file='data/journal_test/events_002.json')
        je.insert_in_db(truncate=True, no_delay=True)
        # Alerts
        ja = Journal('DB test input ALERTS', file='data/journal_test/alerts_002.json')
        ja.insert_in_db(truncate=False, no_delay=True)
        # Check events
        jedb = Journal('Read DB test EVENTS')
        jedb.select_from_db('event')
        self.assertTrue(je.deep_match(jedb, ignore_delay=True, ignore_times=False, ignore_rec_id=False))
        #p rint je
        #p rint jedb
        # Check alerts
        jadb = Journal('Read DB test ALERTS')
        jadb.select_from_db('alert')
        self.assertTrue(ja.deep_match(jadb, ignore_delay=True, ignore_times=False, ignore_rec_id=False))
        #p rint ja
        #p rint jadb
        self.teal.shutdown()
        return
    
    def testJournalWriteAlertDB2(self):
        ''' Test getting alerts without associations '''
        self.teal = Teal('data/journal_test/events_001.conf','stderr',msgLevel=self.msglevel)
        # Events
        je = Journal('DB test input EVENTS', file='data/journal_test/events_002.json')
        je.insert_in_db(truncate=True, no_delay=True)
        # Alerts
        ja = Journal('DB test input ALERTS', file='data/journal_test/alerts_002.json')
        ja.insert_in_db(truncate=False, no_delay=True)
        # Check alerts
        jadb = Journal('Read DB test ALERTS')
        jadb.select_from_db('alert', include_alert_assoc=False)
        jaexp = Journal('DB test expected', 'data/journal_test/alerts_003.json')
        self.assertTrue(jaexp.deep_match(jadb, ignore_delay=True, ignore_times=False, ignore_rec_id=False))
        #p rint ja
        #p rint jadb
        self.teal.shutdown()
        return
    
    def testJournalWriteAlertDB3(self):
        ''' Test getting only some fields of an alert '''
        self.teal = Teal('data/journal_test/events_001.conf','stderr',msgLevel=self.msglevel)
        # Events
        je = Journal('DB test input EVENTS', file='data/journal_test/events_002.json')
        je.insert_in_db(truncate=True, no_delay=True)
        # Alerts
        ja = Journal('DB test input ALERTS', file='data/journal_test/alerts_002.json')
        ja.insert_in_db(truncate=False, no_delay=True)
        # Check alerts
        jadb = Journal('Read DB test ALERTS')
        jadb.select_from_db('alert', include_alert_assoc=False, alert_fields=[ALERT_ATTR_REC_ID, ALERT_ATTR_ALERT_ID, ALERT_ATTR_RECOMMENDATION])
        jaexp = Journal('DB test expected', 'data/journal_test/alerts_005.json')
        self.assertTrue(jaexp.deep_match(jadb, ignore_delay=True, ignore_times=False, ignore_rec_id=False))
        #p rint ja
        #p rint jadb
        self.teal.shutdown()
        return
    
    def testJournalWriteAlertDB4(self):
        ''' Test writing of Alert log queue after reading from DB '''
        # This test does not work with duplicate checking -- probably don't want it to 
        keep_ADC = self.force_env('TEAL_ALERT_DUPLICATE_CHECK', 'No')
        self.teal = Teal('data/journal_test/events_002.conf','stderr',msgLevel=self.msglevel)
        # Events
        je = Journal('DB test input EVENTS', file='data/journal_test/events_002.json')
        je.insert_in_db(truncate=True, no_delay=True)
        # Alerts
        ja = Journal('DB test input ALERTS', file='data/journal_test/alerts_002.json')
        ja.insert_in_db(truncate=False, no_delay=True)
        # Check events
        jedb = Journal('Read DB test EVENTS')
        jedb.select_from_db('event')
        self.assertTrue(je.deep_match(jedb, ignore_delay=True, ignore_times=False, ignore_rec_id=False))
        # Check alerts
        jadb = Journal('Read DB test ALERTS')
        jadb.select_from_db('alert')
        self.assertTrue(ja.deep_match(jadb, ignore_delay=True, ignore_times=False, ignore_rec_id=False))
        # Now insert into the Delivery Queue and make sure all come out 
        jadb.inject_queue(get_service(SERVICE_ALERT_DELIVERY_Q), progress_cb=None, fail_on_invalid=False, no_delay=True)
        listeners = get_service(SERVICE_ALERT_DELIVERY).listeners
        for listener in listeners:
            name = listener.get_name()
            if name == 'Journal':
                j_out_all = listener.journal
        self.assertTrue(j_out_all.wait_for_entries(6))
        self.assertTrue(j_out_all.deep_match(jadb, ignore_delay=True, ignore_times=True))
        self.teal.shutdown()
        self.restore_env('TEAL_ALERT_DUPLICATE_CHECK', keep_ADC)
        return
예제 #30
0
class LocationTest(TealTestCase):

    def setUp(self):
        self.teal = Teal('data/location_test/locationtest_01.conf', 'stderr', msgLevel=self.msglevel, commit_alerts=False, commit_checkpoints=False)
                
    def tearDown(self):
        self.teal.shutdown()
        
    def testSimpleGood(self):
        ''' Test good path simple locations '''
        simple1 = Location('TS','a')
        self.assertEqual(simple1.get_location(),'a')
            
        simple2 = Location('TS','a.b')
        self.assertEqual(simple2.get_location(),'a.b')
        
        simple3 = Location('TS','a.b.c')
        self.assertEqual(simple3.get_location(),'a.b.c')
    
    def testComplexGood(self):
        ''' Test good path complex locations '''
        # Minimum tree specified
        complex1 = Location('TC',"R")
        self.assertEqual(complex1.get_location(),'R')
        
        complex2 = Location('TC',"R-H")
        self.assertEqual(complex2.get_location(),'R-H')
        
        # No value allowed
        complex3 = Location('TC',"R-H-JC")
        self.assertEqual(complex3.get_location(),'R-H-JC')

        # min-only specified
        complex4 = Location('TC',"R-H-MM12345")
        self.assertEqual(complex4.get_location(),'R-H-MM12345')
        
        complex4a = Location('TC',"R-H-MM"+str(sys.maxint))
        self.assertEqual(complex4a.get_location(),'R-H-MM'+str(sys.maxint))
        
        # Range specified
        complex5 = Location('TC',"R-H-PS9")
        self.assertEqual(complex5.get_location(),'R-H-PS9')
        
        # max-only specified
        complex6 = Location('TC',"R-H-K0")
        self.assertEqual(complex6.get_location(),'R-H-K0')
        
        # Full tree specified
        complex7 = Location('TC',"R-H-K20-S")
        self.assertEqual(complex7.get_location(),'R-H-K20-S')
        
    def testComplexBad(self):
        ''' Test invalid complex locations '''
        keep_env = self.force_env('TEAL_LOCATION_VALIDATION', 'IMMEDIATE')
        self.assertRaises(KeyError, Location, 'TZ',"R-H")            # Invalid Type of Location
        self.assertRaises(TypeError, Location, 'TC',["R","H"])       # Create with something that is not a string
        self.assertRaises(ValueError, Location, 'TC',"R01")          # No value allowed
        self.assertRaises(ValueError, Location, 'TC',"R-H02")        # No value allowed child
        self.assertRaises(ValueError, Location, 'TC',"R-H-JC10")     # No value allowed leaf
        self.assertRaises(ValueError, Location, 'TC',"R-H-PS")       # No value specified but required             ) 
        self.assertRaises(ValueError, Location, 'TC',"R-H-PS0")      # Below specified min value (min/max specified) 
        self.assertRaises(ValueError, Location, 'TC',"R-H-PS11")     # Above specified max value (min/max specified)
        self.assertRaises(ValueError, Location, 'TC',"R-H-MM4")      # Below specified min value (min-only specified)
        self.assertRaises(ValueError, Location, 'TC',"R-H-K21")      # Above specified max value (max-only specified)
        self.assertRaises(ValueError, Location, 'TC',"R-H-K20-S12")  # No value allowed on leaf after multi-node component      
        self.assertRaises(ValueError, Location, 'TC',"R-H-K20-S-Z")  # Too many components      
        self.assertRaises(ValueError, Location, 'TC',"R-H-X-S")      # Unknown component      
        self.assertRaises(ValueError, Location, 'TC',"R-xH-X-S")     # Bad front end
        self.assertRaises(ValueError, Location, 'TC',"R-Hx1-X-S")    # Bad back end 
        self.restore_env('TEAL_LOCATION_VALIDATION', keep_env)     
        return  
    
    def testComplexBadDefered(self): 
        ''' Test invalid complex locations with validation deferred '''
        cbad1 = Location('TZ', 'R-H')   
        self.assertEqual('TZ: R-H', str(cbad1)) 
        self.assertRaises(KeyError, cbad1.get_substitution_dict)
        self.assertRaises(KeyError, cbad1.get_comp_value, 'dummy')
        self.assertRaises(KeyError, cbad1.new_location_by_scope, 'dummy')
        self.assertEqual('TZ', cbad1.get_id())
        self.assertEqual('R-H', cbad1.get_location())
        cbad2 = Location('TZ', 'R-H')
        cbad3 = Location('TZ', 'R-I')
        self.assertTrue(cbad1 == cbad2)
        self.assertFalse(cbad1 == cbad3)
        self.assertTrue(cbad1.match(cbad2, None))
        self.assertRaises(KeyError, cbad1.match, cbad2, 'dummy')
        return

    def testSimpleMatch(self):
        simple1 = Location('TS','a.b.c')
        simple2 = Location('TS','a.b.z')
        simple3 = Location('TS','x.y.z')
        
        self.assertTrue(simple1.match(simple1))
        
        self.assertFalse(simple1.match(simple2))
        self.assertTrue(simple1.match(simple2,'child'))
        self.assertTrue(simple1.match(simple2,'parent'))

        self.assertFalse(simple1.match(simple3))
        self.assertFalse(simple1.match(simple3,'child'))
        self.assertFalse(simple1.match(simple3,'parent'))

    def testComplexMatch(self):
        ''' Test matching scopes with complex locations '''
        complex1 = Location('TC',"R-H-PS9")
        complex2 = Location('TC',"R-H-K8-S")
        complex3 = Location('TC',"R-H-K9-S")
        
        self.assertTrue(complex1.match(complex1))
        
        self.assertTrue(complex1.match(complex1))
        self.assertFalse(complex1.match(complex2))
        self.assertTrue(complex1.match(complex2,'home'))
        self.assertTrue(complex1.match(complex2,'top'))

        self.assertFalse(complex2.match(complex3))
        
        # Can do different length w/o problems
        complex2 = Location('TC','R-H')
        complex3 = Location('TC','R-H-K9-S')
        self.assertFalse(complex2.match(complex3,'kilroy'))
        self.assertFalse(complex3.match(complex2,'kilroy'))

        
    def testComplexMatchBad(self):
        ''' Test matching scopes with complex locations and errors '''
        simple1 = Location('TS','a.b.c')
        simple2 = Location('TS','a.b.z')
        complex1 = Location('TC',"R-H-PS9")
        
        # Can't mix location types
        self.assertFalse(simple1.match(complex1)) 
                        
        # Can't use an invalid location component
        self.assertRaises(KeyError, simple1.match, simple2,'mom')        
    
    def testSimpleComponentValues(self):
        ''' Test getting the value of a component for simple locations '''
        simple1 = Location('TS','a.b.c')
        self.assertEquals(simple1.get_comp_value('grandchild'),'c')
        self.assertEquals(simple1.get_comp_value('child'),'b')
        self.assertEquals(simple1.get_comp_value('parent'),'a')

    def testComplexComponentValues(self):
        ''' Test getting the value of a component for complex locations '''
        complex1 = Location('TC',"R-H-K8-S")
        self.assertEquals(complex1.get_comp_value('kilroy'),'8')
    
    def testComplexComponentValuesBad(self):
        ''' Test getting the value of a component for complex locations '''
        complex1 = Location('TC',"R-H-K8-S")
        self.assertRaises(ValueError, complex1.get_comp_value, 'home')
        self.assertRaises(ValueError, complex1.get_comp_value, 'jecarey')
    
    def testNewLocationByScope(self):
        ''' Test creating a new location with a given scope '''
        complex1 = Location('TC',"R-H-K8-S")
        complex2 = complex1.new_location_by_scope('kilroy')
        self.assertEquals(complex2,Location('TC','R-H-K8'))

        complex3 = complex1.new_location_by_scope('home')
        complex4 = complex2.new_location_by_scope('home')
        self.assertEquals(complex3,complex4)
    
        self.assertRaises(KeyError, complex1.new_location_by_scope, 'bad')
    
        # Scoping lower than the id will just return the id
        complex5 = complex3.new_location_by_scope('kilroy')
        self.assertEquals(complex3,complex5)
    
    def testMisc(self):
        ''' Test miscellaneous Location capabilities '''
        l1 = Location("TS",'foo')
        l2 = Location("TC",'R-H-K8-S')
        
        self.assertNotEqual(hash(l1),hash(l2))
        self.assertEquals(hash(l1),hash(l1))

        self.assertEqual(l1.get_id(),'TS')
        self.assertEqual(l2.get_id(),'TC')
        
    def testLocationService(self): 
        ''' Test Location Service public functions '''       
        loc_svc = registry.get_service(SERVICE_LOCATION)
        self.assertTrue(loc_svc.is_scope_valid('TC','kilroy'))
        self.assertFalse(loc_svc.is_scope_valid('TC','archerc'))
        
    def testSubstitutionDict(self):
        ''' Test getting the substitution dictionary '''
        l1 = Location("TS",'foo')
        sd1 = l1.get_substitution_dict()
        self.assertEquals(len(sd1.keys()), 1)
        self.assertTrue('parent' in sd1)
        self.assertEquals(sd1['parent'], 'foo')
        l2 = Location("TC",'R-H-K8-S')
        sd2 = l2.get_substitution_dict()
        self.assertEquals(len(sd2.keys()), 4)
        self.assertTrue('kilroy' in sd2)
        self.assertEquals(sd2['kilroy'], 'K8')
        self.assertTrue('src' in sd2)
        self.assertEquals(sd2['src'], 'S')
        self.assertTrue('top' in sd2)
        self.assertEquals(sd2['top'], 'R')
        self.assertTrue('home' in sd2)
        self.assertEquals(sd2['home'], 'H')
        return