예제 #1
0
 def test_200_log_writer_uses_new_log_file(self):
     """Test switching LogWriterProcess to use new log file specified."""
     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")
     self.uut = log_process.LogWriterProcess("fake_device",
                                             self.exception_queue,
                                             self.command_queue,
                                             self.log_queue, old_log_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.uut._pre_run_hook()  # Open old log file
     self.uut._do_work(
     )  # Process new log file command and opens new log file
     switchboard_process.put_message(self.log_queue, _FULL_LOG_MESSAGE)
     wait_for_queue_writes(self.log_queue)
     self.uut._do_work()  # Writes full log message to new log file
     self.uut._post_run_hook()
     old_lines = self._verify_log_file_and_lines(old_log_path, 1)
     new_lines = self._verify_log_file_and_lines(new_log_path, 1)
     self.assertNotEqual(
         old_lines, new_lines,
         "Expected {!r} == {!r}".format(old_lines, new_lines))
예제 #2
0
 def test_000_log_writer_construct_destruct(self):
     """Test LogWriterProcess constructing and destructing raises no errors."""
     log_file_name = self._testMethodName + ".txt"
     log_path = os.path.join(self.artifacts_directory, log_file_name)
     self.uut = log_process.LogWriterProcess("fake_device",
                                             self.exception_queue,
                                             self.command_queue,
                                             self.log_queue, 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")
예제 #3
0
 def test_010_log_writer_writes_full_log_line(self):
     """Test writing full log line to file."""
     log_file_name = self._testMethodName + ".txt"
     log_path = os.path.join(self.artifacts_directory, log_file_name)
     self.uut = log_process.LogWriterProcess("fake_device",
                                             self.exception_queue,
                                             self.command_queue,
                                             self.log_queue, log_path)
     switchboard_process.put_message(self.log_queue, _FULL_LOG_MESSAGE)
     wait_for_queue_writes(self.log_queue)
     self.uut._pre_run_hook()
     self.uut._do_work()
     self.uut._post_run_hook()
     self._verify_log_file_and_lines(log_path, 1)
예제 #4
0
    def test_100_log_writer_rejects_invalid_command(self):
        """Test LogWriterProcess rejects invalid command."""
        log_file_name = self._testMethodName + ".txt"
        log_path = os.path.join(self.artifacts_directory, log_file_name)
        self.uut = log_process.LogWriterProcess("fake_device",
                                                self.exception_queue,
                                                self.command_queue,
                                                self.log_queue, 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()
예제 #5
0
 def test_011_log_writer_writes_partial_log_line(self):
     """Test writing partial log lines with unicode characters to file."""
     log_file_name = self._testMethodName + ".txt"
     log_path = os.path.join(self.artifacts_directory, log_file_name)
     self.uut = log_process.LogWriterProcess("fake_device",
                                             self.exception_queue,
                                             self.command_queue,
                                             self.log_queue, log_path)
     switchboard_process.put_message(self.log_queue, _PARTIAL_LOG_MESSAGE)
     wait_for_queue_writes(self.log_queue)
     self.uut._pre_run_hook()
     self.uut._do_work()
     self.uut._post_run_hook()
     lines = self._verify_log_file_and_lines(log_path, 1)
     self.assertIn(
         "[NO EOL]", lines[0],
         "Expected '[NO EOL]' at end of partial line found {!r}".format(
             lines[0]))
예제 #6
0
 def test_211_log_writer_new_log_command_handled_before_log_rotate(self):
     """Test new log message could but doesn't trigger rotate log."""
     max_log_size = len(_NEW_LOG_FILE_MESSAGE)
     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")
     next_log_path1 = os.path.join(self.artifacts_directory,
                                   self._testMethodName,
                                   "fake-device-old.00001.txt")
     next_log_path2 = os.path.join(self.artifacts_directory,
                                   self._testMethodName,
                                   "fake-device-new.00001.txt")
     self.uut = log_process.LogWriterProcess("fake_device",
                                             self.exception_queue,
                                             self.command_queue,
                                             self.log_queue,
                                             old_log_path,
                                             max_log_size=max_log_size)
     switchboard_process.put_message(
         self.command_queue, (log_process.CMD_NEW_LOG_FILE, new_log_path))
     wait_for_queue_writes(self.command_queue)
     self.uut._pre_run_hook()  # Opens old log file
     self.uut._do_work(
     )  # Process new log file command and opens new log file
     self.uut._do_work()  # Allows for possible log rotation issue
     self.uut._post_run_hook()
     old_lines = self._verify_log_file_and_lines(old_log_path, 1)
     self._verify_log_file_and_lines(new_log_path, 0)
     self.assertIn(
         log_process.NEW_LOG_FILE_MESSAGE, old_lines[0],
         "Expected {} log message in old log file found {!r}".format(
             log_process.NEW_LOG_FILE_MESSAGE, old_lines))
     self.assertTrue(os.path.exists(new_log_path),
                     "Expected new log file of {}".format(new_log_path))
     self.assertFalse(
         os.path.exists(next_log_path1),
         "Expected no log rotation to {}".format(next_log_path1))
     self.assertFalse(
         os.path.exists(next_log_path2),
         "Expected no log rotation to {}".format(next_log_path2))
예제 #7
0
    def test_101_log_writer_accepts_valid_common_commands(self):
        """Test LogWriterProcess send_command accepts valid common commands."""
        log_file_name = self._testMethodName + ".txt"
        log_path = os.path.join(self.artifacts_directory, log_file_name)
        self.uut = log_process.LogWriterProcess("fake_device",
                                                self.exception_queue,
                                                self.command_queue,
                                                self.log_queue, log_path)

        for command in log_process._VALID_WRITER_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")
            command_message = self.command_queue.get()
            self.assertEqual(
                command, command_message[0],
                "Expected command {} found {}".format(command,
                                                      command_message[0]))
예제 #8
0
 def test_212_log_writer_can_change_max_log_size(self):
     """Test LogWriterProcess can change max_log_size."""
     max_log_size = 0
     old_log_path = os.path.join(self.artifacts_directory,
                                 self._testMethodName,
                                 "fake-device-old.txt")
     next_log_path = os.path.join(self.artifacts_directory,
                                  self._testMethodName,
                                  "fake-device-old.00001.txt")
     self.uut = log_process.LogWriterProcess(
         "fake_device",
         self.exception_queue,
         self.command_queue,
         self.log_queue,
         old_log_path,
         max_log_size=len(_FULL_LOG_MESSAGE))
     switchboard_process.put_message(
         self.command_queue, (log_process.CMD_MAX_LOG_SIZE, max_log_size))
     wait_for_queue_writes(self.command_queue)
     self.uut._pre_run_hook()  # Opens old log file
     self.uut._do_work(
     )  # Process max_log_size command and opens new log file
     switchboard_process.put_message(self.log_queue, _FULL_LOG_MESSAGE)
     wait_for_queue_writes(self.log_queue)
     self.uut._do_work()  # Allows for possible log rotation issue
     self.uut._post_run_hook()
     old_lines = self._verify_log_file_and_lines(old_log_path, 2)
     self.assertIn(
         log_process.CHANGE_MAX_LOG_SIZE, old_lines[0],
         "Expected {} log message in old log file found {!r}".format(
             log_process.CHANGE_MAX_LOG_SIZE, old_lines))
     self.assertIn(
         _FULL_LOG_MESSAGE, old_lines[1],
         "Expected {} log message in old log file found {!r}".format(
             _FULL_LOG_MESSAGE, old_lines))
     self.assertFalse(
         os.path.exists(next_log_path),
         "Expected no log rotation to {}".format(next_log_path))
예제 #9
0
 def test_210_log_writer_rotates_log_file(self):
     """Test LogWriterProcess rotating to new log file."""
     max_log_size = len(_FULL_LOG_MESSAGE)
     old_log_path = os.path.join(self.artifacts_directory,
                                 self._testMethodName, "fake-device.txt")
     new_log_path = os.path.join(self.artifacts_directory,
                                 self._testMethodName,
                                 "fake-device.00001.txt")
     next_log_path = os.path.join(self.artifacts_directory,
                                  self._testMethodName,
                                  "fake-device.00002.txt")
     self.uut = log_process.LogWriterProcess("fake_device",
                                             self.exception_queue,
                                             self.command_queue,
                                             self.log_queue,
                                             old_log_path,
                                             max_log_size=max_log_size)
     switchboard_process.put_message(self.log_queue, _FULL_LOG_MESSAGE)
     wait_for_queue_writes(self.log_queue)
     self.uut._pre_run_hook()
     self.uut._do_work()
     switchboard_process.put_message(self.log_queue, _SHORT_LOG_MESSAGE)
     wait_for_queue_writes(self.log_queue)
     self.uut._do_work()
     self.uut._post_run_hook()
     old_lines = self._verify_log_file_and_lines(old_log_path, 2)
     self._verify_log_file_and_lines(new_log_path, 1)
     self.assertIn(
         log_process.ROTATE_LOG_MESSAGE, old_lines[1],
         "Expected {} log message in old log file found {!r}".format(
             log_process.ROTATE_LOG_MESSAGE, old_lines))
     self.assertTrue(os.path.exists(new_log_path),
                     "Expected log rotation to {}".format(new_log_path))
     self.assertFalse(
         os.path.exists(next_log_path),
         "Expected no log rotation to {}".format(next_log_path))