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

show logging CLI support for logs stored in tmpfs #2641

Merged
merged 5 commits into from
Feb 1, 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 @@ -1253,14 +1253,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
69 changes: 68 additions & 1 deletion tests/show_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import sys
import pytest
import show.main as show
from click.testing import CliRunner
from unittest import mock
from unittest.mock import call, MagicMock
from unittest.mock import call, MagicMock, patch

EXPECTED_BASE_COMMAND = 'sudo '

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
Expand Down Expand Up @@ -49,3 +52,67 @@ def teardown_class(cls):
print("TEARDOWN")
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
os.environ["UTILITIES_UNIT_TESTING"] = "0"

@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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test case to handle syslog.1 exists under the log.tmpfs directory as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gechiang, I have added the test case now.

"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)