def test_gather_sample_max_characters_output_trimming(self):
        monitor_config = {
            "module": "shell_monitor",
            "command": 'echo "foo bar!"',
            "max_characters": 3,
        }
        mock_logger = mock.Mock()
        monitor = ShellMonitor(monitor_config, mock_logger)

        monitor.gather_sample()
        call_args_list = mock_logger.emit_value.call_args_list[0]
        call_args = call_args_list[0]
        call_kwargs = call_args_list[1]

        self.assertEqual(call_args, ("output", "foo..."))
        self.assertEqual(call_kwargs["extra_fields"]["command"], 'echo "foo bar!"')
        self.assertEqual(call_kwargs["extra_fields"]["length"], 9)
    def test_gather_sample_duration_and_non_zero_exit_code(self):
        monitor_config = {
            "module": "shell_monitor",
            "command": 'echo "foo bar!" ; sleep 1; exit 1',
            "max_characters": 200,
        }
        mock_logger = mock.Mock()
        monitor = ShellMonitor(monitor_config, mock_logger)

        monitor.gather_sample()
        call_args_list = mock_logger.emit_value.call_args_list[0]
        call_args = call_args_list[0]
        call_kwargs = call_args_list[1]

        self.assertEqual(call_args, ("output", "foo bar!"))
        self.assertEqual(call_kwargs["extra_fields"]["command"],
                         'echo "foo bar!" ; sleep 1; exit 1')
        self.assertEqual(call_kwargs["extra_fields"]["length"], 9)
        self.assertTrue(call_kwargs["extra_fields"]["duration"], 1)
        self.assertEqual(call_kwargs["extra_fields"]["exit_code"], 1)
    def test_gather_sample_log_all_lines(self):
        monitor_config = {
            "module": "shell_monitor",
            "command": 'echo "line 1\nline 2\nline 3"',
            "max_characters": 100,
            "log_all_lines": True,
        }
        mock_logger = mock.Mock()
        monitor = ShellMonitor(monitor_config, mock_logger)

        monitor.gather_sample()
        call_args_list = mock_logger.emit_value.call_args_list[0]
        call_args = call_args_list[0]
        call_kwargs = call_args_list[1]

        self.assertEqual(call_args, ("output", "line 1\nline 2\nline 3\n"))
        self.assertEqual(call_kwargs["extra_fields"]["command"],
                         'echo "line 1\nline 2\nline 3"')
        self.assertEqual(call_kwargs["extra_fields"]["length"], 21)
        self.assertTrue(call_kwargs["extra_fields"]["duration"] <= 1)
        self.assertEqual(call_kwargs["extra_fields"]["exit_code"], 0)
    def test_gather_sample_stdout_and_stderr_is_combined(self):
        monitor_config = {
            "module": "shell_monitor",
            "command": 'echo "stdout" ; >&2 echo "stderr"',
            "extract": r"line (\d\d\d)",
            "max_characters": 100,
            "log_all_lines": True,
        }
        mock_logger = mock.Mock()
        monitor = ShellMonitor(monitor_config, mock_logger)

        monitor.gather_sample()
        call_args_list = mock_logger.emit_value.call_args_list[0]
        call_args = call_args_list[0]
        call_kwargs = call_args_list[1]

        self.assertEqual(call_args, ("output", "stderr\n\nstdout\n"))
        self.assertEqual(
            call_kwargs["extra_fields"]["command"],
            'echo "stdout" ; >&2 echo "stderr"',
        )
        self.assertEqual(call_kwargs["extra_fields"]["length"], 15)
        self.assertTrue(call_kwargs["extra_fields"]["duration"] <= 1)
        self.assertEqual(call_kwargs["extra_fields"]["exit_code"], 0)