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_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_000_log_filter_construct_destruct(self): """Test LogFilterProcess constructing and destructing raises no errors.""" 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) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, log_path) self.assertFalse(self.uut.is_started(), "Expected process not started, found started") self.assertFalse(self.uut.is_running(), "Expected process to not running, found running")
def test_121_load_filter_file_adds_new_filter(self): """Verifies can add new filter file.""" 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) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, self.mock_parser, log_path) self._append_to_log_file(log_path) self.uut._pre_run_hook() self.uut.send_command(log_process.CMD_ADD_NEW_FILTER, filter_file) wait_for_queue_writes(self.command_queue) self.uut._do_work() # loads filter file self.uut._post_run_hook() self.mock_parser.load_filter_file.assert_called_once_with(filter_file)
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_120_load_filter_file_returns_error(self): """Verifies ParserError seen by method _do_work.""" 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) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, self.mock_parser, log_path) self._append_to_log_file(log_path) self.mock_parser.load_filter_file.side_effect = errors.ParserError( "Adding new filter failed") with self.assertRaisesRegex(errors.ParserError, "Adding new filter failed"): self.uut._pre_run_hook() self.uut.send_command(log_process.CMD_ADD_NEW_FILTER, filter_file) wait_for_queue_writes(self.command_queue) self.uut._do_work() # loads filter file
def test_100_log_filter_rejects_invalid_command(self): """Test LogFilterProcess rejects invalid command.""" 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) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, log_path) self.command_queue.put(("invalid cmd", None)) wait_for_queue_writes(self.command_queue) self.uut._pre_run_hook() with self.assertRaisesRegex(RuntimeError, "received an unknown command"): self.uut._do_work() self.uut._post_run_hook()
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 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_101_log_filter_accepts_valid_common_commands(self): """Test LogFilterProcess send_command accepts valid common commands.""" 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) self.uut = log_process.LogFilterProcess("fake_device", self.exception_queue, self.command_queue, parser_obj, log_path) for command in log_process._VALID_COMMON_COMMANDS: self.uut.send_command(command) wait_for_queue_writes(self.command_queue) self.assertFalse( self.command_queue.empty(), "Expected command queue {} to not be empty".format( self.command_queue)) command_message = self.command_queue.get() self.assertEqual( command, command_message[0], "Expected command {} found {}".format(command, command_message[0]))
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()