-
Notifications
You must be signed in to change notification settings - Fork 659
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
Add show reboot-history
#1154
Add show reboot-history
#1154
Changes from 28 commits
e5e19ab
60085a7
e1244a5
c4b2b6c
561d133
a71c72b
b6af9f4
3a7457c
d8977fe
9531e41
fcd8647
e66777b
29a7e78
89b189c
4659ee2
059cdca
a733df5
e51d44f
c382d89
574726f
28c23f0
984ef40
4220356
3b62af0
b96fb6b
c395e14
510d0ad
59a511d
13bd06b
5c12ffd
ca92e36
f08d628
0c3a97f
5651b17
a060cf9
00a2570
f14bbe5
dfc73ac
e889212
987c433
63c6733
1753f22
f645a38
d7e372b
615c3b4
baeeed9
2f33931
2decd0c
8af9aee
ef5f212
b4a0ebe
9011750
6d0452b
b4075f6
8359c7b
6f838c1
ac52429
cc4f4e9
b645c24
e9fb1fc
c61191d
b7a634c
6dce6ed
c6f02ec
17516c5
81d0736
df9cfcf
cd282e9
e6b55ae
ac2eba5
b927ab7
212274d
f452276
2e8fad6
fd2629f
b59df08
7d20f97
08817ad
b18f0c4
9489bb8
517c2c5
02eb1c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import os | ||
import sys | ||
import textwrap | ||
|
||
import mock | ||
|
||
from click.testing import CliRunner | ||
|
||
test_path = os.path.dirname(os.path.abspath(__file__)) | ||
modules_path = os.path.dirname(test_path) | ||
sys.path.insert(0, modules_path) | ||
|
||
import show.main as show | ||
|
||
module_ = "builtins" | ||
module_ = module_ if module_ in sys.modules else '__builtin__' | ||
|
||
reboot_cause_json_file="/host/reboot-cause/previous-reboot-cause.json" | ||
""" | ||
Note: The following 'show reboot-cause' commands simply call other SONiC | ||
CLI utilities, so the unit tests for the other utilities are expected | ||
to cover testing their functionality: | ||
|
||
show reboot-cause | ||
show reboot-cause history | ||
""" | ||
|
||
class TestShowRebootCause(object): | ||
@classmethod | ||
def setup_class(cls): | ||
print("SETUP") | ||
os.environ["UTILITIES_UNIT_TESTING"] = "1" | ||
|
||
# Test 'show reboot-cause' without previous-reboot-cause.json | ||
def test_reboot_cause_no_history_file(self): | ||
expected_output = "Unknown\n" | ||
runner = CliRunner() | ||
result = runner.invoke(show.cli.commands["reboot-cause"], []) | ||
assert result.output == expected_output | ||
|
||
# Test 'show reboot-cause' with user issued reboot | ||
def test_reboot_cause_user(self): | ||
expected_output = "User issued reboot command [User: admin, Time: Thu Oct 22 03:11:08 UTC 2020]\n" | ||
reboot_cause_user_json = """\ | ||
{"comment": "", "gen_time": "2020_10_22_03_14_07", "cause": "reboot", "user": "admin", "time": "Thu Oct 22 03:11:08 UTC 2020"} | ||
""" | ||
runner = CliRunner() | ||
with mock.patch("os.path.isfile") as mock_isfile: | ||
mock_isfile.return_value = True | ||
open_mocked = mock.mock_open(read_data=textwrap.dedent(reboot_cause_user_json)) | ||
with mock.patch("{}.open".format(module_), open_mocked): | ||
result = runner.invoke(show.cli.commands["reboot-cause"], []) | ||
assert result == expected_output | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work without dedenting? Because |
||
open_mocked.assert_called_once_with(reboot_cause_json_file) | ||
|
||
# Test 'show reboot-cause' with non-user issue reboot (hardware reboot-cause or unknown reboot-cause) | ||
def test_reboot_cause_non_user(self): | ||
expected_output = "Watchdog\n" | ||
reboot_cause_watchdog_json = """\ | ||
{"comment": "", "gen_time": "2020_10_22_03_15_08", "cause": "Watchdog", "user": "", "time": ""} | ||
""" | ||
runner = CliRunner() | ||
with mock.patch("os.path.isfile") as mock_isfile: | ||
mock_isfile.return_value = True | ||
open_mocked = mock.mock_open(read_data=reboot_cause_watchdog_json) | ||
with mock.patch("{}.open".format(module_), open_mocked): | ||
result = runner.invoke(show.cli.commands["reboot-cause"], []) | ||
assert result == expected_output | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work without dedenting? Because |
||
open_mocked.assert_called_once_with(reboot_cause_json_file) | ||
|
||
# Test 'show reboot-cause history' | ||
def test_reboot_cause_history(self): | ||
expected_output = """\ | ||
Name Cause Time User Comment | ||
------------------- ----------- ---------------------------- ------ --------- | ||
2020_10_09_04_53_58 warm-reboot Fri Oct 9 04:51:47 UTC 2020 admin | ||
2020_10_09_02_33_06 reboot Fri Oct 9 02:29:44 UTC 2020 admin | ||
""" | ||
runner = CliRunner() | ||
result = runner.invoke(show.cli.commands["reboot-cause"].commands["history"], []) | ||
assert result.output == expected_output | ||
|
||
@classmethod | ||
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does moving this docstring inside the
if
statement prevent Click from using it as the help message?I think we should move away from this and explicitly declare the help message in the decorator going forward. In this instance, the decorator should read,
@cli.group('reboot-cause', short_help='Show cause of reboot', invoke_without_command=True)
Also, why change the message from "Show cause of most recent reboot"?