Exemplo n.º 1
0
    def test_logging(self):
        """
        calls quilt_status with an invalid key and a specification
        for a log file, then Checks that the log file exists
        """

        # create a randomize log file name in /tmp
        rnum = random.randint(10000, 99999)
        logname = "log" + str(rnum) + ".log"
        logpath = os.path.join('/tmp', logname)

        # call quilt submit -y logging_not_a_pattern -l DEBUG --log-file
        #   random_log_file
        queryStr = 'logging_not_a_pattern'
        quilt_test_core.call_quilt_script('quilt_submit.py',
                                          [queryStr, '--log-file', logpath],
                                          whichReturn=sei_core.EXITCODE,
                                          checkCall=False)

        # check resulting log file contains "logging_not_a_pattern"
        exists = 0 == sei_core.run_process(
            'grep "' + queryStr + '" "' + logpath + '" > /dev/null', shell=True,
            whichReturn=sei_core.EXITCODE, checkCall=False)
        self.assertTrue(exists)

        # delete the temp log file
        os.remove(logpath)
Exemplo n.º 2
0
def get_source_name(partialName):
    # call quilt status and parse out the name of the syslog source
    # fix with ISSUE008
    cmd = os.path.join(get_quilt_lib_dir(), "quilt_status.py") + (
        " | grep " + partialName +
        " | head -n 1 | awk '{print $1}' | sed  -e \"s/{'//\" -e \"s/'://\" -e \"s/'//g\" ")
    srcName = sei_core.run_process(cmd, whichReturn=sei_core.STDOUT,
        logToPython=False, shell=True)
    return srcName
Exemplo n.º 3
0
    def test_basic_status(self):

        quilt_lib_dir = quilt_test_core.get_quilt_lib_dir()
        # assemble the filename for query master
        quilt_status_file = os.path.join(quilt_lib_dir, 'quilt_status.py')

        # call quilt_status (check return code, capture output)
        out = sei_core.run_process([quilt_status_file, '-l', 'DEBUG'],
            whichReturn=sei_core.STDOUT, logToPython=False)

        # assure that name of the test source manager appears in the
        # output, test source name specified in testing 
        # config smd.d dir
        cfg = quilt_core.QuiltConfig()
        smgrs = cfg.GetSourceManagers()
        for smgr in smgrs:
            self.assertTrue(smgr in out)

        # check qmd to be sure that all quilt_status's have 
        # unregistered when process exits
        with quilt_core.GetQueryMasterProxy(cfg) as qm:
            qs = qm.GetClients("QuiltStatus")
            self.assertTrue(len(qs) == 0)
Exemplo n.º 4
0
def call_quilt_script(scriptName, args=None, checkCall=True,
                      whichReturn=sei_core.STDOUT):
    """
    returns the standard output of the script, checks for bad error code and
    throws exception
        scriptName          # base filename of the script in the quilt lib
                            #   directory
        args                # arguments to pass to the script):
    """
    if args is None:
        args = []

    quilt_lib_dir = get_quilt_lib_dir()
    # assemble the filename for query master
    script_file = os.path.join(quilt_lib_dir, scriptName)

    # call quilt_status (check return code, capture output)
    args = [script_file, '-l', 'DEBUG'] + args
    # print("Executing under test: " + str(args))
    out = sei_core.run_process(args,
        whichReturn=whichReturn, logToPython=False,
        checkCall=checkCall)

    return out
Exemplo n.º 5
0
    def Query(self, queryId, sourceQuerySpec,
            queryClientName,
            queryClientNamseServerHost,
            queryClientNamseServerPort):
        """
        Query the source, currently hardcoded to behave as calling a cmdline
        tool that outputs source results on each output line
        """

        try:
            with self._lock:
                # set last query member to queryId
                self._lastQuery = queryId

            # _sourceName should not change, set at init, so ok to read
            # without lock
            logging.info("Source Manager: " + str(self._sourceName) +
                         " received query: " + str(queryId))

            # get the sourcePatternSpec from the pattern name in the
            #   sourceQuerySpec
            # pattern spec should be read only and safe to access without
            #   a lock
            srcPatSpecs = quilt_data.src_spec_get(self._sourceSpec,
                sourcePatterns=True)
            srcPatSpec = quilt_data.src_pat_specs_get(srcPatSpecs,
                quilt_data.src_query_spec_get(sourceQuerySpec,
                    srcPatternName=True))

            # get the variables in the query
            srcQueryVars = quilt_data.src_query_spec_get(sourceQuerySpec,
                variables=True)

            # iterate src query variables map, create a simple var name to
            #   var value map
            varNameValueDict = {}
            for srcVarName, srcVarSpec in srcQueryVars.items():
                varNameValueDict[srcVarName] = quilt_data.var_spec_get(
                    srcVarSpec, value=True)

            # create cmd line for the source
            # use the template in the sourcePatternSpec, and use the values
            #   provided for the variables, and environment variables
            replacements = {}
            # replacements = os.environ.copy()
            for k, v in varNameValueDict.items():
                replacements[k] = v

            # "template" was not added as official schema member because it is
            # specific to this command line case
            templateCmd = srcPatSpec['template']
            template = Template(templateCmd)
            logging.info("Ready to use : " + templateCmd +
                         " with replacements: " + str(replacements))
            cmdline = template.safe_substitute(replacements)

            # setup context for the cmdline stdout callback
            srcQueryId = quilt_data.src_query_spec_get(
                sourceQuerySpec, name=True)
            context = {'queryId': queryId, 'srcQueryId': srcQueryId}

            #           sei_core.run_process_lite(cmdline, shell=True,
            #               outFunc=self.OnGrepLine, outObj=context)

            #           I thought run_process apparently has problems
            sei_core.run_process(cmdline, shell=True,
                whichReturn=sei_core.EXITCODE,
                outFunc=self.OnGrepLine, outObj=context, logToPython=False)

            # Set query result events list in query master using query id
            results = []
            with self._lock:
                if queryId in self._sourceResults:
                    if srcQueryId in self._sourceResults[queryId]:
                        results = list(self._sourceResults[queryId][srcQueryId])

            uri = quilt_core.get_uri(queryClientNamseServerHost,
                queryClientNamseServerPort, queryClientName)

            with Pyro4.Proxy(uri) as query:
                query.AppendSourceQueryResults(srcQueryId, results)
                query.CompleteSrcQuery(srcQueryId)

                # catch exception!
        except Exception, error:
            try:

                # attempt to report error to the query client
                uri = quilt_core.get_uri(queryClientNamseServerHost,
                    queryClientNamseServerPort, queryClientName)
                srcQueryId = quilt_data.src_query_spec_get(
                    sourceQuerySpec, name=True)

                with Pyro4.Proxy(uri) as query:
                    query.OnSourceQueryError(srcQueryId, error)

            except Exception, error2:
                logging.error("Unable to send source query error to " +
                              "query master")
                logging.exception(error2)