Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sujinmkang committed Nov 25, 2020
1 parent 47fae88 commit 69b2221
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
31 changes: 17 additions & 14 deletions show/reboot_cause.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,25 @@ def history():
prefix = REBOOT_CAUSE_TABLE_NAME + TABLE_NAME_SEPARATOR
_hash = '{}{}'.format(prefix, '*')
table_keys = db.keys(db.STATE_DB, _hash)
if table_keys is None:
if table_keys is not None:
click.echo("Reboot-cause history is not yet available in StateDB")
sys.exit(1)

table_keys.sort(reverse=True)
table_keys.sort(reverse=True)

table = []
for tk in table_keys:
entry = db.get_all(db.STATE_DB, tk)
r = []
r.append(tk.replace(prefix,""))
r.append(entry['cause'] if 'cause' in entry else "")
r.append(entry['time'] if 'time' in entry else "")
r.append(entry['user'] if 'user' in entry else "")
r.append(entry['comment'] if 'comment' in entry else "")
table.append(r)
table = []
for tk in table_keys:
entry = db.get_all(db.STATE_DB, tk)
r = []
r.append(tk.replace(prefix,""))
r.append(entry['cause'] if 'cause' in entry else "")
r.append(entry['time'] if 'time' in entry else "")
r.append(entry['user'] if 'user' in entry else "")
r.append(entry['comment'] if 'comment' in entry else "")
table.append(r)

header = ['Name', 'Cause', 'Time', 'User', 'Comment']
click.echo(tabulate(table, header))
header = ['Name', 'Cause', 'Time', 'User', 'Comment']
click.echo(tabulate(table, header))
else:
click.echo("Reboot-cause history is not yet available in StateDB")
sys.exit(1)
4 changes: 2 additions & 2 deletions tests/mock_tables/state_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,13 @@
"cause": "warm-reboot",
"time": "Fri Oct 9 04:51:47 UTC 2020",
"user": "admin",
"comment": ""
"comment": "N/A"
},
"REBOOT_CAUSE|2020_10_09_02_33_06": {
"cause": "reboot",
"time": "Fri Oct 9 02:29:44 UTC 2020",
"user": "admin",
"comment": ""
"comment": "N/A"
},
"CHASSIS_TABLE|CHASSIS 1": {
"module_num": "5"
Expand Down
10 changes: 8 additions & 2 deletions tests/reboot_cause_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import textwrap
import mock
from click.testing import CliRunner
from .mock_tables import dbconnector

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

Expand Down Expand Up @@ -52,8 +58,8 @@ 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
2020_10_09_04_53_58 warm-reboot Fri Oct 9 04:51:47 UTC 2020 admin N/A
2020_10_09_02_33_06 reboot Fri Oct 9 02:29:44 UTC 2020 admin N/A
"""
runner = CliRunner()
result = runner.invoke(show.cli.commands["reboot-cause"].commands["history"], [])
Expand Down

0 comments on commit 69b2221

Please sign in to comment.