def test_remove_seekfile_with_dry_run(self): """--expiration, --remove-seekfile, and --dry-run options """ self.config["pattern_list"] = ["ERROR"] self.config["scantime"] = 2 self.config["expiration"] = 4 log = LogChecker(self.config) # within expiration # Dec 5 12:34:50 hostname test: ERROR line1 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile1, line1) log.check(self.logfile_pattern, remove_seekfile=True) self.seekfile1 = log._create_seek_filename( self.logfile_pattern, self.logfile1) time.sleep(2) # Dec 5 12:34:54 hostname test: ERROR line2 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile2, line2) # seek file of logfile1 should not be purged. log.clear_state() log.check(self.logfile_pattern, remove_seekfile=True) self.seekfile2 = log._create_seek_filename( self.logfile_pattern, self.logfile2) self.assertEqual(log.get_state(), LogChecker.STATE_WARNING) self.assertEqual( log.get_message(), self.MESSAGE_WARNING_ONE.format(line2, self.logfile2)) self.assertTrue(os.path.exists(self.seekfile1)) self.assertTrue(os.path.exists(self.seekfile2)) # with dry run self.config["dry_run"] = True log = LogChecker(self.config) # over expiration # Dec 5 12:34:50 hostname test: ERROR line1 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile1, line1) log.check(self.logfile_pattern, remove_seekfile=True) time.sleep(6) # Dec 5 12:34:54 hostname test: ERROR line2 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile2, line2) log.clear_state() log.check(self.logfile_pattern, remove_seekfile=True) self.assertEqual(log.get_state(), LogChecker.STATE_WARNING) self.assertEqual( log.get_message(), self.MESSAGE_WARNING_ONE.format(line2, self.logfile2)) self.assertTrue(os.path.exists(self.seekfile1)) self.assertTrue(os.path.exists(self.seekfile2))
def test_remove_seekfile_inode_with_dry_run(self): """--trace_inode, --expiration, --remove-seekfile, and --dry-run options """ self.config["pattern_list"] = ["ERROR"] self.config["trace_inode"] = True self.config["scantime"] = 2 self.config["expiration"] = 3 log = LogChecker(self.config) # create logfile # Dec 5 12:34:50 hostname test: ERROR line1 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile, line1) # log rotation os.rename(self.logfile, self.logfile1) # create new logfile # Dec 5 12:34:50 hostname test: ERROR line2 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile, line2) # do check_log_multi, and create seekfile and seekfile1 log.clear_state() log.check(self.logfile_pattern, remove_seekfile=True) seekfile_1 = log._create_seek_filename(self.logfile_pattern, self.logfile, trace_inode=True) seekfile1_1 = log._create_seek_filename(self.logfile_pattern, self.logfile1, trace_inode=True) time.sleep(4) # update logfile # Dec 5 12:34:54 hostname test: ERROR line3 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile, line3) # log rotation, purge old logfile2 os.rename(self.logfile1, self.logfile2) os.rename(self.logfile, self.logfile1) # with dry run self.config["dry_run"] = True log = LogChecker(self.config) log.clear_state() log.check(self.logfile_pattern, remove_seekfile=True) seekfile1_2 = log._create_seek_filename(self.logfile_pattern, self.logfile1, trace_inode=True) self.assertEqual(log.get_state(), LogChecker.STATE_WARNING) self.assertEqual(log.get_message(), self.MESSAGE_WARNING_ONE.format(line3, self.logfile1)) self.assertEqual(seekfile_1, seekfile1_2) self.assertTrue(os.path.exists(seekfile1_1)) self.assertTrue(os.path.exists(seekfile1_2))
def test_trace_inode(self): """--trace_inode """ self.config["pattern_list"] = ["ERROR"] self.config["trace_inode"] = True log = LogChecker(self.config) # within expiration # create logfile # Dec 5 12:34:50 hostname test: ERROR line1 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile, line1) # create seekfile of logfile log.check(self.logfile_pattern) seekfile_1 = log._create_seek_filename(self.logfile_pattern, self.logfile, trace_inode=True) # update logfile # Dec 5 12:34:51 hostname test: ERROR line2 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile, line2) # log rotation os.rename(self.logfile, self.logfile1) # create a new logfile # Dec 5 12:34:52 hostname noop: NOOP line3 = self._make_line(self._get_timestamp(), "noop", "NOOP") self._write_logfile(self.logfile, line3) # create seekfile of logfile log.clear_state() log.check(self.logfile_pattern) seekfile_2 = log._create_seek_filename(self.logfile_pattern, self.logfile, trace_inode=True) seekfile1_2 = log._create_seek_filename(self.logfile_pattern, self.logfile1, trace_inode=True) self.assertEqual(log.get_state(), LogChecker.STATE_WARNING) self.assertEqual(log.get_message(), self.MESSAGE_WARNING_ONE.format(line2, self.logfile1)) self.assertEqual(seekfile_1, seekfile1_2) self.assertTrue(os.path.exists(seekfile_2)) self.assertTrue(os.path.exists(seekfile1_2))
def test_tag(self): """--tag """ self.config["pattern_list"] = ["ERROR"] log = LogChecker(self.config) # create new logfiles # Dec 5 12:34:50 hostname test: ERROR line1 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile, line1) # Dec 5 12:34:50 hostname test: ERROR line2 = self._make_line(self._get_timestamp(), "test", "ERROR") self._write_logfile(self.logfile1, line2) # Dec 5 12:34:50 hostname test: ERROR line3 = self._make_line(self._get_timestamp(), "noop", "NOOP") self._write_logfile(self.logfile2, line3) # create seekfile of logfile seekfile_1 = log._create_seek_filename(self.logfile_pattern, self.logfile, tag=self.tag1) seekfile_2 = log._create_seek_filename(self.logfile_pattern, self.logfile, tag=self.tag1) seekfile_3 = log._create_seek_filename(self.logfile_pattern, self.logfile, tag=self.tag2) log.check(self.logfile, seekfile=seekfile_3) log.clear_state() log.check(self.logfile_pattern, tag=self.tag2) self.assertEqual(log.get_state(), LogChecker.STATE_WARNING) self.assertEqual(log.get_message(), self.MESSAGE_WARNING_ONE.format(line2, self.logfile1)) self.assertEqual(seekfile_1, seekfile_2) self.assertNotEqual(seekfile_1, seekfile_3) self.assertTrue(seekfile_1.find(self.tag1)) self.assertTrue(os.path.exists(seekfile_3))