Beispiel #1
0
    def test_cachetime(self):
        """--cachetime
        """
        self.config["pattern_list"] = ["ERROR"]
        self.config["cachetime"] = 2
        log = LogChecker(self.config)

        cachefile = log._create_cache_filename(self.logfile)

        # within cachetime
        # Dec  5 12:34:50 hostname test: ERROR
        line = self._make_line(self._get_timestamp(), "test", "ERROR")
        self._write_logfile(self.logfile, line)

        # check
        log.clear_state()
        log.check(self.logfile)

        self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
        self.assertEqual(
            log.get_message(),
            self.MESSAGE_WARNING_ONE.format(line, self.logfile))

        # check again
        log.clear_state()
        log.check(self.logfile)

        self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
        self.assertEqual(
            log.get_message(),
            self.MESSAGE_WARNING_ONE.format(line, self.logfile))

        log._remove_cache(cachefile)

        # over cachetime
        # Dec  5 12:34:50 hostname test: ERROR
        line = self._make_line(self._get_timestamp(), "test", "ERROR")
        self._write_logfile(self.logfile, line)

        log.clear_state()
        log.check(self.logfile)

        self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
        self.assertEqual(
            log.get_message(),
            self.MESSAGE_WARNING_ONE.format(line, self.logfile))

        # check again
        time.sleep(self.config["cachetime"] + 1)
        log.clear_state()
        log.check(self.logfile)

        self.assertEqual(log.get_state(), LogChecker.STATE_OK)

        log._remove_cache(cachefile)
Beispiel #2
0
    def test_cachetime_with_dry_run(self):
        """--cachetime and --dry-run
        """
        self.config["pattern_list"] = ["ERROR"]
        self.config["cachetime"] = 60
        log = LogChecker(self.config)

        cachefile = log._create_cache_filename(self.logfile)

        # create a cache file
        # Dec  5 12:34:50 hostname test: ERROR
        line = self._make_line(self._get_timestamp(), "test", "ERROR")
        self._write_logfile(self.logfile, line)

        log.clear_state()
        log.check(self.logfile)

        self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
        self.assertEqual(
            log.get_message(),
            self.MESSAGE_WARNING_ONE.format(line, self.logfile))

        # verify it does not read a cache file.
        self.config["dry_run"] = True
        log = LogChecker(self.config)

        # Dec  5 12:34:50 hostname test: NOOP
        line = self._make_line(self._get_timestamp(), "test", "NOOP")
        self._write_logfile(self.logfile, line)

        log.clear_state()
        log.check(self.logfile)

        self.assertEqual(log.get_state(), LogChecker.STATE_OK)

        # verify a cache file is not updated.
        self.config["dry_run"] = False
        log = LogChecker(self.config)

        # Dec  5 12:34:50 hostname test: ERROR
        line = self._make_line(self._get_timestamp(), "test", "ERROR")
        self._write_logfile(self.logfile, line)

        log.clear_state()
        log.check(self.logfile)

        self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
        self.assertEqual(
            log.get_message(),
            self.MESSAGE_WARNING_ONE.format(line, self.logfile))

        log._remove_cache(cachefile)