def test_210_log_filter_rotates_log_file_only(self): """Test LogFilterProcess rotates to next log file only.""" filters = [] parser_obj = event_parser_default.EventParserDefault( filters, event_file_path="/foo.txt", device_name="device-1234") old_log_path = os.path.join(self.artifacts_directory, self._testMethodName, "fake-device.txt") next_log_path = os.path.join(self.artifacts_directory, self._testMethodName, "fake-device.00001.txt") old_event_path = log_process.get_event_filename(old_log_path) next_event_path = log_process.get_event_filename(next_log_path) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, old_log_path) self._append_to_log_file(old_log_path) self._append_to_log_file(next_log_path) self.uut._pre_run_hook() self.uut._do_work( ) # Opens old log and event files and processes first line self._append_to_log_file(old_log_path, log_line=_ROTATE_LOG_MESSAGE) self.uut._do_work() # Process next line in old log file and closes it self.assertTrue(os.path.exists(old_event_path), "Expected {} to exist".format(old_event_path)) self.assertFalse(os.path.exists(next_event_path), "Expected {} to not exist".format(next_event_path)) self.uut._do_work( ) # Opens next log and event files and processes first line self.assertTrue(os.path.exists(old_event_path), "Expected {} to exist".format(old_event_path)) self.assertFalse(os.path.exists(next_event_path), "Expected {} to not exist".format(next_event_path)) self.uut._post_run_hook()
def test_002_log_filter_creates_event_file(self): """Test log filter creates event file after opening log file.""" filter_file = os.path.join(self.TEST_FILTER_DIR, "optional_description.json") parser_obj = event_parser_default.EventParserDefault( [filter_file], event_file_path="/foo.txt", device_name="device-1234") log_file_name = self._testMethodName + ".txt" log_path = os.path.join(self.artifacts_directory, log_file_name) event_path = log_process.get_event_filename(log_path) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, log_path) self._append_to_log_file(log_path) self.uut._pre_run_hook() self.uut._do_work() # opens log file self.uut._do_work() # writes first event filesize = get_file_size(event_path) self.uut._post_run_hook() self.assertTrue( os.path.exists(event_path), "Expected event file {} to exist, but it doesn't exist".format( event_path)) self.assertGreater( filesize, 0, "Expected event file {} size > 0 found {}".format( event_path, filesize))
def test_201_log_filter_ignores_extra_new_log_file_message(self): """Test LogFilterProcess ignores spurious new log file message.""" filters = [] parser_obj = event_parser_default.EventParserDefault( filters, event_file_path="/foo.txt", device_name="device-1234") old_log_path = os.path.join(self.artifacts_directory, self._testMethodName, "fake-device-old.txt") new_log_path = os.path.join(self.artifacts_directory, self._testMethodName, "fake-device-new.txt") old_event_path = log_process.get_event_filename(old_log_path) new_event_path = log_process.get_event_filename(new_log_path) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, old_log_path) self._append_to_log_file(old_log_path) self._append_to_log_file(new_log_path) self.uut._pre_run_hook() self.uut._do_work( ) # Opens old log and event files and processes first line self._append_to_log_file(old_log_path, log_line=_NEW_LOG_FILE_MESSAGE) self.uut._do_work( ) # Process next line in and tries to switch to next log self.assertTrue(os.path.exists(old_event_path), "Expected {} to exist".format(old_event_path)) self.assertFalse(os.path.exists(new_event_path), "Expected {} to not exist".format(new_event_path)) switchboard_process.put_message( self.command_queue, (log_process.CMD_NEW_LOG_FILE, new_log_path)) wait_for_queue_writes(self.command_queue) self._append_to_log_file(old_log_path, log_line=_NEW_LOG_FILE_MESSAGE) self.uut._do_work() # Process next line in old log file and closes it self.uut._do_work( ) # Opens new log and event files and processes first line self.assertTrue(os.path.exists(old_event_path), "Expected {} to exist".format(old_event_path)) self.assertTrue(os.path.exists(new_event_path), "Expected {} to exist".format(new_event_path)) self.uut._post_run_hook()
def _update_event_filename_and_symlinks(self): self.log_file_symlink = None if self.log_directory == config.DEFAULT_LOG_DIRECTORY: log_symlink_name = "{}-latest.txt".format(self.name) self.log_file_symlink = create_symlink(self._log_file_name, log_symlink_name) self.event_file_name = log_process.get_event_filename(self._log_file_name) self.event_file_symlink = None if self.log_directory == config.DEFAULT_LOG_DIRECTORY: event_symlink_name = "{}-latest-events.txt".format(self.name) self.event_file_symlink = create_symlink(self.event_file_name, event_symlink_name)
def __init__(self, parser_obj, log_path, display_refresh=DISPLAY_REFRESH): """Initialize LogParser class using provided information. Args: parser_obj (Parser): Instance of class Parser log_path (str): Path to log filename containing raw, log event data display_refresh (float): Number of seconds to wait prior to refresh of display Raises: ParserError: If log_path does NOT exist If event_filename already exists If parser_object is None Note: Since the provided log_path is immediately parsed, initializing LogParser using log files exceeding 100 MB can cause test applications to appear to be delayed. The smaller the log file the faster initialization will be. """ if parser_obj is None: raise errors.ParserError("Log parser parameter check failed. " "Bad parser_obj.") if not os.path.isfile(log_path): raise errors.ParserError( "LogParser parameter check failed. " "log file name: {} does not exist.".format(log_path)) self.event_filename = log_process.get_event_filename(log_path) parser_obj.event_file_path = self.event_filename if os.path.isfile(self.event_filename): raise errors.ParserError("LogParser parameter check failed. " "event_filename: {} already exists.".format( self.event_filename)) if display_refresh < 0: raise errors.ParserError( "LogParser parameter check failed. " "Expected display refresh >=0 instead got: {}".format( display_refresh)) self._parser_obj = parser_obj self._parse_events(log_path, display_refresh)
def test_001_log_filter_cant_open_log_file(self): """Test log filter unable to open log file.""" filters = [] parser_obj = event_parser_default.EventParserDefault( filters, event_file_path="/foo.txt", device_name="device-1234") log_file_name = self._testMethodName + ".txt" log_path = os.path.join(self.artifacts_directory, log_file_name) event_path = log_process.get_event_filename(log_path) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, log_path) self.uut._pre_run_hook() self.uut._do_work() self.uut._post_run_hook() self.assertFalse( os.path.exists(event_path), "Expected event file {} to not exist, but it exists".format( event_path))
def test_004_log_filter_handles_line_return_chars(self): """Test log filter tails log file for new lines added.""" filter_file = os.path.join(self.TEST_FILTER_DIR, "optional_description.json") log_file_name = self._testMethodName + ".txt" log_path = os.path.join(self.artifacts_directory, log_file_name) event_path = log_process.get_event_filename(log_path) parser_obj = event_parser_default.EventParserDefault( [filter_file], event_file_path=event_path, device_name="device-1234") self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, log_path) self._append_to_log_file(log_path, log_line=_EVENT_LINE_RETURN_LOG_MESSAGE) self.uut._pre_run_hook() self.uut._do_work() # opens log file self.uut._do_work() # skips writing event due to missing \n filesize1 = get_file_size(event_path) self.uut._do_work() # reads no log line self._append_to_log_file(log_path, log_line="\r\n") self.uut._do_work() # writes first event filesize2 = get_file_size(event_path, size=filesize1) self.uut._post_run_hook() self.assertTrue( os.path.exists(event_path), "Expected event file {} to exist, but it doesn't exist".format( event_path)) self.assertEqual( 0, filesize1, "Expected event file {} to be size 0 but found {}".format( event_path, filesize1)) self.assertLess( filesize1, filesize2, "Expected event file {} to grow from size {}, but found {}".format( event_path, filesize1, filesize2))
def test_003_log_filter_tails_log_file(self): """Test log filter tails log file for new lines added.""" filter_file = os.path.join(self.TEST_FILTER_DIR, "optional_description.json") log_file_name = self._testMethodName + ".txt" log_path = os.path.join(self.artifacts_directory, log_file_name) event_path = log_process.get_event_filename(log_path) parser_obj = event_parser_default.EventParserDefault( [filter_file], event_file_path=event_path, device_name="device-1234") self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, log_path) self._append_to_log_file(log_path) self.uut._pre_run_hook() self.uut._do_work() # opens log file self.uut._do_work() # writes first event filesize1 = get_file_size(event_path) self.uut._do_work() # reads no log line self._append_to_log_file(log_path) self.uut._do_work() # writes second event filesize2 = get_file_size(event_path, size=filesize1) self.uut._post_run_hook() self.assertTrue( os.path.exists(event_path), "Expected event file {} to exist, but it doesn't exist".format( event_path)) self.assertGreater( filesize2, filesize1, "Expected event file {} to grow from size {}, but found {}".format( event_path, filesize1, filesize2))
def test_211_log_filter_new_log_message_doesnt_trigger_rotate(self): """Test rotate log message appears after new log file command.""" filters = [] parser_obj = event_parser_default.EventParserDefault( filters, event_file_path="/foo.txt", device_name="device-1234") old_log_path = os.path.join(self.artifacts_directory, self._testMethodName, "fake-device-old.txt") old_event_path = log_process.get_event_filename(old_log_path) new_log_path = os.path.join(self.artifacts_directory, self._testMethodName, "fake-device-new.txt") new_event_path = log_process.get_event_filename(new_log_path) next_log_path1 = os.path.join(self.artifacts_directory, self._testMethodName, "fake-device-old.00001.txt") next_event_path1 = log_process.get_event_filename(next_log_path1) next_log_path2 = os.path.join(self.artifacts_directory, self._testMethodName, "fake-device-new.00001.txt") next_event_path2 = log_process.get_event_filename(next_log_path2) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, old_log_path) self._append_to_log_file(old_log_path) self._append_to_log_file(next_log_path1) self._append_to_log_file(new_log_path) self.uut._pre_run_hook() self.uut._do_work( ) # Opens old log and event files and processes first line switchboard_process.put_message( self.command_queue, (log_process.CMD_NEW_LOG_FILE, new_log_path)) wait_for_queue_writes(self.command_queue) self._append_to_log_file(old_log_path, log_line=_ROTATE_LOG_MESSAGE) self.uut._do_work( ) # Process next line in old log file and rotates log file self.assertTrue(os.path.exists(old_event_path), "Expected {} to exist".format(old_event_path)) self.assertFalse(os.path.exists(new_event_path), "Expected {} to not exist".format(new_event_path)) self.assertFalse(os.path.exists(next_event_path1), "Expected {} to not exist".format(next_event_path1)) self.assertFalse(os.path.exists(next_event_path2), "Expected {} to not exist".format(next_event_path2)) self._append_to_log_file(next_log_path1, log_line=_NEW_LOG_FILE_MESSAGE) self.uut._do_work( ) # Process next line in next log file 1 and opens new log file self.assertTrue(os.path.exists(old_event_path), "Expected {} to exist".format(old_event_path)) self.assertFalse(os.path.exists(new_event_path), "Expected {} to not exist".format(new_event_path)) self.assertFalse(os.path.exists(next_event_path1), "Expected {} to not exist".format(next_event_path1)) self.assertFalse(os.path.exists(next_event_path2), "Expected {} to not exist".format(next_event_path2)) self.uut._do_work() # Keeps reading from new log file self.assertTrue(os.path.exists(old_event_path), "Expected {} to exist".format(old_event_path)) self.assertTrue(os.path.exists(new_event_path), "Expected {} to exist".format(new_event_path)) self.assertFalse(os.path.exists(next_event_path1), "Expected {} to not exist".format(next_event_path1)) self.assertFalse(os.path.exists(next_event_path2), "Expected {} to not exist".format(next_event_path2)) self.uut._post_run_hook()