Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[202012][show] show logging CLI support for logs stored in tmpfs #2652

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,14 +916,18 @@ def table(verbose):
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def logging(process, lines, follow, verbose):
"""Show system log"""
if os.path.exists("/var/log.tmpfs"):
log_path = "/var/log.tmpfs"
else:
log_path = "/var/log"
if follow:
cmd = "sudo tail -F /var/log/syslog"
cmd = "sudo tail -F {}/syslog".format(log_path)
run_command(cmd, display_cmd=verbose)
else:
if os.path.isfile("/var/log/syslog.1"):
cmd = "sudo cat /var/log/syslog.1 /var/log/syslog"
if os.path.isfile("{}/syslog.1".format(log_path)):
cmd = "sudo cat {}/syslog.1 {}/syslog".format(log_path, log_path)
else:
cmd = "sudo cat /var/log/syslog"
cmd = "sudo cat {}/syslog".format(log_path)

if process is not None:
cmd += " | grep '{}'".format(process)
Expand Down
70 changes: 70 additions & 0 deletions tests/show_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import pytest
import show.main as show
from click.testing import CliRunner
from unittest.mock import MagicMock, patch

EXPECTED_BASE_COMMAND = 'sudo '

@patch('show.main.run_command')
@pytest.mark.parametrize(
"cli_arguments,expected",
[
([], 'cat /var/log/syslog'),
(['xcvrd'], "cat /var/log/syslog | grep 'xcvrd'"),
(['-l', '10'], 'cat /var/log/syslog | tail -10'),
(['-f'], 'tail -F /var/log/syslog'),
]
)
def test_show_logging_default(run_command, cli_arguments, expected):
runner = CliRunner()
result = runner.invoke(show.cli.commands["logging"], cli_arguments)
run_command.assert_called_with(EXPECTED_BASE_COMMAND + expected, display_cmd=False)

@patch('show.main.run_command')
@patch('os.path.isfile', MagicMock(return_value=True))
@pytest.mark.parametrize(
"cli_arguments,expected",
[
([], 'cat /var/log/syslog.1 /var/log/syslog'),
(['xcvrd'], "cat /var/log/syslog.1 /var/log/syslog | grep 'xcvrd'"),
(['-l', '10'], 'cat /var/log/syslog.1 /var/log/syslog | tail -10'),
(['-f'], 'tail -F /var/log/syslog'),
]
)
def test_show_logging_syslog_1(run_command, cli_arguments, expected):
runner = CliRunner()
result = runner.invoke(show.cli.commands["logging"], cli_arguments)
run_command.assert_called_with(EXPECTED_BASE_COMMAND + expected, display_cmd=False)

@patch('show.main.run_command')
@patch('os.path.exists', MagicMock(return_value=True))
@pytest.mark.parametrize(
"cli_arguments,expected",
[
([], 'cat /var/log.tmpfs/syslog'),
(['xcvrd'], "cat /var/log.tmpfs/syslog | grep 'xcvrd'"),
(['-l', '10'], 'cat /var/log.tmpfs/syslog | tail -10'),
(['-f'], 'tail -F /var/log.tmpfs/syslog'),
]
)
def test_show_logging_tmpfs(run_command, cli_arguments, expected):
runner = CliRunner()
result = runner.invoke(show.cli.commands["logging"], cli_arguments)
run_command.assert_called_with(EXPECTED_BASE_COMMAND + expected, display_cmd=False)

@patch('show.main.run_command')
@patch('os.path.isfile', MagicMock(return_value=True))
@patch('os.path.exists', MagicMock(return_value=True))
@pytest.mark.parametrize(
"cli_arguments,expected",
[
([], 'cat /var/log.tmpfs/syslog.1 /var/log.tmpfs/syslog'),
(['xcvrd'], "cat /var/log.tmpfs/syslog.1 /var/log.tmpfs/syslog | grep 'xcvrd'"),
(['-l', '10'], 'cat /var/log.tmpfs/syslog.1 /var/log.tmpfs/syslog | tail -10'),
(['-f'], 'tail -F /var/log.tmpfs/syslog'),
]
)
def test_show_logging_tmpfs_syslog_1(run_command, cli_arguments, expected):
runner = CliRunner()
result = runner.invoke(show.cli.commands["logging"], cli_arguments)
run_command.assert_called_with(EXPECTED_BASE_COMMAND + expected, display_cmd=False)