Skip to content

Commit

Permalink
show logging CLI support for logs stored in tmpfs (#2641)
Browse files Browse the repository at this point in the history
* show logging CLI support for logs stored in tmpfs

Signed-off-by: Mihir Patel <patelmi@microsoft.com>

* Fixed testcase failures

* Reverted unwanted change in a file

* Added testcase for syslog.1 in log.tmpfs directory

* mend

---------

Signed-off-by: Mihir Patel <patelmi@microsoft.com>
  • Loading branch information
mihirpat1 authored Feb 1, 2023
1 parent 5d23934 commit c57c3fa
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
12 changes: 8 additions & 4 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,14 +1257,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(
"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 c57c3fa

Please sign in to comment.