Esempio n. 1
0
    def setUp(self):
        self.__logFactory = SocketReaderFactory()

        self.comp = None
        self.cnc = None

        MostlyCnCServer.APPENDERS.clear()
        RealComponent.APPENDERS.clear()
Esempio n. 2
0
    def setUp(self):
        self.__logFactory = SocketReaderFactory()

        self.__copyDir = tempfile.mkdtemp()
        self.__runConfigDir = tempfile.mkdtemp()
        self.__spadeDir = tempfile.mkdtemp()

        self.comp = None
        self.cnc = None

        MostlyCnCServer.APPENDERS.clear()
        RealComponent.APPENDERS.clear()
Esempio n. 3
0
    def setUp(self):
        self.__logFactory = SocketReaderFactory()

        self.__runConfigDir = None
Esempio n. 4
0
class TestDAQServer(unittest.TestCase):
    HUB_NUMBER = 1021
    DOM_MAINBOARD_ID = "53494d552101"

    def __createLog(self, name, port, expectStartMsg=True):
        return self.__logFactory.createLog(name, port, expectStartMsg)

    def __getInternetAddress(self):
        return ip.getLocalIpAddr()

    def __verifyRegArray(self, rtnArray, expId, logHost, logPort, liveHost,
                         livePort):
        numElem = 6
        self.assertEquals(
            numElem, len(rtnArray),
            'Expected %d-element array, not %d elements' %
            (numElem, len(rtnArray)))
        self.assertEquals(
            expId, rtnArray["id"],
            'Registration should return client ID#%d, not %d' %
            (expId, rtnArray["id"]))
        self.assertEquals(
            logHost, rtnArray["logIP"],
            'Registration should return loghost %s, not %s' %
            (logHost, rtnArray["logIP"]))
        self.assertEquals(
            logPort, rtnArray["logPort"],
            'Registration should return logport#%d, not %d' %
            (logPort, rtnArray["logPort"]))
        self.assertEquals(
            liveHost, rtnArray["liveIP"],
            'Registration should return livehost %s, not %s' %
            (liveHost, rtnArray["liveIP"]))
        self.assertEquals(
            livePort, rtnArray["livePort"],
            'Registration should return liveport#%d, not %d' %
            (livePort, rtnArray["livePort"]))

    def setUp(self):
        self.__logFactory = SocketReaderFactory()

        self.__runConfigDir = None

    def tearDown(self):
        try:
            self.__logFactory.tearDown()
        except:
            traceback.print_exc()

        if self.__runConfigDir is not None:
            shutil.rmtree(self.__runConfigDir, ignore_errors=True)
            self.__runConfigDir = None

        MockServer.APPENDER.checkStatus(10)

    def testRegister(self):
        logPort = 11853
        logger = self.__createLog('file', logPort)

        livePort = 35811
        liver = self.__createLog('live', livePort, False)

        dc = MockServer(logPort=logPort,
                        livePort=livePort,
                        logFactory=self.__logFactory)

        self.assertEqual(dc.rpc_component_list_dicts(), [])

        name = 'foo'
        num = 0
        host = 'localhost'
        port = 666
        mPort = 667

        expId = DAQClient.ID.peekNext()

        if num == 0:
            fullName = name
        else:
            fullName = "%s#%d" % (name, num)

        logger.addExpectedText('Registered %s' % fullName)
        liver.addExpectedText('Registered %s' % fullName)

        rtnArray = dc.rpc_component_register(name, num, host, port, mPort, [])

        localAddr = self.__getInternetAddress()

        self.__verifyRegArray(rtnArray, expId, localAddr, logPort, localAddr,
                              livePort)

        self.assertEqual(dc.rpc_component_count(), 1)

        fooDict = {
            "id": expId,
            "compName": name,
            "compNum": num,
            "host": host,
            "rpcPort": port,
            "mbeanPort": mPort,
            "state": "idle"
        }
        self.assertEqual(dc.rpc_component_list_dicts(), [
            fooDict,
        ])

        logger.checkStatus(100)
        liver.checkStatus(100)

    def testRegisterWithLog(self):
        logPort = 23456
        logger = self.__createLog('log', logPort)

        dc = MockServer(logPort=logPort, logFactory=self.__logFactory)

        logger.checkStatus(100)

        liveHost = ''
        livePort = 0

        name = 'foo'
        num = 0
        host = 'localhost'
        port = 666
        mPort = 667

        expId = DAQClient.ID.peekNext()

        if num == 0:
            fullName = name
        else:
            fullName = "%s#%d" % (name, num)

        logger.addExpectedText('Registered %s' % fullName)

        rtnArray = dc.rpc_component_register(name, num, host, port, mPort, [])

        localAddr = self.__getInternetAddress()

        self.__verifyRegArray(rtnArray, expId, localAddr, logPort, liveHost,
                              livePort)

        logger.checkStatus(100)

    def testNoRunset(self):
        logPort = 11545

        logger = self.__createLog('main', logPort)

        dc = MockServer(logPort=logPort, logFactory=self.__logFactory)

        logger.checkStatus(100)

        moniType = RunOption.MONI_TO_NONE

        self.assertRaises(CnCServerException, dc.rpc_runset_break, 1)
        self.assertRaises(CnCServerException, dc.rpc_runset_list, 1)
        self.assertRaises(CnCServerException, dc.rpc_runset_start_run, 1, 1,
                          moniType)
        self.assertRaises(CnCServerException, dc.rpc_runset_stop_run, 1)

        logger.checkStatus(100)

    def testRunset(self):
        self.__runConfigDir = tempfile.mkdtemp()

        logPort = 21765

        logger = self.__createLog('main', logPort)

        clientPort = DAQPort.RUNCOMP_BASE

        clientLogger = self.__createLog('client', clientPort)

        compId = DAQClient.ID.peekNext()
        compName = 'stringHub'
        compNum = self.HUB_NUMBER
        compHost = 'localhost'
        compPort = 666
        compBeanPort = 0

        cluCfg = MockClusterConfig("clusterFoo")
        cluCfg.addComponent("%s#%d" % (compName, compNum), "java", "",
                            compHost)

        dc = MockServer(clusterConfigObject=cluCfg,
                        copyDir="copyDir",
                        runConfigDir=self.__runConfigDir,
                        spadeDir="/tmp",
                        logPort=logPort,
                        clientLog=clientLogger,
                        logFactory=self.__logFactory)

        logger.checkStatus(100)

        self.assertEqual(dc.rpc_component_count(), 0)
        self.assertEqual(dc.rpc_runset_count(), 0)
        self.assertEqual(dc.rpc_component_list_dicts(), [])

        if compNum == 0:
            fullName = compName
        else:
            fullName = "%s#%d" % (compName, compNum)

        logger.addExpectedText('Registered %s' % fullName)

        dc.rpc_component_register(compName, compNum, compHost, compPort,
                                  compBeanPort, [])

        logger.checkStatus(100)

        self.assertEqual(dc.rpc_component_count(), 1)
        self.assertEqual(dc.rpc_runset_count(), 0)

        connErr = "No connection map entry for ID#%s %s#%d .*" % \
            (compId, compName, compNum)
        logger.addExpectedTextRegexp(connErr)

        rcFile = MockRunConfigFile(self.__runConfigDir)

        domList = [
            MockRunConfigFile.createDOM(self.DOM_MAINBOARD_ID),
        ]
        runConfig = rcFile.create([], domList)

        logger.addExpectedTextRegexp('Loading run configuration .*')
        logger.addExpectedTextRegexp('Loaded run configuration .*')
        logger.addExpectedTextRegexp("Built runset #\d+: .*")

        setId = dc.rpc_runset_make(runConfig, strict=False)

        logger.checkStatus(100)

        self.assertEqual(dc.rpc_component_count(), 0)
        self.assertEqual(dc.rpc_runset_count(), 1)

        rs = dc.rpc_runset_list(setId)
        self.assertEqual(len(rs), 1)

        rsc = rs[0]
        self.assertEqual(compId, rsc["id"])
        self.assertEqual(compName, rsc["compName"])
        self.assertEqual(compNum, rsc["compNum"])
        self.assertEqual(compHost, rsc["host"])
        self.assertEqual(compPort, rsc["rpcPort"])
        self.assertEqual(compBeanPort, rsc["mbeanPort"])
        self.assertEqual("ready", rsc["state"])

        logger.checkStatus(100)

        runNum = 456

        logger.addExpectedText("Starting run #%d with \"%s\"" %
                               (runNum, cluCfg.configName()))

        logger.addExpectedTextRegexp(r"Version info: \S+ \d+" +
                                     r" \S+ \S+ \S+ \S+ \d+\S*")
        clientLogger.addExpectedTextRegexp(r"Version info: \S+ \d+" +
                                           r" \S+ \S+ \S+ \S+ \d+\S*")

        logger.addExpectedText("Run configuration: %s" % runConfig)
        logger.addExpectedText("Cluster configuration: %s" %
                               cluCfg.configName())

        moniType = RunOption.MONI_TO_NONE

        global CAUGHT_WARNING
        if not LIVE_IMPORT and not CAUGHT_WARNING:
            CAUGHT_WARNING = True
            logger.addExpectedTextRegexp(r"^Cannot import IceCube Live.*")

        logger.addExpectedText("Starting run %d..." % runNum)

        self.assertEqual(dc.rpc_runset_start_run(setId, runNum, moniType),
                         'OK')

        logger.checkStatus(10)
        clientLogger.checkStatus(10)

        logger.addExpectedText("0 physics events collected in 0 seconds")
        logger.addExpectedText("0 moni events, 0 SN events, 0 tcals")
        logger.addExpectedText("Run terminated SUCCESSFULLY")

        self.assertEqual(dc.rpc_runset_stop_run(setId), 'OK')

        logger.checkStatus(10)

        self.assertEqual(dc.rpc_component_count(), 0)
        self.assertEqual(dc.rpc_runset_count(), 1)

        logger.checkStatus(10)

        self.assertEquals(dc.rpc_runset_break(setId), 'OK')

        logger.checkStatus(10)

        self.assertEqual(dc.rpc_component_count(), 1)
        self.assertEqual(dc.rpc_runset_count(), 0)

        logger.checkStatus(10)
        clientLogger.checkStatus(10)
Esempio n. 5
0
class CnCLoggerTest(unittest.TestCase):
    def createLog(self, name, port, expectStartMsg=True):
        return self.__logFactory.createLog(name, port, expectStartMsg)

    def setUp(self):
        self.__logFactory = SocketReaderFactory()

        self.__appender = MockAppender('mock')

    def tearDown(self):
        self.__logFactory.tearDown()

        self.__appender.checkStatus(10)

    def testOpenReset(self):
        dc = CnCLogger(self.__appender, True)

        logPort = 12345

        logObj = self.createLog('file', logPort)

        dc.openLog('localhost', logPort, None, None)
        self.assertEqual(dc.getLogHost(), 'localhost')
        self.assertEqual(dc.getLogPort(), logPort)
        self.assertEqual(dc.getLiveHost(), None)
        self.assertEqual(dc.getLivePort(), None)

        logObj.checkStatus(1000)

        dc.resetLog()
        self.failIf(dc.getLogHost() is not None, 'logIP was not cleared')
        self.failIf(dc.getLogPort() is not None, 'logPort was not cleared')

        logObj.checkStatus(1000)

    def testOpenResetLive(self):
        dc = CnCLogger(self.__appender, True)

        livePort = 6789

        liveObj = self.createLog('live', livePort, False)

        dc.openLog(None, None, 'localhost', livePort)
        self.assertEqual(dc.getLogHost(), None)
        self.assertEqual(dc.getLogPort(), None)
        self.assertEqual(dc.getLiveHost(), 'localhost')
        self.assertEqual(dc.getLivePort(), livePort)

        liveObj.checkStatus(1000)

        dc.resetLog()
        self.failIf(dc.getLiveHost() is not None, 'liveIP was not cleared')
        self.failIf(dc.getLivePort() is not None, 'livePort was not cleared')

        liveObj.checkStatus(1000)

    def testOpenResetBoth(self):
        dc = CnCLogger(self.__appender, True)

        logPort = 12345
        livePort = 6789

        logObj = self.createLog('file', logPort)
        liveObj = self.createLog('live', livePort, False)

        dc.openLog('localhost', logPort, 'localhost', livePort)
        self.assertEqual(dc.getLogHost(), 'localhost')
        self.assertEqual(dc.getLogPort(), logPort)
        self.assertEqual(dc.getLiveHost(), 'localhost')
        self.assertEqual(dc.getLivePort(), livePort)

        logObj.checkStatus(1000)
        liveObj.checkStatus(1000)

        dc.resetLog()
        self.failIf(dc.getLogHost() is not None, 'logIP was not cleared')
        self.failIf(dc.getLogPort() is not None, 'logPort was not cleared')
        self.failIf(dc.getLiveHost() is not None, 'liveIP was not cleared')
        self.failIf(dc.getLivePort() is not None, 'livePort was not cleared')

        logObj.checkStatus(1000)
        liveObj.checkStatus(1000)

    def testOpenClose(self):
        dc = CnCLogger(self.__appender, True)

        logHost = 'localhost'
        logPort = 12345

        logObj = self.createLog('file', logPort)

        dc.openLog(logHost, logPort, None, None)
        self.assertEqual(dc.getLogHost(), logHost)
        self.assertEqual(dc.getLogPort(), logPort)
        self.assertEqual(dc.getLiveHost(), None)
        self.assertEqual(dc.getLivePort(), None)

        logObj.addExpectedTextRegexp('End of log')

        dc.closeLog()
        self.failIf(dc.getLogHost() is not None, 'logIP was not cleared')
        self.failIf(dc.getLogPort() is not None, 'logPort was not cleared')
        self.failIf(dc.getLiveHost() is not None, 'liveIP was not cleared')
        self.failIf(dc.getLivePort() is not None, 'livePort was not cleared')

    def testOpenCloseLive(self):
        dc = CnCLogger(self.__appender, True)

        liveHost = 'localhost'
        livePort = 6789

        liveObj = self.createLog('live', livePort, False)

        dc.openLog(None, None, liveHost, livePort)
        self.assertEqual(dc.getLogHost(), None)
        self.assertEqual(dc.getLogPort(), None)
        self.assertEqual(dc.getLiveHost(), liveHost)
        self.assertEqual(dc.getLivePort(), livePort)

        liveObj.addExpectedTextRegexp('End of log')

        dc.closeLog()
        self.failIf(dc.getLogHost() is not None, 'logIP was not cleared')
        self.failIf(dc.getLogPort() is not None, 'logPort was not cleared')
        self.failIf(dc.getLiveHost() is not None, 'liveIP was not cleared')
        self.failIf(dc.getLivePort() is not None, 'livePort was not cleared')

    def testOpenCloseBoth(self):
        dc = CnCLogger(self.__appender, True)

        logHost = 'localhost'
        logPort = 12345
        liveHost = 'localhost'
        livePort = 6789

        logObj = self.createLog('file', logPort)
        liveObj = self.createLog('live', livePort, False)

        dc.openLog(logHost, logPort, liveHost, livePort)
        self.assertEqual(dc.getLogHost(), logHost)
        self.assertEqual(dc.getLogPort(), logPort)
        self.assertEqual(dc.getLiveHost(), liveHost)
        self.assertEqual(dc.getLivePort(), livePort)

        logObj.addExpectedTextRegexp('End of log')
        liveObj.addExpectedTextRegexp('End of log')

        dc.closeLog()
        self.failIf(dc.getLogHost() is not None, 'logIP was not cleared')
        self.failIf(dc.getLogPort() is not None, 'logPort was not cleared')
        self.failIf(dc.getLiveHost() is not None, 'liveIP was not cleared')
        self.failIf(dc.getLivePort() is not None, 'livePort was not cleared')

    def testLogFallback(self):
        dc = CnCLogger(self.__appender, True)

        dfltHost = 'localhost'
        dfltPort = 11111

        dfltObj = self.createLog('dflt', dfltPort)

        logHost = 'localhost'
        logPort = 12345

        logObj = self.createLog('file', logPort)

        dc.openLog(dfltHost, dfltPort, None, None)
        self.assertEqual(dc.getLogHost(), dfltHost)
        self.assertEqual(dc.getLogPort(), dfltPort)

        dfltObj.checkStatus(1000)

        dc.openLog(logHost, logPort, None, None)
        self.assertEqual(dc.getLogHost(), logHost)
        self.assertEqual(dc.getLogPort(), logPort)

        logObj.checkStatus(1000)

        logObj.addExpectedTextRegexp('End of log')
        dfltObj.addExpectedText('Reset log to %s:%d' % (dfltHost, dfltPort))

        dc.closeLog()
        self.assertEqual(dc.getLogHost(), dfltHost)
        self.assertEqual(dc.getLogPort(), dfltPort)

        logObj.checkStatus(1000)

        dfltObj.checkStatus(1000)

        newHost = 'localhost'
        newPort = 45678

        newObj = self.createLog('new', newPort)

        dc.openLog(newHost, newPort, None, None)
        self.assertEqual(dc.getLogHost(), newHost)
        self.assertEqual(dc.getLogPort(), newPort)

        dfltObj.checkStatus(1000)

        newObj.checkStatus(1000)

        newObj.addExpectedTextRegexp('End of log')
        dfltObj.addExpectedText('Reset log to %s:%d' % (dfltHost, dfltPort))

        dc.closeLog()
        self.assertEqual(dc.getLogHost(), dfltHost)
        self.assertEqual(dc.getLogPort(), dfltPort)

        newObj.checkStatus(1000)

        dfltObj.checkStatus(1000)

        dfltObj.addExpectedTextRegexp('End of log')

        dc.closeLog()
        self.assertEqual(dc.getLogHost(), None)
        self.assertEqual(dc.getLogPort(), None)

        dfltObj.checkStatus(1000)

    def testLogFallbackSwitch(self):
        dc = CnCLogger(self.__appender, True)

        dfltHost = 'localhost'
        dfltPort = 11111

        dfltObj = self.createLog('dflt', dfltPort)

        liveHost = 'localhost'
        livePort = 6789

        liveObj = self.createLog('live', livePort, False)

        dc.openLog(dfltHost, dfltPort, None, None)
        self.assertEqual(dc.getLogHost(), dfltHost)
        self.assertEqual(dc.getLogPort(), dfltPort)
        self.assertEqual(dc.getLiveHost(), None)
        self.assertEqual(dc.getLivePort(), None)

        dfltObj.checkStatus(1000)

        dc.openLog(None, None, liveHost, livePort)
        self.assertEqual(dc.getLogHost(), None)
        self.assertEqual(dc.getLogPort(), None)
        self.assertEqual(dc.getLiveHost(), liveHost)
        self.assertEqual(dc.getLivePort(), livePort)

        liveObj.checkStatus(1000)

        liveObj.addExpectedTextRegexp('End of log')
        dfltObj.addExpectedText('Reset log to %s:%d' % (dfltHost, dfltPort))

        dc.closeLog()
        self.assertEqual(dc.getLogHost(), dfltHost)
        self.assertEqual(dc.getLogPort(), dfltPort)
        self.assertEqual(dc.getLiveHost(), None)
        self.assertEqual(dc.getLivePort(), None)

        liveObj.checkStatus(1000)

        dfltObj.checkStatus(1000)

        newHost = 'localhost'
        newPort = 45678

        newObj = self.createLog('new', newPort)

        dc.openLog(newHost, newPort, None, None)
        self.assertEqual(dc.getLogHost(), newHost)
        self.assertEqual(dc.getLogPort(), newPort)

        dfltObj.checkStatus(1000)

        newObj.checkStatus(1000)

        newObj.addExpectedTextRegexp('End of log')
        dfltObj.addExpectedText('Reset log to %s:%d' % (dfltHost, dfltPort))

        dc.closeLog()
        self.assertEqual(dc.getLogHost(), dfltHost)
        self.assertEqual(dc.getLogPort(), dfltPort)

        newObj.checkStatus(1000)

        dfltObj.checkStatus(1000)

        dfltObj.addExpectedTextRegexp('End of log')

        dc.closeLog()
        self.assertEqual(dc.getLogHost(), None)
        self.assertEqual(dc.getLogPort(), None)

        dfltObj.checkStatus(1000)
Esempio n. 6
0
    def setUp(self):
        self.__logFactory = SocketReaderFactory()

        self.__appender = MockAppender('mock')
Esempio n. 7
0
class CnCLoggerTest(unittest.TestCase):
    def createLog(self, name, port):
        return self.__logFactory.createLog(name, port, False)

    def setUp(self):
        self.__logFactory = SocketReaderFactory()

        self.__appender = MockAppender("mock")

    def tearDown(self):
        try:
            self.__logFactory.tearDown()
        except:
            traceback.print_exc()

        self.__appender.checkStatus(10)

    def testOpenReset(self):
        dfltHost = "localhost"
        dfltPort = 54321

        dfltObj = self.createLog("dflt", dfltPort)

        logHost = "localhost"
        logPort = 12345

        logObj = self.createLog("file", logPort)

        for xl in (False, True):
            dc = CnCLogger(self.__appender, quiet=True, extraLoud=xl)

            # set up default logger
            dc.openLog(None, None, dfltHost, dfltPort)

            logObj.addExpectedText("Start of log at LOG=log(%s:%d)" %
                                   (logHost, logPort))

            dc.openLog(logHost, logPort, None, None)
            self.assertEqual(dc.logHost(), logHost)
            self.assertEqual(dc.logPort(), logPort)
            self.assertEqual(dc.liveHost(), None)
            self.assertEqual(dc.livePort(), None)

            logObj.checkStatus(1000)
            dfltObj.checkStatus(1000)

            if xl:
                dfltObj.addExpectedText("Reset log to LOG=live(%s:%d)" %
                                        (dfltHost, dfltPort))

            dc.resetLog()
            self.failIf(dc.logHost() is not None, "logIP was not cleared")
            self.failIf(dc.logPort() is not None, "logPort was not cleared")
            self.assertEqual(
                dc.liveHost(), dfltHost,
                "liveHost should be %s, not %s" % (dfltHost, dc.liveHost()))
            self.assertEqual(
                dc.livePort(), dfltPort,
                "livePort should be %s, not %s" % (dfltPort, dc.livePort()))

            logObj.checkStatus(1000)
            dfltObj.checkStatus(1000)

    def testOpenResetLive(self):
        dfltHost = "localhost"
        dfltPort = 54321

        dfltObj = self.createLog("dflt", dfltPort)

        liveHost = "localhost"
        livePort = 6789

        liveObj = self.createLog("live", livePort)

        for xl in (False, True):
            dc = CnCLogger(self.__appender, quiet=True, extraLoud=xl)

            dfltObj.addExpectedText("Start of log at LOG=log(%s:%d)" %
                                    (dfltHost, dfltPort))

            # set up default logger
            dc.openLog(dfltHost, dfltPort, None, None)

            dfltObj.checkStatus(1000)
            liveObj.checkStatus(1000)

            dc.openLog(None, None, liveHost, livePort)
            self.assertEqual(dc.logHost(), None)
            self.assertEqual(dc.logPort(), None)
            self.assertEqual(dc.liveHost(), liveHost)
            self.assertEqual(dc.livePort(), livePort)

            dfltObj.checkStatus(1000)
            liveObj.checkStatus(1000)

            if xl:
                dfltObj.addExpectedText("Reset log to LOG=log(%s:%d)" %
                                        (dfltHost, dfltPort))

            dc.resetLog()
            self.assertEqual(
                dc.logHost(), dfltHost,
                "logHost should be %s, not %s" % (dfltHost, dc.logHost()))
            self.assertEqual(
                dc.logPort(), dfltPort,
                "logPort should be %s, not %s" % (dfltPort, dc.logPort()))
            self.failIf(dc.liveHost() is not None, "liveIP was not cleared")
            self.failIf(dc.livePort() is not None, "livePort was not cleared")

            liveObj.checkStatus(1000)
            dfltObj.checkStatus(1000)

    def testOpenResetBoth(self):
        dfltHost = "localhost"
        dfltLog = 54321
        dfltLive = 9876

        dLogObj = self.createLog("dLog", dfltLog)
        dLiveObj = self.createLog("dLive", dfltLive)

        host = "localhost"
        logPort = 12345
        livePort = 6789

        logObj = self.createLog("file", logPort)
        liveObj = self.createLog("live", livePort)

        for xl in (False, True):
            dc = CnCLogger(self.__appender, quiet=True, extraLoud=xl)

            dLogObj.addExpectedText(
                ("Start of log at LOG=log(%s:%d)" + " live(%s:%d)") %
                (dfltHost, dfltLog, dfltHost, dfltLive))

            # set up default logger
            dc.openLog(dfltHost, dfltLog, dfltHost, dfltLive)

            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)
            logObj.checkStatus(1000)
            liveObj.checkStatus(1000)

            logObj.addExpectedText(
                ("Start of log at LOG=log(%s:%d)" + " live(%s:%d)") %
                (host, logPort, host, livePort))

            dc.openLog(host, logPort, host, livePort)
            self.assertEqual(dc.logHost(), host)
            self.assertEqual(dc.logPort(), logPort)
            self.assertEqual(dc.liveHost(), host)
            self.assertEqual(dc.livePort(), livePort)

            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)
            logObj.checkStatus(1000)
            liveObj.checkStatus(1000)

            if xl:
                dLogObj.addExpectedText(
                    ("Reset log to LOG=log(%s:%d)" + " live(%s:%d)") %
                    (dfltHost, dfltLog, dfltHost, dfltLive))
                dLiveObj.addExpectedLiveMoni(
                    "log", "Reset log to" + " LOG=log(%s:%d) live(%s:%d)" %
                    (dfltHost, dfltLog, dfltHost, dfltLive))

            dc.resetLog()
            self.assertEqual(
                dc.logHost(), dfltHost,
                "logHost should be %s, not %s" % (dfltHost, dc.logHost()))
            self.assertEqual(
                dc.logPort(), dfltLog,
                "logPort should be %s, not %s" % (dfltLog, dc.logPort()))
            self.assertEqual(
                dc.liveHost(), dfltHost,
                "liveHost should be %s, not %s" % (dfltHost, dc.liveHost()))
            self.assertEqual(
                dc.livePort(), dfltLive,
                "livePort should be %s, not %s" % (dfltLive, dc.livePort()))

            logObj.checkStatus(1000)
            liveObj.checkStatus(1000)
            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)

    def testOpenClose(self):
        dfltHost = "localhost"
        dfltLog = 54321
        dfltLive = 9876

        dLogObj = self.createLog("dLog", dfltLog)
        dLiveObj = self.createLog("dLive", dfltLive)

        logHost = "localhost"
        logPort = 12345

        logObj = self.createLog("file", logPort)

        for xl in (False, True):
            dc = CnCLogger(self.__appender, quiet=True, extraLoud=xl)

            dLogObj.addExpectedText(
                ("Start of log at LOG=log(%s:%d)" + " live(%s:%d)") %
                (dfltHost, dfltLog, dfltHost, dfltLive))

            # set up default logger
            dc.openLog(dfltHost, dfltLog, dfltHost, dfltLive)

            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)

            logObj.addExpectedText("Start of log at LOG=log(%s:%d)" %
                                   (logHost, logPort))

            dc.openLog(logHost, logPort, None, None)
            self.assertEqual(dc.logHost(), logHost)
            self.assertEqual(dc.logPort(), logPort)
            self.assertEqual(dc.liveHost(), None)
            self.assertEqual(dc.livePort(), None)

            logObj.checkStatus(1000)
            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)

            if xl:
                logObj.addExpectedText("End of log")
                dLogObj.addExpectedText(
                    ("Reset log to LOG=log(%s:%d)" + " live(%s:%d)") %
                    (dfltHost, dfltLog, dfltHost, dfltLive))
                dLiveObj.addExpectedLiveMoni(
                    "log", "Reset log to" + " LOG=log(%s:%d) live(%s:%d)" %
                    (dfltHost, dfltLog, dfltHost, dfltLive))

            dc.closeLog()
            self.assertEqual(
                dc.logHost(), dfltHost,
                "logHost should be %s, not %s" % (dfltHost, dc.logHost()))
            self.assertEqual(
                dc.logPort(), dfltLog,
                "logPort should be %s, not %s" % (dfltLog, dc.logPort()))
            self.assertEqual(
                dc.liveHost(), dfltHost,
                "liveHost should be %s, not %s" % (dfltHost, dc.liveHost()))
            self.assertEqual(
                dc.livePort(), dfltLive,
                "livePort should be %s, not %s" % (dfltLive, dc.livePort()))

            logObj.checkStatus(1000)
            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)

    def testOpenCloseLive(self):
        dfltHost = "localhost"
        dfltLog = 54321
        dfltLive = 9876

        dLogObj = self.createLog("dLog", dfltLog)
        dLiveObj = self.createLog("dLive", dfltLive)

        liveHost = "localhost"
        livePort = 6789

        liveObj = self.createLog("live", livePort)

        for xl in (False, True):
            dc = CnCLogger(self.__appender, quiet=True, extraLoud=xl)

            dLogObj.addExpectedText(
                ("Start of log at LOG=log(%s:%d)" + " live(%s:%d)") %
                (dfltHost, dfltLog, dfltHost, dfltLive))

            # set up default logger
            dc.openLog(dfltHost, dfltLog, dfltHost, dfltLive)

            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)

            dc.openLog(None, None, liveHost, livePort)
            self.assertEqual(dc.logHost(), None)
            self.assertEqual(dc.logPort(), None)
            self.assertEqual(dc.liveHost(), liveHost)
            self.assertEqual(dc.livePort(), livePort)

            liveObj.checkStatus(1000)
            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)

            if xl:
                liveObj.addExpectedText("End of log")
                dLogObj.addExpectedText(
                    ("Reset log to LOG=log(%s:%d)" + " live(%s:%d)") %
                    (dfltHost, dfltLog, dfltHost, dfltLive))
                dLiveObj.addExpectedLiveMoni(
                    "log", "Reset log to" + " LOG=log(%s:%d) live(%s:%d)" %
                    (dfltHost, dfltLog, dfltHost, dfltLive))

            dc.closeLog()
            self.assertEqual(
                dc.logHost(), dfltHost,
                "logHost should be %s, not %s" % (dfltHost, dc.logHost()))
            self.assertEqual(
                dc.logPort(), dfltLog,
                "logPort should be %s, not %s" % (dfltLog, dc.logPort()))
            self.assertEqual(
                dc.liveHost(), dfltHost,
                "liveHost should be %s, not %s" % (dfltHost, dc.liveHost()))
            self.assertEqual(
                dc.livePort(), dfltLive,
                "livePort should be %s, not %s" % (dfltLive, dc.livePort()))

            liveObj.checkStatus(1000)
            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)

    def testOpenCloseBoth(self):
        dfltHost = "localhost"
        dfltLog = 54321
        dfltLive = 9876

        dLogObj = self.createLog("dLog", dfltLog)
        dLiveObj = self.createLog("dLive", dfltLive)

        logHost = "localhost"
        logPort = 12345
        liveHost = "localhost"
        livePort = 6789

        logObj = self.createLog("file", logPort)
        liveObj = self.createLog("live", livePort)

        for xl in (False, True):
            dc = CnCLogger(self.__appender, quiet=True, extraLoud=xl)

            dLogObj.addExpectedText(
                ("Start of log at LOG=log(%s:%d)" + " live(%s:%d)") %
                (dfltHost, dfltLog, dfltHost, dfltLive))

            # set up default logger
            dc.openLog(dfltHost, dfltLog, dfltHost, dfltLive)

            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)

            logObj.addExpectedText(
                ("Start of log at LOG=log(%s:%d)" + " live(%s:%d)") %
                (logHost, logPort, liveHost, livePort))

            dc.openLog(logHost, logPort, liveHost, livePort)
            self.assertEqual(dc.logHost(), logHost)
            self.assertEqual(dc.logPort(), logPort)
            self.assertEqual(dc.liveHost(), liveHost)
            self.assertEqual(dc.livePort(), livePort)

            if xl:
                logObj.addExpectedTextRegexp("End of log")
                liveObj.addExpectedTextRegexp("End of log")
                dLogObj.addExpectedText(
                    ("Reset log to LOG=log(%s:%d)" + " live(%s:%d)") %
                    (dfltHost, dfltLog, dfltHost, dfltLive))
                dLiveObj.addExpectedLiveMoni(
                    "log", "Reset log to" + " LOG=log(%s:%d) live(%s:%d)" %
                    (dfltHost, dfltLog, dfltHost, dfltLive))

            dc.closeLog()
            self.assertEqual(
                dc.logHost(), dfltHost,
                "logHost should be %s, not %s" % (dfltHost, dc.logHost()))
            self.assertEqual(
                dc.logPort(), dfltLog,
                "logPort should be %s, not %s" % (dfltLog, dc.logPort()))
            self.assertEqual(
                dc.liveHost(), dfltHost,
                "liveHost should be %s, not %s" % (dfltHost, dc.liveHost()))
            self.assertEqual(
                dc.livePort(), dfltLive,
                "livePort should be %s, not %s" % (dfltLive, dc.livePort()))

            logObj.checkStatus(1000)
            liveObj.checkStatus(1000)
            dLogObj.checkStatus(1000)
            dLiveObj.checkStatus(1000)
Esempio n. 8
0
class TestCnCServer(unittest.TestCase):
    def createLog(self, name, port):
        return self.__logFactory.createLog(name, port)

    def setUp(self):
        self.__logFactory = SocketReaderFactory()

        self.comp = None
        self.cnc = None

        MostlyCnCServer.APPENDERS.clear()
        RealComponent.APPENDERS.clear()

    def tearDown(self):
        for key in RealComponent.APPENDERS:
            RealComponent.APPENDERS[key].WaitForEmpty(10)
        for key in MostlyCnCServer.APPENDERS:
            MostlyCnCServer.APPENDERS[key].checkStatus(10)

        if self.comp is not None:
            self.comp.close()
        if self.cnc is not None:
            self.cnc.closeServer()

        self.__logFactory.tearDown()

    def __runEverything(self):
        catchall = self.createLog('master', 18999)

        catchall.addExpectedText(
            "I'm server %s running on port %d" %
            (MostlyCnCServer.SERVER_NAME, DAQPort.CNCSERVER))
        catchall.addExpectedTextRegexp(r'\S+ \S+ \S+ \S+ \S+ \S+ \S+')

        self.cnc = MostlyCnCServer(logPort=catchall.getPort())
        threading.Thread(target=self.cnc.run, args=()).start()

        catchall.checkStatus(100)

        compName = 'foo'
        compNum = 1
        host = 'localhost'
        cmdPort = 19001
        mbeanPort = 19002

        catchall.addExpectedText(
            ('Got registration for ID#%d %s#%d at' + ' localhost:%d M#%d') %
            (DAQClient.ID, compName, compNum, cmdPort, mbeanPort))

        self.comp = RealComponent(compName, compNum, cmdPort, mbeanPort)

        catchall.checkStatus(100)

        s = self.cnc.rpc_list_components()
        self.assertEquals(1, len(s),
                          'Expected 1 listed component, not %d' % len(s))
        self.assertEquals(
            compName, s[0][1],
            'Expected component %s, not %s' % (compName, s[0][1]))
        self.assertEquals(
            compNum, s[0][2],
            'Expected %s #%d, not #%d' % (compName, compNum, s[0][2]))
        self.assertEquals(
            host, s[0][3], 'Expected %s#%d host %s, not %s' %
            (compName, compNum, host, s[0][3]))
        self.assertEquals(
            cmdPort, s[0][4], 'Expected %s#%d cmdPort %d, not %d' %
            (compName, compNum, cmdPort, s[0][4]))
        self.assertEquals(
            mbeanPort, s[0][5], 'Expected %s#%d mbeanPort %d, not %d' %
            (compName, compNum, mbeanPort, s[0][5]))

        compId = s[0][0]

        catchall.addExpectedText('Built runset with the following components:')

        setId = self.cnc.rpc_runset_make([compName])
        self.assertEquals('connected', self.comp.getState(),
                          'Unexpected state %s' % self.comp.getState())

        catchall.checkStatus(100)

        catchall.addExpectedText('ID#%d %s#%d at %s:%d M#%s %s' % \
                                     (compId, compName, compNum, host, cmdPort,
                                      mbeanPort, 'connected'))

        self.assertEqual(self.cnc.rpc_runset_status(setId), 'OK')

        catchall.checkStatus(100)

        runPort = 18998

        runlog = self.createLog('runlog', runPort)

        self.comp.setExpectedRunLogPort(runPort)

        runlog.addExpectedExact('Test msg')
        runlog.addExpectedText('filename revision date time author')

        logList = [
            (compName, compNum, runPort),
        ]
        self.assertEqual(self.cnc.rpc_runset_log_to(setId, host, logList),
                         'OK')

        runlog.checkStatus(100)

        runlog.addExpectedExact('Config %s#%d' % (compName, compNum))

        self.assertEqual(self.cnc.rpc_runset_configure(setId), 'OK')

        runlog.checkStatus(100)

        cfgName = 'zzz'

        runlog.addExpectedExact('Config %s#%d with %s' %
                                (compName, compNum, cfgName))

        self.assertEqual(self.cnc.rpc_runset_configure(setId, cfgName), 'OK')

        runlog.checkStatus(100)

        runNum = 444

        runlog.addExpectedExact('Start #%d on %s#%d' %
                                (runNum, compName, compNum))

        self.assertEqual(self.cnc.rpc_runset_start_run(setId, runNum), 'OK')

        runlog.checkStatus(100)

        runlog.addExpectedExact('Stop %s#%d' % (compName, compNum))

        self.assertEqual(self.cnc.rpc_runset_stop_run(setId), 'OK')

        runlog.checkStatus(100)

        self.assertEqual(self.cnc.rpc_get_num_components(), 0)
        self.assertEqual(self.cnc.rpc_num_sets(), 1)

        serverAppender = MostlyCnCServer.APPENDERS['server']
        serverAppender.addExpectedExact('End of log')

        self.assertEquals(self.cnc.rpc_runset_break(setId), 'OK')

        serverAppender.checkStatus(100)

        self.assertEqual(self.cnc.rpc_get_num_components(), 1)
        self.assertEqual(self.cnc.rpc_num_sets(), 0)

        serverAppender.checkStatus(100)

        runlog.checkStatus(100)
        catchall.checkStatus(100)

    def testEverything(self):
        self.__runEverything()

    def testEverythingAgain(self):
        if sys.platform != 'darwin':
            print 'Skipping server tests in non-Darwin OS'
            return

        self.__runEverything()
Esempio n. 9
0
 def setUp(self):
     self.__live = None
     self.__run = None
     self.__logFactory = SocketReaderFactory()
Esempio n. 10
0
class TestDAQLive(unittest.TestCase):
    def setUp(self):
        self.__live = None
        self.__run = None
        self.__logFactory = SocketReaderFactory()

    def tearDown(self):
        self.__logFactory.tearDown()
        if self.__run is not None:
            self.__run.close()
        if self.__live is not None:
            self.__live.close()

    def testStartNoConfig(self):
        if not TEST_LIVE:
            print 'Skipping I3Live-related test'
            return

        log = self.__logFactory.createLog('liveMoni', DAQPort.I3LIVE, False)

        port = 9876

        log.addExpectedText('Connecting to DAQRun')
        log.addExpectedText('Started pdaq service on port %d' % port)

        self.__run = MockRun(1)

        self.__live = MockLive(port)

        self.assertRaises(Exception, self.__live.starting, {})

        log.checkStatus(10)

    def testStart(self):
        if not TEST_LIVE:
            print 'Skipping I3Live-related test'
            return

        log = self.__logFactory.createLog('liveMoni', DAQPort.I3LIVE, False)

        port = 9876

        log.addExpectedText('Connecting to DAQRun')
        log.addExpectedText('Started pdaq service on port %d' % port)

        self.__run = MockRun(2)

        self.__live = MockLive(port)

        runConfig = 'xxxCfg'
        runNum = 543

        log.addExpectedText('Starting run %d - %s' % (runNum, runConfig))
        log.addExpectedText("DAQ state is RUNNING")
        log.addExpectedText('Started run %d' % runNum)

        args = {'runConfig': runConfig, 'runNumber': runNum}
        self.__live.starting(args)

    def testStop(self):
        if not TEST_LIVE:
            print 'Skipping I3Live-related test'
            return

        log = self.__logFactory.createLog('liveMoni', DAQPort.I3LIVE, False)

        port = 9876

        log.addExpectedText('Connecting to DAQRun')
        log.addExpectedText('Started pdaq service on port %d' % port)

        self.__run = MockRun(3)

        self.__live = MockLive(port)

        runNum = 0

        log.addExpectedText('Stopping run %d' % runNum)
        log.addExpectedText("DAQ state is STOPPED")
        log.addExpectedText('Stopped run %d' % runNum)

        numPhysics = 5
        numMoni = 10
        numSn = 15
        numTcal = 20

        self.__run.setEventCounts(numPhysics, numMoni, numSn, numTcal)

        log.addExpectedLiveMoni('tcalEvents', numTcal)
        log.addExpectedLiveMoni('moniEvents', numMoni)
        log.addExpectedLiveMoni('snEvents', numSn)
        log.addExpectedLiveMoni('physicsEvents', numPhysics)

        self.__live.stopping()

    def testRecover(self):
        if not TEST_LIVE:
            print 'Skipping I3Live-related test'
            return

        log = self.__logFactory.createLog('liveMoni', DAQPort.I3LIVE, False)

        port = 9876

        log.addExpectedText('Connecting to DAQRun')
        log.addExpectedText('Started pdaq service on port %d' % port)

        self.__run = MockRun(4)

        self.__live = MockLive(port)

        log.addExpectedText('Recovering pDAQ')
        log.addExpectedText('DAQ state is STOPPED')
        log.addExpectedText('Recovered pDAQ')

        self.__live.recovering()
Esempio n. 11
0
class TestDAQServer(unittest.TestCase):
    def __createLog(self, name, port, expectStartMsg=True):
        return self.__logFactory.createLog(name, port, expectStartMsg)

    def __getInternetAddress(self):
        for addrData in socket.getaddrinfo(socket.gethostname(), None):
            if addrData[0] == socket.AF_INET:
                return addrData[4][0]
        return None

    def __verifyRegArray(self, rtnArray, expId, logHost, logPort, liveHost,
                         livePort):
        numElem = 6
        self.assertEquals(
            numElem, len(rtnArray),
            'Expected %d-element array, not %d elements' %
            (numElem, len(rtnArray)))
        self.assertEquals(
            expId, rtnArray[0],
            'Registration should return client ID#%d, not %d' %
            (expId, rtnArray[0]))
        self.assertEquals(
            logHost, rtnArray[1],
            'Registration should return host %s, not %s' %
            (logHost, rtnArray[1]))
        self.assertEquals(
            logPort, rtnArray[2],
            'Registration should return port#%d, not %d' %
            (logPort, rtnArray[2]))
        self.assertEquals(
            liveHost, rtnArray[3],
            'Registration should return livehost %s, not %s' %
            (liveHost, rtnArray[3]))
        self.assertEquals(
            livePort, rtnArray[4],
            'Registration should return liveport#%d, not %d' %
            (livePort, rtnArray[4]))

    def setUp(self):
        self.__logFactory = SocketReaderFactory()

    def tearDown(self):
        self.__logFactory.tearDown()

        MockServer.APPENDER.checkStatus(10)

    def testRegister(self):
        logHost = 'localhost'
        logPort = 11853

        logger = self.__createLog('file', logPort)

        liveHost = 'localhost'
        livePort = 35811

        liver = self.__createLog('live', livePort, False)

        dc = MockServer(logPort, livePort)

        self.assertEqual(dc.rpc_show_components(), [])

        name = 'foo'
        num = 0
        host = 'localhost'
        port = 666
        mPort = 667

        expId = DAQClient.ID

        logger.addExpectedText('Got registration for ID#%d %s#%d at %s:%d' %
                               (expId, name, num, host, port))
        liver.addExpectedText('Got registration for ID#%d %s#%d at %s:%d' %
                              (expId, name, num, host, port))

        rtnArray = dc.rpc_register_component(name, num, host, port, mPort, [])

        localAddr = self.__getInternetAddress()

        self.__verifyRegArray(rtnArray, expId, localAddr, logPort, localAddr,
                              livePort)

        self.assertEqual(dc.rpc_get_num_components(), 1)

        fooStr = 'ID#%d %s#%d at %s:%d M#%d %s' % \
            (DAQClient.ID - 1, name, num, host, port, mPort, 'idle')
        self.assertEqual(dc.rpc_show_components(), [fooStr])

        logger.checkStatus(100)
        liver.checkStatus(100)

    def testRegisterWithLog(self):
        oldPort = 12345

        oldLog = self.__createLog('old', oldPort)

        dc = MockServer(oldPort, None)

        oldLog.checkStatus(100)

        logHost = 'localhost'
        logPort = 23456

        logger = self.__createLog('file', logPort)

        liveHost = ''
        livePort = 0

        dc.rpc_log_to(logHost, logPort, liveHost, livePort)

        logger.checkStatus(100)

        name = 'foo'
        num = 0
        host = 'localhost'
        port = 666
        mPort = 667

        expId = DAQClient.ID

        logger.addExpectedText(
            ('Got registration for ID#%d %s#%d at %s:%d' + ' M#%d') %
            (expId, name, num, host, port, mPort))

        rtnArray = dc.rpc_register_component(name, num, host, port, mPort, [])

        localAddr = self.__getInternetAddress()

        self.__verifyRegArray(rtnArray, expId, localAddr, logPort, localAddr,
                              livePort)

        logger.checkStatus(100)

        logger.addExpectedText('End of log')
        oldLog.addExpectedText('Reset log to localhost:%d' % oldPort)

        dc.rpc_close_log()

        logger.checkStatus(100)
        oldLog.checkStatus(100)

    def testNoRunset(self):
        logPort = 11545

        logger = self.__createLog('main', logPort)

        dc = MockServer(logPort, None)

        logger.checkStatus(100)

        self.assertRaises(ValueError, dc.rpc_runset_break, 1)
        self.assertRaises(ValueError, dc.rpc_runset_configure, 1)
        self.assertRaises(ValueError, dc.rpc_runset_configure, 1, 'xxx')
        self.assertRaises(ValueError, dc.rpc_runset_log_to, 1, 'xxx', [])
        self.assertRaises(ValueError, dc.rpc_runset_start_run, 1, 1)
        self.assertRaises(ValueError, dc.rpc_runset_status, 1)
        self.assertRaises(ValueError, dc.rpc_runset_stop_run, 1)

        logger.checkStatus(100)

    def testRunset(self):
        logPort = 21765

        logger = self.__createLog('main', logPort)

        dc = MockServer(logPort, None)

        logger.checkStatus(100)

        self.assertEqual(dc.rpc_get_num_components(), 0)
        self.assertEqual(dc.rpc_num_sets(), 0)
        self.assertEqual(dc.rpc_show_components(), [])

        id = DAQClient.ID
        name = 'foo'
        num = 99
        host = 'localhost'
        port = 666
        mPort = 0

        clientHost = 'localhost'
        clientPort = 21567

        clientLogger = self.__createLog('client', clientPort)

        compName = 'ID#%d %s#%d at %s:%d' % (id, name, num, host, port)

        logger.addExpectedText('Got registration for %s' % compName)

        dc.rpc_register_component(name, num, host, port, mPort, [])

        logger.checkStatus(100)

        self.assertEqual(dc.rpc_get_num_components(), 1)
        self.assertEqual(dc.rpc_num_sets(), 0)

        logger.addExpectedText('Built runset with the following components:')

        setId = dc.rpc_runset_make([name])

        logger.checkStatus(100)

        self.assertEqual(dc.rpc_get_num_components(), 0)
        self.assertEqual(dc.rpc_num_sets(), 1)

        logger.addExpectedText('%s connected' % compName)

        self.assertEqual(dc.rpc_runset_status(setId), 'OK')

        logger.checkStatus(100)

        clientLogger.addExpectedTextRegexp(r'Version info: unknown unknown' +
                                           ' unknown unknown unknown \S+ \S+')

        self.assertEqual(
            dc.rpc_runset_log_to(setId, clientHost, [
                [name, num, clientPort, 'fatal'],
            ]), 'OK')

        clientLogger.checkStatus(100)

        self.assertEqual(dc.rpc_runset_configure(setId), 'OK')

        self.assertEqual(dc.rpc_runset_configure(setId, 'zzz'), 'OK')

        self.assertEqual(dc.rpc_runset_start_run(setId, 444), 'OK')

        self.assertEqual(dc.rpc_runset_stop_run(setId), 'OK')

        self.assertEqual(dc.rpc_get_num_components(), 0)
        self.assertEqual(dc.rpc_num_sets(), 1)

        self.assertEquals(dc.rpc_runset_break(setId), 'OK')

        self.assertEqual(dc.rpc_get_num_components(), 1)
        self.assertEqual(dc.rpc_num_sets(), 0)

        logger.checkStatus(10)
        clientLogger.checkStatus(10)
Esempio n. 12
0
 def setUp(self):
     self.__logFactory = SocketReaderFactory()
Esempio n. 13
0
class TestCnCServer(unittest.TestCase):
    HUB_NUMBER = 1021
    DOM_MAINBOARD_ID = "53494d552101"

    def createLog(self, name, port, expectStartMsg=True):
        return self.__logFactory.createLog(name, port, expectStartMsg)

    def setUp(self):
        self.__logFactory = SocketReaderFactory()

        self.__copyDir = tempfile.mkdtemp()
        self.__runConfigDir = tempfile.mkdtemp()
        self.__spadeDir = tempfile.mkdtemp()

        self.comp = None
        self.cnc = None

        MostlyCnCServer.APPENDERS.clear()
        RealComponent.APPENDERS.clear()

    def tearDown(self):
        for key in RealComponent.APPENDERS:
            RealComponent.APPENDERS[key].WaitForEmpty(10)
        for key in MostlyCnCServer.APPENDERS:
            MostlyCnCServer.APPENDERS[key].checkStatus(10)

        if self.comp is not None:
            self.comp.close()
        if self.cnc is not None:
            try:
                self.cnc.closeServer()
            except:
                pass

        if self.__copyDir is not None:
            shutil.rmtree(self.__copyDir, ignore_errors=True)
            self.__copyDir = None
        if self.__runConfigDir is not None:
            shutil.rmtree(self.__runConfigDir, ignore_errors=True)
            self.__runConfigDir = None
        if self.__spadeDir is not None:
            shutil.rmtree(self.__spadeDir, ignore_errors=True)
            self.__spadeDir = None

        try:
            self.__logFactory.tearDown()
        except:
            traceback.print_exc()

    def __runEverything(self, forceRestart):
        catchall = self.createLog('master', 18999)

        clientPort = DAQPort.RUNCOMP_BASE

        clientLogger = self.createLog('client', clientPort, False)

        compName = 'stringHub'
        compNum = self.HUB_NUMBER
        compHost = 'localhost'

        cluCfg = MockClusterConfig("clusterFoo")
        cluCfg.addComponent("%s#%d" % (compName, compNum), "java", "",
                            compHost)

        catchall.addExpectedTextRegexp(r'\S+ \S+ \S+ \S+ \S+ \S+ \S+')

        self.cnc = MostlyCnCServer(clusterConfigObject=cluCfg,
                                   copyDir=self.__copyDir,
                                   runConfigDir=self.__runConfigDir,
                                   spadeDir=self.__spadeDir,
                                   logPort=catchall.getPort(),
                                   logFactory=self.__logFactory,
                                   forceRestart=forceRestart)
        t = threading.Thread(name="CnCRun", target=self.cnc.run, args=())
        t.setDaemon(True)
        t.start()

        catchall.checkStatus(100)

        cmdPort = 19001
        mbeanPort = 19002

        if compNum == 0:
            fullName = compName
        else:
            fullName = "%s#%d" % (compName, compNum)

        catchall.addExpectedText('Registered %s' % fullName)

        self.comp = RealComponent(compName, compNum, cmdPort, mbeanPort)

        catchall.checkStatus(100)
        clientLogger.checkStatus(100)

        s = self.cnc.rpc_component_list_dicts()
        self.assertEquals(1, len(s),
                          'Expected 1 listed component, not %d' % len(s))
        self.assertEquals(
            compName, s[0]["compName"],
            'Expected component %s, not %s' % (compName, s[0]["compName"]))
        self.assertEquals(
            compNum, s[0]["compNum"],
            'Expected %s #%d, not #%d' % (compName, compNum, s[0]["compNum"]))
        self.assertEquals(
            compHost, s[0]["host"], 'Expected %s#%d host %s, not %s' %
            (compName, compNum, compHost, s[0]["host"]))
        self.assertEquals(
            cmdPort, s[0]["rpcPort"], 'Expected %s#%d cmdPort %d, not %d' %
            (compName, compNum, cmdPort, s[0]["rpcPort"]))
        self.assertEquals(
            mbeanPort, s[0]["mbeanPort"],
            'Expected %s#%d mbeanPort %d, not %d' %
            (compName, compNum, mbeanPort, s[0]["mbeanPort"]))

        compId = s[0]["id"]

        connErr = "No connection map entry for ID#%s %s#%d .*" % \
            (compId, compName, compNum)
        catchall.addExpectedTextRegexp(connErr)

        rcFile = MockRunConfigFile(self.__runConfigDir)

        domList = [
            MockRunConfigFile.createDOM(self.DOM_MAINBOARD_ID),
        ]
        runConfig = rcFile.create([], domList)

        catchall.addExpectedTextRegexp('Loading run configuration .*')
        catchall.addExpectedTextRegexp('Loaded run configuration .*')
        #clientLogger.addExpectedExact('Config %s#%d with %s' %
        #                              (compName, compNum, runConfig))

        catchall.addExpectedTextRegexp(r"Built runset #\d+: .*")

        setId = self.cnc.rpc_runset_make(runConfig, strict=False)
        self.assertEquals('ready', self.comp.getState(),
                          'Unexpected state %s' % self.comp.getState())

        time.sleep(1)

        catchall.checkStatus(100)

        rs = self.cnc.rpc_runset_list(setId)
        for c in rs:
            if compId != rs[0]["id"]:
                continue

            self.assertEquals(
                compName, c["compName"],
                "Component#%d name should be \"%s\", not \"%s\"" %
                (compId, compName, c["compName"]))
            self.assertEquals(
                compNum, c["compNum"],
                ("Component#%d \"%s\" number should be %d," + "not %d") %
                (compId, compName, compNum, c["compNum"]))
            self.assertEquals(compHost, c["host"],
                              ("Component#%d \"%s#%d\" host should be" +
                               " \"%s\", not \"%s\"") %
                              (compId, compName, compNum, compHost, c["host"]))
            self.assertEquals(
                cmdPort, c["rpcPort"],
                ("Component#%d \"%s#%d\" rpcPort should be" +
                 " \"%s\", not \"%s\"") %
                (compId, compName, compNum, cmdPort, c["rpcPort"]))
            self.assertEquals(
                mbeanPort, c["mbeanPort"],
                ("Component#%d \"%s#%d\" mbeanPort should be" +
                 " \"%s\", not \"%s\"") %
                (compId, compName, compNum, mbeanPort, c["mbeanPort"]))

        catchall.checkStatus(100)

        self.comp.setExpectedRunLogPort(clientPort)

        clientLogger.checkStatus(100)
        catchall.checkStatus(100)

        runNum = 444

        clientLogger.addExpectedTextRegexp("Start of log at LOG=log(\S+:%d)" %
                                           clientPort)
        clientLogger.addExpectedExact('Test msg')
        clientLogger.addExpectedText('filename revision date time author')

        catchall.addExpectedText("Starting run #%d with \"%s\"" %
                                 (runNum, cluCfg.configName()))

        catchall.addExpectedTextRegexp(r"Version info: \S+ \d+" +
                                       r" \S+ \S+ \S+ \S+ \d+\S*")
        catchall.addExpectedText("Run configuration: %s" % runConfig)
        catchall.addExpectedText("Cluster configuration: %s" %
                                 cluCfg.configName())

        moniType = RunOption.MONI_TO_NONE

        clientLogger.addExpectedExact('Start #%d on %s#%d' %
                                      (runNum, compName, compNum))

        catchall.addExpectedText("Starting run %d..." % runNum)

        global ACTIVE_WARNING
        if not LIVE_IMPORT and not ACTIVE_WARNING:
            ACTIVE_WARNING = True
            catchall.addExpectedText("Cannot import IceCube Live code, so" +
                                     " per-string active DOM stats wil not" +
                                     " be reported")

        self.assertEqual(
            self.cnc.rpc_runset_start_run(setId, runNum, moniType), 'OK')

        clientLogger.checkStatus(100)
        catchall.checkStatus(100)

        clientLogger.addExpectedExact('Stop %s#%d' % (compName, compNum))

        numEvts = 0
        numSecs = 0
        numMoni = 0
        numSN = 0
        numTcals = 0

        catchall.addExpectedText("%d physics events collected in %d seconds" %
                                 (numEvts, numSecs))
        catchall.addExpectedText("%d moni events, %d SN events, %d tcals" %
                                 (numMoni, numSN, numTcals))
        catchall.addExpectedText("Run terminated SUCCESSFULLY.")

        if forceRestart:
            catchall.addExpectedText("Cycling components [%s]" % self.comp)

        self.assertEqual(self.cnc.rpc_runset_stop_run(setId), 'OK')

        clientLogger.checkStatus(100)
        catchall.checkStatus(100)

        if forceRestart:
            try:
                rs = self.cnc.rpc_runset_list(setId)
                self.fail("Runset #%d should have been destroyed" % setId)
            except CnCServerException:
                pass
            self.assertEqual(self.cnc.rpc_component_count(), 0)
            self.assertEqual(self.cnc.rpc_runset_count(), 0)
        else:
            self.assertEqual(len(self.cnc.rpc_runset_list(setId)), 1)
            self.assertEqual(self.cnc.rpc_component_count(), 0)
            self.assertEqual(self.cnc.rpc_runset_count(), 1)

            serverAppender = MostlyCnCServer.APPENDERS['server']

            self.assertEquals(self.cnc.rpc_runset_break(setId), 'OK')

            serverAppender.checkStatus(100)

            self.assertEqual(self.cnc.rpc_component_count(), 1)
            self.assertEqual(self.cnc.rpc_runset_count(), 0)

            serverAppender.checkStatus(100)

        clientLogger.checkStatus(100)
        catchall.checkStatus(100)

    def testEverything(self):
        self.__runEverything(False)

    def testEverythingAgain(self):
        if sys.platform != 'darwin':
            print 'Skipping server tests in non-Darwin OS'
            return

        self.__runEverything(False)

    def testForceRestart(self):
        if sys.platform != 'darwin':
            print 'Skipping server tests in non-Darwin OS'
            return

        self.__runEverything(True)
Esempio n. 14
0
    def setUp(self):
        TestDAQRun.LOG_DIR = tempfile.mkdtemp()

        self.__logFactory = SocketReaderFactory()
Esempio n. 15
0
class TestDAQRun(unittest.TestCase):
    NEXTPORT = 9876
    CLUSTER_CONFIG = 'sim-localhost'
    SPADE_DIR = '/tmp'
    LOG_DIR = None

    def __createLoggers(self, dr):
        appender = MockAppender('main')
        dr.setFileAppender(appender)

        catchall = self.__logFactory.createLog('catchall', DAQPort.CATCHALL,
                                               False)
        StubbedDAQRun.setLogSocketServer(catchall)

        return (appender, catchall)

    def __finishRunThreadTest(self, dr, cnc, appender, catchall, ebID, sbID,
                              comps):
        LOG_INFO = False

        time.sleep(0.4)
        self.assertEquals('STOPPED', dr.runState, 'Should be stopped, not ' +
                          dr.runState)

        setId = 1
        runNum = 654
        configName = 'sim5str'

        if LOG_INFO:
            catchall.addExpectedText('Loaded global configuration "%s"' %
                                     configName)
            catchall.addExpectedText('Configuration includes detector in-ice')
            catchall.addExpectedText('Configuration includes detector icetop')

            compSrt = comps[:]
            compSrt.sort(self.__sortCompTuple)

            for c in compSrt:
                catchall.addExpectedText('Component list will require %s#%d' %
                                         (c[1], c[2]))

        catchall.addExpectedText(('Starting run %d (waiting for required %d' +
                                  ' components to register w/ CnCServer)') %
                                 (runNum, len(comps)))
        catchall.addExpectedText('Created Run Set #%d' % setId)

        appender.addExpectedExact(('Version info: %(filename)s %(revision)s' +
                                 ' %(date)s %(time)s %(author)s %(release)s' +
                                 ' %(repo_rev)s') % dr.versionInfo)
        appender.addExpectedExact('Starting run %d...' % runNum)
        appender.addExpectedExact('Run configuration: %s' % configName)
        appender.addExpectedExact('Cluster configuration: %s' %
                                TestDAQRun.CLUSTER_CONFIG)
        if LOG_INFO:
            appender.addExpectedExact('Created logger for CnCServer')
            appender.addExpectedExact('Setting up logging for %d components' %
                                      len(comps))

            nextPort = DAQPort.RUNCOMP_BASE
            for c in compSrt:
                appender.addExpectedExact('%s(%d %s:%d) -> %s:%d' %
                                          (c[1], c[0], c[3], c[4], dr.ip,
                                           nextPort))
                nextPort += 1
            appender.addExpectedExact('Configuring run set...')

        appender.addExpectedExact('Started run %d on run set %d' %
                                (runNum, setId))

        dr.rpc_start_run(runNum, None, configName)

        numTries = 0
        while dr.runState == 'STARTING' and numTries < 500:
            time.sleep(0.1)
            numTries += 1
        self.assertEquals('RUNNING', dr.runState, 'Should be running, not ' +
                          dr.runState)
        self.failIf(cnc.RSBreakFlag, 'Runset should not have been broken')
        self.failUnless(cnc.RSConfigFlag, 'Runset was not configured')
        self.failUnless(cnc.RSStartFlag, 'Runset was not started')
        catchall.checkStatus(10)
        cnc.resetFlags()

        numTries = 0
        while not appender.isEmpty() and numTries < 500:
            time.sleep(0.1)
            numTries += 1

        appender.checkStatus(10)

        subRunId = 1
        domList = [('53494d550101', 0, 1, 2, 3, 4),
                   ['1001', '22', 1, 2, 3, 4, 5],
                   ('a', 0, 1, 2, 3, 4)]

        flashList = [domList[0], ['53494d550122', ] + domList[1][2:]]
        appender.addExpectedExact(("Subrun %d: will ignore missing DOM ('DOM" +
                                 " %s not found in config!')...") %
                                (subRunId, domList[2][0]))
        appender.addExpectedExact('Subrun %d: flashing DOMs (%s)' %
                                (subRunId, str(flashList)))

        dr.rpc_flash(subRunId, domList)
        self.failUnless(cnc.RSFlashFlag, 'Runset should have flashed')
        appender.checkStatus(10)
        catchall.checkStatus(10)

        numEvts = 17
        numMoni = 222
        numSN = 51
        numTCal = 93

        dr.moni.addEntry(ebID, 'backEnd', 'NumEventsSent', str(numEvts))
        dr.moni.addEntry(sbID, 'moniBuilder', 'TotalDispatchedData',
                         str(numMoni))
        dr.moni.addEntry(sbID, 'snBuilder', 'TotalDispatchedData', str(numSN))
        dr.moni.addEntry(sbID, 'tcalBuilder', 'TotalDispatchedData',
                         str(numTCal))

        appender.addExpectedExact('Stopping run %d' % runNum)
        appender.addExpectedExact('%d physics events collected in 0 seconds' %
                                numEvts)
        appender.addExpectedExact('%d moni events, %d SN events, %d tcals' %
                                (numMoni, numSN, numTCal))
        if LOG_INFO:
            appender.addExpectedExact('Stopping component logging')
            appender.addExpectedExact('RPC Call stats:\n%s' % cnc.showStats())
        appender.addExpectedExact('Run terminated SUCCESSFULLY.')
        if LOG_INFO:
            appender.addExpectedExact(('Queueing data for SPADE (spadeDir=%s,' +
                                       ' logDir=%s, runNum=%s)...') %
                                      (TestDAQRun.SPADE_DIR, TestDAQRun.LOG_DIR,
                                       runNum))

        dr.rpc_stop_run()

        numTries = 0
        while dr.runState == 'STOPPING' and numTries < 100:
            time.sleep(0.1)
            numTries += 1
        self.assertEquals('STOPPED', dr.runState, 'Should be stopped, not ' +
                          dr.runState)
        self.failUnless(cnc.RSStopFlag, 'Runset was not stopped')
        appender.checkStatus(10)
        catchall.checkStatus(10)
        cnc.resetFlags()

        moni = dr.rpc_run_monitoring()
        self.failIf(moni is None, 'rpc_run_monitoring returned None')
        self.failIf(len(moni) == 0, 'rpc_run_monitoring returned no data')
        self.assertEquals(numEvts, moni['physicsEvents'],
                          'Expected %d physics events, not %d' %
                          (numEvts, moni['physicsEvents']))
        self.assertEquals(numMoni, moni['moniEvents'],
                          'Expected %d moni events, not %d' %
                          (numMoni, moni['moniEvents']))
        self.assertEquals(numSN, moni['snEvents'],
                          'Expected %d sn events, not %d' %
                          (numSN, moni['snEvents']))
        self.assertEquals(numTCal, moni['tcalEvents'],
                          'Expected %d tcal events, not %d' %
                          (numTCal, moni['tcalEvents']))
        appender.checkStatus(10)
        catchall.checkStatus(10)

        if LOG_INFO:
            catchall.addExpectedText('Breaking run set...')

        dr.rpc_release_runsets()
        self.failUnless(cnc.RSBreakFlag, 'Runset should have been broken')
        appender.checkStatus(10)
        cnc.resetFlags()

        dr.running = False
        time.sleep(0.4)

        numTries = 0
        while not catchall.isEmpty() and numTries < 500:
            time.sleep(0.1)
            numTries += 1

        appender.checkStatus(10)
        catchall.checkStatus(10)

    def __sortCompTuple(self, x, y):
        if x[1] == 'stringHub' and y[1] != 'stringHub':
            return 1
        elif x[1] != 'stringHub' and y[1] == 'stringHub':
            return -1

        if x[0] == y[0]:
            return 0
        elif x[0] < y[0]:
            return -1
        else:
            return 1

    def setUp(self):
        TestDAQRun.LOG_DIR = tempfile.mkdtemp()

        self.__logFactory = SocketReaderFactory()

    def tearDown(self):
        self.__logFactory.tearDown()

        for root, dirs, files in os.walk(TestDAQRun.LOG_DIR,
                                         topdown=False):
            for name in files:
                os.remove(os.path.join(root, name))
            for name in dirs:
                os.rmdir(os.path.join(root, name))

        os.rmdir(TestDAQRun.LOG_DIR)
        TestDAQRun.LOG_DIR = None

    def testParseCompName(self):
        lst = (('ID#1 foo#0 at localhost:12345 ',
                (1, 'foo', 0, 'localhost', 12345)),
               ('ID#22 bar#11 at 192.168.1.10:54321',
                (22, 'bar', 11, '192.168.1.10', 54321)),
               ('bad', ()))

        for l in lst:
            t = DAQRun.parseComponentName(l[0])
            self.assertEqual(len(l[1]), len(t),
                             'Expected %d-element tuple, but got %d elements' %
                             (len(l[1]), len(t)))
            for n in range(0, len(t)):
                self.assertEquals(l[1][n], t[n],
                                  'Expected element#%d to be "%s", not "%s"' %
                                  (n, str(l[1][n]), str(t[n])))

    def testGetNameList(self):
        lst = (('ID#1 foo#0 at localhost:12345 ',
                (1, 'foo', 0, 'localhost', 12345)),
               ('ID#22 bar#11 at 192.168.1.10:54321',
                (22, 'bar', 11, '192.168.1.10', 54321)),
               ('bad', ()))

        names = []
        vals = []
        for l in lst:
            names.append(l[0])
            if len(l[1]) > 0:
                vals.append('%s#%d' % (l[1][1], l[1][2]))

        nlst = list(DAQRun.getNameList(names))
        self.assertEqual(len(vals), len(nlst),
                         'Expected %d-element list, but got %d elements' %
                         (len(vals), len(nlst)))
        for n in range(0, len(nlst)):
            self.assertEquals(vals[n], nlst[n],
                              'Expected element#%d to be "%s", not "%s"' %
                              (n, str(vals[n]), str(nlst[n])))

    def testFindMissing(self):
        required = ['abc#1', 'def#2', 'ghi#3']

        tooFew = []
        missing = None
        for r in required:
            if len(tooFew) == 0 or missing is not None:
                tooFew.append(r)
            else:
                missing = r

        waitList = DAQRun.findMissing(required, tooFew)
        self.assertEquals(1, len(waitList),
                          "Expected ['%s'], not %s" % (missing, str(waitList)))
        self.assertEquals(missing, waitList[0], "Expected '%s' not '%s'" %
                          (missing, waitList[0]))

        justRight = []
        for r in required:
            justRight.append(r)

        waitList = DAQRun.findMissing(required, justRight)
        self.assertEquals(0, len(waitList), "Did not expect %s" % str(waitList))

        tooMany = []
        for r in required:
            tooMany.append(r)
        tooMany.append('jkl#9')

        waitList = DAQRun.findMissing(required, tooMany)
        self.assertEquals(0, len(waitList), "Did not expect %s" % str(waitList))

    def testCreate(self):
        MostlyDAQRun()

    def testWaitForRequiredBad(self):
        comps = [(0, 'abc', 1, 'xxx', 1, 2),
                 (1, 'def', 2, 'yyy', 3, 4),
                 (2, 'ghi', 3, 'zzz', 5, 6)]

        dr = MostlyDAQRun()

        logger = MockLogger('main')
        dr.log = logger

        cnc = MockCnCRPC()
        cnc.setComponents(comps[1:])

        expRunNum = 100
        expId = 123

        dr.runStats.runNum = expRunNum
        dr.runSetID = expId

        DAQRun.COMP_TOUT = 0

        required = []
        for c in comps:
            required.append('%s#%d' % (c[1], c[2]))

        logger.addExpectedExact(('Starting run %d (waiting for required %d' +
                                 ' components to register w/ CnCServer)') %
                                (dr.runStats.runNum, len(required)))

        try:
            dr.build_run_set(cnc, required)
            self.fail('Unexpected success')
        except Exception, e:
            self.assertEquals('Still waiting for ' + required[0], str(e),
                              'Unexpected exception message "%s"' % str(e))

        logger.checkStatus(10)