Ejemplo n.º 1
0
 def _startComponent(self):
     self.generator = AlertGenerator(self.config)
     try:
         # self.proc.startComponent() causes the flow to stop, Harness.py
         # the method just calls prepareToStart() and waits for ever
         # self.proc.startDaemon() no good for this either ... puts everything
         # on background
         self.generator.prepareToStart() # is method of Harness
     except Exception as ex:
         print ex
         self.fail(str(ex))
     logging.debug("AlertGenerator and its sub-components should be running now ...")
Ejemplo n.º 2
0
class AlertGeneratorTest(unittest.TestCase):
    def setUp(self):
        self.testInit = TestInit(__file__)
        self.testInit.setLogging(logLevel = logging.DEBUG)
        self.testInit.clearDatabase()
        self.testInit.setDatabaseConnection()
        self.testInit.setSchema(customModules = ["WMCore.WMBS",'WMCore.Agent.Database',
                                                 "WMCore.ResourceControl"],
                                useDefault = False)
        self.testDir = self.testInit.generateWorkDir()
        # AlertGenerator instance
        self.generator = None
        
        self.config = getConfig(self.testDir)
         
        self.config.section_("CoreDatabase")
        self.config.CoreDatabase.socket = os.environ.get("DBSOCK")
        self.config.CoreDatabase.connectUrl = os.environ.get("DATABASE")
        
        self.testProcesses = []
        self.testComponentDaemonXml = "/tmp/TestComponent/Daemon.xml" 
                

    def tearDown(self):
        self.testInit.clearDatabase()       
        self.testInit.delWorkDir()
        self.generator = None
        utils.terminateProcesses(self.testProcesses)
        # if the directory and file "/tmp/TestComponent/Daemon.xml" after
        # ComponentsPoller test exist, then delete it
        d = os.path.dirname(self.testComponentDaemonXml)
        if os.path.exists(d):
            shutil.rmtree(d)

        
    def _startComponent(self):
        self.generator = AlertGenerator(self.config)
        try:
            # self.proc.startComponent() causes the flow to stop, Harness.py
            # the method just calls prepareToStart() and waits for ever
            # self.proc.startDaemon() no good for this either ... puts everything
            # on background
            self.generator.prepareToStart()
        except Exception, ex:
            print ex
            self.fail(str(ex))
        print "AlertGenerator and its sub-components should be running now ..."
Ejemplo n.º 3
0
 def _startComponent(self):
     self.generator = AlertGenerator(self.config)
     try:
         # self.proc.startComponent() causes the flow to stop, Harness.py
         # the method just calls prepareToStart() and waits for ever
         # self.proc.startDaemon() no good for this either ... puts everything
         # on background
         self.generator.prepareToStart()
     except Exception, ex:
         print ex
         self.fail(str(ex))
Ejemplo n.º 4
0
 def _startComponent(self):
     self.generator = AlertGenerator(self.config)
     try:
         # self.proc.startComponent() causes the flow to stop, Harness.py
         # the method just calls prepareToStart() and waits for ever
         # self.proc.startDaemon() no good for this either ... puts everything
         # on background
         self.generator.prepareToStart() # is method of Harness
     except Exception as ex:
         print ex
         self.fail(str(ex))
     logging.debug("AlertGenerator and its sub-components should be running now ...")
Ejemplo n.º 5
0
class AlertGeneratorTest(unittest.TestCase):
    def setUp(self):
        self.testInit = TestInit(__file__)
        self.testInit.setLogging(logLevel = logging.DEBUG)
        self.testInit.setDatabaseConnection()
        self.testInit.setSchema(customModules = ["WMCore.WMBS",'WMCore.Agent.Database',
                                                 "WMCore.ResourceControl"],
                                useDefault = False)
        self.testDir = self.testInit.generateWorkDir()
        # AlertGenerator instance
        self.generator = None
        self.config = getConfig(self.testDir)
        self.config.section_("CoreDatabase")
        self.config.CoreDatabase.socket = os.environ.get("DBSOCK")
        self.config.CoreDatabase.connectUrl = os.environ.get("DATABASE")
        self.testComponentDaemonXml = os.path.join(self.testDir, "Daemon.xml")


    def tearDown(self):
        self.testInit.clearDatabase()
        self.testInit.delWorkDir()
        self.generator = None
        # if the directory and file "/tmp/TestComponent/Daemon.xml" after
        # ComponentsPoller test exist, then delete it
        d = os.path.dirname(self.testComponentDaemonXml)
        if os.path.exists(d):
            shutil.rmtree(d)


    def _startComponent(self):
        self.generator = AlertGenerator(self.config)
        try:
            # self.proc.startComponent() causes the flow to stop, Harness.py
            # the method just calls prepareToStart() and waits for ever
            # self.proc.startDaemon() no good for this either ... puts everything
            # on background
            self.generator.prepareToStart() # is method of Harness
        except Exception as ex:
            print ex
            self.fail(str(ex))
        logging.debug("AlertGenerator and its sub-components should be running now ...")


    def _stopComponent(self):
        logging.debug("Going to stop the AlertGenerator ...")
        # stop via component method
        try:
            self.generator.stopAlertGenerator()
        except Exception as ex:
            logging.error(ex)
            self.fail(str(ex))
        logging.debug("AlertGenerator should be stopped now.")


    def testAlertProcessorBasic(self):
        """
        Just tests starting and stopping the component machinery.
        Should start and stop all configured pollers.

        """
        # the generator will run full-fledged pollers that may get triggered
        # to send some alerts. need to consume such in order to avoid clashes
        # further tests
        handler, receiver = utils.setUpReceiver(self.config.Alert.address,
                                                self.config.Alert.controlAddr)
        self._startComponent()
        # test that all poller processes are running
        for poller in self.generator._pollers:
            self.assertTrue(poller.is_alive())
        # just give the pollers some time to run
        time.sleep(5)
        self._stopComponent()
        receiver.shutdown()
        print "%s alerts captured by the way (test %s)." % (len(handler.queue),
                                                            inspect.stack()[0][3])


    def testAllFinalClassPollerImplementations(self):
        """
        Any new end (final) implementation of new poller(s) should be add
        here to test its basic flow chain.

        """
        config = getConfig("/tmp")
        # create some non-sence config section. just need a bunch of values defined
        config.AlertGenerator.section_("bogusPoller")
        # only couch-related pollers require couchURL, this way it'll be used at the
        # other ones as well, should do no harm ; it's just because all pollers are
        # probed here in a single test ...
        config.AlertGenerator.bogusPoller.couchURL = os.getenv("COUCHURL", None)
        config.AlertGenerator.bogusPoller.soft = 5 # [percent]
        config.AlertGenerator.bogusPoller.critical = 50 # [percent]
        config.AlertGenerator.bogusPoller.pollInterval = 0.2  # [second]
        config.AlertGenerator.bogusPoller.period = 0.5
        # currently only CouchErrorsPoller uses this config value
        config.AlertGenerator.bogusPoller.observables = 4000

        # need to create some temp directory, real process and it's
        # Daemon.xml so that is looks like agents component process
        # and check back the information, give its own PID
        pid = os.getpid()
        config.component_("TestComponent")
        d = os.path.dirname(self.testComponentDaemonXml)
        config.TestComponent.componentDir = d
        if not os.path.exists(d):
            os.mkdir(d)
        f = open(self.testComponentDaemonXml, 'w')
        f.write(utils.daemonXmlContent % dict(PID_TO_PUT = pid))
        f.close()

        generator = utils.AlertGeneratorMock(config)
        pollers = []
        for pollerClass in finalPollerClasses:
            p = pollerClass(config.AlertGenerator.bogusPoller, generator)
            # poller may send something during below check(), satisfy sender method
            p.sender = lambda alert: 1 + 1
            pollers.append(p)

        for poller in pollers:
            poller.check()
            if hasattr(poller, "_measurements"):
                mes = poller._measurements
                self.assertEqual(len(mes), 1)
                self.assertTrue(isinstance(mes[0], float))
            if hasattr(poller, "_compMeasurements"):
                for measurements in poller._compMeasurements:
                    self.assertEqual(len(measurements), 1)
                    self.assertTrue(isinstance(measurements[0], float))

        shutil.rmtree(d)
Ejemplo n.º 6
0
class AlertGeneratorTest(unittest.TestCase):
    def setUp(self):
        self.testInit = TestInit(__file__)
        self.testInit.setLogging(logLevel = logging.DEBUG)
        self.testInit.setDatabaseConnection()
        self.testInit.setSchema(customModules = ["WMCore.WMBS",'WMCore.Agent.Database',
                                                 "WMCore.ResourceControl"],
                                useDefault = False)
        self.testDir = self.testInit.generateWorkDir()
        # AlertGenerator instance
        self.generator = None
        self.config = getConfig(self.testDir)
        self.config.section_("CoreDatabase")
        self.config.CoreDatabase.socket = os.environ.get("DBSOCK")
        self.config.CoreDatabase.connectUrl = os.environ.get("DATABASE")
        self.testComponentDaemonXml = os.path.join(self.testDir, "Daemon.xml")


    def tearDown(self):
        self.testInit.clearDatabase()
        self.testInit.delWorkDir()
        self.generator = None
        # if the directory and file "/tmp/TestComponent/Daemon.xml" after
        # ComponentsPoller test exist, then delete it
        d = os.path.dirname(self.testComponentDaemonXml)
        if os.path.exists(d):
            shutil.rmtree(d)


    def _startComponent(self):
        self.generator = AlertGenerator(self.config)
        try:
            # self.proc.startComponent() causes the flow to stop, Harness.py
            # the method just calls prepareToStart() and waits for ever
            # self.proc.startDaemon() no good for this either ... puts everything
            # on background
            self.generator.prepareToStart() # is method of Harness
        except Exception as ex:
            print ex
            self.fail(str(ex))
        logging.debug("AlertGenerator and its sub-components should be running now ...")


    def _stopComponent(self):
        logging.debug("Going to stop the AlertGenerator ...")
        # stop via component method
        try:
            self.generator.stopAlertGenerator()
        except Exception as ex:
            logging.error(ex)
            self.fail(str(ex))
        logging.debug("AlertGenerator should be stopped now.")


    def testAlertProcessorBasic(self):
        """
        Just tests starting and stopping the component machinery.
        Should start and stop all configured pollers.

        """
        # the generator will run full-fledged pollers that may get triggered
        # to send some alerts. need to consume such in order to avoid clashes
        # further tests
        handler, receiver = utils.setUpReceiver(self.config.Alert.address,
                                                self.config.Alert.controlAddr)
        self._startComponent()
        # test that all poller processes are running
        for poller in self.generator._pollers:
            self.assertTrue(poller.is_alive())
        # just give the pollers some time to run
        time.sleep(5)
        self._stopComponent()
        receiver.shutdown()
        print "%s alerts captured by the way (test %s)." % (len(handler.queue),
                                                            inspect.stack()[0][3])


    def testAllFinalClassPollerImplementations(self):
        """
        Any new end (final) implementation of new poller(s) should be add
        here to test its basic flow chain.

        """
        config = getConfig("/tmp")
        # create some non-sence config section. just need a bunch of values defined
        config.AlertGenerator.section_("bogusPoller")
        # only couch-related pollers require couchURL, this way it'll be used at the
        # other ones as well, should do no harm ; it's just because all pollers are
        # probed here in a single test ...
        config.AlertGenerator.bogusPoller.couchURL = os.getenv("COUCHURL", None)
        config.AlertGenerator.bogusPoller.soft = 5 # [percent]
        config.AlertGenerator.bogusPoller.critical = 50 # [percent]
        config.AlertGenerator.bogusPoller.pollInterval = 0.2  # [second]
        config.AlertGenerator.bogusPoller.period = 0.5
        # currently only CouchErrorsPoller uses this config value
        config.AlertGenerator.bogusPoller.observables = 4000

        # need to create some temp directory, real process and it's
        # Daemon.xml so that is looks like agents component process
        # and check back the information, give its own PID
        pid = os.getpid()
        config.component_("TestComponent")
        d = os.path.dirname(self.testComponentDaemonXml)
        config.TestComponent.componentDir = d
        if not os.path.exists(d):
            os.mkdir(d)
        f = open(self.testComponentDaemonXml, 'w')
        f.write(utils.daemonXmlContent % dict(PID_TO_PUT = pid))
        f.close()

        generator = utils.AlertGeneratorMock(config)
        pollers = []
        for pollerClass in finalPollerClasses:
            p = pollerClass(config.AlertGenerator.bogusPoller, generator)
            # poller may send something during below check(), satisfy sender method
            p.sender = lambda alert: 1 + 1
            pollers.append(p)

        for poller in pollers:
            poller.check()
            if hasattr(poller, "_measurements"):
                mes = poller._measurements
                self.assertEqual(len(mes), 1)
                self.assertTrue(isinstance(mes[0], types.FloatType))
            if hasattr(poller, "_compMeasurements"):
                for measurements in poller._compMeasurements:
                    self.assertEqual(len(measurements), 1)
                    self.assertTrue(isinstance(measurements[0], types.FloatType))

        shutil.rmtree(d)