Skip to content

Commit

Permalink
[202012][show] show logging CLI support for logs stored in tmpfs (#2652)
Browse files Browse the repository at this point in the history
Signed-off-by: Mihir Patel <patelmi@microsoft.com>
  • Loading branch information
mihirpat1 authored Feb 6, 2023
1 parent c60f771 commit a996abd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
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)

0 comments on commit a996abd

Please sign in to comment.